Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into python-cross-comp…
Browse files Browse the repository at this point in the history
…ilation
  • Loading branch information
ilya-lavrenov committed Apr 22, 2024
2 parents 2d1003b + 5b07693 commit 848bf79
Show file tree
Hide file tree
Showing 30 changed files with 545 additions and 131 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/job_onnx_runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ jobs:
- name: Clone ONNX Runtime
run: |
branch=`tr -s '\n ' < ${ONNX_RUNTIME_UTILS}/version`
git clone --branch $branch --single-branch --recursive https://github.com/microsoft/onnxruntime.git ${ONNX_RUNTIME_REPO}
hash=`tr -s '\n ' < ${ONNX_RUNTIME_UTILS}/version`
git clone --recursive https://github.com/microsoft/onnxruntime.git ${ONNX_RUNTIME_REPO}
cd ${ONNX_RUNTIME_REPO}
git checkout $hash
#
# Tests
Expand All @@ -114,7 +116,7 @@ jobs:
${ONNX_RUNTIME_REPO}/build.sh \
--config RelWithDebInfo \
--use_openvino CPU_FP32 \
--use_openvino CPU \
--build_shared_lib \
--parallel \
--skip_tests \
Expand All @@ -130,6 +132,11 @@ jobs:
- name: Run onnxruntime_test_all
if: ${{ runner.arch != 'ARM64' }} # Ticket: 126277
run: |
# see https://github.com/microsoft/onnxruntime/issues/13197#issuecomment-1264542497
apt-get install --assume-yes --no-install-recommends language-pack-en
locale-gen en_US.UTF-8
update-locale LANG=en_US.UTF-8
source ${INSTALL_DIR}/setupvars.sh
skip_tests=$(tr -s '\n ' ':' < ${ONNX_RUNTIME_UTILS}/skip_tests)
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,8 @@ jobs:

ONNX_Runtime:
name: ONNX Runtime Integration
# Enable back once https://github.com/microsoft/onnxruntime/pull/19184 is merged
if: ${{ 'false' }}
# if: fromJSON(needs.smart_ci.outputs.affected_components).ONNX_RT ||
# fromJSON(needs.smart_ci.outputs.affected_components).ONNX_FE
if: fromJSON(needs.smart_ci.outputs.affected_components).ONNX_RT ||
fromJSON(needs.smart_ci.outputs.affected_components).ONNX_FE
needs: [ Build, Smart_CI ]
uses: ./.github/workflows/job_onnx_runtime.yml
with:
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/linux_arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,8 @@ jobs:

ONNX_Runtime:
name: ONNX Runtime Integration
# Enable back once https://github.com/microsoft/onnxruntime/pull/19184 is merged
if: ${{ 'false' }}
# if: fromJSON(needs.smart_ci.outputs.affected_components).ONNX_RT ||
# fromJSON(needs.smart_ci.outputs.affected_components).ONNX_FE
if: fromJSON(needs.smart_ci.outputs.affected_components).ONNX_RT ||
fromJSON(needs.smart_ci.outputs.affected_components).ONNX_FE
needs: [ Build, Smart_CI ]
uses: ./.github/workflows/job_onnx_runtime.yml
with:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/linux_riscv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ jobs:
git submodule update --init -- ${OPENVINO_REPO}/thirdparty/telemetry
git submodule update --init -- ${OPENVINO_REPO}/src/plugins/intel_cpu
git submodule update --init -- ${OPENVINO_REPO}/thirdparty/open_model_zoo
git submodule update --init -- ${OPENVINO_REPO}/thirdparty/flatbuffers/flatbuffers
popd
#
Expand Down Expand Up @@ -192,8 +193,6 @@ jobs:
-DENABLE_INTEL_GPU=ON \
-DENABLE_PYTHON=ON \
-DENABLE_WHEEL=ON \
-DPYTHON_MODULE_EXTENSION=$(riscv64-linux-gnu-python3-config --extension-suffix) \
-DPYBIND11_PYTHON_EXECUTABLE_LAST=${OPENVINO_BUILD_DIR}/env/bin/python3.10 \
-DENABLE_TESTS=ON \
-DENABLE_PYTHON_PACKAGING=ON \
-DENABLE_SYSTEM_PROTOBUF=ON \
Expand Down
2 changes: 1 addition & 1 deletion cmake/features.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ else()
set(ENABLE_SYSTEM_LIBS_DEFAULT OFF)
endif()

if(ANDROID OR RISCV64)
if(CMAKE_CROSSCOMPILING AND (ANDROID OR RISCV64))
# when protobuf from /usr/include is used, then Android / Risc-V toolchain ignores include paths
# but if we build for Android using vcpkg / conan / etc where flatbuffers is not located in
# the /usr/include folders, we can still use 'system' flatbuffers
Expand Down
5 changes: 3 additions & 2 deletions conan.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"xbyak/6.73#250bc3bc73379f90f255876c1c00a4cd%1691853024.351",
"snappy/1.1.10#916523630083f6d855cb2977de8eefb6%1689780661.062",
"rapidjson/cci.20220822#8ca51918340f3a21127822258e95ec0f%1663194355.698",
"pybind11/2.10.4#dd44c80a5ed6a2ef11194380daae1248%1682692198.909",
"pybind11/2.12.0#1bf487c19d580200ba9b74afcc4174b4%1711607911.876",
"pugixml/1.13#f615c1fcec55122b2e177d17061276e7%1691917296.869",
"protobuf/3.21.12#d9f5f4e3b86552552dda4c0a2e928eeb%1685218275.69",
"opencl-icd-loader/2023.04.17#5f73dd9f0c023d416a7f162e320b9c77%1692732261.088",
Expand All @@ -31,5 +31,6 @@
"automake/1.16.5#058bda3e21c36c9aa8425daf3c1faf50%1688481772.751",
"autoconf/2.71#53be95d228b2dcb30dc199cb84262d8f%1693395343.513"
],
"python_requires": []
"python_requires": [],
"config_requires": []
}
2 changes: 1 addition & 1 deletion conanfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rapidjson/[>=1.1.0]
xbyak/[>=6.62]
snappy/[>=1.1.7]
onnx/1.15.0
pybind11/[>=2.10.1]
pybind11/[>=2.12.0]
flatbuffers/[>=22.9.24]

[tool_requires]
Expand Down
108 changes: 108 additions & 0 deletions src/bindings/python/src/pyopenvino/core/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,116 @@ py::array array_from_tensor(ov::Tensor&& t, bool is_shared) {
return py::array(dtype, t.get_shape(), t.get_strides(), t.data());
}

py::array array_from_constant_copy(ov::op::v0::Constant&& c) {
const auto& ov_type = c.get_element_type();
const auto dtype = Common::type_helpers::get_dtype(ov_type);
if (ov_type.bitwidth() < Common::values::min_bitwidth) {
return py::array(dtype, c.get_byte_size(), c.get_data_ptr());
}
return py::array(dtype, c.get_shape(), constant_helpers::_get_strides(c), c.get_data_ptr());
}

py::array array_from_constant_copy(ov::op::v0::Constant&& c, py::dtype& dst_dtype) {
// floating
if (dst_dtype.is(py::dtype("float64"))) {
return array_helpers::array_from_constant_cast<double>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("float32"))) {
return array_helpers::array_from_constant_cast<float>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("float16"))) {
return array_helpers::array_from_constant_cast<ov::float16>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
}
// signed
else if (dst_dtype.is(py::dtype("int64"))) {
return array_helpers::array_from_constant_cast<int64_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("int32"))) {
return array_helpers::array_from_constant_cast<int32_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("int16"))) {
return array_helpers::array_from_constant_cast<int16_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("int8"))) {
return array_helpers::array_from_constant_cast<int8_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
}
// unsigned
else if (dst_dtype.is(py::dtype("uint64"))) {
return array_helpers::array_from_constant_cast<uint64_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("uint32"))) {
return array_helpers::array_from_constant_cast<uint32_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("uint16"))) {
return array_helpers::array_from_constant_cast<uint16_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
} else if (dst_dtype.is(py::dtype("uint8"))) {
return array_helpers::array_from_constant_cast<uint8_t>(std::forward<ov::op::v0::Constant>(c), dst_dtype);
}
// other
else if (dst_dtype.is(py::dtype("bool"))) {
const auto& ov_type = c.get_element_type();
switch (ov_type) {
case ov::element::f32:
return array_helpers::array_from_constant_cast_bool<float>(std::forward<ov::op::v0::Constant>(c),
dst_dtype);
case ov::element::f64:
return array_helpers::array_from_constant_cast_bool<double>(std::forward<ov::op::v0::Constant>(c),
dst_dtype);
case ov::element::f16:
return array_helpers::array_from_constant_cast_bool<ov::float16>(std::forward<ov::op::v0::Constant>(c),
dst_dtype);
default:
return array_helpers::array_from_constant_cast<ov::fundamental_type_for<ov::element::boolean>>(
std::forward<ov::op::v0::Constant>(c),
dst_dtype);
}
} else {
OPENVINO_THROW("Constant cannot be casted to specified dtype!");
}
}

py::array array_from_constant_view(ov::op::v0::Constant&& c) {
const auto& ov_type = c.get_element_type();
const auto dtype = Common::type_helpers::get_dtype(ov_type);
if (ov_type.bitwidth() < Common::values::min_bitwidth) {
return py::array(dtype, c.get_byte_size(), c.get_data_ptr(), py::cast(c));
}
return py::array(dtype, c.get_shape(), constant_helpers::_get_strides(c), c.get_data_ptr(), py::cast(c));
}

}; // namespace array_helpers

namespace constant_helpers {
std::vector<size_t> _get_strides(const ov::op::v0::Constant& self) {
auto element_type = self.get_element_type();
auto shape = self.get_shape();
if (element_type == ov::element::boolean) {
return _get_byte_strides<char>(shape);
} else if (element_type == ov::element::f16 || element_type == ov::element::bf16) {
// WA for bf16, returned as f16 array
return _get_byte_strides<ov::float16>(shape);
} else if (element_type == ov::element::f32) {
return _get_byte_strides<float>(shape);
} else if (element_type == ov::element::f64) {
return _get_byte_strides<double>(shape);
} else if (element_type == ov::element::i8 || element_type == ov::element::i4) {
// WA for i4, returned as int8 array
return _get_byte_strides<int8_t>(shape);
} else if (element_type == ov::element::i16) {
return _get_byte_strides<int16_t>(shape);
} else if (element_type == ov::element::i32) {
return _get_byte_strides<int32_t>(shape);
} else if (element_type == ov::element::i64) {
return _get_byte_strides<int64_t>(shape);
} else if (element_type == ov::element::u8 || element_type == ov::element::u1 || element_type == ov::element::u4 ||
element_type == ov::element::nf4) {
// WA for u1, u4, nf4, all returned as packed uint8 arrays
return _get_byte_strides<uint8_t>(shape);
} else if (element_type == ov::element::u16) {
return _get_byte_strides<uint16_t>(shape);
} else if (element_type == ov::element::u32) {
return _get_byte_strides<uint32_t>(shape);
} else if (element_type == ov::element::u64) {
return _get_byte_strides<uint64_t>(shape);
} else {
throw std::runtime_error("Unsupported data type!");
}
}
}; // namespace constant_helpers

template <>
ov::op::v0::Constant create_copied(py::array& array) {
// Do not copy data from the array, only return empty tensor based on type.
Expand Down
40 changes: 40 additions & 0 deletions src/bindings/python/src/pyopenvino/core/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,48 @@ py::array as_contiguous(py::array& array, ov::element::Type type);

py::array array_from_tensor(ov::Tensor&& t, bool is_shared);

template <typename T>
py::array array_from_constant_cast_bool(ov::op::v0::Constant&& c, py::dtype& dst_dtype) {
std::vector<char> result;
size_t size = c.get_byte_size() / sizeof(T);

result.reserve(size);

for(size_t i = 0; i < size; i++) {
result.emplace_back(*(static_cast<const T*>(c.get_data_ptr()) + i) != 0 ? 1 : 0);
}

return py::array(dst_dtype, c.get_shape(), result.data());
}

template <typename T>
py::array array_from_constant_cast(ov::op::v0::Constant&& c, py::dtype& dst_dtype) {
auto tmp = c.cast_vector<T>();
return py::array(dst_dtype, c.get_shape(), tmp.data());
}

py::array array_from_constant_copy(ov::op::v0::Constant&& c);

py::array array_from_constant_copy(ov::op::v0::Constant&& c, py::dtype& dst_dtype);

py::array array_from_constant_view(ov::op::v0::Constant&& c);

}; // namespace array_helpers

namespace constant_helpers {
template <typename T>
std::vector<size_t> _get_byte_strides(const ov::Shape& s) {
auto byte_strides = ov::row_major_strides(s);
for (auto&& stride : byte_strides) {
stride *= sizeof(T);
}
return byte_strides;
}

std::vector<size_t> _get_strides(const ov::op::v0::Constant& self);

}; // namespace constant_helpers

// Helpers for shapes
namespace shape_helpers {

Expand Down
6 changes: 6 additions & 0 deletions src/bindings/python/src/pyopenvino/frontend/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ void regclass_frontend_FrontEndManager(py::module m) {
});
}

OPENVINO_SUPPRESS_DEPRECATED_START

// CVS-139194: migrate to new exception handling https://github.com/pybind/pybind11/pull/4772

void regclass_frontend_GeneralFailureFrontEnd(py::module m) {
static py::exception<ov::frontend::GeneralFailure> exc(std::move(m), "GeneralFailure");
py::register_exception_translator([](std::exception_ptr p) {
Expand Down Expand Up @@ -158,3 +162,5 @@ void regclass_frontend_NotImplementedFailureFrontEnd(py::module m) {
}
});
}

OPENVINO_SUPPRESS_DEPRECATED_END
Loading

0 comments on commit 848bf79

Please sign in to comment.