Skip to content

[AMD] Add aiter.topk_softmax for fused_topk#20350

Closed
apinge wants to merge 1 commit into
sgl-project:mainfrom
apinge:aiter_topk_softmax_community
Closed

[AMD] Add aiter.topk_softmax for fused_topk#20350
apinge wants to merge 1 commit into
sgl-project:mainfrom
apinge:aiter_topk_softmax_community

Conversation

@apinge
Copy link
Copy Markdown

@apinge apinge commented Mar 11, 2026

Motivation

This PR introduces aiter.topk_softmax to support fused_topk used in Qwen3.5-397B-A17B .

Modifications

Accuracy Tests

python3 benchmark/mmmu/bench_sglang.py --concurrency 64 --port 8000 --max-new-tokens 512
{'Accounting': {'acc': 1.0, 'num': 2},
 'Agriculture': {'acc': 1.0, 'num': 4},
 'Art': {'acc': 0.909, 'num': 11},
 'Art_Theory': {'acc': 1.0, 'num': 8},
 'Basic_Medical_Science': {'acc': 1.0, 'num': 4},
 'Biology': {'acc': 1.0, 'num': 1},
 'Clinical_Medicine': {'acc': 0.0, 'num': 1},
 'Computer_Science': {'acc': 1.0, 'num': 2},
 'Design': {'acc': 1.0, 'num': 14},
 'Economics': {'acc': 1.0, 'num': 4},
 'Finance': {'acc': 1.0, 'num': 2},
 'Geography': {'acc': 1.0, 'num': 3},
 'History': {'acc': 1.0, 'num': 2},
 'Literature': {'acc': 0.938, 'num': 16},
 'Manage': {'acc': 1.0, 'num': 2},
 'Marketing': {'acc': 1.0, 'num': 5},
 'Math': {'acc': 1.0, 'num': 2},
 'Overall': {'acc': 0.97, 'num': 99},
 'Overall-Art and Design': {'acc': 0.97, 'num': 33},
 'Overall-Business': {'acc': 1.0, 'num': 15},
 'Overall-Health and Medicine': {'acc': 0.909, 'num': 11},
 'Overall-Humanities and Social Science': {'acc': 0.963, 'num': 27},
 'Overall-Science': {'acc': 1.0, 'num': 7},
 'Overall-Tech and Engineering': {'acc': 1.0, 'num': 6},
 'Pharmacy': {'acc': 1.0, 'num': 3},
 'Physics': {'acc': 1.0, 'num': 1},
 'Psychology': {'acc': 1.0, 'num': 4},
 'Public_Health': {'acc': 1.0, 'num': 3},
 'Sociology': {'acc': 1.0, 'num': 5}}
eval out saved to ./val_sglang.json
Overall accuracy: 0.97

Benchmarking and Profiling

Origin kernel in prefill (8k in):
1
Aiter kernel in prefill (8K in)

32

Version:
sglang 0.5.6.post3.dev2176+gc6a99e43b
aiter: 0.1.10.post4.dev0+g6a0e7b26c.d20260222
torch 2.9.1+rocm7.2.0.lw.git7e1940d4
mi350

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

Signed-off-by: apinge <tong.qiu2@amd.com>
@apinge apinge marked this pull request as ready for review March 11, 2026 09:42
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 enhances the fused_topk operation within the MoE layers by integrating a specialized aiter.topk_softmax kernel. This optimization is crucial for improving the performance and efficiency of large language models, particularly for architectures like Qwen3.5-397B-A17B, by leveraging advanced GPU kernels for softmax computations.

Highlights

  • Integration of aiter.topk_softmax: Introduced aiter.topk_softmax to support fused_topk operations, specifically for models like Qwen3.5-397B-A17B, enhancing performance for Mixture-of-Experts (MoE) layers.
  • Conditional topk_softmax implementation: Modified the fused_topk function to conditionally use the aiter_topk_softmax kernel when the _use_aiter flag is enabled, otherwise falling back to the existing topk_softmax implementation, ensuring compatibility and optimized execution.

🧠 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.

Changelog
  • python/sglang/srt/layers/moe/topk.py
    • Imported aiter.topk_softmax from the aiter library.
    • Implemented conditional logic within the fused_topk function to utilize aiter_topk_softmax when _use_aiter is true for the "softmax" scoring function.
    • Added token_expert_indices as a new output tensor for the aiter_topk_softmax call.
Activity
  • The pull request was opened by apinge.
  • Accuracy tests were provided, showing an overall accuracy of 0.97 across various categories.
  • Benchmarking results were included, comparing the performance of the original kernel with the aiter kernel in prefill (8K in) on an MI350 GPU.
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
Copy Markdown
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 support for aiter.topk_softmax within the fused_topk function, aimed at enabling fused top-k operations for specific models on AMD hardware. The changes are straightforward, adding a conditional path for when _use_aiter is active. My review identifies a potential performance issue related to an unnecessary memory allocation for token_expert_indices, which is created but not subsequently used. Addressing this could improve memory efficiency.

Comment on lines +516 to +518
token_expert_indices = torch.empty(
M, topk, dtype=torch.int32, device=hidden_states.device
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The token_expert_indices tensor is allocated here but its value is not used after the call to aiter_topk_softmax. This results in an unnecessary memory allocation on every call to fused_topk when _use_aiter is enabled. A similar pattern is present in fused_topk_deepseek with aiter_biased_grouped_topk.

If this tensor is a required output parameter for the aiter kernels that is not needed by sglang, consider checking if the aiter API allows passing None to avoid the allocation. If it's a workspace, it might be possible to manage it more efficiently, for example by using a pre-allocated buffer from a memory pool.

@hnyls2002
Copy link
Copy Markdown
Collaborator

#21421

@hnyls2002 hnyls2002 closed this Apr 21, 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