transformer_lens.components.layer_norm#

Hooked Transformer Layer Norm Component.

This module contains all the component LayerNorm.

class transformer_lens.components.layer_norm.LayerNorm(cfg: Union[Dict, HookedTransformerConfig], length: Optional[int] = None)#

Bases: Module

__init__(cfg: Union[Dict, HookedTransformerConfig], length: Optional[int] = None)#

LayerNorm with optional length parameter

length (Optional[int]): If the dimension of the LayerNorm. If not provided, assumed to be d_model

forward(x: Union[Float[Tensor, 'batch pos d_model'], Float[Tensor, 'batch pos head_index d_model']]) Union[Float[Tensor, 'batch pos d_model'], Float[Tensor, 'batch pos head_index d_model']]#

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.