Skip to content

Fix two bugs in #825's response residual dropout (it made stage 1 diverge) - #826

Merged
jason718 merged 3 commits into
allenai:visionfrom
jason718:jasonr/fix-response-dropout-loss-masks
Aug 14, 2026
Merged

jason718 merged 3 commits into
allenai:visionfrom
jason718:jasonr/fix-response-dropout-loss-masks

Conversation

@jason718

@jason718 jason718 commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

#825 added Molmo2's response_residual_dropout. On GPU it turned out to be broken in two
independent ways, the second of which silently prevented stage 1 from training at all. Both are
fixed here, each with a regression test that fails on the old code.

Measured on 2 nodes of holmes (B300), 150 steps, from-scratch 4B

step 15 45 75 105 150
before (bug 2) 3.57 3.51 3.52 8.24 ~7.7
dropout disabled (control) 2.71 2.33 2.27 2.14 2.10
known-good reference run 3.26 2.32 2.11 2.03 1.96

The broken version never descends — CE holds at 3.3-4.1 for 75 steps, then blows up to ~9 with
grad norms spiking to 537 / 174 / 156 against a clip of 1.0. Disabling the dropout entirely
recovers the reference curve, which is what localised the bug.

Beaker: baseline 01KZZ9J77QVJ21MGGW2C934K73 (diverged), unfreeze-embeddings ablation
01KZZANAVXRDQNC28P7KGNF4B0 (still diverged, ruling out the embedding hypothesis), no-dropout
control 01KZZANAAV8BC7WM3QDECE2MWM (healthy).

Bug 1 — loss_masks was not forwarded on the dense-logits path

train_batch only passed loss_masks to the model on the response_logits_only=True branch. The
released Molmo2 recipe has response_logits_only: false, so the dense branch is the one that runs,
and it died on the first batch:

ValueError: loss_masks is required when the LM uses masked_dropout

Fixed by passing loss_masks on both branches (MultimodalLM.forward pops it and ignores it when
masked dropout is off).

Bug 2 — dropout sampled per token instead of per element

mm_olmo (olmo/nn/llm.py:916-946) broadcasts the per-token keep probability to the full input
shape and draws one Bernoulli per element:

dropout_shape = list(input.shape)                  # (B, T, D)
keep_prob = keep_prob.broadcast_to(dropout_shape)
multiplier = input.new_empty(dropout_shape).bernoulli_(keep_prob)

#825 called torch.rand_like(keep_prob) while keep_prob was still shaped (B, T, 1) — one
Bernoulli per token, which zeroes a token's entire d_model residual contribution at once.
Across 36 blocks x 2 residual adds that is 72 chances to delete a response token's contribution
outright: destructive rather than regularising, and a good match for training that limps and then
diverges.

Why the existing tests missed it

Every assertion in #825 holds identically under both samplings — "~9% of masked elements are
zero", "survivors are scaled by 1/0.9", "the expectation is preserved". The element-level
statistics are the same; only the correlation structure differs.

  • test_masked_dropout_samples_per_element_not_per_token targets that directly: at rate 0.5 with
    D=8, per-element sampling makes all-or-nothing tokens vanishingly rare (2 * 0.5**8 per token),
    so it asserts almost every masked token is partially dropped.
  • The new multimodal_train_module_test.py exercises train_batch — the real caller — across both
    logit paths. Bug 1 was invisible to tests that called MultimodalLM.forward directly with
    loss_masks already in hand.

Verification

13 tests pass across residual_stream_test.py and train_module/multimodal_train_module_test.py;
both new tests confirmed to fail on the pre-fix code. isort / black / ruff clean; mypy at
the pre-existing baseline. A 2-node run with dropout re-enabled on the fixed code is in flight
(01KZZHA0HJN9AXFE67Y9JAVT15) to confirm the curve now tracks the reference.

Jason Ren added 2 commits August 14, 2026 04:47
Training crashed on the first batch with
`ValueError: loss_masks is required when the LM uses masked_dropout
(response_residual_dropout)`, taking down both a 4-node full run and a 2-node
benchmark within three minutes of starting.

`MultimodalLM.forward` needs `loss_masks` to build the per-token residual drop
mask added in #825, but `train_batch` only forwarded it on the
`response_logits_only=True` branch. The dense branch — which is the default and
the released Molmo2 recipe — called the model without it, so every configuration
that actually uses `response_residual_dropout=0.1` raised immediately.

The gap was in the tests, not just the code: #825 verified masked dropout by
calling `MultimodalLM` directly *with* `loss_masks` supplied, which bypassed the
real caller. Nothing exercised `MultimodalTransformerTrainModule.train_batch`,
and there were no tests for that class at all.

Adds `src/test/train/train_module/multimodal_train_module_test.py`, which builds a
tiny train module single-process on CPU (dp_config=None, a stub trainer for the
metric sink) and runs `train_batch` across both logit paths, with and without
masked dropout. Confirmed it catches the bug: reverting the one-line fix fails
exactly `test_train_batch_with_response_residual_dropout[False]` with the
production error, and passes with it.

The tests are seeded. An unseeded random 2-layer model intermittently produces
non-finite logits, which the trainer's finite-loss guard then reports; that is an
artifact of a tiny randomly-initialised model (the real one starts from pretrained
Qwen3 + SigLIP2 weights) and unrelated to what these tests check. Verified
separately that masked dropout itself never produces non-finite output — 0/200
draws in both fp32 and bf16.

Verified: 150 passed / 20 skipped across src/test/train, src/test/nn/vision and
residual_stream_test. The 2 remaining failures (checkpoint_test remote-S3) and the
isort warning on multimodal_train_module.py are pre-existing on `vision`.
Stage 1 did not train. A 150-step 2-node benchmark held CE at 3.3-4.1 for 75 steps
(never descending) and then blew up to ~9, with grad norms spiking to 537/174/156
against a clip of 1.0. The last known-good from-scratch run descends monotonically
over the same span: 3.26 -> 2.47 (step 20) -> 2.11 (50) -> 1.96 (150).

Two 150-step ablations isolated it. Unfreezing the base embeddings did not help
(still 8.10 at step 75). Disabling `response_residual_dropout` fixed it completely:
2.71 / 2.33 / 2.27 / 2.14 / 2.10 across steps 15-150, tracking the reference.

The bug is in #825's `ResidualStream._masked_dropout`. mm_olmo broadcasts the
per-token keep probability to the **full input shape** and draws one Bernoulli per
element:

    dropout_shape = list(input.shape)                  # (B, T, D)
    keep_prob = keep_prob.broadcast_to(dropout_shape)
    multiplier = input.new_empty(dropout_shape).bernoulli_(keep_prob)

I sampled `torch.rand_like(keep_prob)` with `keep_prob` still shaped `(B, T, 1)`,
which draws one Bernoulli per *token* and therefore zeroes a token's entire
d_model residual contribution at once. Across 36 blocks x 2 residual adds that is
72 chances to delete a response token's contribution outright — destructive rather
than regularising, and consistent with training that limps then diverges.

Now matches mm_olmo: broadcast to `x.shape`, `torch.empty_like(x).bernoulli_()`,
then scale survivors by `1/keep_prob`.

The existing tests could not have caught this: "~9% of masked elements are zero"
and "survivors are scaled by 1/0.9" and "expectation is preserved" all hold
identically for per-token and per-element sampling. Added
`test_masked_dropout_samples_per_element_not_per_token`, which checks that at rate
0.5 with D=8 almost every masked token is *partially* dropped rather than
all-or-nothing (all-or-nothing has probability 2 * 0.5**8 per token under
per-element sampling). Confirmed it fails on the old code and passes on the new.

Verified: 13 passed across residual_stream_test and the train-module tests;
isort/black/ruff clean; mypy reports only the pre-existing 4 errors in other files.
Beaker: B0 baseline 01KZZ9J77QVJ21MGGW2C934K73 (diverged), D1 unfreeze
01KZZANAVXRDQNC28P7KGNF4B0 (still diverged), D2 no-dropout
01KZZANAAV8BC7WM3QDECE2MWM (healthy).
@jason718 jason718 changed the title Fix: pass loss_masks on the dense-logits path so response dropout works Fix two bugs in #825's response residual dropout (it made stage 1 diverge) Aug 14, 2026
…aN logits

The test added earlier in this PR failed about half the time, standalone, with:

    RuntimeError: Non-finite CE loss on rank 0: ce=nan, local_weight=8.0000, logits_nan=True

I had put this down to "an unseeded tiny random model occasionally produces non-finite logits" and
pinned `torch.manual_seed(0)`. That diagnosis was wrong, and the seed did not fix it — 6 of 12
standalone runs still failed. Seeding cannot help, because the parameters were never sampled from
the RNG at all: `MultimodalLMConfig.build` allocates them and leaves them unfilled, so the forward
ran on whatever happened to be in memory.

`MultimodalLM` has no `init_weights` of its own; the training script initialises the three
components separately (LM and ViT from pretrained checkpoints, connector via `reset_parameters`).
The test now does the same. `init_weights` also defaults `device` to `get_default_device()`, which
is CUDA wherever a GPU is visible, so it is pinned to CPU explicitly.

15 consecutive standalone runs now pass, where the previous code failed roughly half the time.
@jason718
jason718 merged commit b08f1d4 into allenai:vision Aug 14, 2026
1 check passed
@jason718
jason718 deleted the jasonr/fix-response-dropout-loss-masks branch August 14, 2026 15:59
jason718 added a commit that referenced this pull request Aug 14, 2026
…t is active (#828)

Stacked on #826 (its two commits appear here; review the top commit
only).

`TransformerModel.apply_activation_checkpointing` hardcoded
`preserve_rng_state = False`, directly
below a `TODO: only preserve RNG state if dropout is active`.
Checkpointing discards activations
and rebuilds them by re-running the forward during backward; with the
RNG state unpreserved, that
recompute draws **fresh** dropout masks, so the gradients are taken with
respect to a different
sample than the loss was.

Harmless until now — no olmo-core model used dropout, so the flag cost
nothing. #825 added Molmo2's
`response_residual_dropout=0.1`, and the released stage-1 recipe sets
`activation_checkpointing: true`, so the exact configuration we are
reproducing hit this on every
step.

## How it surfaced

Measured on 2 nodes of holmes, 150 steps, from-scratch 4B, while
sweeping throughput settings.
Turning activation checkpointing off is supposed to be mathematically
neutral. It moved the loss:

| arm | AC | microbatch | CE @150 | examples/s/device |
|---|---|---|---|---|
| baseline | on | 4 | 2.495 | 6.077 |
| A2 | **off** | 4 | **2.195** | 6.767 |
| A4c | on | 8 | 2.487 | 7.126 |
| C1 | **off** | 8 | **2.194** | 7.750 |

The split follows AC exactly, not microbatch: both AC-off arms land at
2.194/2.195, both AC-on arms
at 2.487/2.495. A neutral knob shifting the loss 0.3 nats is mismatched
gradients, not noise — the
baseline's own step-to-step oscillation is ±0.05. Beaker:
`01KZZHSVW4EMG4R6MYQ2Y66PC3` (A2),
`01KZZHSX95NCGV94CGSWH4HTF9` (A4c), `01KZZJNCNH2V46X9H0MX94DGQM` (C1).

## The fix

mm_olmo gates the same flag on the same condition in
`llm_activation_checkpoint_function`:

```python
preserve_rng_state = (
    (cfg.attention_dropout != 0.0) or
    (cfg.response_residual_dropout != 0.0) or
    (cfg.residual_dropout != 0.0)
)
```

`_dropout_is_active()` walks submodules for `ResidualStream` (whose
`masked_dropout` carries
`response_residual_dropout`) and plain `nn.Dropout`, so the per-block
save/restore is only paid when
there is a mask worth replaying.

## Tests

`test_checkpointed_grads_match_uncheckpointed_grads_under_dropout`
asserts the property that
actually matters — checkpointing must not change the gradient — and a
parametrized test pins the
flag itself so the cost stays off when dropout is disabled. Both fail on
the pre-fix code.

Ran `model_test.py` + `train_module/` with and without the change: 9
failed / 60 passed / 5 skipped
either way, empty set difference in both directions, so the pre-existing
MoE tensor-parallel
failures are untouched. isort/black/ruff clean.

## Note for the stage-1 recipe

With this fix, AC-on and AC-off are genuinely equivalent, and the new
test enforces it. That makes
`ac_config=null` a free +11.4% throughput win on B300 (62 -> 161 GiB
reserved, against 288 GiB
available) — worth considering separately for the stage-1 script, but
not changed here.

Co-authored-by: Jason Ren <[email protected]>
jason718 added a commit that referenced this pull request Aug 14, 2026
Stacked on #826 (its two commits appear here; review the top commit
only).

`DATA_PREFETCH_WORKERS`, `PACK_MAX_CROPS`, `PACK_BUFFER_SIZE` and
`PACK_IMAGE_WEIGHT` were module
constants passed straight into `MixtureDataLoader`, so sweeping any of
them meant editing the
script. They become `ExperimentConfig` fields with identical defaults (4
/ 27 / 48 / 1.0), so the
CLI can reach them.

Motivated by a measurement, not a hypothetical: the 2-node stage-1
baseline on holmes spends
**11.4% of step time in data loading** (0.213 s of a 1.86 s step, median
of the last 50 steps),
which makes prefetch-worker count a real throughput lever. The packer
parameters are the other
lever on examples/sec — and `pack_image_weight` is the one that had to
be pinned to mm_olmo's 1.0
rather than the loader's own 30.0 default, so having it visible in the
config dump is worth
something on its own.

Behaviour at the defaults is unchanged; this only widens what the CLI
can override. Verified that
the defaults still resolve to 4 / 27 / 48 / 1.0 and that
`--data_prefetch_workers=8
--pack_max_crops=45 --pack_image_weight=10.0` each take effect in the
built loader.

Co-authored-by: Jason Ren <[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.

1 participant