nnterp · encyclopedia xglm · nnterp.families.xglm

family xglm

XGLM

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 + self_attn.attention_output + fc2.output == layer_output

The API

02 · support()

Values, by host

model XGLMForCausalLMlayoutwhat 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] XGLMDecoderLayerlayoutwhat 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 input_layernorm's output

layer_mid Residual [batch seq hidden_size] The residual stream after the attention add

the child module final_layer_norm's input

mlp_norm_output Residual [batch seq hidden_size] The normed stream entering the feed-forward path

the child module final_layer_norm's output

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].self_attn XGLMAttentionlayoutwhat it isread at
attention_queries_prerope Queries [batch num_heads seq qk_head_dim] The queries entering the rotary embedding

nowhere: no rotary embedding: XGLM adds sinusoidal absolute position embeddings to the stream

attention_keys_prerope Keys [batch num_kv_heads seq qk_head_dim] The keys entering the rotary embedding

nowhere: no rotary embedding: XGLM adds sinusoidal absolute position embeddings to the stream

attention_queries Queries [batch num_heads seq qk_head_dim] The queries entering attention, already scaled by 1/sqrt(head_dim)

inside the forward: the output of query_states_0

attention_keys Keys [batch num_kv_heads seq qk_head_dim] The keys entering attention

inside the forward: the output of key_states_reshape_0

attention_values Values [batch num_kv_heads seq head_dim] The values entering attention

inside the forward: the output of value_states_reshape_0

attention_scores Pattern [batch num_heads query key] The attention scores entering the softmax, masked

inside the forward: the output of attn_weights_2

attention_premix HeadOutputs [batch seq num_heads head_dim] Every head's output side by side, as the output projection takes it

the child module o_proj's input

attention_output Residual [batch seq hidden_size] What the attention adds to the residual stream

the module's own output

attention_probabilities Pattern [batch num_heads query key] The attention pattern the values are mixed with

inside the forward: the output of nn_functional_dropout_0

attention_head_outputs HeadOutputs [batch seq num_heads head_dim] The per-head outputs before the output projection

inside the forward: the output of attn_output_2

interface method (Queries, Keys, Values) -> (HeadOutputs, Pattern) The attention function this call ran, as f(queries, keys, values) -> (head outputs, pattern), with the rest of the call's arguments

nowhere: the queries, keys and values are not the arguments of one call in this forward that returns the head outputs (the attention does its own arithmetic, or changes the call's output before the projection), so there is no attention function to call again

Printout

What print(model) shows on facebook/xglm-564M: the native modules, standard names and every standard value with its layout.

XGLMForCausalLM(
  (lm_head): Linear(in_features=1024, out_features=256008, bias=False)
  (generator): Generator(
    (streamer): Streamer()
  )
  (embed_tokens): XGLMScaledWordEmbedding(256008, 1024, padding_idx=1)
  (layers): ModuleList(
    (0-23): 24 x XGLMDecoderLayer(
      (self_attn): XGLMAttention(
        (k_proj): Linear(in_features=1024, out_features=1024, bias=True)
        (v_proj): Linear(in_features=1024, out_features=1024, bias=True)
        (q_proj): Linear(in_features=1024, out_features=1024, bias=True)
        (o_proj/out_proj): Linear(in_features=1024, out_features=1024, bias=True)
        (attention_queries) -> Queries [batch num_heads seq qk_head_dim]: The queries entering attention, already scaled by 1/sqrt(head_dim)
        (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_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
        (attention_queries_prerope) -> Queries [batch num_heads seq qk_head_dim]: The queries entering the rotary embedding
        (attention_keys_prerope) -> Keys [batch num_kv_heads seq qk_head_dim]: The keys entering the rotary embedding
        (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
        (interface): The attention function this call ran, as f(queries, keys, values) -> (head outputs, pattern), with the rest of the call's arguments
      )
      (activation_fn): GELUActivation()
      (input_layernorm/self_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True, bias=True)
      (fc1): Linear(in_features=1024, out_features=4096, bias=True)
      (fc2): Linear(in_features=4096, out_features=1024, bias=True)
      (final_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True, bias=True)
      (layer_mid) -> Residual [batch seq hidden_size]: The residual stream after the attention add
      (mlp_norm_output) -> Residual [batch seq hidden_size]: The normed stream entering the feed-forward path
      (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((1024,), 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
)

Config

model.num_layers24
model.hidden_size1024
model.num_heads16
model.num_kv_heads16
model.head_dim64
model.qk_head_dim64
model.vocab_size256008
model.intermediate_size4096
architecturesXGLMForCausalLM
vocab_size256008
max_position_embeddings2048
tie_word_embeddingsTrue
activation_functiongelu
d_model1024
ffn_dim4096
scale_embeddingTrue

The Notes

03 · what to know before you run

The MLP's contribution is layers[i].fc2.output

There is no mlp module, so mlp_output and the other mlp values do not exist. The MLP path adds layers[i].fc2.output to the stream, and its neurons are layers[i].activation_fn.output.

An edit to the keys or values reaches every invoke

attention_keys and attention_values are read after the cache update, over the whole batch, so under several invokes each invoke reads every invoke's keys and values and an edit in one reaches them all. Edit keys and values in a trace with one invoke.

From the family module

04 · nnterp/families/xglm.py

XGLM (XGLMForCausalLM).

model.{embed_tokens, embed_positions, layers[i].{self_attn, self_attn_layer_norm, fc1, fc2, final_layer_norm}, layer_norm} and lm_head. embed_tokens is a scaled embedding (times sqrt(d_model) under scale_embedding), so token_embeddings is the scaled lookup; the sinusoidal positions are added after it, outside any module with a standard name. As on OPT there is no MLP module: fc1 and fc2 sit on the block, so mlp and mlp_output do not exist here and StandardizedTransformer.support says so. The block's own final_layer_norm is the pre-MLP norm and keeps its native name. The block returns a bare tensor. The attention does its own arithmetic whatever attn_implementation says (the model has no other): queries scaled by 1/sqrt(head_dim) as they are projected, queries, keys and values flattened to [batch * num_heads, seq, head_dim] for torch.bmm, the mask added and clamped, softmax, dropout, bmm with the values; the interior needs no eager load. The queries, scores, pattern and head outputs are read where the forward holds them batch first, so each invoke reads and edits its own rows; the keys and values, read after the cache update, are the whole batch's in every invoke.