11import pprint
22import unittest
3- import torch
4- import transformers
53from onnx_diagnostic .ext_test_case import (
64 ExtTestCase ,
75 hide_stdout ,
86 requires_torch ,
97 requires_transformers ,
108 ignore_errors ,
119)
12- from onnx_diagnostic .torch_models .hghub .model_inputs import (
13- config_class_from_architecture ,
14- get_untrained_model_with_inputs ,
15- )
10+ from onnx_diagnostic .torch_models .hghub .model_inputs import get_untrained_model_with_inputs
1611from onnx_diagnostic .torch_models .hghub .hub_api import get_pretrained_config
1712from onnx_diagnostic .torch_models .hghub .hub_data import load_models_testing
1813from onnx_diagnostic .torch_export_patches import bypass_export_some_errors
1914
2015
2116class TestHuggingFaceHubModel (ExtTestCase ):
22- @requires_transformers ("4.50" ) # we limit to some versions of the CI
23- @requires_torch ("2.7" )
24- def test_config_class_from_architecture (self ):
25- config = config_class_from_architecture ("LlamaForCausalLM" )
26- self .assertEqual (config , transformers .LlamaConfig )
27-
2817 @hide_stdout ()
2918 def test_get_untrained_model_with_inputs_tiny_llm (self ):
3019 mid = "arnir0/Tiny-LLM"
@@ -107,91 +96,6 @@ def test_get_untrained_model_with_inputs_clip_vit(self):
10796 # different expected value for different version of transformers
10897 self .assertIn ((data ["size" ], data ["n_weights" ]), [(188872708 , 47218177 )])
10998
110- @hide_stdout ()
111- def test_get_untrained_model_with_inputs_text2text_generation (self ):
112- mid = "sshleifer/tiny-marian-en-de"
113- # mid = "Salesforce/codet5-small"
114- data = get_untrained_model_with_inputs (mid , verbose = 1 )
115- self .assertIn ((data ["size" ], data ["n_weights" ]), [(473928 , 118482 )])
116- model , inputs = data ["model" ], data ["inputs" ]
117- raise unittest .SkipTest (f"not working for { mid !r} " )
118- model (** inputs )
119-
120- @hide_stdout ()
121- def test_get_untrained_model_with_inputs_automatic_speech_recognition (self ):
122- mid = "openai/whisper-tiny"
123- data = get_untrained_model_with_inputs (mid , verbose = 1 )
124- self .assertIn ((data ["size" ], data ["n_weights" ]), [(132115968 , 33028992 )])
125- model , inputs , ds = data ["model" ], data ["inputs" ], data ["dynamic_shapes" ]
126- Dim = torch .export .Dim
127- self .maxDiff = None
128- self .assertIn ("{0:Dim(batch),1:Dim(seq_length)}" , self .string_type (ds ))
129- self .assertEqualAny (
130- {
131- "decoder_input_ids" : {
132- 0 : Dim ("batch" , min = 1 , max = 1024 ),
133- 1 : Dim ("seq_length" , min = 1 , max = 4096 ),
134- },
135- "cache_position" : {0 : Dim ("seq_length" , min = 1 , max = 4096 )},
136- "encoder_outputs" : [{0 : Dim ("batch" , min = 1 , max = 1024 )}],
137- "past_key_values" : [
138- [
139- [
140- {0 : Dim ("batch" , min = 1 , max = 1024 )},
141- {0 : Dim ("batch" , min = 1 , max = 1024 )},
142- ],
143- [
144- {0 : Dim ("batch" , min = 1 , max = 1024 )},
145- {0 : Dim ("batch" , min = 1 , max = 1024 )},
146- ],
147- ],
148- [
149- [
150- {0 : Dim ("batch" , min = 1 , max = 1024 )},
151- {0 : Dim ("batch" , min = 1 , max = 1024 )},
152- ],
153- [
154- {0 : Dim ("batch" , min = 1 , max = 1024 )},
155- {0 : Dim ("batch" , min = 1 , max = 1024 )},
156- ],
157- ],
158- ],
159- },
160- ds ,
161- )
162- model (** inputs )
163- self .assertEqual (
164- "#1[T1r3]" ,
165- self .string_type (torch .utils ._pytree .tree_flatten (inputs ["encoder_outputs" ])[0 ]),
166- )
167- with bypass_export_some_errors (patch_transformers = True , verbose = 10 ):
168- flat = torch .utils ._pytree .tree_flatten (inputs ["past_key_values" ])[0 ]
169- self .assertIsInstance (flat , list )
170- self .assertIsInstance (flat [0 ], torch .Tensor )
171- self .assertEqual (
172- "#8[T1r4,T1r4,T1r4,T1r4,T1r4,T1r4,T1r4,T1r4]" ,
173- self .string_type (flat ),
174- )
175- torch .export .export (model , (), kwargs = inputs , dynamic_shapes = ds , strict = False )
176- with bypass_export_some_errors (patch_transformers = True , verbose = 10 ):
177- flat = torch .utils ._pytree .tree_flatten (inputs ["past_key_values" ])[0 ]
178- self .assertIsInstance (flat , list )
179- self .assertIsInstance (flat [0 ], torch .Tensor )
180- self .assertEqual (
181- "#8[T1r4,T1r4,T1r4,T1r4,T1r4,T1r4,T1r4,T1r4]" ,
182- self .string_type (flat ),
183- )
184- torch .export .export (model , (), kwargs = inputs , dynamic_shapes = ds , strict = False )
185-
186- @hide_stdout ()
187- def test_get_untrained_model_with_inputs_imagetext2text_generation (self ):
188- mid = "HuggingFaceM4/tiny-random-idefics"
189- # mid = "Salesforce/codet5-small"
190- data = get_untrained_model_with_inputs (mid , verbose = 1 )
191- self .assertIn ((data ["size" ], data ["n_weights" ]), [(12742888 , 3185722 )])
192- model , inputs = data ["model" ], data ["inputs" ]
193- model (** inputs )
194-
19599 @hide_stdout ()
196100 @requires_torch ("2.7" , "reduce test time" )
197101 @requires_transformers ("4.50" , "reduce test time" )
@@ -210,11 +114,9 @@ def _diff(c1, c2):
210114 for mid in load_models_testing ():
211115 with self .subTest (mid = mid ):
212116 if mid in {
213- "hf-internal-testing/tiny-random-BeitForImageClassification" ,
214117 "hf-internal-testing/tiny-random-MaskFormerForInstanceSegmentation" ,
215118 "hf-internal-testing/tiny-random-MoonshineForConditionalGeneration" ,
216119 "fxmarty/pix2struct-tiny-random" ,
217- "hf-internal-testing/tiny-random-ViTMSNForImageClassification" ,
218120 "hf-internal-testing/tiny-random-YolosModel" ,
219121 }:
220122 print (f"-- not implemented yet for { mid !r} " )
0 commit comments