19
19
import multiprocessing
20
20
from pathlib import Path
21
21
import re
22
- import warnings
23
22
import numpy as np
24
23
from openvino .runtime import Core , AsyncInferQueue , get_version , PartialShape , Type , Dimension
25
24
from openvino .preprocess import PrePostProcessor
32
31
ov_set_config
33
32
)
34
33
from .dlsdk_async_request import AsyncInferRequestWrapper
35
-
36
34
from ..config import ConfigError
37
35
from ..logging import warning , debug , print_info
38
36
from ..utils import (
49
47
format_map = {
50
48
'f32' : np .float32 , 'i32' : np .int32 , 'i64' : np .int64 ,
51
49
'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
54
51
}
55
52
56
53
PRECISION_STR_TO_TYPE = {
57
54
'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
59
56
}
60
57
61
58
@@ -177,19 +174,14 @@ def predict(self, inputs, metadata=None, return_raw=False, **kwargs):
177
174
raw_results = []
178
175
for infer_inputs in inputs :
179
176
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 ()}
183
178
self ._reshape_input (input_shapes )
184
179
if self .infer_request is None :
185
180
self .infer_request = self .exec_network .create_infer_request ()
186
181
feed_dict = {self .input_to_tensor_name [layer_name ]: data for layer_name , data in infer_inputs .items ()}
187
182
outputs = self .infer_request .infer (inputs = feed_dict )
188
183
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 ()})
193
185
if self .reset_memory_state :
194
186
for state in self .infer_request .query_state ():
195
187
state .reset ()
@@ -211,10 +203,7 @@ def _predict_sequential(self, inputs, metadata=None, return_raw=False, **kwargs)
211
203
feed_dict .update (lstm_inputs_feed )
212
204
infer_inputs = {self .input_to_tensor_name [layer_name ]: data for layer_name , data in feed_dict .items ()}
213
205
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 ()}
218
207
lstm_inputs_feed = self ._fill_lstm_inputs (output_result )
219
208
results .append (output_result )
220
209
if return_raw :
@@ -395,6 +384,7 @@ def _device_specific_configuration(self):
395
384
device_config = self .config .get ('device_config' )
396
385
if device_config :
397
386
self ._set_device_config (device_config )
387
+ self ._set_infer_precision_hint ()
398
388
399
389
def _set_nireq (self ):
400
390
num_requests = self .config .get ('num_requests' )
@@ -475,14 +465,29 @@ def _set_device_config(self, device_config):
475
465
if isinstance (value , dict ):
476
466
if key in self ._devices_list ():
477
467
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 ))
479
469
ov_set_config (self .ie_core , dict (value ), device = key )
480
470
else :
481
- warnings . warn (
471
+ warning (
482
472
f'Configuration for { key } will be skipped as device is not listed in evaluation device' )
483
473
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 ()} ' )
486
491
487
492
def _log_versions (self ):
488
493
versions = self .ie_core .get_versions (self ._device )
0 commit comments