Skip to content

Conversation

@SangChengC
Copy link
Contributor

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @SangChengC, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the LightLLM framework by introducing a flexible and extensible architecture for Vision Transformer (ViT) attention computations. It abstracts the underlying attention implementation, enabling the dynamic selection of optimized backends like FA3 or Triton. This change not only improves the modularity of the system but also paves the way for better performance and easier integration of future ViT attention optimizations within visual models.

Highlights

  • ViT Attention Backend Abstraction: Introduced new abstract classes (BaseVitAttState) and methods (create_vit_att_state) to define a standardized interface for Vision Transformer (ViT) attention operations.
  • Pluggable Backend Implementations: Added concrete implementations for ViT attention using both FA3 (Fa3ViTAttBackend, Fa3VitAttState) and Triton (TritonVitAttState) kernels, allowing for different optimized approaches.
  • Automatic Backend Selection for ViT: Implemented a new utility function (_auto_select_vit_backend) and an exposed getter (get_vit_att_backend_class) to automatically choose the most suitable ViT attention backend based on a defined priority (FA3 over Triton) and validation.
  • Integration into Visual Models: Modified Qwen2Visual and Qwen3Visual models to utilize the newly introduced ViT attention backend mechanism, replacing hardcoded FlashAttention calls with dynamic backend invocation.
  • RPC Server Initialization: Updated the VisualModelRpcServer to handle the initialization and injection of the selected ViT attention backend into the visual models, ensuring proper setup in a distributed environment.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to select different backends for Vision Transformer (ViT) attention, similar to the existing implementation for language model attention. The changes include adding base classes and selection utilities for ViT attention backends, and implementing fa3 and triton backends.

The overall approach is good, but I've found several issues that need to be addressed:

  • There are critical implementation errors in the Triton backend (triton/fp.py) that would cause runtime TypeErrors due to incorrect method signatures and not implementing the required abstract methods.
  • There are type hint inconsistencies in the FA3 backend (fa3/fp.py).
  • Some debugging print statements have been left in the code.
  • A comment in a non-English language suggests a temporary solution that should be properly documented with a TODO.

I've left specific comments with suggestions to fix these issues. After these are addressed, the PR should be in good shape.

Comment on lines 281 to 306
@dataclasses.dataclass
class TritonVitAttState(BaseVitAttState):
def init_state(self):
pass

def _vit_att(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
o: torch.Tensor,
cu_seqlens: torch.Tensor,
max_seqlen: int,
alloc_func=torch.empty,
):
from lightllm.models.vit.triton_kernel.flashattention_nopad import _flash_attention_triton_fwd

_flash_attention_triton_fwd(
q,
k,
v,
o,
cu_seqlens, # q k v cu_seqlens,
max_seqlen,
)
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The class TritonVitAttState does not correctly implement the BaseVitAttState interface, which will lead to runtime errors.

  1. The abstract method vit_att is not implemented. A private method _vit_att is implemented instead. This will raise a TypeError on instantiation.
  2. The method should return the output tensor o to be consistent with the base class.
  3. The alloc_func parameter is unused and can be removed.
  4. The method is missing a return type hint.
@dataclasses.dataclass
class TritonVitAttState(BaseVitAttState):
    def init_state(self):
        pass

    def vit_att(
        self,
        q: torch.Tensor,
        k: torch.Tensor,
        v: torch.Tensor,
        o: torch.Tensor,
        cu_seqlens: torch.Tensor,
        max_seqlen: int,
    ) -> torch.Tensor:
        from lightllm.models.vit.triton_kernel.flashattention_nopad import _flash_attention_triton_fwd

        _flash_attention_triton_fwd(
            q,
            k,
            v,
            o,
            cu_seqlens,  # q k v cu_seqlens,
            max_seqlen,
        )
        return o

o: torch.Tensor,
cu_seqlens: torch.Tensor,
max_seqlen: int,
) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The return type hint for vit_att is None, but the function returns the tensor o. This should be corrected to torch.Tensor to match the base class BaseVitAttState.

Suggested change
) -> None:
) -> torch.Tensor:

for backend_name in priority_list:
if validate(backend_name):
logger.info(f"Auto-selected {backend_name} backend (validated)")
print(f"llm_dtype is {llm_dtype}, backend_name is {backend_name} ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This print statement appears to be for debugging and should be removed from the final code.

for backend_name in priority_list:
if validate(backend_name):
logger.info(f"Auto-selected {backend_name} backend (validated) for ViT")
print(f"llm_dtype is {llm_dtype}, backend_name is {backend_name} ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This print statement appears to be for debugging and should be removed from the final code.

cu_seqlens: torch.Tensor,
max_seqlen: int,
) -> None:
self.backend: Fa3ViTAttBackend = self.backend # for typing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line is redundant for type hinting, as the backend attribute is already typed in the class definition on line 257. It can be removed for cleaner code.

self.cache_client._channel.stream.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.data_type = kvargs["data_type"]

set_global_rank(kvargs["tp_rank_id"]) # 这里看后面怎么改
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The comment # 这里看后面怎么改 ("see how to change this later") suggests this line is a temporary solution. It's better to use a logger.warning or add a TODO comment with more context for future developers. Also, comments should be in English for better maintainability.

Suggested change
set_global_rank(kvargs["tp_rank_id"]) # 这里看后面怎么改
# TODO: Revisit the global rank setting for the visual model process.
set_global_rank(kvargs["tp_rank_id"])

@ModelTC ModelTC deleted a comment from gemini-code-assist bot Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants