Skip to content

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

[ONNX FE] Support converting FP8 QDQ operations (#34364)

Szymon Gutajdff154dacb69
diff --git a/src/frontends/onnx/frontend/src/op/quantize_linear.cpp b/src/frontends/onnx/frontend/src/op/quantize_linear.cpp
index 36b901e6a8f..c3c347f664e 100644
--- a/src/frontends/onnx/frontend/src/op/quantize_linear.cpp
+++ b/src/frontends/onnx/frontend/src/op/quantize_linear.cpp
@@ -2,12 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include <algorithm>
+
#include "core/operator_set.hpp"
#include "exceptions.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/frontend/exception.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/fake_convert.hpp"
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/subtract.hpp"
@@ -34,11 +38,30 @@ void validate_zero_point_type(const Node& onnx_node, const ov::Output<ov::Node>&
const auto& y_zero_point_et = y_zero_point.get_element_type();
CHECK_VALID_NODE(
onnx_node,
- y_zero_point_et.is_static() && (y_zero_point_et == ov::element::u4 || y_zero_point_et == ov::element::i4 ||
- y_zero_point_et == ov::element::u8 || y_zero_point_et == ov::element::i8 ||
- y_zero_point_et == ov::element::u16 || y_zero_point_et == ov::element::i16),
+ y_zero_point_et.is_static() &&
+ (y_zero_point_et == ov::element::u4 || y_zero_point_et == ov::element::i4 ||
+ y_zero_point_et == ov::element::u8 || y_zero_point_et == ov::element::i8 ||
+ y_zero_point_et == ov::element::u16 || y_zero_point_et == ov::element::i16 ||
+ y_zero_point_et == ov::element::f8e4m3 || y_zero_point_et == ov::element::f8e5m2),
"\"y_zero_point\" input data for QuantizeLinear operator must be one of the supported types: u4, i4, u8, i8, "
- "u16 or i16 integer type.");
+ "u16, i16, f8e4m3 or f8e5m2.");
+
+ // The ONNX spec requires the zero point to be 0 for fp8 types.
+ // Both f8e4m3 and f8e5m2 encode 0.0 as the all-zero bit pattern (0x00),
+ // so checking that every raw byte is zero is sufficient.
+ if (y_zero_point_et == ov::element::f8e4m3 || y_zero_point_et == ov::element::f8e5m2) {
+ const auto zp_const = ov::util::get_constant_from_source(y_zero_point);
+ if (zp_const) {
+ const auto raw = zp_const->cast_vector<float>();
+ CHECK_VALID_NODE(onnx_node,
+ std::all_of(raw.begin(),
+ raw.end(),
+ [](const float value) {
+ return value == 0.0f;
+ }),
+ "Expecting \"y_zero_point\" in QuantizeLinear equal zero for 8-bit floating point types.");
+ }
+ }
}
ov::Output<ov::Node> validate_scale(const Node& onnx_node, const ov::Output<ov::Node>& y_scale) {
@@ -123,13 +146,39 @@ std::tuple<std::shared_ptr<ov::Node>, std::shared_ptr<ov::Node>> get_input_bands
return std::make_tuple(input_low, input_high);
}
+
+std::shared_ptr<ov::Node> make_fake_convert(const ov::Output<ov::Node>& y_scale,
+ const ov::Output<ov::Node>& y_zero_point,
+ const ov::Output<ov::Node>& data) {
+ const ov::element::Type& destination_type = y_zero_point.get_element_type();
+ const ov::element::Type& data_type = data.get_element_type();
+
+ // Normalize x by the quantization scale: x_scaled = x / y_scale.
+ // FakeConvert will then simulate fp8 quantization and dequantization on x_scaled
+ // using a unit scale (scale=1), since the data is already in the fp8 value range.
+ const auto x_scaled = std::make_shared<v1::Divide>(data, y_scale);
+ const auto unit_scale = std::make_shared<v0::Constant>(data_type, ov::Shape{}, 1.0f);
+
+ // FakeConvert simulates: fp8_dequant(fp8_quant(x_scaled)) in f32
+ const auto fake_convert = std::make_shared<v13::FakeConvert>(x_scaled, unit_scale, destination_type);
+
+ // Convert the fp8-rounded floating-point value to the actual fp8 output type
+ return std::make_shared<v0::Convert>(fake_convert, destination_type);
+}
+
} // namespace
+
std::shared_ptr<ov::Node> make_fake_quantize(const ov::Output<ov::Node>& y_scale,
const ov::Output<ov::Node>& y_zero_point,
const ov::Output<ov::Node>& data) {
const ov::element::Type& destination_type = y_zero_point.get_element_type();
const ov::element::Type& data_type = data.get_element_type();
+ // For 8-bit floating point output types, use FakeConvert instead of FakeQuantize
+ if (destination_type == ov::element::f8e4m3 || destination_type == ov::element::f8e5m2) {
+ return make_fake_convert(y_scale, y_zero_point, data);
+ }
+
std::shared_ptr<ov::Node> output_low;
std::shared_ptr<ov::Node> output_high;
std::tie(output_low, output_high) = detail::get_output_bands(destination_type, data_type);
diff --git a/src/frontends/onnx/tests/models/quant_dequant_f8e4m3.prototxt b/src/frontends/onnx/tests/models/quant_dequant_f8e4m3.prototxt
new file mode 100644
index 00000000000..6a4dd6d8830
--- /dev/null
+++ b/src/frontends/onnx/tests/models/quant_dequant_f8e4m3.prototxt
@@ -0,0 +1,71 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "scale"
+ input: "zero_point"
+ output: "y_q"
+ name: "QuantizeLinear"
+ op_type: "QuantizeLinear"
+ }
+ node {
+ input: "y_q"
+ input: "scale"
+ output: "y"
+ name: "DequantizeLinear"
+ op_type: "DequantizeLinear"
+ }
+ name: "test_graph"
+ initializer {
+ dims: 1
+ data_type: 17
+ name: "zero_point"
+ raw_data: "\000"
+ }
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 5 }
+ }
+ }
+ }
+ }
+ input {
+ name: "scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {}
+ }
+ }
+ }
+ input {
+ name: "zero_point"
+ type {
+ tensor_type {
+ elem_type: 17
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {}
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 19
+}
diff --git a/src/frontends/onnx/tests/models/quant_dequant_f8e4m3_blocksize.prototxt b/src/frontends/onnx/tests/models/quant_dequant_f8e4m3_blocksize.prototxt
new file mode 100644
index 00000000000..1afed62e9bf
--- /dev/null
+++ b/src/frontends/onnx/tests/models/quant_dequant_f8e4m3_blocksize.prototxt
@@ -0,0 +1,99 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "scale"
+ input: "zero_point"
+ output: "y_q"
+ name: "QuantizeLinear"
+ op_type: "QuantizeLinear"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 2
+ type: INT
+ }
+ }
+ node {
+ input: "y_q"
+ input: "scale"
+ input: "zero_point"
+ output: "y"
+ name: "DequantizeLinear"
+ op_type: "DequantizeLinear"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 2
+ type: INT
+ }
+ }
+ name: "test_graph"
+ initializer {
+ dims: 2
+ dims: 2
+ data_type: 17
+ name: "zero_point"
+ raw_data: "\000\000\000\000"
+ }
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 4 }
+ }
+ }
+ }
+ }
+ input {
+ name: "scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "zero_point"
+ type {
+ tensor_type {
+ elem_type: 17
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 4 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/models/quant_dequant_f8e4m3_per_axis.prototxt b/src/frontends/onnx/tests/models/quant_dequant_f8e4m3_per_axis.prototxt
new file mode 100644
index 00000000000..e7206e6f0fc
--- /dev/null
+++ b/src/frontends/onnx/tests/models/quant_dequant_f8e4m3_per_axis.prototxt
@@ -0,0 +1,85 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "scale"
+ input: "zero_point"
+ output: "y_q"
+ name: "QuantizeLinear"
+ op_type: "QuantizeLinear"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ }
+ node {
+ input: "y_q"
+ input: "scale"
+ output: "y"
+ name: "DequantizeLinear"
+ op_type: "DequantizeLinear"
+ attribute {
+ name: "axis"
+ i: 1
+ type: INT
+ }
+ }
+ name: "test_graph"
+ initializer {
+ dims: 3
+ data_type: 17
+ name: "zero_point"
+ raw_data: "\000\000\000"
+ }
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+ input {
+ name: "scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+ input {
+ name: "zero_point"
+ type {
+ tensor_type {
+ elem_type: 17
+ shape {
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 19
+}
diff --git a/src/frontends/onnx/tests/models/quant_dequant_f8e5m2.prototxt b/src/frontends/onnx/tests/models/quant_dequant_f8e5m2.prototxt
new file mode 100644
index 00000000000..592aeb17dba
--- /dev/null
+++ b/src/frontends/onnx/tests/models/quant_dequant_f8e5m2.prototxt
@@ -0,0 +1,73 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ input: "scale"
+ input: "zero_point"
+ output: "y_q"
+ name: "QuantizeLinear"
+ op_type: "QuantizeLinear"
+ }
+ node {
+ input: "y_q"
+ input: "scale"
+ output: "y"
+ name: "DequantizeLinear"
+ op_type: "DequantizeLinear"
+ }
+ name: "test_graph"
+ initializer {
+ dims: 1
+ data_type: 19
+ name: "zero_point"
+ raw_data: "\000"
+ }
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 5 }
+ }
+ }
+ }
+ }
+ input {
+ name: "scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ input {
+ name: "zero_point"
+ type {
+ tensor_type {
+ elem_type: 19
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {}
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 19
+}
diff --git a/src/frontends/onnx/tests/onnx_import_quant.in.cpp b/src/frontends/onnx/tests/onnx_import_quant.in.cpp
index 3b60b4c2f76..189969c176d 100644
--- a/src/frontends/onnx/tests/onnx_import_quant.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_quant.in.cpp
@@ -1316,3 +1316,156 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_blocksize) {
test_case.add_expected_output(std::vector<std::uint8_t>{119, 111, 190, 190});
test_case.run();
}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e4m3 - positive values
+///
+/// Expected outputs were verified against ONNX Runtime 1.19
+///
+/// Q-DQ round-trip formula: y = fp8_round(x / scale) * scale
+/// where fp8_round saturates out-of-range values to the fp8 type maximum.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e4m3) {
+ auto model = convert_model("quant_dequant_f8e4m3.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({0.0f, 1.0f, 2.0f, 100000.0f, 200.0f}); // x
+ test_case.add_input<float>({2.0f}); // scale
+
+ // Q: x/2 = [0, 0.5, 1, 50000, 100]
+ // 50000 saturates to 448 (f8e4m3fn max); 100 rounds to 96 (nearest even)
+ // DQ: Q_out * 2 = [0, 1, 2, 896, 192]
+ test_case.add_expected_output<float>({0.0f, 1.0f, 2.0f, 896.0f, 192.0f});
+ test_case.run();
+}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e5m2 - positive values
+///
+/// Expected outputs were verified against ONNX Runtime 1.19
+///
+/// Q-DQ round-trip formula: y = fp8_round(x / scale) * scale
+/// where fp8_round saturates out-of-range values to the fp8 type maximum.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e5m2) {
+ auto model = convert_model("quant_dequant_f8e5m2.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({0.0f, 1.0f, 2.0f, 100000.0f, 200.0f}); // x
+ test_case.add_input<float>({2.0f}); // scale
+
+ // Q: x/2 = [0, 0.5, 1, 50000, 100]
+ // 50000 rounds to 49152 (nearest f8e5m2, within max 57344); 100 rounds to 96
+ // DQ: Q_out * 2 = [0, 1, 2, 98304, 192]
+ test_case.add_expected_output<float>({0.0f, 1.0f, 2.0f, 98304.0f, 192.0f});
+ test_case.run();
+}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e4m3 - negative values
+///
+/// Expected outputs were verified against ONNX Runtime 1.19
+///
+/// Q-DQ round-trip formula: y = fp8_round(x / scale) * scale
+/// where fp8_round saturates out-of-range values to the fp8 type maximum.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e4m3_negative) {
+ auto model = convert_model("quant_dequant_f8e4m3.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-1.0f, -0.5f, 0.0f, 0.5f, 1.0f}); // x
+ test_case.add_input<float>({1.0f}); // scale
+
+ test_case.add_expected_output<float>({-1.0f, -0.5f, 0.0f, 0.5f, 1.0f});
+ test_case.run();
+}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e4m3 - value saturation
+///
+/// Expected outputs were verified against ONNX Runtime 1.19
+///
+/// Q-DQ round-trip formula: y = fp8_round(x / scale) * scale
+/// where fp8_round saturates out-of-range values to the fp8 type maximum.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e4m3_saturation) {
+ auto model = convert_model("quant_dequant_f8e4m3.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-500.0f, -450.0f, -96.0f, 96.0f, 500.0f}); // x
+ test_case.add_input<float>({1.0f}); // scale
+
+ test_case.add_expected_output<float>({-448.0f, -448.0f, -96.0f, 96.0f, 448.0f});
+ test_case.run();
+}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e5m2 - sub-unity scale
+///
+/// Expected outputs were verified against ONNX Runtime 1.19
+///
+/// Q-DQ round-trip formula: y = fp8_round(x / scale) * scale
+/// where fp8_round saturates out-of-range values to the fp8 type maximum.
+///
+/// f8e5m2 with sub-unity scale: scale=0.5 shifts the quantization grid
+/// x/0.5 = [-2, -0.5, 0, 0.5, 2] — all exactly representable in f8e5m2
+/// DQ: Q_out * 0.5 recovers x exactly (lossless for this input)
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e5m2_small_scale) {
+ auto model = convert_model("quant_dequant_f8e5m2.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({-1.0f, -0.25f, 0.0f, 0.25f, 1.0f}); // x
+ test_case.add_input<float>({0.5f}); // scale
+
+ // x/0.5 = [-2, -0.5, 0, 0.5, 2]: exact in f8e5m2 - Q_out * 0.5 = x
+ test_case.add_expected_output<float>({-1.0f, -0.25f, 0.0f, 0.25f, 1.0f});
+ test_case.run();
+}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e4m3 - per-axis mode
+///
+/// Per-axis quantization (axis=1) on input shape (2, 3):
+/// scale has shape (3,) — one scale per column.
+/// y[r,c] = fp8_round(x[r,c] / scale[c]) * scale[c]
+///
+/// With scale = [2.0, 4.0, 8.0]:
+/// x / scale (broadcast per column):
+/// row 0: [4/2, 8/4, 16/8] = [2.0, 2.0, 2.0] -- exact in f8e4m3
+/// row 1: [6/2, 1000/4, 4000/8] = [3.0, 250.0, 500.0]
+///
+/// f8e4m3 rounding (max = 448.0):
+/// 250.0 - nearest representable is 256.0 (distance 240-250 is 10, 250-256 is 6)
+/// 500.0 - saturates to 448.0 (exceeds f8e4m3fn max)
+///
+/// DQ (multiply by per-column scale):
+/// row 0: [2×2, 2×4, 2×8] = [ 4.0, 8.0, 16.0]
+/// row 1: [3×2, 256×4, 448×8] = [ 6.0, 1024.0, 3584.0]
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e4m3_per_axis) {
+ auto model = convert_model("quant_dequant_f8e4m3_per_axis.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({4.0f, 8.0f, 16.0f, 6.0f, 1000.0f, 4000.0f}); // x, shape (2, 3), row-major
+ test_case.add_input<float>({2.0f, 4.0f, 8.0f}); // scale, shape (3,)
+
+ test_case.add_expected_output<float>({2, 3}, std::vector<float>{4.0f, 8.0f, 16.0f, 6.0f, 1024.0f, 3584.0f});
+ test_case.run();
+}
+
+/// 8-bit floating point QuantizeLinear - DequantizeLinear test - f8e4m3 - blocked quantization mode
+///
+/// Blocked quantization (axis=1, block_size=2) on input shape (2, 4):
+/// scale shape is (2, ceil(4/2)) = (2, 2) — one independent scale per 2-element block.
+/// y[r, c] = fp8_round(x[r, c] / scale[r, c/block_size]) * scale[r, c/block_size]
+///
+/// scale = [[2.0, 4.0], x = [[ 2.0, 6.0, 8.0, 500.0],
+/// [1.0, 4.0]] [ 1.0, 3.0, 56.0, 2000.0]]
+///
+/// row 0, block 0 (cols 0-1), scale=2.0: x/s = [1.0, 3.0] - exact - DQ [2.0, 6.0]
+/// row 0, block 1 (cols 2-3), scale=4.0: x/s = [2.0, 125.0] - 125.0 rounds to 128.0
+/// (|125-120|=5 vs |125-128|=3, 128 is nearer) - DQ [8.0, 512.0]
+///
+/// row 1, block 0 (cols 0-1), scale=1.0: x/s = [1.0, 3.0] - exact - DQ [1.0, 3.0]
+/// row 1, block 1 (cols 2-3), scale=4.0: x/s = [14.0, 500.0] - 14.0 exact;
+/// 500.0 > 448.0 (f8e4m3fn max) - saturates - DQ [56.0, 1792.0]
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_quantize_linear_f8e4m3_blocksize) {
+ auto model = convert_model("quant_dequant_f8e4m3_blocksize.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>({2.0f, 6.0f, 8.0f, 500.0f, 1.0f, 3.0f, 56.0f, 2000.0f}); // x, shape (2, 4), row-major
+ test_case.add_input<float>({2.0f, 4.0f, 1.0f, 4.0f}); // scale, shape (2, 2), row-major
+
+ test_case.add_expected_output<float>({2, 4},
+ std::vector<float>{2.0f, 6.0f, 8.0f, 512.0f, 1.0f, 3.0f, 56.0f, 1792.0f});
+ test_case.run();
+}