Repository navigation
prune_layers: cut a hybrid model's layer pattern with its layers - #906
Merged
Merged
Conversation
A Nemotron-H config.json carries its layer structure as hybrid_override_pattern (one character per layer: M mamba, * attention, - mlp, E moe) or, in the newer configs, as the layers_block_type list. Megatron builds a hybrid model's layers from that pattern, so a cut that set num_hidden_layers to N but kept the 42-character pattern of Nemotron 3 Nano 4B gave a config whose model does not build. The cut now keeps the first N entries of either form (also under --keep-layer-tables: the pattern is the structure, not an index table). A model with attention layers must keep at least one: a cut without one stops and names the first attention layer and the smallest --layers that keeps it. The report's layer kinds name each kept hybrid layer. Tests use the published Nemotron 3 Nano 4B config.json. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review fixes: - full_attention (the layers_block_type kind transformers 5 saves) counts as the attention a hybrid cut must keep; linear_attention does not; - a config with both hybrid_override_pattern and layers_block_type gets both cut, and the two must name the same layers; - a pattern character or list kind the script does not know stops it, instead of passing through; - an end-to-end test cuts a Nemotron 3 Nano 30B-A3B shaped checkpoint (its published config.json, 'E' MoE layers with routed and shared experts) and checks every expert of the kept layers stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
michaellchung
force-pushed
the
own/prune-hybrid-pattern
branch
from
November 26, 2026 17:01
797652e to
e05425c
Compare
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.
Summary
scripts/prune_layers.py(the layer cut of theyeto image adopttrainer smoke) now cuts a hybrid (Mamba) model's per-layer pattern together withnum_hidden_layers.A Nemotron-H
config.jsoncarries its layer structure ashybrid_override_pattern, one character per layer (Mmamba,*attention,-mlp,Emoe), or, in the newer configs (Nemotron 3.5), as thelayers_block_typelist. Megatron builds a hybrid model's layers from that pattern. Before this change, a cut to N layers kept the full pattern: on Nemotron 3 Nano 4B,--layers 8gavenum_hidden_layers: 8with a 42-character pattern. transformers accepts that config, but the hybrid model does not build from it.*andattention/full_attentioncount as one kind); otherwise the cut stops. This also applies under--keep-layer-tables, because the pattern is the layer structure itself, not an index table like the Kimi K3 lists.*,attention, orfull_attention(the kind transformers 5 saves inlayers_block_type);linear_attentiondoes not count. A cut without one stops with exit 1, names the first attention layer, and names the smallest--layersthat keeps it (for Nemotron 3 Nano 4B:keep at least --layers 13). A pattern shorter than N is refused.D,|,/, ...) stops it, so a structure it cannot read is never cut by guess.[prune] layer i: ...,PRUNE.json) name each kept hybrid layer (mamba/attention/mlp/moe).Tests use the published
config.jsonof Nemotron 3 Nano 4B and of Nemotron 3 Nano 30B-A3B (tests/data/prune_layers/). They cover:--dry-runwrites nothing);layers_block_typeform, including the transformers 5 kinds;EMoE layers, routed and shared experts: every expert of the kept layers stays.Ten mutations of the new code fail the tests: no
full_attention; one form cut; no agreement check; no unknown-symbol check, for each form;Eread as mlp; experts or shared experts dropped; an extra known kind; no cut.The
yeto image adoptsmoke (#894) only calls this script with--layers N; it does not change the script.Follow-up (not in this PR)
yeto.memory_estimate.param_breakdowncounts every layer of a hybrid model as attention plus a gated MLP. It gives 7.4B for Nemotron 3 Nano 4B (3.97B real) and 102B for Nemotron 3 Nano 30B-A3B. A correct count needs a per-layer-type model (Mamba in/out projections, 2-matrix relu2 MLPs, single-type layers), not a small formula fix. The overestimate only makes the smoke pick fewer layers.Test plan
pytest tests/test_prune_layers.py: 16 passed (5 on main).test_k31_customer_plan_golden+tests/golden/breakers: 52 passed on main and on the branch. examples/k31run up --dry-runand the pool--dry-run, compared line by line with origin/main, timestamps and--source-sha256normalized. Each case reached the full plan: traineryeto launch --dry-runrc=0, pool plan printed, the only failures were the offline model_pull and sandbox status checks. customer dry-run diff: none.🤖 Generated with Claude Code