Repository navigation
Fix int32 pointer-offset overflow in valid-prefix SwiGLU kernels - #950
Merged
Merged
Conversation
This was referenced Nov 7, 2026
CI's CHANGELOG check requires an entry for source changes, and black 23.12.1 wraps the long allocation line in the new large-offset backward test. Co-Authored-By: Claude Fable 5.1 <[email protected]>
no0p
marked this pull request as ready for review
November 7, 2026 20:50
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
AkshitaB
approved these changes
Nov 9, 2026
no0p
added a commit
that referenced
this pull request
Nov 10, 2026
## What this changes Adds one option to the routed-expert MoE layer: `RoutedExpertsConfig.fused_no_grad_activation` (default `True`, so nothing changes unless a caller opts in). Setting it to `False` makes the expert activation under `torch.no_grad()` use the same eager PyTorch expression as training, `up * F.silu(gate)`, instead of the fused Triton kernel that the layer currently selects whenever gradients are off. ## Background for readers outside the RL work Inside each MoE expert, the layer computes a SwiGLU activation. OLMo-core has two implementations of it: - **Eager** (`up * F.silu(gate)`): plain PyTorch ops. This is what every gradient-enabled forward uses, so it is the arithmetic that training actually sees. - **Fused valid-prefix kernel** (`swiglu_valid_prefix`): a Triton kernel introduced with the fused MoE stack in #844. It does the same math in one pass over memory and can skip padded rows. It has no autograd rule, so the layer only picks it when gradients are disabled. For pretraining this distinction never matters: training always runs with gradients on, and the fused kernel is only taken on forward-only paths. RL post-training (MILES) is different. Each update needs the "before the update" log-probabilities of the sampled tokens, which the loss uses as the denominator of its importance ratio. The straightforward way to get them is a dedicated forward pass with gradients off, run before the training forward. MILES takes a shortcut when there is exactly one optimizer step per batch: the weights have not changed yet, so it reuses the training forward's own log-probabilities and skips the separate pass. To make sure that shortcut is valid, it periodically runs both passes on the same tokens and expert routes and requires them to agree. Today those two forwards take different activation code. They agree only because #888 added a `match_eager_rounding` flag that makes the fused kernel round the way eager does. A kernel defect found this week (#950) showed how fragile that is: the two paths disagreed, and the scoring check was the only thing that noticed. With this option, the scoring forward and the training forward run identical ops, so their agreement holds by construction rather than by careful kernel matching. The consistency check then only has to catch non-determinism or memory corruption, which is what it is good at. ## Scope - Existing callers keep the fused default. Explicit FP8 and wave-kernel callers are unaffected. - Eager computes all capacity rows rather than only the valid prefix, and materializes one intermediate the size of the activation. In the RL scoring pass, which runs once per process in the default configuration, that is a few milliseconds. Workloads that score every update should measure. - This is a separate design change from the pointer-overflow repair in #950. #950 fixes a bug in the fused kernel for everyone who uses it; this PR lets RL scoring stop depending on that kernel at all. Both remain necessary. ## Validation Tests cover config serialization, fused/eager dispatch under both grad modes, exact eager output, and unchanged gradients. CPU validation: 7 passed, 39 GPU cases skipped across the new and existing SwiGLU suites; Black/isort/Ruff pass. GPU dispatch and large-pack model validation are pending in the MILES integration smoke. No runtime performance estimate is claimed. --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
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.
What this changes
The three valid-prefix SwiGLU Triton kernels in
olmo_core/kernels/swiglu.py(static forward, dynamic-row forward, and backward) compute memory offsets asrow_idx * stride + col_idx. All three did that multiplication in signed 32-bit integers. This change widens the row and column indices to int64 before the multiply, in all three kernels. It changes no rounding, filtering, or thresholds, and adds regression tests that cross the 32-bit boundary.Background for readers outside the RL work
Inside each MoE expert, the layer computes a SwiGLU activation over a buffer with one row per (token, expert) pair and
2 * hiddencolumns. OLMo-core has two implementations of that activation. Gradient-enabled forwards use plain PyTorch (up * F.silu(gate)). Forwards with gradients off useswiglu_valid_prefix, a fused Triton kernel added with the fused MoE stack in #844, which does the same math in one memory pass and can skip padded rows.A 32-bit signed integer holds values up to 2,147,483,647. The kernel's offset for row
risr * 2048when the expert hidden size is 1024, so it overflows oncerreaches 1,048,576. Past that row the offset wraps negative and the kernel reads its inputs from, and writes its outputs to, memory about 4 GB below the buffer. The result is wrong values for those rows and silent writes into whatever tensor lives at the wrapped address. The row count and the stride each fit comfortably in int32; only their product does not, which is why nothing in the wrapper's validation caught it.Why it surfaced now. The rows a GPU handles per layer are roughly its tokens times the experts per token. With 16 experts per token, a 65,536-token packed sequence is 1,048,576 rows, exactly the boundary. MILES RL training recently raised its packing limit from 34,816 to 65,536 tokens to fit long-prompt datasets, and the MILES scoring pass runs with gradients off, so it was the first workload to take this kernel across the line. Pretraining never reaches the kernel because it always runs with gradients on. The MILES scoring consistency check, which compares the no-gradient scoring forward against the gradient-enabled training forward on identical tokens, is what noticed the disagreement.
Evidence
Offline compilation confirms an i32 multiply followed by signed pointer widening. A one-GPU reproducer confirms exact agreement below the boundary and wrong values beginning exactly at it:
Both static and dynamic variants have the same result. The reproducer uses a controlled sentinel prefix inside its own allocation, so wrapped reads remain mapped and cannot depend on unrelated allocations. All 46 tests in the targeted SwiGLU suite pass on one A100, including new large-offset forward/backward regressions; CPU execution passes 6 with 40 GPU skips. Black, isort, Ruff, and diff checks pass.
Performance. The widening has no measurable cost. On one A100-80GB at hidden 1024, bf16, median of 50 launches, the patched kernel is within +2% / −3% of the pinned kernel at 131K, 524K, and 1,044K rows for both variants, inside run-to-run noise; all variants move about 1.7 TB/s, so the kernel is memory-bound and the extra index arithmetic hides behind the loads. Outputs are bit-identical to the pinned kernel and to eager at every shape.
Runs:
01M6VRJVFPXDG9F7525MMBRQ8W; diagnostic source bundle:01M6VRJ8K2AV60DYNSRHF0FRC2.Scope and what remains