Skip to content

Commit

Permalink
[PyOV] Optimize dtype to Type conversion (#24588)
Browse files Browse the repository at this point in the history
### Details:
- Remove costly conversion to string by replacing it with unique ids of
each dtype.
- More on `dtype.num`:
https://numpy.org/devdocs/reference/generated/numpy.dtype.num.html#numpy.dtype.num

### Tickets:
 - *...*
  • Loading branch information
Jan Iwaszkiewicz committed May 21, 2024
1 parent c76b5f2 commit 0c65b35
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,10 @@ endif()
# Build the code
#

if(Python3_VERSION_MINOR GREATER_EQUAL 11)
set(pybind11_min_version 2.9.2)
else()
set(pybind11_min_version 2.8.0)
endif()
if(CMAKE_CROSSCOMPILING)
set(pybind11_min_version 2.12.0)
else()
set(pybind11_min_version 2.10.0)
endif()
# search for FindPython3.cmake instead of legacy modules
set(PYBIND11_FINDPYTHON ON)
Expand Down
50 changes: 30 additions & 20 deletions src/bindings/python/src/pyopenvino/core/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,34 @@ py::dtype get_dtype(const ov::element::Type& ov_type) {
return ov_type_to_dtype().at(ov_type);
}

const std::map<std::string, ov::element::Type>& dtype_to_ov_type() {
static const std::map<std::string, ov::element::Type> dtype_to_ov_type_mapping = {
{"float16", ov::element::f16},
{"float32", ov::element::f32},
{"float64", ov::element::f64},
{"int8", ov::element::i8},
{"int16", ov::element::i16},
{"int32", ov::element::i32},
{"int64", ov::element::i64},
{"uint8", ov::element::u8},
{"uint16", ov::element::u16},
{"uint32", ov::element::u32},
{"uint64", ov::element::u64},
{"bool", ov::element::boolean},
{"bytes_", ov::element::string},
{"str_", ov::element::string},
{"bytes", ov::element::string},
{"str", ov::element::string},
const std::map<int, ov::element::Type>& dtype_num_to_ov_type() {
static const std::map<int, ov::element::Type> dtype_to_ov_type_mapping = {
{23, ov::element::f16}, // float16
{11, ov::element::f32}, // float32
{12, ov::element::f64}, // float64
{1, ov::element::i8}, // int8
{3, ov::element::i16}, // int16
#ifdef _WIN32
{7, ov::element::i32}, // int32
{9, ov::element::i64}, // int64
#else
{5, ov::element::i32}, // int32
{7, ov::element::i64}, // int64
#endif
{2, ov::element::u8}, // uint8
{4, ov::element::u16}, // uint16
#ifdef _WIN32
{8, ov::element::u32}, // uint32
{10, ov::element::u64}, // uint64
#else
{6, ov::element::u32}, // uint32
{8, ov::element::u64}, // uint64
#endif
{0, ov::element::boolean}, // bool
{18, ov::element::string}, // bytes_
{19, ov::element::string}, // str_
{18, ov::element::string}, // bytes
{19, ov::element::string}, // str
};
return dtype_to_ov_type_mapping;
}
Expand All @@ -66,7 +76,7 @@ ov::element::Type get_ov_type(const py::array& array) {
if (ctype == 'U' || ctype == 'S') {
return ov::element::string;
}
return dtype_to_ov_type().at(py::str(array.dtype()));
return dtype_num_to_ov_type().at(array.dtype().num());
}

ov::element::Type get_ov_type(py::dtype& dtype) {
Expand All @@ -76,7 +86,7 @@ ov::element::Type get_ov_type(py::dtype& dtype) {
if (ctype == 'U' || ctype == 'S') {
return ov::element::string;
}
return dtype_to_ov_type().at(py::str(dtype));
return dtype_num_to_ov_type().at(dtype.num());
}

}; // namespace type_helpers
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/python/src/pyopenvino/core/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const std::map<ov::element::Type, py::dtype>& ov_type_to_dtype();

py::dtype get_dtype(const ov::element::Type& ov_type);

const std::map<std::string, ov::element::Type>& dtype_to_ov_type();
const std::map<int, ov::element::Type>& dtype_num_to_ov_type();

ov::element::Type get_ov_type(const py::array& array);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ set(PYBIND_FE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/mock_mo_python_api.cpp)

source_group("src" FILES ${PYBIND_FE_SRC})

if(Python3_VERSION_MINOR GREATER_EQUAL 11)
set(pybind11_min_version 2.9.2)
else()
set(pybind11_min_version 2.8.0)
endif()
if(CMAKE_CROSSCOMPILING)
set(pybind11_min_version 2.12.0)
else()
set(pybind11_min_version 2.10.0)
endif()
# search for FindPython3.cmake instead of legacy modules
set(PYBIND11_FINDPYTHON ON)
Expand Down

0 comments on commit 0c65b35

Please sign in to comment.