55# This source code is licensed under the BSD-style license found in the
66# LICENSE file in the root directory of this source tree.
77
8- from typing import List
9-
10- import torch
11- from executorch .backends .xnnpack .partition .xnnpack_partitioner import XnnpackPartitioner
12- from executorch .examples .models import Backend , Model , MODEL_NAME_TO_MODEL
13- from executorch .examples .models .model_factory import EagerModelFactory
14- from executorch .examples .xnnpack import MODEL_NAME_TO_OPTIONS
15- from executorch .examples .xnnpack .quantization .utils import quantize as quantize_xnn
16- from executorch .exir import EdgeCompileConfig , to_edge_transform_and_lower
17- from executorch .extension .pybindings .portable_lib import (
18- _load_for_executorch_from_buffer ,
19- )
20- from test_base import ModelTest
21-
22-
23- def test_model_xnnpack (model : Model , quantize : bool ) -> None :
24- model_instance , example_inputs , _ , _ = EagerModelFactory .create_model (
25- * MODEL_NAME_TO_MODEL [str (model )]
8+ try :
9+ from typing import List
10+
11+ print ("Dumping env: " , file = sys .stderr )
12+ import os
13+ for name , value in os .environ .items ():
14+ print (f" { name } : { value } " )
15+
16+ import sys
17+ print ("Trying to import ET..." , file = sys .stderr )
18+ import executorch
19+ print (f"ET path: { executorch .__path__ } " , file = sys .stderr )
20+
21+ import torch
22+ from executorch .backends .xnnpack .partition .xnnpack_partitioner import XnnpackPartitioner
23+ from executorch .examples .models import Backend , Model , MODEL_NAME_TO_MODEL
24+ from executorch .examples .models .model_factory import EagerModelFactory
25+ from executorch .examples .xnnpack import MODEL_NAME_TO_OPTIONS
26+ from executorch .examples .xnnpack .quantization .utils import quantize as quantize_xnn
27+ from executorch .exir import EdgeCompileConfig , to_edge_transform_and_lower
28+ from executorch .extension .pybindings .portable_lib import (
29+ _load_for_executorch_from_buffer ,
2630 )
31+ from test_base import ModelTest
2732
28- model_instance .eval ()
29- ref_outputs = model_instance (* example_inputs )
3033
31- if quantize :
32- quant_type = MODEL_NAME_TO_OPTIONS [str (model )].quantization
33- model_instance = torch .export .export_for_training (
34- model_instance , example_inputs
35- )
36- model_instance = quantize_xnn (
37- model_instance .module (), example_inputs , quant_type
34+ def test_model_xnnpack (model : Model , quantize : bool ) -> None :
35+ model_instance , example_inputs , _ , _ = EagerModelFactory .create_model (
36+ * MODEL_NAME_TO_MODEL [str (model )]
3837 )
3938
40- lowered = to_edge_transform_and_lower (
41- torch .export .export (model_instance , example_inputs ),
42- partitioner = [XnnpackPartitioner ()],
43- compile_config = EdgeCompileConfig (
44- _check_ir_validity = False ,
45- ),
46- ).to_executorch ()
39+ model_instance .eval ()
40+ ref_outputs = model_instance (* example_inputs )
41+
42+ if quantize :
43+ quant_type = MODEL_NAME_TO_OPTIONS [str (model )].quantization
44+ model_instance = torch .export .export_for_training (
45+ model_instance , example_inputs
46+ )
47+ model_instance = quantize_xnn (
48+ model_instance .module (), example_inputs , quant_type
49+ )
4750
48- loaded_model = _load_for_executorch_from_buffer (lowered .buffer )
49- et_outputs = loaded_model ([* example_inputs ])
51+ lowered = to_edge_transform_and_lower (
52+ torch .export .export (model_instance , example_inputs ),
53+ partitioner = [XnnpackPartitioner ()],
54+ compile_config = EdgeCompileConfig (
55+ _check_ir_validity = False ,
56+ ),
57+ ).to_executorch ()
5058
51- if isinstance ( ref_outputs , torch . Tensor ):
52- ref_outputs = ( ref_outputs , )
59+ loaded_model = _load_for_executorch_from_buffer ( lowered . buffer )
60+ et_outputs = loaded_model ([ * example_inputs ] )
5361
54- assert len (ref_outputs ) == len (et_outputs )
55- for i in range (len (ref_outputs )):
56- assert torch .allclose (ref_outputs [i ], et_outputs [i ], atol = 1e-5 )
62+ if isinstance (ref_outputs , torch .Tensor ):
63+ ref_outputs = (ref_outputs ,)
5764
65+ assert len (ref_outputs ) == len (et_outputs )
66+ for i in range (len (ref_outputs )):
67+ assert torch .allclose (ref_outputs [i ], et_outputs [i ], atol = 1e-5 )
5868
59- def run_tests (model_tests : List [ModelTest ]) -> None :
60- for model_test in model_tests :
61- if model_test .backend == Backend .Xnnpack :
62- test_model_xnnpack (model_test .model , quantize = False )
63- else :
64- raise RuntimeError (f"Unsupported backend { model_test .backend } ." )
6569
70+ def run_tests (model_tests : List [ModelTest ]) -> None :
71+ for model_test in model_tests :
72+ if model_test .backend == Backend .Xnnpack :
73+ test_model_xnnpack (model_test .model , quantize = False )
74+ else :
75+ raise RuntimeError (f"Unsupported backend { model_test .backend } ." )
6676
67- if __name__ == "__main__" :
68- run_tests (
69- model_tests = [
70- ModelTest (
71- model = Model .Mv3 ,
72- backend = Backend .Xnnpack ,
73- ),
74- ]
75- )
77+
78+ if __name__ == "__main__" :
79+ run_tests (
80+ model_tests = [
81+ ModelTest (
82+ model = Model .Mv3 ,
83+ backend = Backend .Xnnpack ,
84+ ),
85+ ]
86+ )
87+ except :
88+ pass
0 commit comments