Repository navigation
Allow eager no-gradient SwiGLU for scoring consistency checks - #951
Merged
Merged
Conversation
This was referenced Nov 7, 2026
no0p
marked this pull request as ready for review
November 7, 2026 20:47
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. |
no0p
added a commit
that referenced
this pull request
Nov 9, 2026
## 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 as `row_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 * hidden` columns. OLMo-core has two implementations of that activation. Gradient-enabled forwards use plain PyTorch (`up * F.silu(gate)`). Forwards with gradients off use `swiglu_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 `r` is `r * 2048` when the expert hidden size is 1024, so it overflows once `r` reaches 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: | Case | Pinned kernel | Patched kernel | |---|---:|---:| | 1,044,480 rows, hidden 1,024 | 0 mismatching rows | 0 | | 1,052,672 rows, hidden 1,024 | 4,096 mismatching rows, starting at 1,048,576 | 0 | 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: 1. Boundary reproduction, patched comparison, and targeted GPU suite: [Beaker](https://beaker.org/ex/01M6VRJVFEXM5QAM40RD4C58S0). Results dataset: `01M6VRJVFPXDG9F7525MMBRQ8W`; diagnostic source bundle: `01M6VRJ8K2AV60DYNSRHF0FRC2`. 2. Pinned vs patched kernel timing, plus eager reference: [Beaker](https://beaker.org/ex/01M6VVM7200Y9GY6M5T6G69JSZ). ## Scope and what remains - The static kernels originate in #844; #888 added the dynamic-row forward variant with the same arithmetic. Both current main and the runtime's pinned Core revision contain the bug. - This PR fixes the kernel for every caller. The companion #951 is a separate design change that lets RL scoring run the eager activation under no-grad so its consistency check no longer depends on two implementations matching. Both are needed: this one because the kernel corrupts memory for anyone who uses it at scale, that one because a correctness check should not rest on kernel parity. - The large-pack B300 model-level validation remains pending; this PR establishes the kernel defect and fix without yet claiming it explains every observed model-level discrepancy. - The adjacent legacy MoE and MXFP8 kernels also contain offset expressions needing separate large-buffer validation; they are not changed in this narrowly scoped fix. --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
AkshitaB
approved these changes
Nov 9, 2026
no0p
force-pushed
the
codex/eager-no-grad-activation
branch
from
November 9, 2026 22:06
7315220 to
c4086c8
Compare
no0p
enabled auto-merge (squash)
November 9, 2026 23:51
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
Adds one option to the routed-expert MoE layer:
RoutedExpertsConfig.fused_no_grad_activation(defaultTrue, so nothing changes unless a caller opts in). Setting it toFalsemakes the expert activation undertorch.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:
up * F.silu(gate)): plain PyTorch ops. This is what every gradient-enabled forward uses, so it is the arithmetic that training actually sees.swiglu_valid_prefix): a Triton kernel introduced with the fused MoE stack in Add the OLMoDDP fused MoE / expert-parallel training stack #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_roundingflag 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
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.