Skip to content

Fix int32 pointer-offset overflow in valid-prefix SwiGLU kernels - #950

Merged
no0p merged 2 commits into
mainfrom
codex/swiglu-int64-offsets
Nov 9, 2026
Merged

no0p merged 2 commits into
mainfrom
codex/swiglu-int64-offsets

Conversation

@no0p

@no0p no0p commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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. Results dataset: 01M6VRJVFPXDG9F7525MMBRQ8W; diagnostic source bundle: 01M6VRJ8K2AV60DYNSRHF0FRC2.
  2. Pinned vs patched kernel timing, plus eager reference: Beaker.

Scope and what remains

  • The static kernels originate in Add the OLMoDDP fused MoE / expert-parallel training stack #844; Add opt-in Core interfaces for RL training and weight publication #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 Allow eager no-gradient SwiGLU for scoring consistency checks #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.

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
no0p marked this pull request as ready for review November 7, 2026 20:50
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T20:51:26.728845Z 903cfd4 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@no0p
no0p requested a review from AkshitaB November 7, 2026 20:52

@AkshitaB AkshitaB left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@no0p LGTM. One optional improvement: parameterize the new dispatch test over BF16 as well as FP32, since low-precision rounding motivates this change.

@no0p
no0p merged commit 4485fe3 into main Nov 9, 2026
17 of 25 checks passed
@no0p
no0p deleted the codex/swiglu-int64-offsets branch November 9, 2026 22:03
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]>
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.

2 participants