Skip to content

feat: add Zamba2ForCausalLM architecture adapter#1486

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
mukund1985:feat/zamba2-adapter
Jul 6, 2026
Merged

feat: add Zamba2ForCausalLM architecture adapter#1486
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
mukund1985:feat/zamba2-adapter

Conversation

@mukund1985

@mukund1985 mukund1985 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Closes #1463

What this adds

Zamba2ArchitectureAdapter for Zamba2ForCausalLM (Zyphra/Zamba2-1.2B, Zamba2-7B, Zamba2-7B-Instruct).

Zamba2 is a hybrid Mamba-2 / shared global-attention model. Most layers are pure Mamba-2 SSM ("mamba" in layers_block_type); a recurring subset are hybrid layers ("hybrid") that run through a shared Zamba2AttentionDecoderLayer whose weights are tied across num_mem_blocks slots.

Design

SSMBlockBridge for all layers. hook_in/hook_out fire on every layer regardless of type. For Mamba layers, norm and mixer submodules are wired normally (exposing in_proj, conv1d, inner_norm, out_proj hooks). For hybrid layers, both are marked optional=True so component_setup skips them gracefully — hybrid layers have no top-level .input_layernorm or .mamba.

is_stateful=False. Zamba2 threads its unified cache via past_key_values (the standard KV path). Using is_stateful=True routed generation into the Mamba cache_params= path, which collided with Zamba2's own internal cache call and produced wrong tokens. Verified empirically that the standard KV path produces token-identical output to hf_model.generate().

positional_embedding_type="none". RoPE is handled inside the HF attention block via position_ids; no model-level rotary bridge needed.

Framework change (shared files)

Five config attributes (mamba_expand, mamba_ngroups, num_mem_blocks, layers_block_type, use_shared_attention_adapter) were not in _HF_PASSTHROUGH_ATTRS, so they silently fell back to adapter defaults. Added them to both sources/transformers.py and sources/_bridge_builder.py. These are Mamba-family attributes that future SSM adapters will also need.

Tests

21 integration tests against Zyphra/Zamba2-1.2B (38 layers: 32 mamba + 6 hybrid):

  • Config flags and block structure
  • Forward parity: exact match vs HF (max_diff == 0.0)
  • Generation parity: greedy output matches hf_model.generate() bit-for-bit
  • Hook coverage: mixer hooks on Mamba layers, block-level hooks only on hybrid layers

Closes TransformerLensOrg#1463

Supports Zyphra/Zamba2-1.2B, Zamba2-7B, Zamba2-7B-Instruct via
TransformerBridge. Zamba2 is a hybrid Mamba-2 / shared-attention model
where layers_block_type alternates between "mamba" (pure SSM) and
"hybrid" (SSM + shared global-attention block, weight-tied across
num_mem_blocks slots).

Key design decisions:
- SSMBlockBridge for all layers: hook_in/hook_out fire regardless of
  layer type. Mamba layers expose mixer submodule hooks (in_proj,
  conv1d, inner_norm, out_proj); hybrid layers have norm and mixer
  marked optional=True so component_setup skips them gracefully.
- is_stateful=False: Zamba2 threads its unified cache via
  past_key_values (standard KV path), not the Mamba cache_params path.
  Setting is_stateful=True collided with Zamba2's own cache call.
- positional_embedding_type="none": RoPE is handled inside the HF
  attention block; no model-level rotary bridge needed.
- Added mamba_expand, mamba_ngroups, num_mem_blocks, layers_block_type,
  use_shared_attention_adapter to _HF_PASSTHROUGH_ATTRS in both
  sources/transformers.py and sources/_bridge_builder.py so these
  config fields reach the adapter reliably.

21 integration tests pass against Zyphra/Zamba2-1.2B (38 layers:
32 mamba + 6 hybrid). Forward parity is exact (max_diff == 0.0);
greedy generation matches HF bit-for-bit across both layer types.
@mukund1985 mukund1985 force-pushed the feat/zamba2-adapter branch from 98a7560 to 73d1576 Compare July 6, 2026 14:44
@mukund1985

Copy link
Copy Markdown
Contributor Author

@jlarson4 ready for review when you get a chance. One thing worth flagging: the PR touches sources/transformers.py and sources/_bridge_builder.py to add five Mamba-family config attributes to _HF_PASSTHROUGH_ATTRS — called out in the PR body under "Framework change". Happy to discuss the approach if you'd prefer a different way to handle those.

@jlarson4 jlarson4 linked an issue Jul 6, 2026 that may be closed by this pull request
1 task
@jlarson4

jlarson4 commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks @mukund1985! I'll review once the CI checks finish

@jlarson4

jlarson4 commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@mukund1985 this looks great! Merging

@jlarson4 jlarson4 merged commit ab46ac0 into TransformerLensOrg:dev Jul 6, 2026
25 checks passed
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.

[Proposal] Add Zamba2 shared-attention hybrid adapter (Zamba2ForCausalLM)

2 participants