Skip to content

Molmo2-Stage1: enable LM activation checkpointing, microbatch 4, and the 2D-knapsack packer - #824

Merged
jason718 merged 1 commit into
allenai:visionfrom
jason718:jasonr/stage1-ac-and-packer
Aug 14, 2026
Merged

jason718 merged 1 commit into
allenai:visionfrom
jason718:jasonr/stage1-ac-and-packer

Conversation

@jason718

Copy link
Copy Markdown
Contributor

The two remaining settings from the released Molmo2-4B-Pretrain/config.yaml that were
reachable without new code. Both are already-implemented capabilities this script never
turned on.

Activation checkpointing + microbatch 4

Released config: activation_checkpointing: true, llm.activation_checkpoint: whole_layer,
device_train_microbatch_size: 4.

ac_config already exists on MultimodalTransformerTrainModuleConfig and wires to
Transformer.apply_activation_checkpointing — it was simply left None, which is why
microbatch >1 OOM'd in earlier probes. Setting mode=full (checkpoint every block, i.e.
mm_olmo's whole_layer) and microbatch 4 together, since the first is what pays for the
second. Vision and connector AC were already on by default, matching the released config's
vit activation_checkpointing: true and connector_activation_checkpointing: true.

Divisibility: 128 % (4 x dp_world_size) must be 0, so microbatch 4 supports world sizes
up to 32 — the released 4B ran 16 GPUs, the 8B ran 32. At 64 GPUs use microbatch 2.

2D-knapsack packing

Released config: packing: {mode: dynamic_solver, buffer_size: 48, text_weight: 1.0, image_weight: 1.0}.

iter_dynamic_packs (a verbatim port of mm_olmo's solver) has been in the repo since #806,
but stage 1 never passed pack_max_crops, so it silently fell through to token-only
next-fit. Now wired — with image_weight=1.0, since MixtureDataLoader defaults to
30.0 and wiring it up without overriding that would still have diverged.

PACK_MAX_CROPS = 3 * (1 + 8) — the crop budget is the knapsack's second dimension. Measured
with build_image_token_ids, an 8-crop example costs at most 9 crops (1 global + 8 high-res)
and ~1348 image tokens, so at seq 2560 the token budget binds first and the crop cap
mainly bounds worst-case ViT/collator memory. Set below a single example's 9 crops, it would
force that example into its own mostly-padding pack.

Measured effect, and where it actually comes from

Synthetic stream shaped like the real mixture (image examples ~1400 tok / 9 crops, text-only
~600 tok), 81 packs each:

stream knapsack next-fit
image-only 1.00 ex/pack, 54.7% fill identical
with text-only (Tulu) 1.70 ex/pack, 69.3% fill 1.00 ex/pack, 44.3% fill

So the gain is almost entirely cross-modal packing. iter_packs deliberately refuses to
mix text-only and image-bearing examples in one pack ("head-truncating a cross-modal pack can
orphan <im_patch> tokens from their pooled rows"); the knapsack has no such guard, and
mm_olmo likewise packs across modalities whenever nlp > 0
(shortcut_max_len_images: args.nlp == 0).

That removed guard deserves review attention. The invariant it protects — one
<im_patch> token per pooled row — held in 0/81 packs for both packers on both streams,
because the knapsack respects the token budget by construction so nothing gets truncated.
Note also that the equal-length synthetic understates the knapsack on image-only data: real
caption lengths vary, which a knapsack can exploit and sequential next-fit cannot.

Verification

  • 242 passed / 17 skipped across src/test/nn/vision and src/test/data/multimodal.
  • Packer exercised directly (numbers above), including the <im_patch>/pooled-row invariant.
  • isort / black / ruff / mypy clean.

Not yet validated

Whether microbatch 4 with AC actually fits at seq 2560 has not been confirmed on GPU.
That is the entire point of the change and it matches what mm_olmo ran on comparable
hardware, but it warrants a short GPU run before a full launch — earlier microbatch probes
OOM'd precisely because AC was off.

…the 2D-knapsack packer

The two remaining released-config settings that were reachable without new code.
Both are already-implemented capabilities this script simply never turned on.

Activation checkpointing + microbatch 4

The released config has `activation_checkpointing: true` with
`llm.activation_checkpoint: whole_layer` and `device_train_microbatch_size: 4`.
`ac_config` already exists on MultimodalTransformerTrainModuleConfig and wires to
`Transformer.apply_activation_checkpointing`; it was just left None, which is why
microbatch >1 OOM'd previously. Setting mode=full (checkpoint every block, i.e.
mm_olmo's whole_layer) and microbatch 4 together. Vision and connector AC were
already on by default, matching the released config's vit
`activation_checkpointing: true` and `connector_activation_checkpointing: true`.

Divisibility note: 128 % (4 x dp_world_size) must be 0, so microbatch 4 supports
world sizes up to 32 — the released 4B ran 16 GPUs and the 8B ran 32. At 64 GPUs
use microbatch 2.

2D-knapsack packing

The released config has `packing: {mode: dynamic_solver, buffer_size: 48,
text_weight: 1.0, image_weight: 1.0}`. `iter_dynamic_packs` (a verbatim port) has
been in the repo since #806 but stage 1 never passed `pack_max_crops`, so it fell
through to token-only next-fit. Now wired, with image_weight 1.0 — note the
MixtureDataLoader default is 30.0, so wiring it up without overriding that would
still have diverged.

`PACK_MAX_CROPS = 3 * (1 + 8)`: the crop budget is the knapsack's second dimension.
Measured with build_image_token_ids, an 8-crop example costs at most 9 crops
(1 global + 8 high-res) and ~1348 image tokens, so at seq 2560 the token budget
binds first and the crop cap mainly bounds worst-case ViT/collator memory. Below a
single example's 9 crops it would force that example into its own mostly-padding
pack.

Measured effect, and where it comes from

On a synthetic stream shaped like the real mixture (image examples ~1400 tok /
9 crops, text-only ~600 tok), 81 packs each:

  image-only stream : knapsack 1.00 ex/pack 54.7% fill | next-fit identical
  with text-only    : knapsack 1.70 ex/pack 69.3% fill | next-fit 1.00, 44.3%

So the gain is almost entirely cross-modal packing. `iter_packs` deliberately
refuses to mix text-only and image-bearing examples in one pack ("head-truncating a
cross-modal pack can orphan <im_patch> tokens from their pooled rows"); the
knapsack has no such guard, and mm_olmo likewise packs across modalities whenever
nlp > 0 (`shortcut_max_len_images: args.nlp == 0`).

That guard is worth being explicit about, since this removes it for stage 1. The
invariant it protects — one <im_patch> token per pooled row — held in 0/81 packs
for both packers on both streams, because the knapsack respects the token budget by
construction so nothing gets truncated. The equal-length synthetic also understates
the knapsack on image-only data; real caption lengths vary, which it can exploit
and sequential next-fit cannot.

Verified: 249 passed / 17 skipped across src/test/nn/vision,
src/test/data/multimodal and embedding_test; isort/black/ruff/mypy clean.

NOT yet validated on GPU: whether microbatch 4 with AC actually fits at seq 2560.
That is the point of the change and it matches what mm_olmo ran on comparable
hardware, but it should get a short GPU run before a full launch.
jason718 added a commit that referenced this pull request Aug 14, 2026
…dual dropout (#825)

> Stacked on #824 (AC + knapsack packer). Review that one first; this
branch contains its commit.

Closes the last two released-config settings that needed real code.
Auditing the remaining
list first showed **three of the five were not gaps at all**:

| Item | Finding |
|---|---|
| `multi_component_grad_norm: true` | **Dead field** in mm_olmo —
declared at `trainer_config.py:720`, never read anywhere in the repo. |
| `float32_attention: true` | **No-op on the path the released config
uses.** With `attention_type: sdpa` it casts only q,k to fp32; under
`amp_bf16` autocast SDPA casts them back (output is bf16). Running the
same code *outside* autocast raises `Expected query, key, and value to
have the same dtype` — it only works because autocast nullifies it. It
would matter on the `direct` path, which the released config doesn't
use. |
| `enable_variable_sized_token_pooling: true` | **Collator memory
optimization** over mm_olmo's own `TOKEN_POOLING_KEYS` buffers, which
our pipeline doesn't use. No modelling effect. |

That left compile scope (throughput) and response dropout (modelling).

## Compile: `compile_vit: blocks`, `compile_connector: dynamic`

`VisionTransformer.apply_compile` compiles each ViT block, mirroring the
per-block strategy
`Transformer.apply_compile` already uses.
`VisionConnector.apply_compile` compiles pooling and
projector with `dynamic=True` — the pooled-group count follows the
per-batch crop count, so a
static compile would recompile on every new shape.

Both sit behind new `compile_vision` / `compile_connector` flags on the
train module (default
`True`) and only fire when `compile_model` is set, so this is inert
unless compilation is
already on.

## Response residual dropout

mm_olmo's `Dropout(mask_p=...)` gives response tokens a different keep
probability from
everything else, applied at **both** residual adds:

```python
keep_prob = drop_mask * (1 - mask_p) + (1 - drop_mask) * (1 - p)
```

Our `ResidualStream` had a single uniform `nn.Dropout`, which is why
this has been carried as
a `KNOWN DELTA` comment in the stage-1 script since the beginning.

`ResidualStream` gains `masked_dropout` implementing exactly that
formula, plus an optional
`drop_mask` threaded `Transformer.forward` → `TransformerBlock` → both
residual streams.
`MultimodalLM.forward` derives the mask from `loss_masks` (`loss_masks >
0` *is* "is a response
token") and only builds it while training and when a block will consume
it. `drop_mask` is an
explicit block kwarg rather than riding `**kwargs`, so it isn't
forwarded into the sequence
mixer.

**Blast radius is contained:** `masked_dropout` defaults to 0 everywhere
and is validated to
require the `default` block type (what Molmo2 uses). At 0 the path is
byte-identical to before,
so every other model in the repo is untouched. The stage-1 script sets
0.1.

It is deliberately **not** defaulted on the `molmo2_4B`/`molmo2_8B`
factories: those describe
the released weights for inference and must keep matching the HF-derived
config, so the
training regularizer lives with the other training settings.

## Verification

Masked-dropout semantics, measured over 4000 tokens:

```
unmasked (prompt/image) tokens zeroed : 0.0000   <- never dropped
masked (response) tokens zeroed       : 0.0910   <- rate 0.1
surviving value                       : 1.1111   = 1/0.9, inverted dropout
E[response]                           : 1.0100   <- unbiased
eval mode                             : no dropout
masked_dropout=0                      : byte-identical to the plain path
```

* End to end through `MultimodalLM`: train-mode forward is stochastic
with the rate set and
  deterministic without it; eval is deterministic either way.
* 6 new tests in `src/test/nn/residual_stream_test.py`.
* **510 passed** / 1195 skipped across `src/test/nn` and
`src/test/data/multimodal`. The one
failure, `convert_test.py::test_logprobs_match_after_roundtrip[gemma3]`,
is a gated-HF-repo
  auth error that fails identically on `vision`.
* **Real-weight Molmo2-4B parity: 5 passed** on GPU (logits, LM forward,
embedding, training
  logits).
* `isort`/`black`/`ruff` clean on changed files; `mypy` at the 21-error
`vision` baseline. (The
two isort errors reported for `connector.py` and
`multimodal_train_module.py` are
  pre-existing — same count on the baseline.)

## Not measured

The **throughput gain** from compiling the ViT and connector. That needs
a GPU run, best done
together with the activation-checkpointing change in #824.

---------

Co-authored-by: Jason Ren <[email protected]>
@jason718
jason718 merged commit cb17582 into allenai:vision Aug 14, 2026
1 check passed
@jason718
jason718 deleted the jasonr/stage1-ac-and-packer branch August 14, 2026 03:05
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