Coverage for transformer_lens/utilities/architectures.py: 82%

58 statements  

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

1"""Centralized architecture classification for TransformerLens. 

2 

3Single source of truth for architecture type detection. Used by the bridge 

4loading pipeline, benchmarks, and verification tools. 

5""" 

6 

7from typing import Optional 

8 

9# Encoder-decoder models (T5, BART, etc.) 

10SEQ2SEQ_ARCHITECTURES: set[str] = { 

11 "T5ForConditionalGeneration", 

12 "MT5ForConditionalGeneration", 

13 "T5WithLMHeadModel", 

14 "T5GemmaForConditionalGeneration", 

15 "LongT5ForConditionalGeneration", 

16 "T5Gemma2ForConditionalGeneration", 

17 "BartForConditionalGeneration", 

18 "MBartForConditionalGeneration", 

19 "M2M100ForConditionalGeneration", 

20 "MarianMTModel", 

21 "PegasusForConditionalGeneration", 

22 "BlenderbotForConditionalGeneration", 

23 "BlenderbotSmallForConditionalGeneration", 

24 "LEDForConditionalGeneration", 

25 "SwitchTransformersForConditionalGeneration", 

26} 

27 

28# Post-norm decoders: ln1/ln2 normalize each sublayer's OUTPUT before the residual 

29# add, so LN folding and writing-weight centering (which assume the gain sits on a 

30# sublayer's INPUT) are not valid algebra for them. 

31POST_NORM_ARCHITECTURES: set[str] = { 

32 "Olmo2ForCausalLM", 

33 "Olmo3ForCausalLM", 

34} 

35 

36# Masked language models (BERT-style, no text generation) 

37MASKED_LM_ARCHITECTURES: set[str] = { 

38 "BertForMaskedLM", 

39 "RobertaForMaskedLM", 

40 "AlbertForMaskedLM", 

41 "DistilBertForMaskedLM", 

42 "ElectraForMaskedLM", 

43 "BD3LM", 

44} 

45 

46# Vision-language multimodal models 

47MULTIMODAL_ARCHITECTURES: set[str] = { 

48 "Emu3ForConditionalGeneration", 

49 "LlavaForConditionalGeneration", 

50 "LlavaNextForConditionalGeneration", 

51 "LlavaOnevisionForConditionalGeneration", 

52 "Gemma3ForConditionalGeneration", 

53 "Gemma4ForConditionalGeneration", 

54 "Qwen3_5ForConditionalGeneration", 

55 "Qwen3_5MoeForConditionalGeneration", 

56 "Idefics3ForConditionalGeneration", 

57 "Florence2ForConditionalGeneration", 

58 "Mistral3ForConditionalGeneration", 

59 "Llama4ForConditionalGeneration", 

60 "MuseGlimmerForConditionalGeneration", 

61 "Qwen2_5_VLForConditionalGeneration", 

62 "Qwen3VLForConditionalGeneration", 

63 "Qwen3VLMoeForConditionalGeneration", 

64 "Glm4vForConditionalGeneration", 

65} 

66 

67# Audio-conditioned text decoders (audio encoder + causal LM); load via 

68# AutoModelForSeq2SeqLM but behave as text decoders for classification. 

69AUDIO_TEXT_ARCHITECTURES: set[str] = { 

70 "Qwen2AudioForConditionalGeneration", 

71 "GlmAsrForConditionalGeneration", 

72 "AudioFlamingo3ForConditionalGeneration", 

73 "MusicFlamingoForConditionalGeneration", 

74} 

75 

76# Audio encoder + text decoder (Whisper); load via AutoModelForSpeechSeq2Seq and 

77# take log-mel input_features plus decoder token ids. 

78AUDIO_SEQ2SEQ_ARCHITECTURES: set[str] = { 

79 "WhisperForConditionalGeneration", 

80} 

81 

82# Audio spectrogram models for classification 

83AUDIO_CLASSIFICATION_ARCHITECTURES: set[str] = { 

84 "ASTForAudioClassification", 

85} 

86 

87# Audio encoder models (HuBERT, wav2vec2, etc.) 

88AUDIO_ARCHITECTURES: set[str] = { 

89 "HubertForCTC", 

90 "HubertModel", 

91 "HubertForSequenceClassification", 

92 "Wav2Vec2ForCTC", 

93 "Wav2Vec2Model", 

94 # Pretraining checkpoints (facebook/wav2vec2-base/-large declare this class) 

95 # load their encoder via AutoModel -> Wav2Vec2Model. 

96 "Wav2Vec2ForPreTraining", 

97} | AUDIO_CLASSIFICATION_ARCHITECTURES 

98 

99# Vision-only (non-multimodal, no text tower) encoder models. Split into the 

100# two HF AutoModel classes they load under: bare encoders load via AutoModel, 

101# classification heads load via AutoModelForImageClassification. 

102VISION_MODEL_ARCHITECTURES: set[str] = { 

103 "ViTModel", 

104 "DeiTModel", 

105} 

106VISION_CLASSIFICATION_ARCHITECTURES: set[str] = { 

107 "ViTForImageClassification", 

108 "DeiTForImageClassification", 

109} 

110VISION_ARCHITECTURES: set[str] = VISION_MODEL_ARCHITECTURES | VISION_CLASSIFICATION_ARCHITECTURES 

111 

112# Text models whose remote code registers only under plain AutoModel 

113# (the class itself carries the LM head). 

114BASE_AUTOMODEL_ARCHITECTURES: set[str] = { 

115 "DreamModel", 

116} 

117 

118 

119def classify_architecture(architecture: str) -> str: 

120 """Classify an architecture string into a model type. 

121 

122 Returns one of: "seq2seq", "masked_lm", "multimodal", "audio", "vision", "causal_lm" 

123 """ 

124 if architecture in SEQ2SEQ_ARCHITECTURES or architecture in AUDIO_SEQ2SEQ_ARCHITECTURES: 

125 return "seq2seq" 

126 if architecture in MASKED_LM_ARCHITECTURES: 

127 return "masked_lm" 

128 if architecture in MULTIMODAL_ARCHITECTURES: 

129 return "multimodal" 

130 if architecture in AUDIO_ARCHITECTURES: 

131 return "audio" 

132 if architecture in VISION_ARCHITECTURES: 

133 return "vision" 

134 return "causal_lm" 

135 

136 

137def get_architectures_for_config(config) -> list[str]: 

138 """Extract architecture strings from an HF config object.""" 

139 architectures = [] 

140 if hasattr(config, "original_architecture"): 140 ↛ 141line 140 didn't jump to line 141 because the condition on line 140 was never true

141 architectures.append(config.original_architecture) 

142 if hasattr(config, "architectures") and config.architectures: 142 ↛ 144line 142 didn't jump to line 144 because the condition on line 142 was always true

143 architectures.extend(config.architectures) 

144 return architectures 

145 

146 

147def classify_model_config(config) -> str: 

148 """Classify a model by its HF config. 

149 

150 Checks config.is_encoder_decoder first, then falls back to architecture list. 

151 Returns one of: "seq2seq", "masked_lm", "multimodal", "audio", "vision", "causal_lm" 

152 """ 

153 if getattr(config, "is_encoder_decoder", False): 153 ↛ 154line 153 didn't jump to line 154 because the condition on line 153 was never true

154 return "seq2seq" 

155 for arch in get_architectures_for_config(config): 155 ↛ 159line 155 didn't jump to line 159 because the loop on line 155 didn't complete

156 model_type = classify_architecture(arch) 

157 if model_type != "causal_lm": 157 ↛ 155line 157 didn't jump to line 155 because the condition on line 157 was always true

158 return model_type 

159 return "causal_lm" 

160 

161 

162def classify_model_name( 

163 model_name: str, 

164 trust_remote_code: bool = False, 

165 token: Optional[str] = None, 

166) -> str: 

167 """Classify a model by its HuggingFace model name. 

168 

169 Loads the config once, classifies from it. If token is None, reads 

170 HF_TOKEN from the environment automatically. 

171 Returns one of: "seq2seq", "masked_lm", "multimodal", "audio", "vision", "causal_lm" 

172 """ 

173 try: 

174 from transformers import AutoConfig 

175 

176 if token is None: 176 ↛ 181line 176 didn't jump to line 181 because the condition on line 176 was always true

177 from transformer_lens.utilities.hf_utils import get_hf_token 

178 

179 token = get_hf_token() 

180 

181 config = AutoConfig.from_pretrained( 

182 model_name, trust_remote_code=trust_remote_code, token=token 

183 ) 

184 return classify_model_config(config) 

185 except Exception: 

186 return "causal_lm" 

187 

188 

189def is_masked_lm_model( 

190 model_name: str, trust_remote_code: bool = False, token: Optional[str] = None 

191) -> bool: 

192 """Check if a model is a masked language model (BERT-style).""" 

193 return ( 

194 classify_model_name(model_name, trust_remote_code=trust_remote_code, token=token) 

195 == "masked_lm" 

196 ) 

197 

198 

199def is_encoder_decoder_model( 

200 model_name: str, trust_remote_code: bool = False, token: Optional[str] = None 

201) -> bool: 

202 """Check if a model is an encoder-decoder architecture (T5, BART, etc.).""" 

203 return ( 

204 classify_model_name(model_name, trust_remote_code=trust_remote_code, token=token) 

205 == "seq2seq" 

206 ) 

207 

208 

209def is_multimodal_model( 

210 model_name: str, trust_remote_code: bool = False, token: Optional[str] = None 

211) -> bool: 

212 """Check if a model is a multimodal vision-language model (LLaVA, Gemma3).""" 

213 return ( 

214 classify_model_name(model_name, trust_remote_code=trust_remote_code, token=token) 

215 == "multimodal" 

216 ) 

217 

218 

219def is_audio_model( 

220 model_name: str, trust_remote_code: bool = False, token: Optional[str] = None 

221) -> bool: 

222 """Check if a model is an audio encoder model (HuBERT, wav2vec2).""" 

223 return ( 

224 classify_model_name(model_name, trust_remote_code=trust_remote_code, token=token) == "audio" 

225 )