55
66from typing import Tuple
77
8+ import pytest
89import timm # type: ignore[import-untyped]
910
1011import torch
2526)
2627from torchvision import transforms # type: ignore[import-untyped]
2728
28- deit_tiny = timm .models .deit .deit_tiny_patch16_224 (pretrained = True )
29-
30- deit_tiny .eval ()
31-
3229normalize = transforms .Normalize (
3330 mean = IMAGENET_INCEPTION_MEAN , std = IMAGENET_INCEPTION_STD
3431)
3532model_inputs = (normalize (torch .rand ((1 , 3 , 224 , 224 ))),)
33+ image_fp16 = torch .randn ((1 , 3 , 224 , 224 ), generator = torch .Generator ().manual_seed (0 ))
34+ model_inputs_fp16 = (normalize (image_fp16 ).to (torch .float16 ),)
3635
3736input_t = Tuple [torch .Tensor ]
3837
3938
40- def test_deit_tiny_tosa_FP ():
41- pipeline = TosaPipelineFP [input_t ](
42- deit_tiny ,
39+ @pytest .fixture (scope = "module" )
40+ def deit_tiny ():
41+ return timm .models .deit .deit_tiny_patch16_224 (pretrained = True ).eval ()
42+
43+
44+ @pytest .fixture (scope = "module" )
45+ def get_deit_model (deit_tiny ):
46+ models = {torch .float32 : deit_tiny }
47+
48+ def get_model (dtype ):
49+ if dtype not in models :
50+ model = timm .models .deit .deit_tiny_patch16_224 (pretrained = False )
51+ model .load_state_dict (deit_tiny .state_dict ())
52+ models [dtype ] = model .to (dtype ).eval ()
53+ return models [dtype ]
54+
55+ return get_model
56+
57+
58+ fp_test_data = [
59+ pytest .param (
60+ torch .float32 ,
4361 model_inputs ,
62+ {"atol" : 1e-3 , "rtol" : 1e-3 },
63+ id = "fp32" ,
64+ ),
65+ pytest .param (
66+ torch .float16 ,
67+ model_inputs_fp16 ,
68+ {"atol" : 8e-2 , "rtol" : 8e-2 },
69+ marks = pytest .mark .slow ,
70+ id = "fp16" ,
71+ ),
72+ ]
73+
74+
75+ @pytest .mark .parametrize ("dtype,inputs,pipeline_kwargs" , fp_test_data )
76+ def test_deit_tiny_tosa_FP (dtype , inputs , pipeline_kwargs , get_deit_model ):
77+ pipeline = TosaPipelineFP [input_t ](
78+ get_deit_model (dtype ),
79+ inputs ,
4480 aten_op = [],
4581 exir_op = [],
4682 use_to_edge_transform_and_lower = True ,
83+ ** pipeline_kwargs ,
4784 )
4885 pipeline .run ()
4986
5087
51- def test_deit_tiny_tosa_INT ():
88+ def test_deit_tiny_tosa_INT (deit_tiny ):
5289 pipeline = TosaPipelineINT [input_t ](
5390 deit_tiny ,
5491 model_inputs ,
@@ -63,7 +100,7 @@ def test_deit_tiny_tosa_INT():
63100 pipeline .run ()
64101
65102
66- def test_deit_tiny_u55_INT ():
103+ def test_deit_tiny_u55_INT (deit_tiny ):
67104 pipeline = EthosU55PipelineINT [input_t ](
68105 deit_tiny ,
69106 model_inputs ,
@@ -81,7 +118,7 @@ def test_deit_tiny_u55_INT():
81118
82119
83120@common .XfailIfNoCorstone320
84- def test_deit_tiny_u85_INT ():
121+ def test_deit_tiny_u85_INT (deit_tiny ):
85122 pipeline = EthosU85PipelineINT [input_t ](
86123 deit_tiny ,
87124 model_inputs ,
@@ -95,7 +132,7 @@ def test_deit_tiny_u85_INT():
95132
96133
97134@common .SkipIfNoModelConverter
98- def test_deit_tiny_vgf_quant ():
135+ def test_deit_tiny_vgf_quant (deit_tiny ):
99136 pipeline = VgfPipeline [input_t ](
100137 deit_tiny ,
101138 model_inputs ,
@@ -110,13 +147,15 @@ def test_deit_tiny_vgf_quant():
110147
111148
112149@common .SkipIfNoModelConverter
113- def test_deit_tiny_vgf_no_quant ():
150+ @pytest .mark .parametrize ("dtype,inputs,pipeline_kwargs" , fp_test_data )
151+ def test_deit_tiny_vgf_no_quant (dtype , inputs , pipeline_kwargs , get_deit_model ):
114152 pipeline = VgfPipeline [input_t ](
115- deit_tiny ,
116- model_inputs ,
153+ get_deit_model ( dtype ) ,
154+ inputs ,
117155 aten_op = [],
118156 exir_op = [],
119157 use_to_edge_transform_and_lower = True ,
120158 quantize = False ,
159+ ** pipeline_kwargs ,
121160 )
122161 pipeline .run ()
0 commit comments