Skip to content

Commit 8cbb117

Browse files
Validate option / minor
1 parent e2415af commit 8cbb117

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

examples/openvino/aot/aot_openvino_compiler.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def load_model(suite: str, model_name: str):
5151
raise ValueError(msg)
5252

5353

54-
def load_calibration_dataset(dataset_path: str, suite: str, model: torch.nn.Module):
54+
def load_calibration_dataset(dataset_path: str, suite: str, model: torch.nn.Module, model_name: str):
5555
val_dir = f"{dataset_path}/val"
5656

5757
if suite == "torchvision":
58-
transform = torchvision_models.get_model_weights(model.name).transforms()
58+
transform = torchvision_models.get_model_weights(model_name).DEFAULT.transforms()
5959
else:
6060
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
6161

@@ -87,14 +87,16 @@ def dump_inputs(calibration_dataset, dest_path):
8787
return input_files, targets
8888

8989

90-
def main(suite: str, model_name: str, input_shape, quantize: bool, dataset_path: str, device: str):
90+
def main(suite: str, model_name: str, input_shape, quantize: bool, validate: bool, dataset_path: str, device: str):
9191
# Ensure input_shape is a tuple
9292
if isinstance(input_shape, list):
9393
input_shape = tuple(input_shape)
9494
elif not isinstance(input_shape, tuple):
9595
msg = "Input shape must be a list or tuple."
9696
raise ValueError(msg)
9797

98+
calibration_dataset = None
99+
98100
# Load the selected model
99101
model = load_model(suite, model_name)
100102
model = model.eval()
@@ -114,7 +116,7 @@ def main(suite: str, model_name: str, input_shape, quantize: bool, dataset_path:
114116
if not dataset_path:
115117
msg = "Quantization requires a calibration dataset."
116118
raise ValueError(msg)
117-
calibration_dataset = load_calibration_dataset(dataset_path, suite, model)
119+
calibration_dataset = load_calibration_dataset(dataset_path, suite, model, model_name)
118120

119121
captured_model = aten_dialect.module()
120122
quantizer = OpenVINOQuantizer()
@@ -146,12 +148,15 @@ def transform(x):
146148
exec_prog = lowered_module.to_executorch(config=executorch.exir.ExecutorchBackendConfig())
147149

148150
# Serialize and save it to a file
149-
model_name = f"{model_name}_{'int8' if quantize else 'fp32'}.pte"
150-
with open(model_name, "wb") as file:
151+
model_file_name = f"{model_name}_{'int8' if quantize else 'fp32'}.pte"
152+
with open(model_file_name, "wb") as file:
151153
exec_prog.write_to_file(file)
152-
print(f"Model exported and saved as {model_name} on {device}.")
154+
print(f"Model exported and saved as {model_file_name} on {device}.")
155+
156+
if validate:
157+
if calibration_dataset is None:
158+
calibration_dataset = load_calibration_dataset(dataset_path, suite, model, model_name)
153159

154-
if quantize:
155160
print("Start validation of the quantized model:")
156161
# 1: Dump inputs
157162
dest_path = Path("tmp_inputs")
@@ -172,18 +177,17 @@ def transform(x):
172177
subprocess.run(
173178
[
174179
"../../../cmake-openvino-out/examples/openvino/openvino_executor_runner",
175-
f"--model_path={model_name}",
180+
f"--model_path={model_file_name}",
176181
f"--input_list_path={inp_list_file}",
177182
f"--output_folder_path={out_path}",
178183
]
179184
)
180185

181186
# 3: load the outputs and compare with the targets
182-
183187
predictions = []
184188
for i in range(len(input_files)):
185189
tensor = np.fromfile(out_path / f"output_{i}_0.raw", dtype=np.float32)
186-
predictions.append(torch.tensor(np.argmax(tensor)))
190+
predictions.append(torch.argmax(torch.tensor(tensor)))
187191

188192
acc_top1 = accuracy_score(predictions, targets)
189193
print(f"acc@1: {acc_top1}")
@@ -207,6 +211,11 @@ def transform(x):
207211
help="Input shape for the model as a list or tuple (e.g., [1, 3, 224, 224] or (1, 3, 224, 224)).",
208212
)
209213
parser.add_argument("--quantize", action="store_true", help="Enable model quantization.")
214+
parser.add_argument(
215+
"--validate",
216+
action="store_true",
217+
help="Enable model validation. --dataset argument is requred for the validation.",
218+
)
210219
parser.add_argument("--dataset", type=str, help="Path to the calibration dataset.")
211220
parser.add_argument(
212221
"--device",
@@ -219,4 +228,4 @@ def transform(x):
219228

220229
# Run the main function with parsed arguments
221230
with nncf.torch.disable_patching():
222-
main(args.suite, args.model, args.input_shape, args.quantize, args.dataset, args.device)
231+
main(args.suite, args.model, args.input_shape, args.quantize, args.validate, args.dataset, args.device)

examples/openvino/executor_runner/openvino_executor_runner.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ void dump_outputs(Result<Method> &method, const char *output_folder_path,
8282
std::ofstream fout(output_file_name.c_str(), std::ios::binary);
8383
fout.write(output_tensor.const_data_ptr<char>(), output_tensor.nbytes());
8484
fout.close();
85-
ET_LOG(Info, "Write outputs to file %s", output_file_name.c_str());
8685
}
8786
}
8887

@@ -135,8 +134,6 @@ ProcessInputsResult process_inputs(Result<Method> &method,
135134
method_meta.input_tensor_meta(input_index);
136135
auto input_data_ptr = inputs[input_index].toTensor().data_ptr<char>();
137136

138-
ET_LOG(Info, "Read inputs from file %s",
139-
input_files[input_index].c_str());
140137
std::ifstream fin(input_files[input_index], std::ios::binary);
141138
fin.seekg(0, fin.end);
142139
size_t file_size = fin.tellg();

0 commit comments

Comments
 (0)