[Zamba2] Fix block_id to use hybrid order for num_mem_blocks>1 - #48150
Draft
aryansk wants to merge 1 commit into
Draft
[Zamba2] Fix block_id to use hybrid order for num_mem_blocks>1#48150aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
Zamba2Model.get_layers previously computed block_id as layer_id % num_mem_blocks using the global layer index, while the tie cycle and the adapter selection (i % num_mem_blocks == block_id where i iterates over hybrid layers) use hybrid-order. For configs with num_mem_blocks>1 (e.g. Zyphra/Zamba2-2.7B with 9 hybrids at [6,12,18,24,30,36,42,47,51] and num_mem_blocks=2) this mis-aligns block ids ([0,0,0,0,0,0,0,1,1] vs expected [0,1,0,1,0,1,0,1,0]) and causes tie_weights_keys validation to fail (layers.12 vs layers.47). Track hybrid_layer_idx and compute block_id = hybrid_layer_idx % num_mem_blocks so structure, tie cycle and checkpoint agree. For num_mem_blocks==1 behavior is identical. Fixes huggingface#47994. Co-authored fix mirrors closed huggingface#48018; modular source updated and generated modeling file kept in sync. AI assistance disclosure: used Muse Spark for diagnosis and fix with human verification (meta-device construction + Zamba2ModelTest 129 passed).
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: zamba2 |
Contributor
CI recapDashboard: View test results in Grafana |
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.
Fixes #47994
Problem
Zamba2Model(config)raisesValueError: tie_weights_keysbefore any weight is loaded for any config withnum_mem_blocks > 1(e.g.Zyphra/Zamba2-2.7B-instruct,num_mem_blocks=2, 54 layers, hybrids at[6,12,18,24,30,36,42,47,51]). Repro onmainwithtorch.device("meta"):Root cause in
Zamba2Model.get_layers:block_id = layer_id % num_mem_blocksuses the global layer index, while the tie cycle just above advances once per hybrid layer, andZamba2Attention/Zamba2MLPselect adapter slots withi % num_mem_blocks == block_idwhereiruns over hybrid layers. For 2.7B the global rule yields[0,0,0,0,0,0,0,1,1]where the cycle expects[0,1,0,1,0,1,0,1,0]; layer 12 ends up tied to 47 with a disjoint adapter set, failing validation that was added to catch silent mis-ties.Where
num_mem_blocks == 1(1.2B, theZamba2Configdefault) both conventions are identical, so no currently-working config changes behavior.Change
hybrid_layer_idxinZamba2Model.get_layersand computeblock_id = hybrid_layer_idx % config.num_mem_blocks(increment per hybrid). This makes structure, tie cycle and checkpoint agree.src/transformers/models/zamba2/modular_zamba2.pyand keptmodeling_zamba2.pyin sync (generated file; single logical fix in two files). Minimal diff: +3/-1 per file, no comment bloat.531params matching the published checkpoint's531tensors (per Zamba2:num_mem_blocks > 1checkpoints cannot be constructed —block_iduses the global layer index while the tie cycle uses hybrid order #47994), andtie_weights_keyscorrectly alternates (6→18→30→42→51share block 0,12→24→36→47share block 1).Why this approach
range(num_fwd_mem_blocks)withi % num_mem_blocks == block_id). Alternatives like keeping global modulo would require changing both adapter selection and tie cycle, which diverges from the checkpoint layout. This keeps the checkpoint as ground truth.get_layerspath and includes disclosure/differentiation below.Testing
Zamba2Config(num_mem_blocks=2)raisesValueErrorforlayers.12:layers.47with mismatchedgate_up_proj_adapter_listindices (0,2,4,6,8vs1,3,5,7).model._tied_weights_keysas above, per-layerblock_idfollowshybrid_idx % 2(6:0,12:1,18:0,24:1,30:0,36:1,42:0,47:1,51:0), andZamba2ForCausalLMalso constructs.num_mem_blocks=1regression still passes.pytest tests/models/zamba2/test_modeling_zamba2.py(withdatasets/parameterized): 129 passed, 146 skipped (run in local venv, Python 3.13, torch). No failures in the Zamba2 common suite.ruff check src/transformers/models/zamba2/{modeling_zamba2,modular_zamba2}.py→All checks passed!Documentation and release impact
Review notes
num_mem_blocks > 1checkpoints cannot be constructed —block_iduses the global layer index while the tie cycle uses hybrid order #47994, issue stillOPEN, unassigned. Prior PR [Zamba2] Fix layer construction and weight tying for num_mem_blocks > 1 #48018 correctly fixed both files but was closed after maintainer note about waiting for issue author's own PR offer; CI was green (run 32016223085). This PR is intentionally minimal and acknowledges that prior work.hybrid_layer_idxin both modular and generated file; no additional test file (lane scope keeps PR to source fix; the reporter's suggested construction test is ready to add if maintainers prefer — small 9-layernum_mem_blocks=2case proven in validation).modular_zamba2.pyis source of truth,modeling_zamba2.pykept identical.