From 24c4ccc6210ebe3e3c6c7ff07d0335636faf50be Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Tue, 8 Feb 2022 13:28:25 +0300 Subject: [PATCH] [PYTHON API] add __hash__ for Type (#10059) * define hash operator for type * Fix code style --- .../pyopenvino/graph/types/element_type.cpp | 1 + .../tools/benchmark/utils/inputs_filling.py | 28 +++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp b/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp index 606da4e479f..204d125a330 100644 --- a/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp +++ b/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp @@ -38,6 +38,7 @@ void regclass_graph_Type(py::module m) { return ""; }); + type.def("__hash__", &ov::element::Type::hash); type.def( "__eq__", [](const ov::element::Type& a, const ov::element::Type& b) { diff --git a/tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py b/tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py index 824d01b2232..09eae98aadd 100644 --- a/tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py +++ b/tools/benchmark_tool/openvino/tools/benchmark/utils/inputs_filling.py @@ -8,7 +8,7 @@ import numpy as np from collections import defaultdict from pathlib import Path -from openvino.runtime import Tensor, PartialShape +from openvino.runtime import Tensor, PartialShape, Type from .constants import IMAGE_EXTENSIONS, BINARY_EXTENSIONS from .logging import logger @@ -138,7 +138,7 @@ def get_image_tensors(image_paths, info, batch_sizes): niter = max(num_shapes, num_images) for i in range(niter): shape = list(info.shapes[i % num_shapes]) if num_shapes else [] - dtype = get_dtype(info.element_type.get_type_name())[0] + dtype = get_dtype(info.element_type)[0] images = np.ndarray(shape=shape, dtype=dtype) image_index = processed_frames current_batch_size = 1 if process_with_original_shapes else batch_sizes[i % num_shapes] @@ -201,15 +201,15 @@ def get_image_tensors(image_paths, info, batch_sizes): def get_dtype(precision): format_map = { - 'f32' : (np.float32, np.finfo(np.float32).min, np.finfo(np.float32).max), - 'i32' : (np.int32, np.iinfo(np.int32).min, np.iinfo(np.int32).max), - 'i64' : (np.int64, np.iinfo(np.int64).min, np.iinfo(np.int64).max), - 'f16' : (np.float16, np.finfo(np.float16).min, np.finfo(np.float16).max), - 'i16' : (np.int16, np.iinfo(np.int16).min, np.iinfo(np.int16).max), - 'u16' : (np.uint16, np.iinfo(np.uint16).min, np.iinfo(np.uint16).max), - 'i8' : (np.int8, np.iinfo(np.int8).min, np.iinfo(np.int8).max), - 'u8' : (np.uint8, np.iinfo(np.uint8).min, np.iinfo(np.uint8).max), - 'boolean' : (np.uint8, 0, 1), + Type.f32 : (np.float32, np.finfo(np.float32).min, np.finfo(np.float32).max), + Type.i32 : (np.int32, np.iinfo(np.int32).min, np.iinfo(np.int32).max), + Type.i64 : (np.int64, np.iinfo(np.int64).min, np.iinfo(np.int64).max), + Type.f16 : (np.float16, np.finfo(np.float16).min, np.finfo(np.float16).max), + Type.i16 : (np.int16, np.iinfo(np.int16).min, np.iinfo(np.int16).max), + Type.u16 : (np.uint16, np.iinfo(np.uint16).min, np.iinfo(np.uint16).max), + Type.i8 : (np.int8, np.iinfo(np.int8).min, np.iinfo(np.int8).max), + Type.u8 : (np.uint8, np.iinfo(np.uint8).min, np.iinfo(np.uint8).max), + Type.boolean : (np.uint8, 0, 1), } if precision in format_map.keys(): return format_map[precision] @@ -224,7 +224,7 @@ def get_binary_tensors(binary_paths, info, batch_sizes): tensors = [] for i in range(niter): shape_id = i % num_shapes - dtype = get_dtype(info.element_type.get_type_name())[0] + dtype = get_dtype(info.element_type)[0] shape = list(info.shapes[shape_id]) binaries = np.ndarray(shape=shape, dtype=dtype) if info.layout.has_name('N'): @@ -266,7 +266,7 @@ def get_image_sizes(app_input_info): def get_image_info_tensors(image_sizes, layer): im_infos = [] for shape, image_size in zip(layer.shapes, image_sizes): - im_info = np.ndarray(shape, dtype=get_dtype(layer.element_type.get_type_name())[0]) + im_info = np.ndarray(shape, dtype=get_dtype(layer.element_type)[0]) for b in range(shape[0]): for i in range(shape[1]): im_info[b][i] = image_size if i in [0, 1] else 1 @@ -275,7 +275,7 @@ def get_image_info_tensors(image_sizes, layer): def fill_tensors_with_random(layer): - dtype, rand_min, rand_max = get_dtype(layer.element_type.get_type_name()) + dtype, rand_min, rand_max = get_dtype(layer.element_type) # np.random.uniform excludes high: add 1 to have it generated if np.dtype(dtype).kind in ['i', 'u', 'b']: rand_max += 1