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] Cast: round FLOAT to int4/uint4 with ties to even (#38162)
HU-skread741bf9d1c
diff --git a/src/frontends/onnx/frontend/src/op/cast.cpp b/src/frontends/onnx/frontend/src/op/cast.cppindex cd2298fd1ad..64399e47d63 100644--- a/src/frontends/onnx/frontend/src/op/cast.cpp+++ b/src/frontends/onnx/frontend/src/op/cast.cpp@@ -20,7 +20,7 @@ ov::OutputVector cast(const ov::frontend::onnx::Node& node) {// truncating.const ov::element::Type data_type = data.get_element_type();if (data_type.is_real() && (elem_type == ov::element::i4 || elem_type == ov::element::u4)) {- auto data_rounded = std::make_shared<v5::Round>(data, v5::Round::RoundMode::HALF_AWAY_FROM_ZERO);+ auto data_rounded = std::make_shared<v5::Round>(data, v5::Round::RoundMode::HALF_TO_EVEN);return {std::make_shared<v0::Convert>(data_rounded, elem_type)};}diff --git a/src/frontends/onnx/tests/models/cast_float32_to_int4_ties.prototxt b/src/frontends/onnx/tests/models/cast_float32_to_int4_ties.prototxtnew file mode 100644index 00000000000..38e8a01d212--- /dev/null+++ b/src/frontends/onnx/tests/models/cast_float32_to_int4_ties.prototxt@@ -0,0 +1,45 @@+ir_version: 10+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "input"+ output: "output"+ op_type: "Cast"+ attribute {+ name: "to"+ i: 22+ type: INT+ }+ }+ name: "test-model-cast-ties"+ input {+ name: "input"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 22+ shape {+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 21+}diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cppindex 9937da6875a..5e084294c51 100644--- a/src/frontends/onnx/tests/onnx_import.in.cpp+++ b/src/frontends/onnx/tests/onnx_import.in.cpp@@ -6967,6 +6967,18 @@ OPENVINO_TEST(${BACKEND_NAME}, cast_float32_to_int4) {test_case.run();}+OPENVINO_TEST(${BACKEND_NAME}, cast_float32_to_int4_ties_to_even) {+ // ONNX int4 casting rounds ties to the nearest even integer (docs/technical/int4.md#cast).+ auto model = convert_model("cast_float32_to_int4_ties.onnx");++ auto test_case = ov::test::TestCase(model, s_device);++ test_case.add_input<float>(Shape{4}, std::vector<float>{2.5f, 3.5f, -2.5f, -3.5f});+ test_case.add_expected_output<uint8_t>({0x42, 0xCE});++ test_case.run();+}+OPENVINO_TEST(${BACKEND_NAME}, castlike_float16_to_uint32) {auto model = convert_model("castlike_float16_to_uint32.onnx");