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
[ONNX FE] Use BitwiseLeftShift/BitwiseRightShift for BitShift operator (#39853)
Abhinav Saini6dbcf6d54b4d
diff --git a/src/frontends/onnx/frontend/src/op/bitshift.cpp b/src/frontends/onnx/frontend/src/op/bitshift.cppindex 5f95b55b8c3..a77db165928 100644--- a/src/frontends/onnx/frontend/src/op/bitshift.cpp+++ b/src/frontends/onnx/frontend/src/op/bitshift.cpp@@ -4,10 +4,8 @@#include "core/operator_set.hpp"#include "exceptions.hpp"-#include "openvino/op/constant.hpp"-#include "openvino/op/divide.hpp"-#include "openvino/op/multiply.hpp"-#include "openvino/op/power.hpp"+#include "openvino/op/bitwise_left_shift.hpp"+#include "openvino/op/bitwise_right_shift.hpp"using namespace ov::op;namespace ov::frontend::onnx::ai_onnx::opset_1 {@@ -25,13 +23,13 @@ ov::OutputVector bitshift(const ov::frontend::onnx::Node& node) {"attribute. Given: ",direction);- auto shift =- std::make_shared<v1::Power>(v0::Constant::create(input_y.get_element_type(), ov::Shape{1}, {2}), input_y);-+ // Use the native bitwise shift operations so that results wrap around on overflow+ // exactly like the ONNX BitShift specification requires. The previous decomposition into+ // Power + Multiply/Divide saturated overflowed values on some plugins (e.g. u8 on CPU).if (direction == "RIGHT") {- return {std::make_shared<v1::Divide>(input_x, shift)};+ return {std::make_shared<v15::BitwiseRightShift>(input_x, input_y)};} else {- return {std::make_shared<v1::Multiply>(input_x, shift)};+ return {std::make_shared<v15::BitwiseLeftShift>(input_x, input_y)};}}diff --git a/src/frontends/onnx/tests/models/bitshift_left_uint8.prototxt b/src/frontends/onnx/tests/models/bitshift_left_uint8.prototxtnew file mode 100644index 00000000000..6df527e8300--- /dev/null+++ b/src/frontends/onnx/tests/models/bitshift_left_uint8.prototxt@@ -0,0 +1,59 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "x"+ input: "y"+ output: "z"+ op_type: "BitShift"+ attribute {+ name: "direction"+ s: "LEFT"+ type: STRING+ }+ }+ name: "BitShiftLeftUint8Graph"+ input {+ name: "x"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 5+ }+ }+ }+ }+ }+ input {+ name: "y"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 5+ }+ }+ }+ }+ }+ output {+ name: "z"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 5+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 11+}diff --git a/src/frontends/onnx/tests/models/bitshift_right_uint8.prototxt b/src/frontends/onnx/tests/models/bitshift_right_uint8.prototxtnew file mode 100644index 00000000000..7cbd4c40f48--- /dev/null+++ b/src/frontends/onnx/tests/models/bitshift_right_uint8.prototxt@@ -0,0 +1,59 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "x"+ input: "y"+ output: "z"+ op_type: "BitShift"+ attribute {+ name: "direction"+ s: "RIGHT"+ type: STRING+ }+ }+ name: "BitShiftRightUint8Graph"+ input {+ name: "x"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 5+ }+ }+ }+ }+ }+ input {+ name: "y"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 5+ }+ }+ }+ }+ }+ output {+ name: "z"+ type {+ tensor_type {+ elem_type: 2+ shape {+ dim {+ dim_value: 5+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 11+}diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cppindex 5ff8248bd4b..b64016c67c8 100644--- a/src/frontends/onnx/tests/onnx_import.in.cpp+++ b/src/frontends/onnx/tests/onnx_import.in.cpp@@ -8315,6 +8315,32 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_bitshift_uint32_y) {test_case.run();}+/// @brief Testing ONNX BitShift with direction="LEFT" on uint8 inputs+/// Verifies that overflowed values wrap around (modulo 2^8) as required by the ONNX+/// specification instead of being saturated to the maximum value of the type.+OPENVINO_TEST(${BACKEND_NAME}, onnx_bitshift_left_uint8) {+ const auto model = convert_model("bitshift_left_uint8.onnx");+ auto test_case = test::TestCase(model, s_device);++ test_case.add_input<uint8_t>(Shape{5}, {0, 1, 16, 128, 255});+ test_case.add_input<uint8_t>(Shape{5}, {0, 1, 2, 3, 7});+ test_case.add_expected_output<uint8_t>(Shape{5}, {0, 2, 64, 0, 128});++ test_case.run();+}++/// @brief Testing ONNX BitShift with direction="RIGHT" on uint8 inputs+OPENVINO_TEST(${BACKEND_NAME}, onnx_bitshift_right_uint8) {+ const auto model = convert_model("bitshift_right_uint8.onnx");+ auto test_case = test::TestCase(model, s_device);++ test_case.add_input<uint8_t>(Shape{5}, {0, 2, 64, 128, 255});+ test_case.add_input<uint8_t>(Shape{5}, {0, 1, 2, 7, 4});+ test_case.add_expected_output<uint8_t>(Shape{5}, {0, 1, 16, 1, 15});++ test_case.run();+}+/// @brief Testing ONNX LayerNormalization with 2 inputs (no "bias" input provided)/// Verifies a fix for the "bias" input not treated as fully optional.OPENVINO_TEST(${BACKEND_NAME}, onnx_layer_normalization_unbiased) {