Skip to content

Commit ce7f28a

Browse files
authored
Merge pull request #3553 from eaidova/ea/inf_precision_hint
AC: support inference precision hint setting
2 parents 9f8174e + 90c5724 commit ce7f28a

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

tools/accuracy_checker/openvino/tools/accuracy_checker/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
limitations under the License.
1515
"""
1616

17-
__version__ = "0.9.3"
17+
__version__ = "0.9.4"

tools/accuracy_checker/openvino/tools/accuracy_checker/argparser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ def add_openvino_specific_args(parser):
302302
'static - convert undefined shapes to static before execution',
303303
required=False, default='default'
304304
)
305+
openvino_specific_args.add_argument(
306+
'--inference_precision_hint',
307+
help='Inference Precision hint for device',
308+
required=False
309+
)
305310
openvino_specific_args.add_argument(
306311
'--use_new_api', type=cast_to_bool, help='switch to processing using OpenVINO 2.0 API', required=False,
307312
default=ov_new_api_available()

tools/accuracy_checker/openvino/tools/accuracy_checker/config/config_reader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,12 @@ def _async_evaluation_args(launcher_entry):
840840

841841
kaldi_binaries = arguments.kaldi_bin_dir if 'kaldi_bin_dir' in arguments else None
842842
kaldi_logs = arguments.kaldi_log_file if 'kaldi_log_file' in arguments else None
843+
precision_hint = arguments.inference_precision_hint if 'inference_precision_hint' in arguments else None
843844
if kaldi_binaries:
844845
launcher_entry['_kaldi_bin_dir'] = kaldi_binaries
845846
launcher_entry['_kaldi_log_file'] = kaldi_logs
846-
847+
if precision_hint:
848+
launcher_entry['_inference_precision_hint'] = precision_hint
847849
if launcher_entry['framework'].lower() not in ['dlsdk', 'openvino']:
848850
return launcher_entry
849851

@@ -936,6 +938,7 @@ def provide_model_type(launcher, arguments):
936938
def provide_models(launchers, arguments):
937939
input_precisions = arguments.input_precision if 'input_precision' in arguments else None
938940
input_layout = arguments.layout if 'layout' in arguments else None
941+
939942
provide_precision_and_layout(launchers, input_precisions, input_layout)
940943
if 'models' not in arguments or not arguments.models:
941944
return launchers

tools/accuracy_checker/openvino/tools/accuracy_checker/launcher/dlsdk_launcher_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ def check_model_source(entry, fetch_only=False, field_uri=None, validation_schem
198198
'_model_type': StringField(
199199
choices=['xml', 'blob', 'onnx', 'paddle', 'tf'],
200200
description='hint for model type in automatic model search', optional=True),
201+
'_inference_precision_hint': StringField(
202+
description='Model execution precision for device',
203+
optional=True
204+
)
201205
}
202206

203207

tools/accuracy_checker/openvino/tools/accuracy_checker/launcher/openvino_launcher.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import multiprocessing
2020
from pathlib import Path
2121
import re
22-
import warnings
2322
import numpy as np
2423
from openvino.runtime import Core, AsyncInferQueue, get_version, PartialShape, Type, Dimension
2524
from openvino.preprocess import PrePostProcessor
@@ -32,7 +31,6 @@
3231
ov_set_config
3332
)
3433
from .dlsdk_async_request import AsyncInferRequestWrapper
35-
3634
from ..config import ConfigError
3735
from ..logging import warning, debug, print_info
3836
from ..utils import (
@@ -49,13 +47,12 @@
4947
format_map = {
5048
'f32': np.float32, 'i32': np.int32, 'i64': np.int64,
5149
'fp16': np.float16, 'f16': np.float16, 'i16': np.int16, 'u16': np.uint16,
52-
'i8': np.int8, 'u8': np.uint8,
53-
'boolean': np.uint8
50+
'i8': np.int8, 'u8': np.uint8, 'boolean': np.uint8
5451
}
5552

5653
PRECISION_STR_TO_TYPE = {
5754
'FP32': Type.f32, 'FP16': Type.f16, 'U8': Type.u8, 'U16': Type.u16, 'I8': Type.i8, 'I16': Type.i16,
58-
'I32': Type.i32, 'I64': Type.i64, 'BOOL': Type.boolean
55+
'I32': Type.i32, 'I64': Type.i64, 'BOOL': Type.boolean, 'INT8': Type.u8, 'BF16': Type.bf16
5956
}
6057

6158

@@ -177,19 +174,14 @@ def predict(self, inputs, metadata=None, return_raw=False, **kwargs):
177174
raw_results = []
178175
for infer_inputs in inputs:
179176
if self._do_reshape:
180-
input_shapes = {
181-
layer_name: data.shape for layer_name, data in infer_inputs.items()
182-
}
177+
input_shapes = {layer_name: data.shape for layer_name, data in infer_inputs.items()}
183178
self._reshape_input(input_shapes)
184179
if self.infer_request is None:
185180
self.infer_request = self.exec_network.create_infer_request()
186181
feed_dict = {self.input_to_tensor_name[layer_name]: data for layer_name, data in infer_inputs.items()}
187182
outputs = self.infer_request.infer(inputs=feed_dict)
188183
raw_results.append(outputs)
189-
results.append({
190-
out_node.get_node().friendly_name: out_res
191-
for out_node, out_res in outputs.items()
192-
})
184+
results.append({out_node.get_node().friendly_name: out_res for out_node, out_res in outputs.items()})
193185
if self.reset_memory_state:
194186
for state in self.infer_request.query_state():
195187
state.reset()
@@ -211,10 +203,7 @@ def _predict_sequential(self, inputs, metadata=None, return_raw=False, **kwargs)
211203
feed_dict.update(lstm_inputs_feed)
212204
infer_inputs = {self.input_to_tensor_name[layer_name]: data for layer_name, data in feed_dict.items()}
213205
out_tensors = self.infer_request.infer(infer_inputs)
214-
output_result = {
215-
out_node.get_node().friendly_name: out_tensor
216-
for out_node, out_tensor in out_tensors.items()
217-
}
206+
output_result = {out_node.get_node().friendly_name: out_tensor for out_node, out_tensor in out_tensors.items()}
218207
lstm_inputs_feed = self._fill_lstm_inputs(output_result)
219208
results.append(output_result)
220209
if return_raw:
@@ -395,6 +384,7 @@ def _device_specific_configuration(self):
395384
device_config = self.config.get('device_config')
396385
if device_config:
397386
self._set_device_config(device_config)
387+
self._set_infer_precision_hint()
398388

399389
def _set_nireq(self):
400390
num_requests = self.config.get('num_requests')
@@ -475,14 +465,29 @@ def _set_device_config(self, device_config):
475465
if isinstance(value, dict):
476466
if key in self._devices_list():
477467
if key not in self.ie_core.available_devices:
478-
warnings.warn('{} device is unknown. Config loading may lead to error.'.format(key))
468+
warning('{} device is unknown. Config loading may lead to error.'.format(key))
479469
ov_set_config(self.ie_core, dict(value), device=key)
480470
else:
481-
warnings.warn(
471+
warning(
482472
f'Configuration for {key} will be skipped as device is not listed in evaluation device')
483473
else:
484-
warnings.warn('Option {key}: {value} will be skipped because device to which it should be '
485-
'applied is not specified or option is not a dict-like'.format(key=key, value=value))
474+
warning(f'Option {key}: {value} will be skipped because device to which it should be '
475+
f'applied is not specified or option is not a dict-like')
476+
477+
def _set_infer_precision_hint(self):
478+
precision_hint = self.config.get('_inference_precision_hint')
479+
if precision_hint is None:
480+
return
481+
supported_props = self.ie_core.get_property(self._device, 'SUPPORTED_PROPERTIES')
482+
if 'INFERENCE_PRECISION_HINT' not in supported_props:
483+
warning(f'inference precision hint is not supported for device {self._device}, option will be ingnored')
484+
return
485+
if not precision_hint.upper() in PRECISION_STR_TO_TYPE and not precision_hint in format_map:
486+
raise ConfigError(f'Unknown precision {precision_hint} for inference precision hint')
487+
precision_type = PRECISION_STR_TO_TYPE.get(precision_hint.upper(), precision_hint)
488+
self.ie_core.set_property(self._device, {'INFERENCE_PRECISION_HINT': precision_type})
489+
current_precision = self.ie_core.get_property(self._device, 'INFERENCE_PRECISION_HINT')
490+
print_info(f'Inference precision: {current_precision.get_type_name()}')
486491

487492
def _log_versions(self):
488493
versions = self.ie_core.get_versions(self._device)

0 commit comments

Comments
 (0)