Skip to content

prune_layers: cut a hybrid model's layer pattern with its layers - #906

Merged
michaellchung merged 2 commits into
mainfrom
own/prune-hybrid-pattern
Nov 26, 2026
Merged

michaellchung merged 2 commits into
mainfrom
own/prune-hybrid-pattern

Conversation

@michaellchung

@michaellchung michaellchung commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

scripts/prune_layers.py (the layer cut of the yeto image adopt trainer smoke) now cuts a hybrid (Mamba) model's per-layer pattern together with num_hidden_layers.

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 (Nemotron 3.5), as the layers_block_type list. 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 8 gave num_hidden_layers: 8 with a 42-character pattern. transformers accepts that config, but the hybrid model does not build from it.

  • Either form is cut to its first N entries. When both are present, both are cut, and the two must name the same layers (* and attention / full_attention count 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.
  • A model with attention layers must keep at least one. Attention means *, attention, or full_attention (the kind transformers 5 saves in layers_block_type); linear_attention does not count. A cut without one stops with exit 1, names the first attention layer, and names the smallest --layers that keeps it (for Nemotron 3 Nano 4B: keep at least --layers 13). A pattern shorter than N is refused.
  • A pattern character or list kind the script does not know (D, |, /, ...) stops it, so a structure it cannot read is never cut by guess.
  • The report's layer kinds ([prune] layer i: ..., PRUNE.json) name each kept hybrid layer (mamba / attention / mlp / moe).
  • Other configs are unchanged.

Tests use the published config.json of Nemotron 3 Nano 4B and of Nemotron 3 Nano 30B-A3B (tests/data/prune_layers/). They cover:

  • the pattern cut at several N;
  • the refusal, from the function and from the CLI (--dry-run writes nothing);
  • the layers_block_type form, including the transformers 5 kinds;
  • both forms present, agreeing and not;
  • unknown characters and kinds;
  • an end-to-end cut of a fake Nano-4B checkpoint;
  • an end-to-end cut of a fake Nano-30B-A3B checkpoint with E MoE 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; E read as mlp; experts or shared experts dropped; an extra known kind; no cut.

The yeto image adopt smoke (#894) only calls this script with --layers N; it does not change the script.

Follow-up (not in this PR)

  • yeto.memory_estimate.param_breakdown counts 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.
  • The smoke picks N as the largest layer count (at most 16) whose state fits. For a hybrid model it could also raise N to the first attention layer + 1 before cutting, instead of failing on the refusal.

Test plan

  • pytest tests/test_prune_layers.py: 16 passed (5 on main).
  • Customer gate: test_k31_customer_plan_golden + tests/golden/breakers: 52 passed on main and on the branch. examples/k31 run up --dry-run and the pool --dry-run, compared line by line with origin/main, timestamps and --source-sha256 normalized. Each case reached the full plan: trainer yeto launch --dry-run rc=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

michaellchung and others added 2 commits November 27, 2026 00:58
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
michaellchung force-pushed the own/prune-hybrid-pattern branch from 797652e to e05425c Compare November 26, 2026 17:01
@michaellchung
michaellchung merged commit a074dc7 into main Nov 26, 2026
7 checks passed
@michaellchung
michaellchung deleted the own/prune-hybrid-pattern branch November 26, 2026 17:30
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