Repository navigation
refactor(models): one shared init_buffers_post_meta (stacked on #3833) - #3857
Closed
faresobeid wants to merge 1 commit into
Closed
faresobeid wants to merge 1 commit into
faresobeid wants to merge 1 commit into
Conversation
faresobeid
marked this pull request as ready for review
October 4, 2026 12:34
This was referenced Oct 4, 2026
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.
Stacked on #3833 (
chore/remove-transformers-modeling). The PR base is that branch, so the diff shows only this change. Retarget tomainonce #3833 merges.Summary
PrimeModel.init_buffers_post_metais now one shared method. It walksself.modules()and callsreset_buffers()on each module that defines it. This replaces the 13 per-arch overrides, which differed from each other.RotaryEmbedding, Qwen3.5 vision rotary, Qwen3.8 rotary, andNGramEmbeddingrenamereset_parameterstoreset_buffers.DeepseekV4RotaryEmbedding.init_buffers_post_metabecomesreset_buffers. The old name was unsafe for a generic walk because the norms usereset_parametersto re-init their weights.MoE.reset_bufferszeroestokens_per_expert,routing_confidence_sumandselection_bias._reset_runtime_moe_buffersintrainer/model.pyis deleted. It existed only because half the archs did not zero their MoE stats ininit_buffers_post_meta.Diff: 21 files, +19 / −107.
Behavior change
None for training.
max_viofrom uninitialized memory. That is wrong.setup_modelalready calls_reset_runtime_moe_buffersafter both load paths, so the stats were zero before any forward. This PR keeps that result in one place.selection_bias. It is now zeroed before weight loading on every arch. Before, only laguna, qwen3_5 and qwen3_8 zeroed it; the others left it uninitialized until the checkpoint load overwrote it. With a checkpoint that ships the bias, the result is the same. Without one, it is now zero instead of garbage.tid2eid(DeepSeek-V4 hash routing) is still left to the checkpoint.Checks
prime_kernelshas noindexed_attention). Steps: build on meta,to_empty, fill every buffer with NaN,init_buffers_post_meta, then the old_reset_runtime_moe_bufferswhere it exists. Result:attention_scalingequals a freshly built model, on Chore/remove transformers modeling #3833 and on this branch;init_buffers_post_metaalone.get_load_balance_stats.max_vio,routing_confidenceandtokens_per_expertare identical between Chore/remove transformers modeling #3833 and this branch.pytest tests/unit/train tests/unit/test_configs.py -m "not gpu": 351 passed, 1 failed. The failure istest_cpu_adamnative, which fails the same way on Chore/remove transformers modeling #3833 (environment issue).ruff checkandruff format: clean.GPU checks (1×8 H200, SLURM jobs 2386/2393)
to_empty(cuda)with NaN buffers, theninit_buffers_post_meta. Every non-persistent buffer and rotary scale matches a fresh model, on Chore/remove transformers modeling #3833 and on this branch. On this branch no MoE stat orselection_biasis still NaN after init.test_deepseek_v4.py -k "init_buffers_post_meta or hash_table_survives": 2 passed (Chore/remove transformers modeling #3833: 2 passed).test_qwen3_8_flash_next.py+qwen3_5/test_vlm.py: 15 passed (Chore/remove transformers modeling #3833: 15 passed).prime_kernelshas noindexed_attention). It is covered by its test file above.Conflicts
MoE.forward, the routers andmodel.pyfreeze logic, notreset_buffers.Note
Low Risk
Refactor of model materialization before weight load; normal checkpoint training should be unchanged, with only a minor fix for uninitialized selection_bias when biases are absent from the checkpoint.
Overview
Unifies post–meta-device buffer initialization across all PrimeRL models instead of thirteen architecture-specific
init_buffers_post_metaoverrides.PrimeModel.init_buffers_post_metanow walksself.modules()and callsreset_buffers()on any submodule that defines it. Rotary layers, DeepSeek-V4 RoPE, Qwen3.5 vision RoPE, Qwen3.8 RoPE/NGramEmbedding, andMoEimplementreset_buffers(renamed fromreset_parameterswhere that name would collide with weight re-init).MoE.reset_bufferszeroes routing stats and optionalselection_bias.trainer/model.pydrops_reset_runtime_moe_buffers; MoE stat clearing is covered by the shared init path before checkpoint load.Reviewed by Cursor Bugbot for commit 6f8fa80. Bugbot is set up for automated code reviews on this repo. Configure here.