OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[PyOV] Expose `u2` element type (#33304)
Przemyslaw Wysocki0a5eedc91d74
diff --git a/src/bindings/python/src/openvino/helpers/packing.py b/src/bindings/python/src/openvino/helpers/packing.pyindex cf085e3eca2..fb4a36fd756 100644--- a/src/bindings/python/src/openvino/helpers/packing.py+++ b/src/bindings/python/src/openvino/helpers/packing.py@@ -9,7 +9,7 @@ from openvino import Type, Shapedef pack_data(array: np.ndarray, type: Type) -> np.ndarray:- """Represent array values as u1,u4 or i4 openvino element type and pack them into uint8 numpy array.+ """Represent array values as u1, u2, u4 or i4 openvino element type and pack them into uint8 numpy array.If the number of elements in array is odd we pad them with zero value to be able to fit the bitsequence into the uint8 array.@@ -20,10 +20,10 @@ def pack_data(array: np.ndarray, type: Type) -> np.ndarray::param array: numpy array with values to pack.:type array: numpy array- :param type: Type to interpret the array values. Type must be u1, u4, i4, nf4 or f4e2m1.+ :param type: Type to interpret the array values. Type must be u1, u2, u4, i4, nf4 or f4e2m1.:type type: openvino.Type"""- assert type in [Type.u1, Type.u4, Type.i4, Type.nf4, Type.f4e2m1], "Packing algorithm for the" "data types stored in 1, 2 or 4 bits"+ assert type in [Type.u1, Type.u2, Type.u4, Type.i4, Type.nf4, Type.f4e2m1], "Packing algorithm for the" "data types stored in 1, 2 or 4 bits"minimum_regular_dtype = np.int8 if type == Type.i4 else np.uint8casted_to_regular_type = array.astype(dtype=minimum_regular_dtype, casting="unsafe")@@ -57,12 +57,12 @@ def unpack_data(array: np.ndarray, type: Type, shape: Union[list, Shape]) -> np.:param array: numpy array to unpack.:type array: numpy array- :param type: Type to extract from array values. Type must be u1, u4, i4, nf4 or f4e2m1.+ :param type: Type to extract from array values. Type must be u1, u2, u4, i4, nf4 or f4e2m1.:type type: openvino.Type:param shape: the new shape for the unpacked array.:type shape: Union[list, openvino.Shape]"""- assert type in [Type.u1, Type.u4, Type.i4, Type.nf4, Type.f4e2m1], "Unpacking algorithm for the" "data types stored in 1, 2 or 4 bits"+ assert type in [Type.u1, Type.u2, Type.u4, Type.i4, Type.nf4, Type.f4e2m1], "Unpacking algorithm for the" "data types stored in 1, 2 or 4 bits"unpacked = np.unpackbits(array.view(np.uint8))shape = list(shape)if type.bitwidth == 1:diff --git a/src/bindings/python/src/openvino/opset13/ops.py b/src/bindings/python/src/openvino/opset13/ops.pyindex 8faddc18a2e..0978d916a16 100644--- a/src/bindings/python/src/openvino/opset13/ops.py+++ b/src/bindings/python/src/openvino/opset13/ops.py@@ -312,7 +312,7 @@ def constant(Requires data to be C_CONTIGUOUS if `True`.Disabled by default if:- value is a scalar.- - dtype is one of: Type.u1, Type.i4, Type.u4, Type.nf4, Type.bf16.+ - dtype is one of: Type.u1, Type.u2, Type.i4, Type.u4, Type.nf4, Type.bf16.- dtype force conversion of data.:return: The Constant node initialized with provided data."""@@ -332,7 +332,7 @@ def constant(# Handle type casting, when dtype is not None:if dtype:# Expect packed data, use different constructor to handle it correctly:- if dtype in [Type.u1, Type.i4, Type.u4, Type.nf4, Type.f4e2m1]:+ if dtype in [Type.u1, Type.u2, Type.i4, Type.u4, Type.nf4, Type.f4e2m1]:display_shared_memory_warning(f"Constant initialized with packed type of {dtype}")return Constant(dtype, Shape(_value.shape), _value.flatten().tolist())elif dtype in [Type.bf16, Type.f8e8m0, Type.f8e4m3, Type.f8e5m2]:diff --git a/src/bindings/python/src/pyopenvino/core/common.cpp b/src/bindings/python/src/pyopenvino/core/common.cppindex 3ab448fe071..760609d83da 100644--- a/src/bindings/python/src/pyopenvino/core/common.cpp+++ b/src/bindings/python/src/pyopenvino/core/common.cpp@@ -20,17 +20,18 @@ namespace type_helpers {const std::map<ov::element::Type, py::dtype>& ov_type_to_dtype() {static const std::map<ov::element::Type, py::dtype> ov_type_to_dtype_mapping = {- {ov::element::f16, py::dtype("float16")}, {ov::element::bf16, py::dtype("float16")},- {ov::element::f32, py::dtype("float32")}, {ov::element::f64, py::dtype("float64")},- {ov::element::i8, py::dtype("int8")}, {ov::element::i16, py::dtype("int16")},- {ov::element::i32, py::dtype("int32")}, {ov::element::i64, py::dtype("int64")},- {ov::element::u8, py::dtype("uint8")}, {ov::element::u16, py::dtype("uint16")},- {ov::element::u32, py::dtype("uint32")}, {ov::element::u64, py::dtype("uint64")},- {ov::element::boolean, py::dtype("bool")}, {ov::element::u1, py::dtype("uint8")},- {ov::element::u4, py::dtype("uint8")}, {ov::element::nf4, py::dtype("uint8")},- {ov::element::i4, py::dtype("int8")}, {ov::element::f8e4m3, py::dtype("uint8")},- {ov::element::f8e5m2, py::dtype("uint8")}, {ov::element::string, py::dtype("bytes_")},- {ov::element::f4e2m1, py::dtype("uint8")}, {ov::element::f8e8m0, py::dtype("uint8")},+ {ov::element::f16, py::dtype("float16")}, {ov::element::bf16, py::dtype("float16")},+ {ov::element::f32, py::dtype("float32")}, {ov::element::f64, py::dtype("float64")},+ {ov::element::i8, py::dtype("int8")}, {ov::element::i16, py::dtype("int16")},+ {ov::element::i32, py::dtype("int32")}, {ov::element::i64, py::dtype("int64")},+ {ov::element::u8, py::dtype("uint8")}, {ov::element::u16, py::dtype("uint16")},+ {ov::element::u32, py::dtype("uint32")}, {ov::element::u64, py::dtype("uint64")},+ {ov::element::boolean, py::dtype("bool")}, {ov::element::u1, py::dtype("uint8")},+ {ov::element::u2, py::dtype("uint8")}, {ov::element::u4, py::dtype("uint8")},+ {ov::element::nf4, py::dtype("uint8")}, {ov::element::i4, py::dtype("int8")},+ {ov::element::f8e4m3, py::dtype("uint8")}, {ov::element::f8e5m2, py::dtype("uint8")},+ {ov::element::string, py::dtype("bytes_")}, {ov::element::f4e2m1, py::dtype("uint8")},+ {ov::element::f8e8m0, py::dtype("uint8")},};return ov_type_to_dtype_mapping;}@@ -272,6 +273,8 @@ py::array as_contiguous(py::array& array, ov::element::Type type) {return array.cast<py::array_t<bool, py::array::c_style | py::array::forcecast>>();case ov::element::u1:return array.cast<py::array_t<uint8_t, py::array::c_style | py::array::forcecast>>();+ case ov::element::u2:+ return array.cast<py::array_t<uint8_t, py::array::c_style | py::array::forcecast>>();// need to create a view on array to cast it correctlycase ov::element::f16:case ov::element::bf16:diff --git a/src/bindings/python/src/pyopenvino/core/tensor.cpp b/src/bindings/python/src/pyopenvino/core/tensor.cppindex a82121287f6..a21cb10440e 100644--- a/src/bindings/python/src/pyopenvino/core/tensor.cpp+++ b/src/bindings/python/src/pyopenvino/core/tensor.cpp@@ -315,7 +315,7 @@ void regclass_Tensor(py::module m) {Returns numpy array with corresponding shape and dtype.- For tensors with OpenVINO specific element type, such as u1, u4 or i4+ For tensors with OpenVINO specific element type, such as u1, u2, u4 or i4it returns linear array, with uint8 / int8 numpy dtype.For tensors with string element type, returns a numpy array of bytesdiff --git a/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp b/src/bindings/python/src/pyopenvino/graph/ops/constant.cppindex c5f704cf8bc..f1bc60e7170 100644--- a/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp+++ b/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp@@ -108,7 +108,8 @@ void regclass_graph_op_Constant(py::module m) {return _cast_vector<int32_t>(self);} else if (element_type == ov::element::i64) {return _cast_vector<int64_t>(self);- } else if (element_type == ov::element::u8 || element_type == ov::element::u1) {+ } else if (element_type == ov::element::u8 || element_type == ov::element::u1 ||+ element_type == ov::element::u2 || element_type == ov::element::u4) {return _cast_vector<uint8_t>(self);} else if (element_type == ov::element::u16) {return _cast_vector<uint16_t>(self);@@ -157,7 +158,8 @@ void regclass_graph_op_Constant(py::module m) {return _get_buffer_info<int32_t>(self);} else if (element_type == ov::element::i64) {return _get_buffer_info<int64_t>(self);- } else if (element_type == ov::element::u8 || element_type == ov::element::u1) {+ } else if (element_type == ov::element::u8 || element_type == ov::element::u1 ||+ element_type == ov::element::u2 || element_type == ov::element::u4) {return _get_buffer_info<uint8_t>(self);} else if (element_type == ov::element::u16) {return _get_buffer_info<uint16_t>(self);diff --git a/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp b/src/bindings/python/src/pyopenvino/graph/types/element_type.cppindex eff76661900..8d4d28e836c 100644--- a/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp+++ b/src/bindings/python/src/pyopenvino/graph/types/element_type.cpp@@ -42,6 +42,7 @@ void regclass_graph_Type(py::module m) {type.attr("i32") = ov::element::i32;type.attr("i64") = ov::element::i64;type.attr("u1") = ov::element::u1;+ type.attr("u2") = ov::element::u2;type.attr("u4") = ov::element::u4;type.attr("u8") = ov::element::u8;type.attr("u16") = ov::element::u16;diff --git a/src/bindings/python/tests/test_graph/test_constant.py b/src/bindings/python/tests/test_graph/test_constant.pyindex a64146ea1c4..81a9f9ffd2b 100644--- a/src/bindings/python/tests/test_graph/test_constant.py+++ b/src/bindings/python/tests/test_graph/test_constant.py@@ -249,6 +249,7 @@ def test_init_bf16_direct(ov_type, numpy_dtype, shared_flag):("ov_type", "src_dtype"),[(Type.u1, np.uint8),+ (Type.u2, np.uint8),(Type.u4, np.uint8),(Type.i4, np.int8),(Type.nf4, np.uint8),diff --git a/src/bindings/python/tests/test_runtime/test_tensor.py b/src/bindings/python/tests/test_runtime/test_tensor.pyindex 6eef0ec18b4..43f835ca366 100644--- a/src/bindings/python/tests/test_runtime/test_tensor.py+++ b/src/bindings/python/tests/test_runtime/test_tensor.py@@ -35,6 +35,7 @@ from tests.utils.helpers import generate_image, generate_relu_compiled_model(ov.Type.u64, np.uint64),(ov.Type.boolean, bool),(ov.Type.u1, np.uint8),+ (ov.Type.u2, np.uint8),(ov.Type.u4, np.uint8),(ov.Type.i4, np.int8),],@@ -372,6 +373,7 @@ def test_can_set_shape_other_dims():"ov_type",[(ov.Type.u1),+ (ov.Type.u2),(ov.Type.u4),(ov.Type.i4),],@@ -387,6 +389,7 @@ def test_cannot_create_roi_from_packed_tensor(ov_type):"ov_type",[(ov.Type.u1),+ (ov.Type.u2),(ov.Type.u4),(ov.Type.i4),],@@ -412,6 +415,7 @@ def test_cannot_get_strides_for_packed_tensor(ov_type):"ov_type",[(ov.Type.u1),+ (ov.Type.u2),(ov.Type.u4),(ov.Type.i4),],@@ -438,6 +442,7 @@ def test_init_with_packed_buffer(dtype, ov_type):("low", "high", "ov_type", "dtype"),[(0, 2, ov.Type.u1, np.uint8),+ (0, 4, ov.Type.u2, np.uint8),(0, 16, ov.Type.u4, np.uint8),(-8, 7, ov.Type.i4, np.int8),(0, 16, ov.Type.nf4, np.uint8),diff --git a/src/bindings/python/tests/test_runtime/test_type.py b/src/bindings/python/tests/test_runtime/test_type.pyindex 275f4f77669..81ea2507fe7 100644--- a/src/bindings/python/tests/test_runtime/test_type.py+++ b/src/bindings/python/tests/test_runtime/test_type.py@@ -61,6 +61,10 @@ def test_dtype_ovtype_conversion(dtype_string, dtype, ovtype):(Type.u32, True, False, False, True, False, False, "u32", 4, 32),(Type.u64, True, False, False, True, False, False, "u64", 8, 64),(Type.boolean, True, False, False, True, True, False, "boolean", 1, 8),+ (Type.u1, True, False, False, True, False, False, "u1", 1, 1),+ (Type.u2, True, False, False, True, False, False, "u2", 1, 2),+ (Type.u4, True, False, False, True, False, False, "u4", 1, 4),+ (Type.i4, True, False, False, True, True, True, "i4", 1, 4),])def test_basic_ovtypes(ovtype,static_flag,