Skip to content

feat(kernel): add clamped SwiGLU, sparse-EP skip, and MLA return_lse - #894

Merged
Andrewxu313 merged 8 commits into
mainfrom
tairan/hy4-kernel-primitives
Dec 4, 2026
Merged

Andrewxu313 merged 8 commits into
mainfrom
tairan/hy4-kernel-primitives

Conversation

@Andrewxu313

Copy link
Copy Markdown
Contributor

Description

Add three general-purpose kernel primitives for high-EP BF16 MoE and MLA models with learnable attention sinks:

  1. Optional swiglu_limit clamping (gate = clamp(gate, max=limit), up = clamp(up, min=-limit, max=limit) in FP32) inside fused_moe_bf16's _silu_and_mul_kernel.
  2. Sentinel -1 non-local slot skipping in moe_align_block_size, _silu_and_mul_kernel, moe_weighted_sum, and ResidentEPMoELayer.
  3. Optional return_lse: bool = False on mla_paged_decode, sparse_mla_decode, and sparse_mla_prefill (returning natural-log (out, lse) on SM90 FlashMLA / FA3; raising NotImplementedError on unsupported backends), plus configurable weight_keys on ResidentEPMoELayer / build_layer_shard / fill_layer_shard.

Motivation

Under high expert parallelism, mapping non-local routing slots to local expert 0 with weight 0 routes all non-local global slots into wasted GEMM tiles on local expert 0. Skipping -1 slots in moe_align_block_size and guarding zero-weight reads in moe_weighted_sum avoids 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 / Docker
  • docs — documentation only

File changes

File Δ Note
batchgen_kernels/triton/fused_moe_bf16.py mod Optional swiglu_limit clamping and -1 slot skip in moe_align_block_size / _silu_and_mul_kernel
batchgen_kernels/triton/moe_weighted_sum.py mod Guard HBM loads on weights != 0.0 so skipped slots never read uninitialized c3
batchgen/moe/fused_moe_bf16_resident.py mod Emit -1 for non-local slots; add optional swiglu_limit and weight_keys
batchgen/attention/mla/paged_decode.py mod Optional return_lse=True returning (out, lse) on SM90 FlashMLA
batchgen/attention/mla/sparse_dispatch.py mod Optional return_lse=True unpacking (out, lse) on SM90 FA3
tests/test_hy4_kernel_primitives.py add Unit tests covering -1 skip, clamped SwiGLU, weight_keys, and return_lse contracts

Checklist

  • I have read the CONTRIBUTING guide and the PR Merge Policy Contract.
  • I have updated the tests (if applicable).
  • I have updated the documentation (if applicable).
  • If README.md changed, I ran python3 scripts/check_readme_update.py --base-ref origin/main and preserved all News history.

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
Andrewxu313 force-pushed the tairan/hy4-kernel-primitives branch from 86559b9 to 5e83d67 Compare December 4, 2026 01:41
@github-actions github-actions Bot added the ci:run Trigger build + GPU regression on H20 label Dec 4, 2026
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
Andrewxu313 merged commit 3e48213 into main Dec 4, 2026
1 check passed
Andrewxu313 added a commit that referenced this pull request Dec 4, 2026
Record merge base with origin/main after #894 and #884 landed so PR #885 pushes cleanly without rewriting history.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run Trigger build + GPU regression on H20

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant