Coverage for transformer_lens/model_bridge/supported_architectures/olmo2.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2026-10-23 20:45 +0000

1"""OLMo 2 architecture adapter.""" 

2 

3from typing import Any 

4 

5from transformer_lens.model_bridge.architecture_adapter import ArchitectureAdapter 

6from transformer_lens.model_bridge.generalized_components import ( 

7 BlockBridge, 

8 EmbeddingBridge, 

9 LinearBridge, 

10 PositionEmbeddingsAttentionBridge, 

11 RMSNormalizationBridge, 

12 RotaryEmbeddingBridge, 

13 UnembeddingBridge, 

14) 

15 

16 

17class Olmo2ArchitectureAdapter(ArchitectureAdapter): 

18 """Architecture adapter for OLMo 2 models. 

19 

20 OLMo 2 uses a post-norm architecture with RMSNorm, Q/K normalization in attention, 

21 rotary position embeddings (RoPE), and gated MLP (SwiGLU). Key differences from 

22 pre-norm models like Llama: 

23 

24 - Post-norm: RMSNorm is applied AFTER attention and AFTER MLP, not before. 

25 ln1_post maps to post_attention_layernorm, ln2_post to post_feedforward_layernorm; 

26 there is no ln1/ln2 (no pre-sublayer norm). 

27 - Q/K normalization: Per-head RMSNorm applied to queries and keys after projection. 

28 - No biases on any projections. 

29 

30 Optional Parameters (may not exist in state_dict): 

31 ------------------------------------------------- 

32 - blocks.{i}.attn.b_Q - No bias on query projection 

33 - blocks.{i}.attn.b_K - No bias on key projection 

34 - blocks.{i}.attn.b_V - No bias on value projection 

35 - blocks.{i}.attn.b_O - No bias on output projection 

36 - blocks.{i}.mlp.b_in - No bias on MLP up_proj 

37 - blocks.{i}.mlp.b_gate - No bias on MLP gate_proj 

38 - blocks.{i}.mlp.b_out - No bias on MLP down_proj 

39 - blocks.{i}.ln1_post.b - RMSNorm has no bias 

40 - blocks.{i}.ln2_post.b - RMSNorm has no bias 

41 - ln_final.b - RMSNorm has no bias 

42 """ 

43 

44 # Attention bridge seam; EXAONE-4 swaps in its NoPE-gating variant. 

45 _attention_bridge_cls = PositionEmbeddingsAttentionBridge 

46 

47 def __init__(self, cfg: Any) -> None: 

48 """Initialize the OLMo 2 architecture adapter.""" 

49 super().__init__(cfg) 

50 

51 self._set_rms_rotary_defaults() 

52 # OLMo-2 uses post-norm (RMSNorm AFTER attention/MLP), so layer norm 

53 # folding into QKV/MLP weights is incorrect — the norms apply to the 

54 # output, not the input. Same pattern as BERT and Phi-3. 

55 self.supports_fold_ln = False 

56 # Force eager attention for numerical consistency with benchmark reference. 

57 # PositionEmbeddingsAttentionBridge delegates to native HF attention, so 

58 # both bridge and reference must use the same implementation. 

59 self.cfg.attn_implementation = "eager" 

60 

61 self.weight_processing_conversions = { 

62 **self._qkvo_weight_conversions(), 

63 } 

64 

65 self.component_mapping = { 

66 "embed": EmbeddingBridge(name="model.embed_tokens"), 

67 "rotary_emb": RotaryEmbeddingBridge(name="model.rotary_emb", config=self.cfg), 

68 "blocks": BlockBridge( 

69 name="model.layers", 

70 submodules={ 

71 # Post-norm only: BlockBridge derives hook_attn_out / hook_mlp_out 

72 # (norm outputs) and hook_resid_mid (mlp.hook_in) from these keys. 

73 "ln1_post": RMSNormalizationBridge( 

74 name="post_attention_layernorm", config=self.cfg 

75 ), 

76 "ln2_post": RMSNormalizationBridge( 

77 name="post_feedforward_layernorm", config=self.cfg 

78 ), 

79 "attn": self._attention_bridge_cls( 

80 name="self_attn", 

81 config=self.cfg, 

82 submodules={ 

83 "q": LinearBridge(name="q_proj"), 

84 "k": LinearBridge(name="k_proj"), 

85 "v": LinearBridge(name="v_proj"), 

86 "o": LinearBridge(name="o_proj"), 

87 "q_norm": RMSNormalizationBridge(name="q_norm", config=self.cfg), 

88 "k_norm": RMSNormalizationBridge(name="k_norm", config=self.cfg), 

89 }, 

90 requires_attention_mask=True, 

91 requires_position_embeddings=True, 

92 ), 

93 "mlp": self._build_mlp_bridge(), 

94 }, 

95 ), 

96 "ln_final": RMSNormalizationBridge(name="model.norm", config=self.cfg), 

97 "unembed": UnembeddingBridge(name="lm_head", config=self.cfg), 

98 } 

99 

100 def _build_mlp_bridge(self): 

101 """MLP bridge seam; FlexOlmo swaps in the MoE variant.""" 

102 return self._gated_mlp()