Repository navigation
[Dsv4.1] Mega-mhc integration #42808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kpham-sgl
merged 14 commits into
sgl-project:main
from
Oasis-Git:perf/dsv41-mega-mhc-prefill
Oct 11, 2026
+137
−9
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4a10ec9
[DSV4.1] Add opt-in DeepGEMM Mega mHC prefill integration
Oasis-Git 070d49e
Merge main and port MegaMHC prefill to HcState
Oasis-Git 1fd6533
Clarify DeepGEMM Mega mHC attribution and test comments
Oasis-Git 41f8aab
Merge branch 'main' into perf/dsv41-mega-mhc-prefill
Oasis-Git ade5718
Restrict Mega mHC prefill to SM10x
42c1a27
Allow Mega mHC prefill with CUDA graphs
7523917
Merge branch 'main' into perf/dsv41-mega-mhc-prefill
Oasis-Git 4897fd5
Merge main into perf/dsv41-mega-mhc-prefill
kpham-sgl 33cde18
Remove standalone manual Mega mHC kernel test
kpham-sgl 431d211
Merge branch 'main' into perf/dsv41-mega-mhc-prefill
kpham-sgl 8d9a216
Remove redundant Mega mHC tensor and token-range guards
Oasis-Git 69e989f
Move Mega mHC DeepGEMM import to module scope
Oasis-Git 6830d43
fix: narrow Mega mHC tail boundary gating
Oasis-Git 0ac7ee3
Simplify Mega mHC eligibility and separate tail boundary policy
Oasis-Git File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """SGLang DSv4.1 prefill integration of Mega mHC. | ||
|
|
||
| Credit: DeepSeek's DeepGEMM project provides the fused Mega mHC kernel. | ||
| https://github.com/deepseek-ai/DeepGEMM | ||
| """ | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| def mhc_mega_boundary( | ||
| x, | ||
| residual, | ||
| pre, | ||
| post, | ||
| comb, | ||
| fn, | ||
| scale, | ||
| base, | ||
| weight, | ||
| rms_eps, | ||
| hc_eps, | ||
| norm_eps, | ||
| sinkhorn_iters, | ||
| ): | ||
| """Update the residual, collapse with carried pre, and compute next stats. | ||
|
|
||
| The collapse uses ``pre`` from the preceding sublayer, not the new pre | ||
| produced from the updated residual. Preserve this DSv4.1 shifted ordering. | ||
| Used by the opt-in SM10x prefill path. Before CUDA graph capture, warm up | ||
| on the capture stream to initialize DeepGEMM's per-stream barriers. | ||
| """ | ||
| import deep_gemm | ||
|
|
||
| rows, hidden = x.shape | ||
| hc = residual.shape[1] | ||
| updated = torch.empty_like(residual) | ||
| normalized = torch.empty_like(x) | ||
| next_pre = torch.empty((rows, hc, 1), device=x.device, dtype=torch.float32) | ||
| next_post = torch.empty_like(next_pre) | ||
| next_comb = torch.empty_like(comb) | ||
| deep_gemm.mega_mhc( | ||
| x=x, | ||
| residual=residual, | ||
| shifted_prev_mix=pre.unsqueeze(-1), | ||
| post_mix=post.unsqueeze(-1), | ||
| comb_res_mix=comb, | ||
| fn=fn, | ||
| mix_scales=scale, | ||
| mix_bases=base, | ||
| hc_mult=hc, | ||
| hc_norm_eps=rms_eps, | ||
| hc_pre_eps=hc_eps, | ||
| hc_post_scale=2.0, | ||
| sinkhorn_eps=hc_eps, | ||
| num_sinkhorn_iters=sinkhorn_iters, | ||
| rmsnorm_weight=weight, | ||
| rmsnorm_eps=norm_eps, | ||
| rmsnorm_scale=1.0, | ||
| new_residual=updated, | ||
| new_prev_mix=next_pre, | ||
| new_post_mix=next_post, | ||
| new_comb_res_mix=next_comb, | ||
| y_bf16=normalized, | ||
| ) | ||
| return updated, normalized, (next_pre.squeeze(-1), next_post.squeeze(-1), next_comb) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.