nnterp · encyclopedia mamba2 · nnterp.families.mamba2

family mamba2

Mamba-2

The block

01 · the residual stream

One block of the stack. Hover any part for its nnterp name; click to pin the card.

embed_tokenstoken_embeddings
→
layers× 24
→
normfinal_norm_output
→
lm_headlm_head_output
→
logitsthe output
i = 0

identity layers[i].input + linear_attn.attention_output == layer_output

The API

02 · support()

Values, by host

⚠︎ marks a value this checkpoint has only under some load: hover it for the condition.

model Mamba2ForCausalLMlayoutwhat it isread at
logits Logits [batch seq vocab_size] The logits the model returns, after anything it does past lm_head (a softcap, a scale)

the module's own output

token_embeddings Residual [batch seq hidden_size] The embedding module's output; layers[0].input is what enters the first block

the child module embed_tokens's output

final_norm_output Residual [batch seq hidden_size] The residual stream after the final norm, what lm_head reads

the child module norm's output

lm_head_output Logits [batch seq vocab_size] The head's logits before anything the model does past lm_head (a softcap, a scale)

the child module lm_head's output

next_token_probs NextTokenProbs [batch vocab_size] The next-token distribution at the last position; derived, read-only

the module's own output

input_ids Tokens [batch seq] The token ids the model was called with

the module's own input

attention_mask Tokens [batch seq] The attention mask the model was called with; zeros are padding

the module's own input

input_size scalar [batch, seq] of the current call; read-only

the module's own input

model.layers[i] Mamba2Blocklayoutwhat it isread at
layer_input Residual [batch seq hidden_size] The residual stream entering the block

the module's own input

attention_norm_output Residual [batch seq hidden_size] The normed stream entering the attention

the child module norm's output

layer_mid scalar Unavailable: a state-space block: one sublayer, the mixer, so there is no stream between sublayers and no MLP

nowhere: the family does not serve it

mlp_norm_output scalar Unavailable: a state-space block: one sublayer, the mixer, so there is no stream between sublayers and no MLP

nowhere: the family does not serve it

mlp_input scalar Unavailable: a state-space block: one sublayer, the mixer, so there is no stream between sublayers and no MLP

nowhere: the family does not serve it

layer_output Residual [batch seq hidden_size] The residual stream leaving the block, a tensor even when the block returns a tuple

the module's own output

model.layers[i].linear_attn Mamba2Mixerlayoutwhat it isread at
attention_output Residual [batch seq hidden_size] What the mixer adds to the residual stream

the module's own output

⚠︎state scalar Unavailable: the chunk scan computes every token's state in one tensor per call, not one occurrence per token to walk with tracer.iter; read states, or state_after(t), after nnterp.chunk_per_token(model)

nowhere: the family does not serve it

⚠︎states States [batch seq heads key_dim value_dim] The state after every token of this call; needs nnterp.chunk_per_token(model)

computed from other values read in the same call

⚠︎attention_queries SSDQueries [batch seq groups state_dim] C, the queries reading the state

inside the forward: the arguments of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), argument C

⚠︎attention_keys SSDKeys [batch seq groups state_dim] B, the keys writing into the state

inside the forward: the arguments of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), argument B

⚠︎attention_values SSDValues [batch seq heads head_dim] x, the values written into the state

inside the forward: the arguments of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), argument hidden_states

⚠︎betas Gates [batch seq heads] dt, the per-token write strength into the state

inside the forward: the arguments of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), argument dt

⚠︎decays Gates [batch seq heads] A * dt, the per-token log decay of the state

inside the forward: the arguments of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), argument dt

⚠︎state_input State | None [batch heads key_dim value_dim] The state entering the layer, or None at the start of a prompt (a copy of the cache's buffer)

inside the forward: the arguments of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), argument state

⚠︎attention_head_outputs SSDHeadOutputs [batch seq heads head_dim] y, the per-head outputs before the gated norm and the output projection

inside the forward: the output of the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), the element that call returns it in

⚠︎state_output State [batch heads key_dim value_dim] The state leaving the layer

inside the forward: at or inside the kernel this call runs (a prompt's chunked one or a decode step's recurrent one), the element that call returns it in

set_state_after scalar Unavailable: the chunk scan computes every boundary state in one cumulative step from the initial state, so a state written at token t does not flow into later tokens' states; assign state_input to change where a call starts

nowhere: the family does not serve it

Printout

What print(model) shows on AntonV/mamba2-130m-hf: the native modules, standard names and every standard value with its layout.

Mamba2ForCausalLM(
  (lm_head): Linear(in_features=768, out_features=50288, bias=False)
  (generator): Generator(
    (streamer): Streamer()
  )
  (embed_tokens): Embedding(50288, 768)
  (layers): ModuleList(
    (0-23): 24 x Mamba2Block(
      (norm): Mamba2RMSNorm()
      (linear_attn/mixer): Mamba2Mixer(
        (act): SiLUActivation()
        (conv1d): Conv1d(1792, 1792, kernel_size=(4,), stride=(1,), padding=(3,), groups=1792)
        (in_proj): Linear(in_features=768, out_features=3352, bias=False)
        (norm): MambaRMSNormGated()
        (out_proj): Linear(in_features=1536, out_features=768, bias=False)
        (attention_queries) -> SSDQueries [batch seq groups state_dim]: C, the queries reading the state
        (attention_keys) -> SSDKeys [batch seq groups state_dim]: B, the keys writing into the state
        (attention_values) -> SSDValues [batch seq heads head_dim]: x, the values written into the state
        (betas) -> Gates [batch seq heads]: dt, the per-token write strength into the state
        (decays) -> Gates [batch seq heads]: A * dt, the per-token log decay of the state
        (state_input) -> State | None [batch heads key_dim value_dim]: The state entering the layer, or None at the start of a prompt (a copy of the cache's buffer)
        (attention_head_outputs) -> SSDHeadOutputs [batch seq heads head_dim]: y, the per-head outputs before the gated norm and the output projection
        (state_output) -> State [batch heads key_dim value_dim]: The state leaving the layer
        (states) -> States [batch seq heads key_dim value_dim]: The state after every token of this call; needs nnterp.chunk_per_token(model)
        (state): Unavailable: the chunk scan computes every token's state in one tensor per call, not one occurrence per token to walk with tracer.iter; read states, or state_after(t), after nnterp.chunk_per_token(model)
        (set_state_after): Unavailable: the chunk scan computes every boundary state in one cumulative step from the initial state, so a state written at token t does not flow into later tokens' states; assign state_input to change where a call starts
        (attention_output) -> Residual [batch seq hidden_size]: What the mixer adds to the residual stream
      )
      (attention_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the attention
      (layer_mid): Unavailable: a state-space block: one sublayer, the mixer, so there is no stream between sublayers and no MLP
      (layer_input) -> Residual [batch seq hidden_size]: The residual stream entering the block
      (mlp_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the MLP
      (mlp_input) -> Residual [batch seq hidden_size]: What the MLP is called with
      (layer_output) -> Residual [batch seq hidden_size]: The residual stream leaving the block, a tensor even when the block returns a tuple
    )
  )
  (norm): Mamba2RMSNorm()
  (logits) -> Logits [batch seq vocab_size]: The logits the model returns, after anything it does past lm_head (a softcap, a scale)
  (token_embeddings) -> Residual [batch seq hidden_size]: The embedding module's output; layers[0].input is what enters the first block
  (final_norm_output) -> Residual [batch seq hidden_size]: The residual stream after the final norm, what lm_head reads
  (lm_head_output) -> Logits [batch seq vocab_size]: The head's logits before anything the model does past lm_head (a softcap, a scale)
  (next_token_probs) -> NextTokenProbs [batch vocab_size]: The next-token distribution at the last position; derived, read-only
  (input_ids) -> Tokens [batch seq]: The token ids the model was called with
  (attention_mask) -> Tokens [batch seq]: The attention mask the model was called with; zeros are padding
  (input_size): [batch, seq] of the current call; read-only
)

Config

model.num_layers24
model.hidden_size768
model.num_heads24
model.num_kv_heads24
model.head_dim64
model.qk_head_dim64
model.vocab_size50288
model.intermediate_size1536
num_hidden_layers24
hidden_size768
head_dim64
vocab_size50288
hidden_actsilu
tie_word_embeddingsTrue
layer_norm_epsilon1e-05
state_size128
expand2
conv_kernel4
residual_in_fp32True
n_groups1
chunk_size256
time_step_min0.001

The Notes

03 · what to know before you run

A gated norm over every head follows attention_head_outputs

out = h + mixer(norm(h))
mixer: y = ssd(x, dt, A, B, C) + D * x       # x: attention_values; y: attention_head_outputs
       return out_proj(norm(y * silu(z)))    # one norm over the mixer's whole width

The mixer gates y with silu(z) and then normalizes it over all heads together, whatever n_groups is. A head's term in attention_output depends on every other head, so zeroing one head of attention_head_outputs rescales the rest.

C and B are shared by the heads of a group

attention_queries (C) and attention_keys (B) have one row per group of heads, and the 130m to 2.7b checkpoints have one group, so an edit to either reaches every head. Mamba-Codestral has eight groups.

The pattern is rebuilt from the scan's values

There is no attention_probabilities. Per head the scan is a causal linear map over tokens, and the served values give its weights exactly:

y_t        = sum_s alpha[t, s] * x_s + D * x_t
alpha[t, s] = (C_t . B_s) * exp(decays_{s+1} + ... + decays_t) * betas_s

Repeat C and B from groups to heads first.

From the family module

04 · nnterp/families/mamba2.py

Mamba-2 (Mamba2ForCausalLM).

A pure state-space model: no attention and no MLP. The tree::

backbone.embeddings
backbone.layers[i]       Mamba2Block
    .norm                RMSNorm, the block's only norm
    .mixer               Mamba2Mixer, an SSD mixer
backbone.norm_f
lm_head

Each block is hidden + mixer(norm(hidden)) (the residual in float32 when residual_in_fp32), so the block's one contribution is the mixer's output and layer.input + linear_attn.attention_output == layer_output. The mixer is nnterp.StateSpace under the standard name linear_attn (the recurrent mixer's name on every hybrid). The block's norm keeps its name: a "norm" key would also match the mixer's own gated norm, and its output is linear_attn.input. There is no self_attn and no mlp, so support lists neither.

The sizes are the mixer's: num_heads and head_dim are the SSD heads (config.num_heads, config.head_dim), and intermediate_size is the mixer's inner width, expand * hidden_size, what in_proj expands to and out_proj reads from (the model has no MLP). With residual_in_fp32 the residual stream is float32 whatever the weights are, and the model casts it to lm_head's dtype and its logits to float32, so project_on_vocab is the family's own.