Repository navigation
feat(kernel): add clamped SwiGLU, sparse-EP skip, and MLA return_lse - #894
Merged
Merged
Conversation
Add optional swiglu_limit clamping and out-of-range expert slot skipping (-1) to fused_moe_bf16 and ResidentEPMoELayer so high-EP shards skip non-local tiles instead of piling onto expert 0, guard zero-weight loads in moe_weighted_sum, and expose return_lse on mla_paged_decode and sparse_mla_{decode,prefill} for per-head learnable softmax sinks.
Upstream FA3 flash_attn_with_kvcache(return_softmax_lse=True) returns (out, softmax_lse, *rest); return exact 2-tuple (res[0], res[1]) from _fa3_sparse.
Replace torch.bincount with scatter_add_ in moe_align_block_size to remove host syncs during CUDA graph capture, release intermediate buffers early, and chunk batches above 16k tokens to bound peak HBM.
Lower fused_moe_bf16 prefill chunk size to 4096 tokens to bound activation HBM under large packed batches, and select the small M tile when running sparse resident-EP shards with small local expert counts.
Replace multi-op PyTorch moe_align_block_size on small resident-EP shards with a single-CTA zero-atomic deterministic Triton kernel, and widen the small-batch BF16 MoE K/N tile configuration.
Andrewxu313
force-pushed
the
tairan/hy4-kernel-primitives
branch
from
December 4, 2026 01:41
86559b9 to
5e83d67
Compare
Bound global all_gather/reduce_scatter buffer memory in ResidentEPMoELayer.forward on large prepacked prefill waves and replace world all_reduce with reduce_scatter directly into each rank's local slice.
Allow callers of sparse_mla_decode to override FA3 num_splits on Hopper while preserving the default policy when omitted, and add a BLOCK_M=32 MoE tile configuration for medium resident-EP slot counts.
Drop the experimental (32, 128, 128) tile in fused_moe_bf16 so resident-EP batches up to 4096 slots stay on the higher-occupancy (16, 128, 128) schedule.
Andrewxu313
added a commit
that referenced
this pull request
Dec 4, 2026
This was referenced Dec 4, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Add three general-purpose kernel primitives for high-EP BF16 MoE and MLA models with learnable attention sinks:
swiglu_limitclamping (gate = clamp(gate, max=limit),up = clamp(up, min=-limit, max=limit)in FP32) insidefused_moe_bf16's_silu_and_mul_kernel.-1non-local slot skipping inmoe_align_block_size,_silu_and_mul_kernel,moe_weighted_sum, andResidentEPMoELayer.return_lse: bool = Falseonmla_paged_decode,sparse_mla_decode, andsparse_mla_prefill(returning natural-log(out, lse)on SM90 FlashMLA / FA3; raisingNotImplementedErroron unsupported backends), plus configurableweight_keysonResidentEPMoELayer/build_layer_shard/fill_layer_shard.Motivation
Under high expert parallelism, mapping non-local routing slots to local expert
0with weight0routes all non-local global slots into wasted GEMM tiles on local expert0. Skipping-1slots inmoe_align_block_sizeand guarding zero-weight reads inmoe_weighted_sumavoids this wasted work without host synchronization. Returning natural-log LSE from FlashMLA/FA3 allows exact FP32 learnable attention sinks without custom attention kernels.Type of Change
model— add/extend model support (models/**+ registration seam + model kernels only)kernel— add/optimize a compute kernel (batchgen_kernels/**+ in-tree kernel dirs)core— change scheduling/serving/runtime scaffolding (the only type that may)fix— narrow bug fix (+ a regression test)infra— build / CI / packaging / scripts / Dockerdocs— documentation onlyFile changes
batchgen_kernels/triton/fused_moe_bf16.pyswiglu_limitclamping and-1slot skip inmoe_align_block_size/_silu_and_mul_kernelbatchgen_kernels/triton/moe_weighted_sum.pyweights != 0.0so skipped slots never read uninitializedc3batchgen/moe/fused_moe_bf16_resident.py-1for non-local slots; add optionalswiglu_limitandweight_keysbatchgen/attention/mla/paged_decode.pyreturn_lse=Truereturning(out, lse)on SM90 FlashMLAbatchgen/attention/mla/sparse_dispatch.pyreturn_lse=Trueunpacking(out, lse)on SM90 FA3tests/test_hy4_kernel_primitives.py-1skip, clamped SwiGLU,weight_keys, andreturn_lsecontractsChecklist
README.mdchanged, I ranpython3 scripts/check_readme_update.py --base-ref origin/mainand preserved all News history.