Repository navigation
Fix OLMoDDP Torch 2.13 training and preserve HF inference numerics - #885
Conversation
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6110087010
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dd65536b67
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f09d694f68
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| weight = torch.cat((expert.up_proj.weight.t(), expert.gate_proj.weight.t()), dim=1) | ||
| up, gate = (x.reshape(-1, x.shape[-1]) @ weight).chunk(2, dim=-1) | ||
| hidden = expert.act_fn(gate) * up | ||
| down_weight = expert.down_proj.weight.t().contiguous().unsqueeze(0) |
There was a problem hiding this comment.
Avoid repacking full expert weights on every forward
When a newly exported model runs through the eager Hugging Face path, every dense or shared-expert invocation concatenates the complete up/gate matrices and makes a contiguous copy of the complete down matrix. During token-by-token generation this repeats for every layer and token, adding weight-sized allocations and copies that can substantially increase latency and peak memory—especially for the supported 32B export—and can cause otherwise fitting models to OOM. Preserve the packed layout in cached/module-owned tensors, with appropriate invalidation for weight updates, rather than rebuilding both matrices in each forward.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed that the eager HF reference now pays for packing dense/shared weights on each call. I am leaving caching as a separate performance follow-up: the production native vLLM plugin uses its own kernels and does not call this helper, while a simple persistent cache would retain a second copy of these weights across all layers. A safe optimization needs owned packed storage (or a bounded cache), plus qualification for load/assign, device/dtype moves, weight updates, gradients and serialization. The current implementation preserves strict core/HF numerics and bounded temporary storage; it is not being presented as the optimized generation path. Leaving this thread open so that tradeoff is visible to reviewers.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3c84d1c900
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| return (up * gate * torch.sigmoid(gate)).to(up_gate.dtype) | ||
|
|
||
|
|
||
| @torch.library.custom_op("olmo3_moe_hf::core_combine", mutates_args=()) |
There was a problem hiding this comment.
Make custom-op registration idempotent
Loading an exported checkpoint with trust_remote_code=True in a process that has already imported the in-tree model—such as an export-and-validate workflow—or loading two separately cached revisions imports moe_numerics.py under another module name and executes this decorator again. PyTorch permits a qualified operator to be created only once, so the second import fails during module initialization with a duplicate olmo3_moe_hf::core_combine registration instead of loading the model; reuse the existing operator or otherwise guard both custom-op definitions.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ed144f371d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if self.config.moe_use_core_numerics: | ||
| return _core_shared_expert_forward(self, x) |
There was a problem hiding this comment.
Preserve separate projections for standard dense layers
When an exported non-KDA model contains a standard ReorderedNormTransformerBlock dense layer (dense_layers_use_shared_expert=False), the global moe_use_core_numerics=True still sends its HF Olmo3MoeDenseMLP through this packed projection helper. The source FeedForward.forward evaluates w1 and w3 as separate linear operations, so packing them into one GEMM changes BF16 CUDA accumulation order and can change exported logits—the opposite of the intended numerical preservation. Gate this packed path on the dense layer actually originating from SharedExperts, not only on the model-wide numerics flag.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4fb8ce6ce3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| return all( | ||
| type(proj) is nn.Linear and not proj._forward_hooks and not proj._forward_pre_hooks | ||
| for proj in (expert.up_proj, expert.gate_proj, expert.down_proj) |
There was a problem hiding this comment.
Honor Accelerate offload hooks before packing projections
When an exported model uses Accelerate CPU/disk offloading (for example via accelerate.cpu_offload) on these projection modules, Accelerate wraps forward and stores _hf_hook without populating _forward_hooks; the weights may therefore remain meta/offloaded until that wrapped call runs. This predicate still selects the packed path, which reads .weight directly and bypasses the load/offload hook, causing large-model inference to fail. Treat wrapped/Accelerate-hooked projections as unpackable so their normal forwards execute.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7124894b95
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| K = topk_ids.shape[1] | ||
| values = hidden_states.new_zeros((N, K, H)) | ||
| for expert_id, expert in enumerate(self): | ||
| output = self._expert_forward(expert, hidden_states) |
There was a problem hiding this comment.
Invoke routed expert modules so their hooks run
When a consumer registers a forward/pre-forward hook on an individual routed expert, the new core-numerics paths bypass expert.__call__: this compile fallback calls _expert_forward directly, the eager loop does the same, and the grouped path reads the projections itself. These hooks were honored by the previous expert(hidden_states) implementation, so newly exported models can silently omit expert-level adapters, instrumentation, or offload behavior; detect such hooks and route through the module call (or explicitly preserve their semantics).
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9a12340363
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| return bool( | ||
| module._forward_hooks | ||
| or module._forward_pre_hooks | ||
| or hasattr(module, "_hf_hook") | ||
| or "forward" in module.__dict__ |
There was a problem hiding this comment.
Honor backward hooks before bypassing module calls
When a consumer registers a full backward or backward-pre hook on a routed expert or one of its projections, this predicate still treats the module as safe to bypass because it checks only forward hooks. The packed paths then read projection weights directly, and _expert_forward may skip the expert's __call__, so those backward hooks are never attached or invoked during fine-tuning or gradient instrumentation; include _backward_hooks and _backward_pre_hooks in the customization guard.
Useful? React with 👍 / 👎.
No code yet. The branch starts at #916's head (4fb33e9), which includes the main sync (#885, #910-#912, #920). #916's earlier ac87d76 trained the 64K mixed mid-training checkpoint this SFT starts from. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KshPetwmP4KzQGok1mmMXs
No code yet. The branch starts at #916's head (4fb33e9), which includes the main sync (#885, #910-#912, #920). #916's earlier ac87d76 trained the 64K mixed mid-training checkpoint this SFT starts from. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KshPetwmP4KzQGok1mmMXs
OLMoE3 training on B300 with Torch 2.13 failed before its first optimizer step: packed BF16 parameters could violate compiled kernels’ alignment assumptions, and a separately compiled loss could replace Dynamo’s stream registry before shared-expert backward. Cold-cache LC data packing also exceeded the default distributed startup timeout.
This preserves 16-byte starts in flat model parameter buffers while keeping logical parameter shapes and packed collective offsets unchanged. Shared-expert stream contexts run outside the compiled graph on both the no-EP and rowwise-NVSHMEM paths; child math stays compiled, with the existing arithmetic, stream dependencies, and overlap. Experiment configs now expose the process-group timeout (default unchanged at 15 minutes), allowing the companion LC workload to use two hours for cold-cache packing.
HF exports also now serialize
moe_use_core_numerics=true. The exported HF model reproduces the source no-EP expert arithmetic: packed dense/shared projections, packed SwiGLU with a single rounding, and TE index-map combination in routing-slot order. BF16 combination rounds multiplication separately on pre-SM90 GPUs and uses fused updates on SM90+, matching the respective TE kernels. CUDA inference without Triton uses the existing Torch activation fallback. Prior HF configs retain their original FP32-combination implementation. This fixes a real model-forward difference rather than substituting a special verifier, and leaves the strict conversion tolerance unchanged.Companion to scaling-ladders #74.
Validation
Two-rank CPU regression checks BF16 pointer alignment, mixed sharded/replicated FP32 master synchronization, and exact values before/after updates. It failed before the buffer fix.
B300 stream regressions at
52c4d42: 3 passed. They compare forward outputs and input/weight gradients after a separate compiled loss, including the split shared-expert path with and without activation recomputation.Configuration tests verify timeout overrides survive serialization and reach distributed initialization in both training and checkpoint evaluation: 2 passed.
Production-command 275M Cx8 canary: PT, MT, and LC each completed five steps with finite gradients, zero skipped updates, and checkpoint audits passing.
All seventeen 275M/450M/810M no-EP PT/MT/LC topologies completed 50 steps each on B300 (fifteen using core
97a4bc3, the two LC retries using core2122d42). Immutable training image:01M28XX5N4KM6PD0G81KC41BDW; Torch2.13.0+cu130, Triton3.7.1, FLA0.5.2, FA44.0.0b16. Detailed topology/source pins and job IDs are recorded in the companion ladder PR.Focused CPU HF/config tests, lint, type, and formatting checks pass. Current-head CI at
b5db31dpassed: 22 checks passed, 1 skipped.Distributed qualification
All seven 1.4B EP4/EP8 production topologies completed 50 steps with finite gradients and zero skipped updates, completing all 24 approved B300 systems configurations in the companion ladder. Every replica exit is verified. The one-GPU stream regression does not qualify distributed expert communication. All three production PT/MT/LC conversions and tokenizer export audits now pass; downstream PT/MT/LC native vLLM smoke evaluations also passed (10,000 examples each, zero errors, exit 0). Exact HF/core conversion does not establish exact HF/vLLM numerical parity.
Current A100 qualification, H100, and B300 at
f09d694: 16 tests passed on each GPU, covering actual core/TE BF16 and FP32 experts, the real dense/shared GEMM dimensions, CUDA without Triton, FP64 rounding oracles, and full-graph forward/gradients. Each GPU also produced maximum logit difference0.0on three real PT-checkpoint input seeds, using contiguous HF weights as in the exporter. All jobs exited successfully;rtol=atol=1e-4is unchanged.