family phi
A parallel block, in which one LayerNorm feeds both the attention and the MLP and the block adds their outputs at once, with a bias on every attention projection and on lm_head, and rotary on only 32 dimensions of each head.
One block of the stack. Hover any part for its nnterp name; click to pin the card.
One block of the stack. Hover any part for its nnterp name; click to pin the card.
identity layers[i].input + self_attn.attention_output + mlp.mlp_output == layer_output
⚠︎ marks a value this checkpoint has only under some load: hover it for the condition.
What print(model) shows on microsoft/phi-1: the native modules, standard names and every standard value with its layout.
PhiForCausalLM(
(lm_head): Linear(in_features=2048, out_features=51200, bias=True)
(generator): Generator(
(streamer): Streamer()
)
(embed_tokens): Embedding(51200, 2048)
(layers): ModuleList(
(0-23): 24 x PhiDecoderLayer(
(self_attn): PhiAttention(
(q_proj): Linear(in_features=2048, out_features=2048, bias=True)
(k_proj): Linear(in_features=2048, out_features=2048, bias=True)
(v_proj): Linear(in_features=2048, out_features=2048, bias=True)
(o_proj/dense): Linear(in_features=2048, out_features=2048, bias=True)
(attention_queries) -> Queries [batch num_heads seq qk_head_dim]: The queries entering attention
(attention_keys) -> Keys [batch num_kv_heads seq qk_head_dim]: The keys entering attention
(attention_values) -> Values [batch num_kv_heads seq head_dim]: The values entering attention
(attention_scores) -> Pattern [batch num_heads query key]: The attention scores entering the softmax, masked
(attention_premix) -> HeadOutputs [batch seq num_heads head_dim]: Every head's output side by side, as the output projection takes it
(attention_output) -> Residual [batch seq hidden_size]: What the attention adds to the residual stream
(attention_probabilities) -> Pattern [batch num_heads query key]: The attention pattern the values are mixed with
(attention_head_outputs) -> HeadOutputs [batch seq num_heads head_dim]: The per-head outputs before the output projection
)
(mlp): PhiMLP(
(act_fn/activation_fn): NewGELUActivation()
(fc1): Linear(in_features=2048, out_features=8192, bias=True)
(down_proj/fc2): Linear(in_features=8192, out_features=2048, bias=True)
(mlp_activation) -> Neurons [batch seq intermediate_size]: The activation function's output
(mlp_neurons) -> Neurons [batch seq intermediate_size]: What the down projection receives: act(gate)·up on a gated MLP, the activation itself otherwise
(mlp_output) -> Residual [batch seq hidden_size]: What the MLP adds to the residual stream
)
(input_layernorm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True, bias=True)
(resid_dropout): Dropout(p=0.0, inplace=False)
(layer_mid): Unavailable: a parallel block: the attention and the MLP read the same input, so there is no stream between them
(mlp_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the MLP: the one norm feeds both sublayers
(layer_input) -> Residual [batch seq hidden_size]: The residual stream entering the block
(attention_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the attention
(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): LayerNorm((2048,), eps=1e-05, elementwise_affine=True, bias=True)
(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
)What print(model) shows on microsoft/phi-1_5: the native modules, standard names and every standard value with its layout.
PhiForCausalLM(
(lm_head): Linear(in_features=2048, out_features=51200, bias=True)
(generator): Generator(
(streamer): Streamer()
)
(embed_tokens): Embedding(51200, 2048)
(layers): ModuleList(
(0-23): 24 x PhiDecoderLayer(
(self_attn): PhiAttention(
(q_proj): Linear(in_features=2048, out_features=2048, bias=True)
(k_proj): Linear(in_features=2048, out_features=2048, bias=True)
(v_proj): Linear(in_features=2048, out_features=2048, bias=True)
(o_proj/dense): Linear(in_features=2048, out_features=2048, bias=True)
(attention_queries) -> Queries [batch num_heads seq qk_head_dim]: The queries entering attention
(attention_keys) -> Keys [batch num_kv_heads seq qk_head_dim]: The keys entering attention
(attention_values) -> Values [batch num_kv_heads seq head_dim]: The values entering attention
(attention_scores) -> Pattern [batch num_heads query key]: The attention scores entering the softmax, masked
(attention_premix) -> HeadOutputs [batch seq num_heads head_dim]: Every head's output side by side, as the output projection takes it
(attention_output) -> Residual [batch seq hidden_size]: What the attention adds to the residual stream
(attention_probabilities) -> Pattern [batch num_heads query key]: The attention pattern the values are mixed with
(attention_head_outputs) -> HeadOutputs [batch seq num_heads head_dim]: The per-head outputs before the output projection
)
(mlp): PhiMLP(
(act_fn/activation_fn): NewGELUActivation()
(fc1): Linear(in_features=2048, out_features=8192, bias=True)
(down_proj/fc2): Linear(in_features=8192, out_features=2048, bias=True)
(mlp_activation) -> Neurons [batch seq intermediate_size]: The activation function's output
(mlp_neurons) -> Neurons [batch seq intermediate_size]: What the down projection receives: act(gate)·up on a gated MLP, the activation itself otherwise
(mlp_output) -> Residual [batch seq hidden_size]: What the MLP adds to the residual stream
)
(input_layernorm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True, bias=True)
(resid_dropout): Dropout(p=0.0, inplace=False)
(layer_mid): Unavailable: a parallel block: the attention and the MLP read the same input, so there is no stream between them
(mlp_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the MLP: the one norm feeds both sublayers
(layer_input) -> Residual [batch seq hidden_size]: The residual stream entering the block
(attention_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the attention
(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): LayerNorm((2048,), eps=1e-05, elementwise_affine=True, bias=True)
(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
)What print(model) shows on microsoft/phi-2: the native modules, standard names and every standard value with its layout.
PhiForCausalLM(
(lm_head): Linear(in_features=2560, out_features=51200, bias=True)
(generator): Generator(
(streamer): Streamer()
)
(embed_tokens): Embedding(51200, 2560)
(layers): ModuleList(
(0-31): 32 x PhiDecoderLayer(
(self_attn): PhiAttention(
(q_proj): Linear(in_features=2560, out_features=2560, bias=True)
(k_proj): Linear(in_features=2560, out_features=2560, bias=True)
(v_proj): Linear(in_features=2560, out_features=2560, bias=True)
(o_proj/dense): Linear(in_features=2560, out_features=2560, bias=True)
(attention_queries) -> Queries [batch num_heads seq qk_head_dim]: The queries entering attention
(attention_keys) -> Keys [batch num_kv_heads seq qk_head_dim]: The keys entering attention
(attention_values) -> Values [batch num_kv_heads seq head_dim]: The values entering attention
(attention_scores) -> Pattern [batch num_heads query key]: The attention scores entering the softmax, masked
(attention_premix) -> HeadOutputs [batch seq num_heads head_dim]: Every head's output side by side, as the output projection takes it
(attention_output) -> Residual [batch seq hidden_size]: What the attention adds to the residual stream
(attention_probabilities) -> Pattern [batch num_heads query key]: The attention pattern the values are mixed with
(attention_head_outputs) -> HeadOutputs [batch seq num_heads head_dim]: The per-head outputs before the output projection
)
(mlp): PhiMLP(
(act_fn/activation_fn): NewGELUActivation()
(fc1): Linear(in_features=2560, out_features=10240, bias=True)
(down_proj/fc2): Linear(in_features=10240, out_features=2560, bias=True)
(mlp_activation) -> Neurons [batch seq intermediate_size]: The activation function's output
(mlp_neurons) -> Neurons [batch seq intermediate_size]: What the down projection receives: act(gate)·up on a gated MLP, the activation itself otherwise
(mlp_output) -> Residual [batch seq hidden_size]: What the MLP adds to the residual stream
)
(input_layernorm): LayerNorm((2560,), eps=1e-05, elementwise_affine=True, bias=True)
(resid_dropout): Dropout(p=0.1, inplace=False)
(layer_mid): Unavailable: a parallel block: the attention and the MLP read the same input, so there is no stream between them
(mlp_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the MLP: the one norm feeds both sublayers
(layer_input) -> Residual [batch seq hidden_size]: The residual stream entering the block
(attention_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the attention
(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): LayerNorm((2560,), eps=1e-05, elementwise_affine=True, bias=True)
(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
)h = input_layernorm(x)
out = self_attn(h) + mlp(h) + x
input_layernorm is the block's only norm. Its output is both self_attn.input and mlp.input,
the same tensor, and the block has no post_attention_layernorm. attention_output is dense's
output and mlp_output is fc2's, and nothing normalizes either before the add. Nothing connects
the two sublayers of one block: zeroing attention_output leaves that block's mlp.input
bit-identical, so block i's attention first reaches an MLP at block i + 1.
with model.trace(prompt):
x = model.layers[1].input.save()
attn = model.layers[1].self_attn.attention_output.save()
mlp_in = model.layers[1].mlp.input.save()
mlp = model.layers[1].mlp.mlp_output.save()
out = model.layers[1].layer_output.save()
assert torch.equal(attn + mlp + x, out) # the block's own order, bit for bit
with model.trace(prompt):
model.layers[1].self_attn.attention_output[:] = 0
mlp_in_ablated = model.layers[1].mlp.input.save()
assert torch.equal(mlp_in, mlp_in_ablated)
The block sums the attention and the MLP first and adds the residual last. In the identity's
order, x + attn + mlp, the sum differs from layer_output by rounding: by up to 7e-9 on the
pinned checkpoint in float32, and by up to 2.0 over phi-2's blocks in bfloat16, whose stream
entries reach 800, enough to fail torch.testing.assert_close at its bfloat16 tolerance.
The block returns a bare tensor, so model.layers[i].output is layer_output.
resid_dropout wraps both contributions and embed_dropout the embeddings (resid_pdrop is
0.1 on phi-2); both are inert in eval.
self_attn.q_proj, k_proj, v_proj and dense are separate Linears, each with a bias:
dense is the attention's output projection, not a layer of the MLP. The MLP is fc1, the
activation (gelu_new on all three checkpoints) and fc2, both with a bias. Keys and values have
num_heads heads: phi-1 and phi-1.5 leave num_key_value_heads unset, which transformers fills
with num_attention_heads, and phi-2 sets it to 32. The class has a qk_layernorm option, a
LayerNorm over each query and key head before the rotary; it is false on all three checkpoints,
so nothing normalizes the queries and keys.
partial_rotary_factor is 0.5 of a 64-dimensional head on phi-1 and phi-1.5, and 0.4 of an
80-dimensional head on phi-2: 32 dimensions on every checkpoint. The rotation is rotate_half on those 32, pairing
dimension i with i + 16; the other dimensions of every query and key are the projection's
output unchanged and carry no position. attention_queries and attention_keys are read after the
rotary; for the queries before it, read the projection in the same trace:
with model.trace(prompt):
q_raw = model.layers[1].self_attn.source.self_q_proj_0.output.save()
q = model.layers[1].self_attn.attention_queries.save()
b, s, _ = q_raw.shape
q_pre = q_raw.view(b, s, model.num_heads, model.head_dim).transpose(1, 2)
rot = int(model.head_dim * model.config.rope_parameters["partial_rotary_factor"]) # 32
assert torch.equal(q_pre[..., rot:], q[..., rot:]) # past rot: no rotation
The scale is head_dim ** -0.5, and softmax(attention_scores) equals
attention_probabilities.
phi-1.5 and phi-2 are stored in float16 (torch_dtype), phi-1 in float32, and a load without
dtype keeps it. The eager path multiplies queries by keys and scales the product after, and on
phi-2 the unscaled product passes float16's largest value, 65504: on
"The Eiffel Tower is in the city of" it reaches 1.1e6 at block 29 and stays above 65504 at blocks
30 and 31. Under attn_implementation="eager" in float16, block 29's attention_output is not
finite and every logit is NaN. The default sdpa load in float16 is finite (p(Paris) = 0.94),
and so is eager in bfloat16 (0.92). Load the attention interior in bfloat16 or float32:
model = StandardizedTransformer(
"microsoft/phi-2", dispatch=True, attn_implementation="eager", dtype=torch.bfloat16
)
On phi-1.5 the same prompt's largest product is 8.7e3, inside float16's range. With the dtype
set, the eager and sdpa loads compute the same attention: no softcap, sink or window.
The stream at the first position has a single dimension far larger than anything else. At block 16 of phi-2 on the sample prompt, dimension 743 of position 0 is about -790, and no entry at any other position exceeds 17 in magnitude. phi-1.5 shows the same at block 12: -684 at dimension 725, against at most 19 elsewhere. The tokenizer adds no BOS, so position 0 is the prompt's own first token. A norm or a mean of the stream taken over positions is dominated by it; leave position 0 out, or read it on its own.
logits equals lm_head.output, and project_on_vocab applied to the last block's
layer_output equals logits exactly, final_layernorm's bias and lm_head's included. A logit
lens therefore adds the same vector at every block, whatever the input:
bias_logits = model.lm_head.weight @ model.norm.bias + model.lm_head.bias
On phi-2 its standard deviation is 0.33 logits, against 2.3 for a whole lens readout at block 16
on the sample prompt (lm_head.bias alone: 0.04), and its top tokens are \n, -, ., \n\n
and ,. embed_tokens and lm_head are separate matrices. The vocabulary is padded: 51200 rows
against 50295 tokenizer tokens. The 905 extra rows are not zero: at the sample prompt's last position
their logits are below -1 and their total probability is about 1e-6.
The three checkpoints ship CodeGen's -mono tokenizer: GPT-2's 50257 tokens plus one token for
each run of 2 to 31 spaces and of 2 to 9 tabs. A run of two or more spaces is one token, and the
word after it loses its leading space:
model.tokenizer(" return x").input_ids # [50284, 7783, 2124]: ' ', 'return', ' x'
<|endoftext|> (50256) is its bos_token and eos_token, and the tokenizer adds neither.
The sizes, from the configs:
float32 filesfloat16 filesfloat16 filesAll three set a context of 2048 tokens and untied embeddings.
Phi 1 / 1.5 / 2 (PhiForCausalLM).
Llama's containers with a parallel block: one input_layernorm feeds both
sublayers and x + attn + mlp is summed at the end (resid_dropout on
each, inert in eval). The final norm is final_layernorm. There is no
post_attention_layernorm.