@@ -54,7 +54,12 @@ def test_model_builder_id(self):
5454
5555 sess = onnxruntime .InferenceSession (model_name , providers = ["CPUExecutionProvider" ])
5656 del data ["inputs" ]["position_ids" ]
57- feeds = make_feeds ([i .name for i in sess .get_inputs ()], data ["inputs" ], use_numpy = True )
57+ feeds = make_feeds (
58+ [i .name for i in sess .get_inputs ()],
59+ data ["inputs" ],
60+ use_numpy = True ,
61+ check_flatten = False ,
62+ )
5863 expected = data ["model" ](** data ["inputs" ])
5964
6065 try :
@@ -69,6 +74,106 @@ def test_find_names_pattern(self):
6974 self .assertEqual ("past_key_values_key_%d" , find_names_pattern (pats ))
7075 self .assertEqual ("past_key_values_key_%d" , find_names_pattern (pats [:1 ]))
7176
77+ @requires_transformers ("4.52" )
78+ def test_model_buildersupported_classes (self ):
79+ import torch
80+
81+ def has_final_norm (module , orig_model ):
82+ if orig_model .__class__ .__name__ .startswith ("Peft" ):
83+ model = orig_model .base_model .model
84+ else :
85+ model = orig_model
86+
87+ hf_norm = (
88+ hasattr (model , "model" )
89+ and hasattr (model .model , "norm" )
90+ and module == model .model .norm
91+ )
92+ hf_final_layernorm = (
93+ hasattr (model , "model" )
94+ and hasattr (model .model , "final_layernorm" )
95+ and module == model .model .final_layernorm
96+ )
97+ hf_transformer_final_layernorm = (
98+ hasattr (model , "transformer" )
99+ and hasattr (model .transformer , "encoder" )
100+ and hasattr (model .transformer .encoder , "final_layernorm" )
101+ and module == model .transformer .encoder .final_layernorm
102+ )
103+ hf_language_model_norm = (
104+ hasattr (model , "model" )
105+ and hasattr (model .model , "language_model" )
106+ and hasattr (model .model .language_model , "norm" )
107+ and module == model .model .language_model .norm
108+ )
109+
110+ gguf_final_norm = hasattr (model , "final_norm" ) and module == model .final_norm
111+ hf_names = [
112+ hf_norm ,
113+ hf_final_layernorm ,
114+ hf_transformer_final_layernorm ,
115+ hf_language_model_norm ,
116+ ]
117+ gguf_names = [gguf_final_norm ]
118+ return any (hf_names + gguf_names )
119+
120+ data = get_untrained_model_with_inputs ("microsoft/Phi-3.5-mini-instruct" )
121+ model = data ["model" ]
122+
123+ exclude_lm_head = False # extra_options, use hidden_stats instead of logits (outputs)
124+ exclude_embeds = False # extra_options, use input_embeds instead of input_ids (inputs)
125+
126+ # num_hidden_layers
127+ num_layers = (
128+ model .config .num_hidden_layers
129+ if hasattr (model .config , "num_hidden_layers" )
130+ else model .config .num_layers
131+ )
132+
133+ cls = []
134+ layer_id = - 1
135+ prefix_layer = None
136+ for name , module in model .named_modules ():
137+ if isinstance (module , torch .nn .ModuleList ):
138+ prefix_layer = name
139+ continue
140+ if prefix_layer and not name .startswith (prefix_layer ):
141+ layer_id = - 1
142+ if isinstance (module , torch .nn .Embedding ) or (
143+ hasattr (model , "embedding" ) and module == model .embedding
144+ ):
145+ if not exclude_embeds :
146+ cls .append (("make_embedding" , layer_id , name , module ))
147+ continue
148+ if (
149+ module .__class__ .__name__ .endswith ("DecoderLayer" )
150+ or module .__class__ .__name__ .endswith ("GLMBlock" )
151+ ) and layer_id < num_layers :
152+ layer_id += 1
153+ cls .append (("make_layer" , layer_id , name , module ))
154+ continue
155+ if layer_id == num_layers and has_final_norm (module , model ):
156+ cls .append (("make_layernorm" , layer_id , name , module ))
157+ continue
158+ if isinstance (module , torch .nn .Linear ) or (
159+ hasattr (model , "lm_head" ) and module == model .lm_head
160+ ):
161+ if not exclude_lm_head :
162+ cls .append (("make_lm_head" , layer_id , name , module ))
163+ continue
164+
165+ cls .append (("skipped" , layer_id , name , module ))
166+
167+ colnames = ["converter" , "layer_id" , "name" , "class" ]
168+ cls = [dict (zip (colnames , (* obs [:3 ], obs [3 ].__class__ .__name__ ))) for obs in cls ]
169+ length = {k : max (len (str (obs [k ])) for obs in cls ) + 1 for k in cls [0 ]}
170+ msg = []
171+ for obs in cls :
172+ cc = [f"{ v } { ' ' * (length [k ] - len (str (v )))} " for k , v in obs .items ()]
173+ msg .append (" " .join (cc ))
174+ self .assertEqual (len (msg ), 30 )
175+ self .assertEqual (cls [- 1 ]["layer_id" ], - 1 )
176+
72177
73178if __name__ == "__main__" :
74179 unittest .main (verbosity = 2 )
0 commit comments