transformer_lens.model_bridge.sources.native package

Submodules

Module contents

TL-native model source for TransformerBridge.

class transformer_lens.model_bridge.sources.native.NativeAttention(cfg: TransformerBridgeConfig, rotary: NativeRotary | None = None)

Bases: Module

Split-QKV causal self-attention. Returns (out, pattern).

accepts_pattern_fn: AttentionBridge injects its hook_pattern as pattern_fn, applied BEFORE the value matmul — so hook edits genuinely re-weight the attention output instead of only decorating the returned tuple (which the wrapper cannot recompute from).

accepts_pattern_fn = True
causal_mask: torch.Tensor
forward(hidden_states: Tensor, attention_mask: Tensor | None = None, position_ids: Tensor | None = None, pattern_fn: Callable[[Tensor], Tensor] | None = None, **kwargs) → tuple[Tensor, Tensor]

Define 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.

class transformer_lens.model_bridge.sources.native.NativeBlock(cfg: TransformerBridgeConfig, rotary: NativeRotary | None = None)

Bases: Module

Pre-LN transformer block. Layout adapts to cfg.attn_only and cfg.gated_mlp.

forward(hidden_states: Tensor, attention_mask: Tensor | None = None, position_ids: Tensor | None = None, **kwargs) → tuple[Tensor]

Define 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.

class transformer_lens.model_bridge.sources.native.NativeMLP(cfg: TransformerBridgeConfig)

Bases: Module

Two-layer MLP with configurable activation.

act: Callable[[torch.Tensor], torch.Tensor]
forward(hidden_states: Tensor, **kwargs) → Tensor

Define 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.

class transformer_lens.model_bridge.sources.native.NativeModel(cfg: TransformerBridgeConfig)

Bases: Module

TL-native transformer. See module docstring for the supported feature set.

forward(input_ids: Tensor | None = None, attention_mask: Tensor | None = None, position_ids: Tensor | None = None, inputs_embeds: Tensor | None = None, **kwargs) → Tensor

Returns logits directly.

pos: nn.Embedding | None
rotary: NativeRotary | None
transformer_lens.model_bridge.sources.native.boot(config: TransformerBridgeConfig, tokenizer: Any | None = None, device: str | device | None = None, dtype: dtype | None = None, model_name: str = 'native') → TransformerBridge
transformer_lens.model_bridge.sources.native.boot(config: dict[str, Any], tokenizer: Any | None = None, device: str | device | None = None, dtype: dtype | None = None, model_name: str = 'native') → TransformerBridge

Build a bridge around a small, randomly-initialized TL-native model.

No HuggingFace Hub call, no transformers import. config.init_mode and config.seed control reproducibility.

transformer_lens.model_bridge.sources.native.initialize_native_model(model: NativeModel, cfg: TransformerBridgeConfig, seed: int | None = None) → None

Initialize model weights in-place. Honors cfg.init_mode and cfg.seed.