4
4
# except in compliance with the License. See the license file in the root
5
5
# directory of this source tree for more details.
6
6
7
- import nncf . experimental
8
- import nncf . experimental . torch
7
+ import argparse
8
+
9
9
import executorch
10
- import nncf
11
10
import timm
12
11
import torch
13
12
import torchvision .datasets as datasets
14
13
import torchvision .models as torchvision_models
15
- import torchvision .transforms as transforms
16
- from transformers import AutoModel
17
- from executorch .exir .backend .backend_details import CompileSpec
18
- from executorch .backends .openvino .preprocess import OpenvinoBackend
19
- from executorch .backends .openvino .partitioner import OpenvinoPartitioner
20
- from executorch .exir import EdgeProgramManager , to_edge
21
- from torch .export import export , ExportedProgram
22
- from torch .export .exported_program import ExportedProgram
23
- import argparse
24
14
from executorch .backends .openvino import OpenVINOQuantizer
25
- #from nncf.experimental.torch.fx.quantization.quantizer.openvino_quantizer import OpenVINOQuantizer
26
- from nncf .experimental .torch .fx .quantization .quantize_pt2e import quantize_pt2e
27
- from torch .ao .quantization .quantize_pt2e import (
28
- convert_pt2e ,
29
- prepare_pt2e ,
30
- )
15
+ from executorch .backends .openvino .partitioner import OpenvinoPartitioner
16
+ from executorch .exir import EdgeProgramManager
17
+ from executorch .exir import to_edge
18
+ from executorch .exir .backend .backend_details import CompileSpec
31
19
from sklearn .metrics import accuracy_score
32
20
from timm .data import resolve_data_config
33
21
from timm .data .transforms_factory import create_transform
22
+ from torch .export import ExportedProgram
23
+ from torch .export import export
24
+ from torch .export .exported_program import ExportedProgram
25
+ from transformers import AutoModel
26
+
27
+ import nncf
28
+ import nncf .experimental
29
+ import nncf .experimental .torch
30
+ from nncf .experimental .torch .fx .quantization .quantize_pt2e import quantize_pt2e
31
+
34
32
35
33
# Function to load a model based on the selected suite
36
34
def load_model (suite : str , model_name : str ):
@@ -54,10 +52,7 @@ def load_calibration_dataset(dataset_path: str, suite: str, model: torch.nn.Modu
54
52
else :
55
53
transform = create_transform (** resolve_data_config (model .pretrained_cfg , model = model ))
56
54
57
- val_dataset = datasets .ImageFolder (
58
- val_dir ,
59
- transform = transform
60
- )
55
+ val_dataset = datasets .ImageFolder (val_dir , transform = transform )
61
56
62
57
calibration_dataset = torch .utils .data .DataLoader (
63
58
val_dataset , batch_size = 1 , shuffle = False , num_workers = 0 , pin_memory = True
@@ -78,7 +73,7 @@ def main(suite: str, model_name: str, input_shape, quantize: bool, dataset_path:
78
73
model = model .eval ()
79
74
80
75
# Provide input
81
- example_args = (torch .randn (* input_shape ), )
76
+ example_args = (torch .randn (* input_shape ),)
82
77
83
78
# Export the model to the aten dialect
84
79
aten_dialect : ExportedProgram = export (model , example_args )
@@ -93,14 +88,19 @@ def main(suite: str, model_name: str, input_shape, quantize: bool, dataset_path:
93
88
calibration_dataset = load_calibration_dataset (dataset_path , suite , model )
94
89
95
90
captured_model = aten_dialect .module ()
96
- #visualize_fx_model(captured_model, f"{model_name}_fp32.svg")
97
91
quantizer = OpenVINOQuantizer ()
98
92
99
93
print ("PTQ: Quantize the model" )
94
+
100
95
def transform (x ):
101
96
return x [0 ]
102
97
103
- quantized_model = quantize_pt2e (captured_model , quantizer , calibration_dataset = nncf .Dataset (calibration_dataset , transform_func = transform ), fold_quantize = False )
98
+ quantized_model = quantize_pt2e (
99
+ captured_model ,
100
+ quantizer ,
101
+ calibration_dataset = nncf .Dataset (calibration_dataset , transform_func = transform ),
102
+ fold_quantize = False ,
103
+ )
104
104
105
105
aten_dialect : ExportedProgram = export (quantized_model , example_args )
106
106
@@ -154,69 +154,53 @@ def transform(x):
154
154
# 2: Run the executor
155
155
print ("Run openvino_executor_runner..." )
156
156
import subprocess
157
- breakpoint ()
158
- subprocess .run (["../../../cmake-openvino-out/examples/openvino/openvino_executor_runner" ,
159
- f"--model_path={ model_name } " ,
160
- f"--input_list_path={ inp_list_file } " ,
161
- f"--output_folder_path={ out_path } " ,
162
- #f"--num_iter={len(input_files)}"
163
- ])
157
+
158
+ subprocess .run (
159
+ [
160
+ "../../../cmake-openvino-out/examples/openvino/openvino_executor_runner" ,
161
+ f"--model_path={ model_name } " ,
162
+ f"--input_list_path={ inp_list_file } " ,
163
+ f"--output_folder_path={ out_path } " ,
164
+ # f"--num_iter={len(input_files)}"
165
+ ]
166
+ )
164
167
165
168
# 3: load the outputs and compare with the targets
166
169
import numpy as np
170
+
167
171
predictions = []
168
172
for i in range (len (input_files )):
169
- predictions .append (
170
- np .fromfile (
171
- os .path .join (out_path , f"output_{ i } .raw" ), dtype = np .float32
172
- )
173
- )
173
+ predictions .append (np .fromfile (os .path .join (out_path , f"output_{ i } .raw" ), dtype = np .float32 ))
174
174
175
- k_val = [1 , 5 ]
176
175
acc_top1 = accuracy_score (predictions , targets )
177
176
print (f"acc@1: { acc_top1 } " )
178
177
179
178
180
- from torch .fx .passes .graph_drawer import FxGraphDrawer
181
- def visualize_fx_model (model : torch .fx .GraphModule , output_svg_path : str ):
182
- g = FxGraphDrawer (model , output_svg_path )
183
- g .get_dot_graph ().write_svg (output_svg_path )
184
-
185
- def generate_inputs (dest_path : str , file_name : str , inputs = None , input_list = None ):
186
- input_list_file = None
187
- input_files = []
188
-
189
- # Prepare input list
190
- if input_list is not None :
191
- input_list_file = f"{ dest_path } /{ file_name } "
192
- with open (input_list_file , "w" ) as f :
193
- f .write (input_list )
194
- f .flush ()
195
-
196
- # Prepare input data
197
- if inputs is not None :
198
- for idx , data in enumerate (inputs ):
199
- for i , d in enumerate (data ):
200
- file_name = f"{ dest_path } /input_{ idx } _{ i } .raw"
201
- if not isinstance (d , torch .Tensor ):
202
- d = torch .tensor (d )
203
- d .detach ().numpy ().tofile (file_name )
204
- input_files .append (file_name )
205
-
206
- return input_list_file , input_files
207
-
208
179
if __name__ == "__main__" :
209
180
# Argument parser for dynamic inputs
210
181
parser = argparse .ArgumentParser (description = "Export models with executorch." )
211
- parser .add_argument ("--suite" , type = str , required = True , choices = ["timm" , "torchvision" , "huggingface" ],
212
- help = "Select the model suite (timm, torchvision, huggingface)." )
182
+ parser .add_argument (
183
+ "--suite" ,
184
+ type = str ,
185
+ required = True ,
186
+ choices = ["timm" , "torchvision" , "huggingface" ],
187
+ help = "Select the model suite (timm, torchvision, huggingface)." ,
188
+ )
213
189
parser .add_argument ("--model" , type = str , required = True , help = "Model name to be loaded." )
214
- parser .add_argument ("--input_shape" , type = eval , required = True ,
215
- help = "Input shape for the model as a list or tuple (e.g., [1, 3, 224, 224] or (1, 3, 224, 224))." )
190
+ parser .add_argument (
191
+ "--input_shape" ,
192
+ type = eval ,
193
+ required = True ,
194
+ help = "Input shape for the model as a list or tuple (e.g., [1, 3, 224, 224] or (1, 3, 224, 224))." ,
195
+ )
216
196
parser .add_argument ("--quantize" , action = "store_true" , help = "Enable model quantization." )
217
197
parser .add_argument ("--dataset" , type = str , help = "Path to the calibration dataset." )
218
- parser .add_argument ("--device" , type = str , default = "CPU" ,
219
- help = "Target device for compiling the model (e.g., CPU, GPU). Default is CPU." )
198
+ parser .add_argument (
199
+ "--device" ,
200
+ type = str ,
201
+ default = "CPU" ,
202
+ help = "Target device for compiling the model (e.g., CPU, GPU). Default is CPU." ,
203
+ )
220
204
221
205
args = parser .parse_args ()
222
206
0 commit comments