Repository navigation
Mixed midtraining: faster vision encoder and connector - #924
Merged
Merged
Conversation
A crop the exchange sends to another rank is encoded there and draws that rank's dropout randomness, and the changed per-rank batch shapes advance every rank's random stream differently, so with vision dropout the balanced features and gradients would no longer match unbalanced encoding up to rounding. balance_vit_crops now requires zero vision attention and residual dropout, as trim_microbatch_image_padding does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0159PTUsp2w3YTANVDZ4BUss
…LayerNorm The trainable vision encoder was the largest part of a mixed mid-training step on B300s. - ViTAttention projects q, k and v in one matmul (wq, wk and wv stay separate parameters, so checkpoints are unchanged) and hands them to the attention as strided views, without head-transposed copies. - On SM100 GPUs that self-attention tries cuDNN's attention first (vision_scaled_dot_product_attention's cudnn_first_on_sm100; every other caller keeps flash and memory-efficient attention). PyTorch's flash attention is slow there for head dimension 72; cuDNN does the same work in about a third of the time, as accurate against a float32 reference. - The blocks' LayerNorms run olmo_core.kernels.layer_norm: Triton kernels not specialized on the row count, so the crop count changing every micro-batch costs no recompiles. PyTorch's weight and bias gradient reduction ran far below memory bandwidth: at 180K rows of 1152, its backward takes 3.0 ms and these kernels' 0.7 ms (forward 0.45 and 0.21 ms). - VIT_CROP_MICROBATCH defaults to 0, one encoder forward per micro-batch: with 16-crop chunks the GPU sat idle about 38% of the encoder's time on kernel launches. Without chunks, recomputation under activation checkpointing holds one block's activations for all crops (about 10 GB at 400 crops). Encoder on one B300, 240 crops with activation checkpointing: 650 ms instead of 1,147 ms per micro-batch. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0159PTUsp2w3YTANVDZ4BUss
Compact images keep each packed sequence's pooled rows padded to the batch's longest, so on a text sequence the connector pooled about 40K padding rows, and its gather's backward sorted every padded slot onto the first patch: about 470 ms per micro-batch, which every other rank waited for. - With compact images and host-side indices (the trainer's case), MultimodalLM.encode_images pools only the valid rows, indexing the encoder's features of all the batch's crops directly instead of placing them in the padded per-example layout first. A batch without image rows pools one padded row, which keeps the connector in the step. - The connector gathers with one index_select (backward: one index_add_) and computes the attention over each group's few patches as elementwise float32 ops instead of generic attention kernels. Outputs and gradients are unchanged up to rounding. The connector output RMS diagnostic now covers the valid rows only (it averaged in the padding). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0159PTUsp2w3YTANVDZ4BUss
Codex Review SummaryThis 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. |
jason718
pushed a commit
that referenced
this pull request
Oct 18, 2026
Batch building in a forked child process (batch_prefetch_process) and example loading in forked worker processes (prefetch_processes), both off by default, with identical batches, order and resume state; fewer large fresh allocations while packing and collating (reused knapsack table, defer_packed_image_copy, shared-memory compact images in the child); the recipe builds 8 batches ahead. With #924 on the 90/10 stage at 64 GPUs: 8.42 s/step median vs 20.92 on #916, CE equal to 4 decimals over 300 steps.
jason718
added a commit
that referenced
this pull request
Oct 18, 2026
cuDNN 9.10.2 (olmo-eval's torch 2.10 / cu128 image) builds no execution plan on B300 for the ViT's attention, with or without gradients, and PyTorch does not fall back, so every stage-1 eval of a checkpoint on #924's code failed. cuDNN 9.20.0 (the training image) runs all of it. Older cuDNNs now take the flash / efficient backends as before #924.
RustinS
added a commit
that referenced
this pull request
Oct 19, 2026
…lity check torch.backends.cudnn.version() raises when the loaded cuDNN is older than the one PyTorch was built against. The training image is built against 9.20 and loads 9.13, so cudnn_first_supported crashed every training job at the first vision forward. Read the loaded library's version directly instead, and lower the cuDNN-first threshold to 9.13: 9.10.2 builds no execution plan for the ViT's attention on SM100, while 9.13 ran all of it in the #924 training runs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0159PTUsp2w3YTANVDZ4BUss
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.
Into #916's branch, after #923 (1a2064c): three commits, reviewable separately. The encoder and
connector changes are always on and add no options.
Summary
another rank would draw that rank's dropout randomness, so
balance_vit_cropsnow requires zerovision attention and residual dropout, as
trim_microbatch_image_paddingdoes.ViTAttentionprojects q, k and v in one matmul over the concatenatedwq,wkandwvweights (the parameters stay separate, so checkpoints are unchanged) and hands them to the
attention as strided views, without the head-transposed copies. This applies to
self-attention without grouped KV heads; anything else takes the existing path.
(
vision_scaled_dot_product_attention(..., cudnn_first_on_sm100=True)). Every other caller,and every other GPU, keeps flash and memory-efficient attention.
olmo_core.kernels.layer_norm: Triton forward and backward kernelsthat are never specialized on the row count, with float32 statistics and a deterministic
weight and bias reduction. CPU inputs, rows wider than 8,192 and environments without Triton
fall back to
F.layer_norm. The modules are stillnn.LayerNorms with the same state dict.VIT_CROP_MICROBATCHdefaults to 0: one encoder forward per micro-batch instead of 16-cropchunks.
MultimodalLM.encode_imagespools only the valid pooled rows, indexing the encoder's features of all the batch's crops
directly instead of first placing them in the padded per-example layout. A batch without
image rows pools one fully padded row, so the connector stays in every step.
index_select(backward: oneindex_add_)instead of advanced indexing, and computes the attention over each group's few patches as
elementwise float32 ops (no grouped KV heads; otherwise the existing SDPA path).
Why
With crops balanced (#923) and batches built outside the trainer process (#925), the trainable vision encoder was the largest part of a 90/10 step, and three things made it
slow on B300s:
backward; the GPU sat idle about 38% of the encoder's time waiting on kernel launches.
time. cuDNN does the same work in 37% of the time. FA4 cannot run head dimension 72, and padding
the heads to 96 or 128 is slower than cuDNN.
LayerNorm reduces the weight and bias gradients far below memory bandwidth at 180K rows.
torch.compilefixes that only for static shapes, and the crop count changes every micro-batch.On a text sequence the connector pooled about 40K padding rows (compact images keep each packed
sequence's pooled rows padded to the batch's longest), and the gather's backward sorted every
padded slot onto the first patch: about 470 ms per micro-batch, which every other rank waited for.
Results
Encoder alone, one B300, 240 crops with activation checkpointing:
LayerNorm at 179K x 1152 bf16, forward / backward:
torch.compile, dynamic shapeslayer_norm_fnolmo_core.kernels.layer_normIn training, 90/10 split (
--recipe.visual_data=stage1_v3 --recipe.text_loss_share=0.9, the21,000-step schedule of the v3 90/10 run), crops balanced, batches built in a child process, 8 B300s,
same batches; seconds per step from CUDA events:
End to end, 16 GPUs
90/10 split on 2 nodes, same batches as the 2-node runs of #923; medians over steps 11-200, or
11-100 for the branches as committed:
Mean CE by window:
the load-balancing loss differs by at most 0.0001. The prototype's mean offset fell from -0.0002
to 0.0000 by steps 151-200. The committed branches and the prototype differ by 0.0002 per step
on average, as much as the prototype differs from Mixed midtraining: opt-in crop balancing for the vision encoder #923.
Numerics, against a float32 reference of the original path: encoder output error 7.50e-3 for both
the original and the new bf16 path, median gradient error 8.93e-3 and 8.88e-3; connector 5.9e-3
and 5.7e-3. A 100-step A/B on 8 GPUs, same batches: CE differs from the original by 0.00032 on
average (largest 0.0013), while two runs of the new code differ by 0.00027 (largest 0.0012).
Behavior changes to note:
vision_sdpa_backendsexcluded cuDNN for vision ("cuDNN math pitfalls"). This turns it on onlyfor the ViT's unmasked self-attention on SM100, where it was measured as accurate as flash.
all of a micro-batch's crops: about 10 GB more peak memory at 400 crops.
VIT_CROP_MICROBATCHstill sets a chunk size.
connector output RMSdiagnostic now covers the valid rows only; it used to average in thepadding rows' outputs.
Tests
kernels/layer_norm_test.py: forward and all three gradients against float32 PyTorch forfloat32 and bf16, 1 to 4,099 rows, widths 72 and 1,152; strided inputs; the CPU fallback.
image_vit_test.py: the fused self-attention matches separate projections and plain softmaxattention, outputs and gradients (CPU); on GPU, at SigLIP's sizes, it is as close to float32 as
the unfused path; the block norms keep
nn.LayerNorm's type and state dict.connector_test.py: the few-key attention matches SDPA, with and without masks and with fullymasked groups; the connector matches the advanced-indexing and SDPA formulation, outputs and
gradients, with partly padded groups and padded rows.
multimodal_test.py: the connector receives only the valid rows (one padded row for a text-onlybatch);
_compact_pooled_rowsoffsets; the existing compact-versus-padded tests, bitwise on CPU.src/test/nn/vision/passes, including the CLIP, SigLIP, SigLIP2 and Molmo2 parity testsagainst Hugging Face.
test_document_mode_isolates_packed_examples_through_kdafails the same wayon [Do not merge yet] Mixed midtraining on OLMo 3.5: the OLMo-core side of scaling-ladders#83 #916's head, 1a2064c (its tolerance is the size of the tiny model's logits), and so does
test_native_checkpoint_loads_into_multimodal_and_roundtrips(as noted in Mixed midtraining: opt-in crop balancing for the vision encoder #923).multimodal_ddp_train_module_test.py:balance_vit_cropswith vision attention or residualdropout fails at construction.
How to test
The GPU tests need one GPU.
🤖 Generated with Claude Code
https://claude.ai/code/session_0159PTUsp2w3YTANVDZ4BUss