Skip to content

Commit 29bc381

Browse files
Formating
1 parent f39a51b commit 29bc381

File tree

3 files changed

+63
-82
lines changed

3 files changed

+63
-82
lines changed

backends/openvino/quantizer/quantizer.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
# Copyright (c) 2025 Intel Corporation
2-
# Licensed under the Apache License, Version 2.0 (the "License");
3-
# you may not use this file except in compliance with the License.
4-
# You may obtain a copy of the License at
5-
# http://www.apache.org/licenses/LICENSE-2.0
6-
# Unless required by applicable law or agreed to in writing, software
7-
# distributed under the License is distributed on an "AS IS" BASIS,
8-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9-
# See the License for the specific language governing permissions and
10-
# limitations under the License.
1+
# Copyright (c) Intel Corporation
2+
#
3+
# Licensed under the BSD License (the "License"); you may not use this file
4+
# except in compliance with the License. See the license file in the root
5+
# directory of this source tree for more details.
116

127
from collections import defaultdict
138
from typing import Dict, List, Optional, Tuple

examples/openvino/aot/aot_openvino_compiler.py

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@
44
# except in compliance with the License. See the license file in the root
55
# directory of this source tree for more details.
66

7-
import nncf.experimental
8-
import nncf.experimental.torch
7+
import argparse
8+
99
import executorch
10-
import nncf
1110
import timm
1211
import torch
1312
import torchvision.datasets as datasets
1413
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
2414
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
3119
from sklearn.metrics import accuracy_score
3220
from timm.data import resolve_data_config
3321
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+
3432

3533
# Function to load a model based on the selected suite
3634
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
5452
else:
5553
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
5654

57-
val_dataset = datasets.ImageFolder(
58-
val_dir,
59-
transform=transform
60-
)
55+
val_dataset = datasets.ImageFolder(val_dir, transform=transform)
6156

6257
calibration_dataset = torch.utils.data.DataLoader(
6358
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:
7873
model = model.eval()
7974

8075
# Provide input
81-
example_args = (torch.randn(*input_shape), )
76+
example_args = (torch.randn(*input_shape),)
8277

8378
# Export the model to the aten dialect
8479
aten_dialect: ExportedProgram = export(model, example_args)
@@ -93,14 +88,19 @@ def main(suite: str, model_name: str, input_shape, quantize: bool, dataset_path:
9388
calibration_dataset = load_calibration_dataset(dataset_path, suite, model)
9489

9590
captured_model = aten_dialect.module()
96-
#visualize_fx_model(captured_model, f"{model_name}_fp32.svg")
9791
quantizer = OpenVINOQuantizer()
9892

9993
print("PTQ: Quantize the model")
94+
10095
def transform(x):
10196
return x[0]
10297

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+
)
104104

105105
aten_dialect: ExportedProgram = export(quantized_model, example_args)
106106

@@ -154,69 +154,53 @@ def transform(x):
154154
# 2: Run the executor
155155
print("Run openvino_executor_runner...")
156156
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+
)
164167

165168
# 3: load the outputs and compare with the targets
166169
import numpy as np
170+
167171
predictions = []
168172
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))
174174

175-
k_val = [1, 5]
176175
acc_top1 = accuracy_score(predictions, targets)
177176
print(f"acc@1: {acc_top1}")
178177

179178

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-
208179
if __name__ == "__main__":
209180
# Argument parser for dynamic inputs
210181
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+
)
213189
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+
)
216196
parser.add_argument("--quantize", action="store_true", help="Enable model quantization.")
217197
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+
)
220204

221205
args = parser.parse_args()
222206

examples/openvino/executor_runner/openvino_executor_runner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ int main(int argc, char** argv) {
180180
while (std::getline(input_list, file_path)) {
181181
auto input_files = split(file_path, " ");
182182
ET_LOG(Info, "INPUT_FILES.SIZE: %ld", input_files.size());
183+
ET_LOG(Info, "NUM_INPUTS: %ld", num_inputs);
183184
if (input_files.size() == 0) {
184185
break;
185186
}
@@ -189,6 +190,7 @@ int main(int argc, char** argv) {
189190
method_meta.input_tensor_meta(input_index);
190191
auto input_data_ptr = inputs[input_index].toTensor().data_ptr<char>();
191192

193+
ET_LOG(Info, "READ FILE %s", std::string(input_files[input_index]));
192194
std::ifstream fin(input_files[input_index], std::ios::binary);
193195
fin.seekg(0, fin.end);
194196
size_t file_size = fin.tellg();

0 commit comments

Comments
 (0)