Skip to content

Conversation

WeiHaocheng
Copy link
Collaborator

@WeiHaocheng WeiHaocheng commented Jul 4, 2025

feat: moe prepare support topk % 4 != 0

Summary by CodeRabbit

  • Refactor
    • Enhanced pipeline configuration flexibility by introducing a templated structure to replace fixed size constants, improving adaptability for different data sizes.
  • Tests
    • Updated and expanded test cases to cover additional scenarios for local gather operations.

@WeiHaocheng WeiHaocheng requested review from syuoni and dongxuy04 July 4, 2025 02:50
@WeiHaocheng WeiHaocheng self-assigned this Jul 4, 2025
@WeiHaocheng WeiHaocheng marked this pull request as ready for review July 4, 2025 02:50
@WeiHaocheng WeiHaocheng requested a review from a team as a code owner July 4, 2025 02:50
@WeiHaocheng WeiHaocheng requested a review from hlu1 July 4, 2025 02:50
@WeiHaocheng
Copy link
Collaborator Author

/bot run

@WeiHaocheng WeiHaocheng force-pushed the feat/main_moe_prepare_general_topk branch from 4a3cad6 to 2c7a3ac Compare July 14, 2025 03:21
@WeiHaocheng
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #11754 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #11754 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #8706 completed with status: 'FAILURE'

@WeiHaocheng WeiHaocheng force-pushed the feat/main_moe_prepare_general_topk branch 2 times, most recently from f7358bc to 6b718a3 Compare July 15, 2025 02:37
@WeiHaocheng WeiHaocheng enabled auto-merge (squash) July 15, 2025 02:37
@WeiHaocheng
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #11869 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #11869 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #8797 completed with status: 'FAILURE'

@WeiHaocheng WeiHaocheng force-pushed the feat/main_moe_prepare_general_topk branch from 6b718a3 to 78f9117 Compare July 20, 2025 10:13
Copy link
Contributor

coderabbitai bot commented Jul 20, 2025

"""

Walkthrough

The changes refactor packet and pipeline size management in the MoE preparation kernels by introducing a templated PipelineConfig struct to encapsulate size-related constants. All usages of hardcoded packet, unit, and buffer sizes are replaced with references to PipelineConfig members. Test cases are updated to include new parameter values for broader coverage.

Changes

File(s) Change Summary
cpp/tensorrt_llm/kernels/moePrepareKernels.cu Refactored to use templated PipelineConfig for all packet/unit/buffer size constants and types; updated kernel and host logic to select configurations based on runtime parameters.
cpp/tensorrt_llm/kernels/moePrepareKernels.h Removed fixed size macros/constants; introduced templated PipelineConfig struct encapsulating all size-related constants; replaced FIFO size with fixed 1MB constant.
tests/unittest/_torch/thop/test_moe_alltoall.py Updated test parameter tuples to change top_k and max_token_count_per_rank values; added a new test case for extended coverage.

Sequence Diagram(s)

sequenceDiagram
    participant Host
    participant Kernel
    participant PipelineConfig

    Host->>PipelineConfig: Select config based on topK
    Host->>Kernel: Launch allToAllMetadataDevice<PipelineConfig, ExpertType, ScaleType>()
    Kernel->>PipelineConfig: Access size/type constants
    Kernel->>Kernel: Perform packet/unit operations using PipelineConfig
Loading

Poem

In the warren of code, a rabbit hopped,
Swapping hardcodes for templates, the macros all dropped.
Now packets and units, with configs in tow,
Flexibly dance as the kernels now flow.
With tests newly tuned and constants set free,
The code hops ahead, as happy as me! 🐇✨
"""

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
cpp/tensorrt_llm/kernels/moePrepareKernels.cu (1)

642-643: Consider documenting the performance trade-offs between configurations.

The PipelineConfig<1, 64> processes 4x more packets per step compared to PipelineConfig<4, 16>, which may have different performance characteristics. Consider adding a comment explaining this design choice.

Also applies to: 653-654

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98428f3 and 78f9117.

📒 Files selected for processing (3)
  • cpp/tensorrt_llm/kernels/moePrepareKernels.cu (14 hunks)
  • cpp/tensorrt_llm/kernels/moePrepareKernels.h (1 hunks)
  • tests/unittest/_torch/thop/test_moe_alltoall.py (1 hunks)
🔇 Additional comments (5)
tests/unittest/_torch/thop/test_moe_alltoall.py (1)

473-480: Test coverage looks good for the new feature.

The test modifications appropriately cover the new functionality:

  • Line 474: Changed top_k from 4 to 3 (not divisible by 4)
  • Line 480: Added new test case with top_k=6 (not divisible by 4)

These changes align with the PR objective of supporting cases where topk % 4 != 0.

cpp/tensorrt_llm/kernels/moePrepareKernels.h (2)

47-57: Good refactoring with the templated PipelineConfig struct.

The templated approach provides flexibility for different pipeline configurations based on runtime parameters. The computed constants are correctly derived from the template parameters.


59-60: Verify that the 1MB FIFO size is sufficient for all pipeline configurations.

The fixed FIFO size of 1MB replaces the previous dynamic calculation. Please ensure this size accommodates all possible PipelineConfig specializations, especially with larger packet sizes or step counts.

cpp/tensorrt_llm/kernels/moePrepareKernels.cu (2)

640-661: Correct implementation of conditional pipeline configuration.

The logic properly handles both cases:

  • topK % 4 == 0: Uses PipelineConfig<4, 16> with vectorized types (int4/float4) for better performance
  • topK % 4 != 0: Uses PipelineConfig<1, 64> with scalar types (int/float)

The static assertions ensure FIFO size sufficiency for both configurations.


669-671: Workspace size calculation is correctly updated.

The new calculation allocates 1MB FIFO buffer plus metadata (MoeCommFifoConnInfo) per rank, which aligns with the fixed FIFO size design.

@WeiHaocheng WeiHaocheng force-pushed the feat/main_moe_prepare_general_topk branch from 78f9117 to 94f7f18 Compare July 20, 2025 10:16
@WeiHaocheng
Copy link
Collaborator Author

/bot run

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 78f9117 and 94f7f18.

📒 Files selected for processing (2)
  • cpp/tensorrt_llm/kernels/moePrepareKernels.cu (14 hunks)
  • cpp/tensorrt_llm/kernels/moePrepareKernels.h (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cpp/tensorrt_llm/kernels/moePrepareKernels.h
🔇 Additional comments (14)
cpp/tensorrt_llm/kernels/moePrepareKernels.cu (14)

322-327: LGTM: Good architectural improvement

The refactoring from a templated communicator type to a base pointer improves flexibility and reduces template complexity while maintaining type safety.


334-334: Correct parameterization

Good use of template parameters to replace hardcoded constants. The initialization logic is preserved correctly.


346-351: Proper buffer arithmetic with template parameters

The pointer arithmetic correctly uses PipelineConfig constants, maintaining the original logic while adding flexibility.


369-369: Consistent template parameter usage

Buffer offset calculation properly uses the templated constants.


386-390: Correct packet retrieval logic

The receive packet logic properly uses template parameters while preserving the original buffer management semantics.


406-406: Proper buffer addressing

Packet pointer calculation correctly uses templated constants.


427-427: Well-designed template parameters

The addition of ExpertType and ScaleType template parameters enables support for both vectorized and scalar operations based on topK requirements.


436-444: Correct template parameter usage in kernel setup

The group size calculation and local array sizing properly use template parameters. The pipeline instantiation correctly uses the new template approach.


450-450: Correct unit count calculation

Unit count properly uses template parameter for flexible pipeline configuration.


461-487: Proper templated memory operations

The memory access patterns correctly use ExpertType and ScaleType templates, enabling both vectorized and scalar operations. Buffer offset calculations using PipelineConfig constants are accurate.


495-495: Correct static copy offset

Buffer offset calculation properly uses template parameter.


528-543: Well-implemented receive path templating

The receive logic correctly uses templated types for both expert and scale data handling. The pointer arithmetic and type casting are properly implemented.


551-551: Consistent offset usage

Static copy operations correctly use template-based offset calculations.


670-670: Simplified MOE prepare workspace calculation is adequately covered

Automated tests in tests/unittest/_torch/thop/test_moe_alltoall.py invoke get_moe_prepare_workspace_size_per_rank(ep_size) and exercise the prepare kernel across multiple ep_size scenarios, confirming that the fixed-1 MB FIFO workspace suffices. No additional changes required.

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12380 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12380 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9199 completed with status: 'SUCCESS'

@WeiHaocheng WeiHaocheng merged commit fddb7f1 into NVIDIA:main Jul 22, 2025
3 checks passed
NVShreyas pushed a commit to NVShreyas/TensorRT-LLM that referenced this pull request Jul 28, 2025
Ransiki pushed a commit to Ransiki/TensorRT-LLM that referenced this pull request Jul 29, 2025
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.

3 participants