Coverage for transformer_lens/model_bridge/supported_architectures/openai_gpt.py: 92%

22 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2026-11-25 00:28 +0000

1"""OpenAI GPT (GPT-1) architecture adapter. 

2 

3The original GPT (``OpenAIGPTLMHeadModel``, openai-community/openai-gpt). 

4GPT-2's block internals under different top-level names — combined-QKV 

5``c_attn`` Conv1D, ``c_fc``/``c_proj`` MLP — but POST-norm (``ln_1(x + attn)``, 

6``ln_2(n + mlp)``), no final LayerNorm, and embeddings at 

7``transformer.tokens_embed`` / ``transformer.positions_embed``. 

8""" 

9 

10from typing import Any 

11 

12from transformer_lens.model_bridge.architecture_adapter import ArchitectureAdapter 

13from transformer_lens.model_bridge.generalized_components import ( 

14 BlockBridge, 

15 EmbeddingBridge, 

16 JointQKVAttentionBridge, 

17 LinearBridge, 

18 NormalizationBridge, 

19 PosEmbedBridge, 

20 UnembeddingBridge, 

21) 

22 

23 

24class _OpenAIGPTJointQKVAttentionBridge(JointQKVAttentionBridge): 

25 """GPT-1's Block concatenates ``[h] + attn_outputs[1:]`` — it requires the 

26 attention module to return a list, not the tuple modern blocks accept.""" 

27 

28 # OpenAIGPTModel hands its blocks a [batch, 1, 1, seq] padding mask; the 

29 # causal mask lives in the attention module's ``bias`` buffer. 

30 broadcast_mask_is_padding_only = True 

31 

32 def _process_output(self, output: Any) -> Any: 

33 processed = super()._process_output(output) 

34 if isinstance(processed, tuple): 34 ↛ 36line 34 didn't jump to line 36 because the condition on line 34 was always true

35 return list(processed) 

36 return [processed] 

37 

38 

39class OpenAIGPTArchitectureAdapter(ArchitectureAdapter): 

40 """Architecture adapter for OpenAIGPTLMHeadModel (GPT-1) models.""" 

41 

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

43 """Initialize the OpenAI GPT architecture adapter.""" 

44 super().__init__(cfg) 

45 

46 self.cfg.normalization_type = "LN" 

47 self.cfg.positional_embedding_type = "standard" 

48 self.cfg.final_rms = False 

49 self.cfg.gated_mlp = False 

50 self.cfg.attn_only = False 

51 

52 # GPT-1 is post-LN; fold-LN assumes pre-LN and would fold norms into 

53 # the wrong sublayers. 

54 self.supports_fold_ln = False 

55 self.supports_center_writing_weights = False 

56 self.weight_processing_conversions = {} 

57 

58 self.component_mapping = { 

59 "embed": EmbeddingBridge(name="transformer.tokens_embed"), 

60 "pos_embed": PosEmbedBridge(name="transformer.positions_embed"), 

61 "blocks": BlockBridge( 

62 name="transformer.h", 

63 config=self.cfg, 

64 submodules={ 

65 "attn": _OpenAIGPTJointQKVAttentionBridge( 

66 name="attn", 

67 config=self.cfg, 

68 submodules={ 

69 "qkv": LinearBridge(name="c_attn"), 

70 "o": LinearBridge(name="c_proj"), 

71 }, 

72 ), 

73 # Post-norm: ln_1 follows attention, ln_2 follows the MLP. 

74 "ln1": NormalizationBridge(name="ln_1", config=self.cfg), 

75 "mlp": self._ungated_mlp(up="c_fc", down="c_proj"), 

76 "ln2": NormalizationBridge(name="ln_2", config=self.cfg), 

77 }, 

78 # Post-norm: ln2.hook_in is the post-MLP sum n+m; the true 

79 # attn->MLP mid-point n = ln_1(x+a) is mlp.hook_in. 

80 hook_alias_overrides={ 

81 "hook_resid_mid": "mlp.hook_in", 

82 }, 

83 ), 

84 "unembed": UnembeddingBridge(name="lm_head"), 

85 }