Skip to content

layer

layer

Layer: a decoder block, whose residual stream is layer_output.

Residual module-attribute

Residual = Float[Tensor, f'{BATCH} {SEQ} {HIDDEN}']

FlatResidual module-attribute

FlatResidual = Float[Tensor, f'{TOKENS} {HIDDEN}']

Streams module-attribute

Streams = Float[Tensor, f'{BATCH} {SEQ} {STREAMS} {HIDDEN}']

StreamWeights module-attribute

StreamWeights = Float[Tensor, f'{BATCH} {SEQ} {STREAMS}']

StreamMixing module-attribute

StreamMixing = Float[Tensor, f'{BATCH} {SEQ} {STREAMS} {STREAMS}']

PARALLEL module-attribute

PARALLEL = 'a parallel block: the attention and the MLP read the same input, so there is no stream between them'

POST_NORM_ATTENTION module-attribute

POST_NORM_ATTENTION = 'a post-norm block: the attention reads the residual stream itself, layer_input'

POST_NORM_MLP module-attribute

POST_NORM_MLP = 'a post-norm block: the MLP reads the residual stream itself, layer_mid'

STATE_SPACE module-attribute

STATE_SPACE = 'a state-space block: one sublayer, the mixer, so there is no stream between sublayers and no MLP'

ONE_SUBLAYER module-attribute

ONE_SUBLAYER = 'a one-sublayer block: its norm feeds its one sublayer, so there is no stream between sublayers'

Layer

Layer(*args: Any, **kwargs: Any)

Bases: Standard

A decoder block. Its hidden states are layer_output, whatever the block returns.

ATTRIBUTE DESCRIPTION
self_attn

The softmax attention, an Attention; absent on a hybrid's linear blocks.

TYPE: Attention

linear_attn

The recurrent mixer, a LinearAttention (gated DeltaNet) or a StateSpace (Mamba-2); hybrids and state-space models only.

TYPE: LinearAttention

mlp

The feed-forward, an Mlp; absent on OPT.

TYPE: Mlp

input_layernorm,

The block's norms under their aliased names, where the family has them (their meaning varies; see the README).

TYPE: post_attention_layernorm

Between layer_input and layer_output the block has four more boundaries, each the input or output of one of its modules: attention_norm_output, layer_mid, mlp_norm_output and mlp_input. They are defined on the pre-norm block (x + attn(norm1(x)), then + mlp(norm2(.))) and keyed on the aliased norms, so on a block built otherwise the family's subclass points them at its own modules or marks them unavailable with the reason; a block without the module reports no <module> module on this block.

self_attn instance-attribute

self_attn: Attention

linear_attn instance-attribute

linear_attn: LinearAttention

mlp instance-attribute

mlp: Mlp

input_layernorm instance-attribute

input_layernorm: Envoy

post_attention_layernorm instance-attribute

post_attention_layernorm: Envoy

returns_tuple class-attribute instance-attribute

returns_tuple = False

skip_with

skip_with(hidden: Tensor) -> None

Skip this block, handing hidden on as its residual stream.

The block does not run; hidden takes the place of its layer_output, packed the way the block would have returned it (a tuple family gets (hidden, None): the second element is the attention weights nothing downstream reads). Call it inside a trace, before the block runs.

layer_input

layer_input(value: Tensor) -> Residual

The residual stream entering this block, before any of its norms.

The block's first argument where the block is called with the stream (every transformers family), and the stream wherever an engine passes it otherwise (vLLM's fused blocks take it in two halves). With the contributions it is the identity the values are defined by: layer_input + attention_output + mlp_output == layer_output. In-place edits and assignment reach the model.

attention_norm_output

attention_norm_output(value: Tensor) -> Residual

The attention's pre-norm output: what the attention is called with, [batch, seq, hidden_size].

input_layernorm.output on a pre-norm block. In-place edits and assignment reach the model.

layer_mid

layer_mid(value: Tensor) -> Residual

The residual stream between the two sublayers, [batch, seq, hidden_size].

What the MLP's pre-norm is called with: layer_input + attention_output on a pre-norm block, where it is read as post_attention_layernorm.input. A parallel block has no such stream, and marks this unavailable. In-place edits and assignment reach the model.

mlp_norm_output

mlp_norm_output(value: Tensor) -> Residual

The MLP's pre-norm output, [batch, seq, hidden_size]: post_attention_layernorm.output on a pre-norm block.

The same tensor as mlp_input where nothing sits between the norm and the MLP. In-place edits and assignment reach the model.

mlp_input

mlp_input(value: Tensor) -> Residual

The MLP's first argument, [batch, seq, hidden_size]. In-place edits and assignment reach the model.

layer_output

layer_output(value: Tensor) -> Any