Skip to content

Fold Fireworks' four DeepGEMM build patches into main (SiTU, MHC graph replay, sigmoid-bf16 gate) - #19

Merged
vipulSharma18 merged 5 commits into
mainfrom
vipul/fold-fireworks-build-patches
Oct 14, 2026
Merged

vipulSharma18 merged 5 commits into
mainfrom
vipul/fold-fireworks-build-patches

Conversation

@vipulSharma18

@vipulSharma18 vipulSharma18 commented Oct 13, 2026 •

Copy link
Copy Markdown

What

This moves Fireworks' four DeepGEMM build-time patches into the tree, so Fireworks can build fw-ai/DeepGEMM main directly with no patches. The patches come from third-party/ in the Fireworks repo. Each is one commit, in the order Fireworks applies them, with the original authors:

Commit Patch Change
7f60195 deepgemm-situ.patch SiTU activation for the fp8×fp4 MegaMoE (situ_beta / situ_linear_beta)
8fed407 deepgemm-mhc-graph-replay.patch mega MHC barrier reset for CUDA-graph replay
2423d3e deepgemm-sigmoid-bf16.patch BF16-logit sigmoid mega gate scoring
bb9e55e deepgemm-mhc-capability.patch capability flag for the MHC graph-replay fix

Size: 10 files, +81/−19.

Conflict resolution

main's side-LoRA refactor (#9) shifted the code that three situ hunks touch:

  • the situ accept in get_symm_buffer_size_for_mega_moe_v3;
  • current-device allocation in SymmBuffer;
  • forwarding situ_kwargs from fp8_fp4_mega_moe.

They are re-anchored with exactly the same added and removed lines. The SiTU kernel, types.cuh and the host impl are byte-identical to the patched pin.

The side-LoRA forward paths still reject situ, because their kernels only implement SwiGLU/GeGLU. That matches current behavior. Side-LoRA sizing goes through v3, so it now accepts situ.

Verification (B300)

X is the build Fireworks ships today: c2e49f5 plus the four patches. Y is this branch, built with no patches.

Check Result
MegaMoE output parity 40/40 outputs bitwise equal. Covers fp8×fp4 SwiGLU (DSv4 shape), SwiGLU decode, SiTU (Kimi) and BF16 MegaMoE, at EP2 and EP8.
Single-GPU kernels 1076/1076 tensors bitwise equal. Covers dense and grouped BF16/FP8 GEMM, mega gate (sigmoid, sigmoid_bf16, sqrtsoftplus), and mega mhc eager and after graph replay.
Fireworks callers All 62 deep_gemm.* names Fireworks uses exist with identical signatures. get_symm_buffer_for_mega_moe and SymmBuffer gain a trailing side_lora=False, which existing calls don't see.
JIT cache keys Only the sm100 BF16 GEMM keys change (405 keys). That comes from main's own #17 (a new template argument), not from this PR. Outputs are bitwise equal and perf is unchanged (geomean 1.000 over 470 shapes). The Hopper (sm90) precompile produces identical keys (190 entries).
New in-tree tests (e5c0103) SiTU vs reference and SwiGLU/GeGLU unchanged in test_mega_moe.py (1 and 2 ranks); sigmoid_bf16 reference in test_mega_gate.py; mega_mhc capture-and-replay in test_mega_mhc.py (fails 3/3 without the per-call reset). All pass on B300.
Existing tests Pass: test_mega_moe fp8×fp4 and BF16 at 8 ranks, compat, mega_gate, mega_mhc, an fp8_fp4 GEMM subset, test_bf16, and 51 cheap side-LoRA tests.

Not run:

  • the multi-rank native side-LoRA test;
  • the block_m and headroom tests, which need torchrun;
  • the training tests;
  • anything on real Hopper hardware (only the sm90 precompiled cache keys were compared).

Follow-ups

🤖 Generated with Claude Code

YLGH and others added 4 commits October 12, 2026 19:32
Kimi K3 SiTU activation for the SM100 FP8xFP4 MegaMoE, previously applied at
build time from Fireworks' third-party/deepgemm-situ.patch. Same +/- lines as
the patch; three hunks were re-anchored by hand on main (side-LoRA shifted the
context): the `situ` accept in get_symm_buffer_size_for_mega_moe_v3, the
current-device symmetric-buffer allocation in SymmBuffer, and forwarding
`situ_kwargs` from fp8_fp4_mega_moe to `_C`.

SiTU stays unsupported in the side-LoRA paths added by #9 (as in production,
which has no side-LoRA): their kernels only implement SwiGLU/GeGLU and
fp8_fp4_mega_moe_side_lora / bf16_mega_moe_side_lora still reject it.

Co-authored-by: Ishaan Shivhare <[email protected]>
Co-authored-by: Matt Murphy <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ph-replay.patch)

Clear the active split-barrier lines before each mega_mhc launch so CUDA graph replays, which reuse %gridid, cannot observe a previous replay's completed tags. Previously applied at build time from Fireworks' third-party/deepgemm-mhc-graph-replay.patch (fw-ai/fireworks#49884); applied unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d-bf16.patch)

Add scoring_func='sigmoid_bf16' (ScoringType::SigmoidBF16), which rounds logits to BF16 before the sigmoid like low-latency routers, and the deep_gemm.bf16_mega_gate_supports_sigmoid_bf16 flag. Previously applied at build time from Fireworks' third-party/deepgemm-sigmoid-bf16.patch (fw-ai/fireworks#49884); applied unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…apability.patch)

Export deep_gemm.mega_mhc_supports_graph_replay so callers can detect the graph-replay barrier reset. Previously applied at build time from Fireworks' third-party/deepgemm-mhc-capability.patch (fw-ai/fireworks#49884); applied unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Risk: medium. Not approved. This is not a mechanical low-risk change: it adds SiTU kernel numerics, MHC CUDA-graph barrier reset, and sigmoid-bf16 router scoring, so a human should check activation/scoring correctness and graph-replay safety. Bugbot passed with no findings; Security Agent was not present. Human review is needed and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Approver

@vipulSharma18
vipulSharma18 added this pull request to stack #20 October 13, 2026 20:33
@vipulSharma18
vipulSharma18 requested a review from a team October 13, 2026 20:37
@vipulSharma18

Copy link
Copy Markdown
Author

@cursor is it possible for you to review and approve?

@cursor

cursor Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

I can't approve this PR, because my GitHub access is read-only and I have no approve action, so someone with write access needs to do that. I did review the full diff (10 files, +81/−19) and found nothing blocking. I'd be comfortable seeing it approved once the test gap below is addressed or accepted.

What I checked and found correct

  • SiTU kernel: for SwiGLU and GeGLU, gate_numerator and activated_up are just gate and up. So the existing math is unchanged, including the "PRE_DOWN must remain bitwise identical" expression. SiTU only changes the formula when its constant parameter is set.
  • Infinity as a template argument: the default situ_linear_beta is infinity, and it converts cleanly into the compiled kernel template. to_string(float) already turns infinity into cute::numeric_limits<float>::infinity(), so the != infinity check in the kernel works as written.
  • Training safety: the forward pass now accepts situ, but every backward and training entry still rejects it. That covers backward.py lines 273 and 1254 and mega_moe.hpp lines 552, 736 and 886. So SiTU forward results can't be silently paired with a SwiGLU backward.
  • Barrier reset: split_barriers.narrow(1, 0, num_m_blocks) stays in range, since num_tokens <= kNumMaxTokens is already asserted. The barrier buffer is per stream and only created outside graph capture, so the reset gets captured into the graph and runs on every replay, which is what the fix needs.
  • Mega gate: SigmoidBF16 = 2 uses a free slot in the enum (Identity is 3). Both the scalar and vector scoring paths round the logit to BF16 before the sigmoid.

Non-blocking suggestions

  1. No in-tree tests for the new features. tests/test_mega_gate.py:108 still asserts scoring_func in ('sigmoid', 'sqrtsoftplus', 'identity'), and nothing tests SiTU or mHC graph replay. The B300 parity runs in the description are strong evidence, but CI won't catch a future regression. Small additions would close this: a SiTU case in test_mega_moe, a sigmoid_bf16 reference in test_mega_gate, and one capture-and-replay check for mega_mhc.
  2. SiTU parameters are silently ignored for other activations. If you pass situ_beta with activation="swiglu", it has no effect but still creates a separate compiled kernel in the JIT cache. Rejecting situ_* unless activation == "situ" would be cleaner.
  3. Small per-call cost in mega_mhc. The zero_() adds one tiny kernel launch per call and sits between the previous kernel and mega_mhc, so it may reduce how much they overlap during decode. It's probably negligible, but worth a look in a decode profile.
  4. Stale comment. The comment at mega_mhc.hpp:32 says one-time zeroing is not replayed with the graph. With the new per-call reset that's no longer the whole story.

I didn't run any builds or GPU tests in this environment, and I made no changes to the branch.

Open in Web Open in Cursor 

@vipulSharma18

Copy link
Copy Markdown
Author

bugbot approve

@vipulSharma18

Copy link
Copy Markdown
Author

/bugbot approve

- tests/test_mega_moe.py: test_mega_moe_gated_activations(_ep2) runs fp8_fp4_mega_moe (EP1, and EP2 when
  two GPUs exist) for SwiGLU, GeGLU and SiTU against a dequantized PyTorch reference that follows the
  kernel's numerical boundaries. SiTU is checked for three (situ_beta, situ_linear_beta) settings and must
  differ from SwiGLU; SwiGLU/GeGLU outputs are bitwise unchanged with situ params unset, explicitly None,
  or set (they are ignored there, as today).
- tests/test_mega_gate.py: `sigmoid_bf16` reference (BF16-rounded logit before the sigmoid) and
  test_mega_gate_sigmoid_bf16 on inputs with order-independent exact FP32 logits; checks the capability
  flag and that plain-sigmoid scores are distinguishable.
- tests/test_mega_mhc.py: test_mega_mhc_graph_replay captures mega_mhc once and replays it back to back
  with different inputs, bitwise against eager, plus the capability flag. With the per-call barrier reset
  disabled in a scratch build this test fails reliably.
- Keep pytest from collecting the `test_filter` helper and test_mega_moe's spawn entry point.
- csrc/apis/mega_mhc.hpp: fix the stale comment on per-stream barrier allocation (comment only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vipulSharma18

Copy link
Copy Markdown
Author

Thanks @cursor. I've closed the test gap in e5c0103 and fixed the stale comment.

New tests (B300; on this branch built with no patches, test_mega_moe.py, test_mega_gate.py and test_mega_mhc.py pass: 8 tests)

  • test_mega_moe.py: test_mega_moe_gated_activations (1 rank) and _ep2 (2 ranks) run fp8_fp4_mega_moe for SwiGLU, GeGLU and SiTU against a PyTorch reference.
    • SiTU is checked at three (situ_beta, situ_linear_beta) settings. The error vs the reference is 4–7e-7, and the output differs from SwiGLU by ≥2.7e-2, so the check has teeth.
    • SwiGLU/GeGLU are bitwise identical whether the situ params are omitted, None or set.
  • test_mega_gate.py: adds a sigmoid_bf16 reference that rounds the logit to BF16 before the sigmoid, plus test_mega_gate_sigmoid_bf16 and the capability flag. Plain-sigmoid scores differ by 3–5e-8, so the feature is actually exercised.
  • test_mega_mhc.py: test_mega_mhc_graph_replay captures once and replays 8 times back to back with changing inputs, bitwise against eager. With the per-call reset removed in a scratch build it failed 3/3 runs; with the reset it passed 3/3.
  • mega_mhc.hpp:32: stale comment updated; comment only.
  • Collection fix: __test__ = False stops pytest collecting the imported test_filter and the spawn entry point as tests.

Not changed: I left the "reject situ_* for other activations" suggestion out on purpose. This PR folds the production patches as-is, and passing situ params with SwiGLU is ignored today; the new test pins that behavior. The mega_mhc zero_() overhead is likewise the shipped behavior.

🤖 Generated with Claude Code

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Risk: medium. Not approved. This is not a mechanical low-risk change: it adds SiTU kernel numerics, MHC CUDA-graph barrier reset, and sigmoid-bf16 router scoring, so a human should check activation/scoring correctness and graph-replay safety. Bugbot passed with no findings; Security Agent was not present. Human review is needed and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Approver

@vipulSharma18
vipulSharma18 merged commit 22b3823 into main Oct 14, 2026
2 checks passed
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