Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Add support of u2 type for Subtract op in template plugin (#31645)

Oleg Pipikin84c084304c9e
diff --git a/src/plugins/template/backend/ops/ops_evaluates.hpp b/src/plugins/template/backend/ops/ops_evaluates.hpp
index 26a46773a55..633967d7a24 100644
--- a/src/plugins/template/backend/ops/ops_evaluates.hpp
+++ b/src/plugins/template/backend/ops/ops_evaluates.hpp
@@ -574,4 +574,8 @@ extern template bool evaluate_node<ov::op::v16::SparseFillEmptyRows>(std::shared
extern template bool evaluate_node<ov::op::v13::ScaledDotProductAttention>(std::shared_ptr<ov::Node> node,
ov::TensorVector& outputs,
- const ov::TensorVector& inputs);
\ No newline at end of file
+ const ov::TensorVector& inputs);
+
+extern template bool evaluate_node<ov::op::v1::Subtract>(std::shared_ptr<ov::Node> node,
+ ov::TensorVector& outputs,
+ const ov::TensorVector& inputs);
diff --git a/src/plugins/template/backend/ops/subtract.cpp b/src/plugins/template/backend/ops/subtract.cpp
new file mode 100644
index 00000000000..e4062e92fef
--- /dev/null
+++ b/src/plugins/template/backend/ops/subtract.cpp
@@ -0,0 +1,68 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/reference/subtract.hpp"
+
+#include "evaluate_node.hpp"
+#include "openvino/core/type/element_iterator.hpp"
+#include "openvino/core/type/element_type_traits.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/reference/convert.hpp"
+
+namespace {
+template <ov::element::Type_t ET>
+bool evaluate(const std::shared_ptr<ov::op::v1::Subtract>& op,
+ ov::TensorVector& outputs,
+ const ov::TensorVector& inputs) {
+ return false;
+}
+
+template <>
+bool evaluate<ov::element::u2>(const std::shared_ptr<ov::op::v1::Subtract>& op,
+ ov::TensorVector& outputs,
+ const ov::TensorVector& inputs) {
+ constexpr const auto conversion_type = ov::element::i8;
+ using T = typename ov::element_type_traits<conversion_type>::value_type;
+ const auto& in0_shape = inputs[0].get_shape();
+ const auto& in1_shape = inputs[1].get_shape();
+ const auto& out_shape = outputs[0].get_shape();
+
+ ov::Tensor converted_in0(conversion_type, in0_shape);
+ ov::reference::convert(ov::element::iterator<ov::element::u2>(inputs[0].data()),
+ ov::element::iterator<conversion_type>(converted_in0.data()),
+ shape_size(in0_shape));
+
+ ov::Tensor converted_in1(conversion_type, in1_shape);
+ ov::reference::convert(ov::element::iterator<ov::element::u2>(inputs[1].data()),
+ ov::element::iterator<conversion_type>(converted_in1.data()),
+ shape_size(in1_shape));
+
+ ov::Tensor converted_out(conversion_type, out_shape);
+
+ ov::reference::subtract<T>(converted_in0.data<T>(),
+ converted_in1.data<T>(),
+ converted_out.data<T>(),
+ in0_shape,
+ in1_shape,
+ op->get_autob());
+
+ ov::reference::convert(ov::element::iterator<conversion_type>(converted_out.data()),
+ ov::element::iterator<ov::element::u2>(outputs[0].data()),
+ shape_size(out_shape));
+
+ return true;
+}
+} // namespace
+
+template <>
+bool evaluate_node<ov::op::v1::Subtract>(std::shared_ptr<ov::Node> node,
+ ov::TensorVector& outputs,
+ const ov::TensorVector& inputs) {
+ switch (node->get_output_element_type(0)) {
+ case ov::element::u2:
+ return evaluate<ov::element::u2>(ov::as_type_ptr<ov::op::v1::Subtract>(node), outputs, inputs);
+ default:
+ OPENVINO_THROW("Unhandled data type ", node->get_element_type(), " in evaluate_node()");
+ }
+}
diff --git a/src/plugins/template/backend/opset_int_tbl.hpp b/src/plugins/template/backend/opset_int_tbl.hpp
index 9298f176769..3a83a3539d1 100644
--- a/src/plugins/template/backend/opset_int_tbl.hpp
+++ b/src/plugins/template/backend/opset_int_tbl.hpp
@@ -66,6 +66,7 @@ _OPENVINO_OP_REG(Split, op::v1)
_OPENVINO_OP_REG(Reshape, op::v1)
_OPENVINO_OP_REG(Select, op::v1)
_OPENVINO_OP_REG(GatherTree, op::v1)
+_OPENVINO_OP_REG(Subtract, op::v1)
_OPENVINO_OP_REG(Assign, op::v3)
_OPENVINO_OP_REG(Bucketize, op::v3)
diff --git a/src/plugins/template/tests/functional/op_reference/subtract.cpp b/src/plugins/template/tests/functional/op_reference/subtract.cpp
index eab10f985c6..209d17553c0 100644
--- a/src/plugins/template/tests/functional/op_reference/subtract.cpp
+++ b/src/plugins/template/tests/functional/op_reference/subtract.cpp
@@ -105,6 +105,40 @@ std::vector<SubtractParams> generateParamsForSubtract() {
return params;
}
+std::vector<SubtractParams> generateParamsForSubtractU2() {
+ std::vector<SubtractParams> params{
+ SubtractParams(ov::Shape{2, 2},
+ ov::Shape{2, 2},
+ ov::Shape{2, 2},
+ ov::element::Type_t::u2,
+ std::vector<uint8_t>{0b00011110}, // 2 3 1 0
+ std::vector<uint8_t>{0b11100100}, // 0 1 2 3
+ std::vector<uint8_t>{0b01111010}), // 2 2 3 1
+ SubtractParams(ov::Shape{3, 2, 1},
+ ov::Shape{1, 6},
+ ov::Shape{3, 2, 6},
+ ov::element::Type_t::u2,
+ std::vector<uint8_t>{0b11100100, 0b00000100}, // 0 1 2 3, 0 1 + padding 0 0
+ std::vector<uint8_t>{0b11100100, 0b00000110}, // 0 1 2 3, 2 1 + padding 0 0
+ std::vector<uint8_t>{0b01101100, // 0 3 2 1
+ 0b00011110, // 2 3 1 0
+ 0b00111011, // 3 2 3 0
+ 0b11000110, // 2 1 0 3
+ 0b10110100, // 0 1 3 2
+ 0b10010001, // 1 0 1 2
+ 0b01101100, // 0 3 2 1
+ 0b00011110, // 2 3 1 0
+ 0b00111011}), // 3 2 3 0
+ SubtractParams(ov::Shape{1},
+ ov::Shape{1},
+ ov::Shape{1},
+ ov::element::Type_t::u2,
+ std::vector<uint8_t>{0b00000011}, // 3
+ std::vector<uint8_t>{0b00000010}, // 2
+ std::vector<uint8_t>{0b00000001})}; // 1
+ return params;
+}
+
template <element::Type_t IN_ET>
std::vector<SubtractParams> generateParamsForSubtractFloat() {
using T = typename element_type_traits<IN_ET>::value_type;
@@ -126,7 +160,8 @@ std::vector<SubtractParams> generateCombinedParamsForSubtract() {
generateParamsForSubtract<element::Type_t::i64>(),
generateParamsForSubtract<element::Type_t::i32>(),
generateParamsForSubtract<element::Type_t::u64>(),
- generateParamsForSubtract<element::Type_t::u32>()};
+ generateParamsForSubtract<element::Type_t::u32>(),
+ generateParamsForSubtractU2()};
std::vector<SubtractParams> combinedParams;