Skip to content

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

[Transformations] Relax conversion to fp16 relative error (#36005)

Mikhail Ryzhov55076c4014d8
diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hpp
index 0cca19676f5..9e8b0a6592d 100644
--- a/src/common/transformations/include/transformations/utils/utils.hpp
+++ b/src/common/transformations/include/transformations/utils/utils.hpp
@@ -107,8 +107,7 @@ bool has_constant_value(const std::shared_ptr<Node>& node,
return false;
}
- const bool is_scalar_or_single_elem = is_scalar(constant->get_shape()) || shape_size(constant->get_shape()) == 1;
- if (!is_scalar_or_single_elem) {
+ if (!is_scalar_or_single_elem_constant(constant)) {
return false;
}
diff --git a/src/common/transformations/src/transformations/common_optimizations/compress_float_constants.cpp b/src/common/transformations/src/transformations/common_optimizations/compress_float_constants.cpp
index 716d9e3b855..7bef307e665 100644
--- a/src/common/transformations/src/transformations/common_optimizations/compress_float_constants.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/compress_float_constants.cpp
@@ -24,6 +24,7 @@
#include "transformations/rt_info/decompression.hpp"
#include "transformations/rt_info/disable_fp16_compression.hpp"
#include "transformations/rt_info/old_api_map_element_type_attribute.hpp"
+#include "transformations/utils/utils.hpp"
namespace v0 = ov::op::v0;
namespace v1 = ov::op::v1;
@@ -45,7 +46,7 @@ namespace {
/// `f16_compression_max_abs_error` (RoPE-like high-magnitude values), or
/// * the combined count of elements outside the finite FP16 range *plus*
/// elements whose relative round-trip error exceeds
-/// `f16_compression_max_rel_error` reaches
+/// the selected relative-error threshold reaches
/// `f16_compression_keep_threshold` (75% by default).
///
/// Used by `CompressFloatConstantsImpl` for f64 constants on all platforms and
@@ -56,6 +57,10 @@ std::shared_ptr<ov::Node> change_constant_precision_to_fp16(std::shared_ptr<v0::
bool postponed = false) {
using src_type = typename ov::element_type_traits<PREC_FROM>::value_type;
+ const bool is_scalar_or_single_elem = ov::op::util::is_scalar_or_single_elem_constant(constant);
+ const double max_relative_error = is_scalar_or_single_elem ? ov::reference::f16_scalar_compression_max_rel_error
+ : ov::reference::f16_tensor_compression_max_rel_error;
+
const auto* src_data = constant->get_data_ptr<src_type>();
const auto size = ov::shape_size(constant->get_shape());
@@ -77,7 +82,6 @@ std::shared_ptr<ov::Node> change_constant_precision_to_fp16(std::shared_ptr<v0::
dst_data[i] = std::numeric_limits<ov::float16>::lowest();
num_rejected++;
} else {
- constexpr double max_relative_error = ov::reference::f16_compression_max_rel_error;
constexpr double max_abs_error = ov::reference::f16_compression_max_abs_error;
const auto f16_val = static_cast<ov::float16>(src_data[i]);
const double src_val = static_cast<double>(src_data[i]);
@@ -97,7 +101,7 @@ std::shared_ptr<ov::Node> change_constant_precision_to_fp16(std::shared_ptr<v0::
// If the combined count of rejected elements (values outside the finite FP16
// range PLUS in-range values whose FP16 round-trip relative error exceeds
- // f16_compression_max_rel_error) reaches f16_compression_keep_threshold
+ // the selected relative-error threshold) reaches f16_compression_keep_threshold
// (inclusive, 75% by default), keep the Constant in its original precision.
const double rejected_proportion = static_cast<double>(num_rejected) / static_cast<double>(size);
@@ -214,7 +218,7 @@ CompressFloatConstantsImpl::CompressFloatConstantsImpl(bool postponed) {
// If the combined count of rejected elements (values outside the finite FP16
// range PLUS in-range values whose FP16 round-trip relative error exceeds
- // f16_compression_max_rel_error) reaches f16_compression_keep_threshold
+ // the selected relative-error threshold) reaches f16_compression_keep_threshold
// (inclusive, 75% by default), keep the Constant in FP32.
const float rejected_proportion = static_cast<float>(check.rejected_count) / static_cast<float>(size);
if (rejected_proportion >= ov::reference::f16_compression_keep_threshold)
diff --git a/src/common/transformations/tests/common_optimizations/compress_float_constants_test.cpp b/src/common/transformations/tests/common_optimizations/compress_float_constants_test.cpp
index a7d7050b436..c65ed87d5f9 100644
--- a/src/common/transformations/tests/common_optimizations/compress_float_constants_test.cpp
+++ b/src/common/transformations/tests/common_optimizations/compress_float_constants_test.cpp
@@ -605,15 +605,34 @@ TEST_F(TransformationTestsF, KeepFWPrecisionForBF16Constants_test_1) {
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}
-// Scalar constant with high FP16 relative rounding error (e.g., log(16) = 2.7725887,
-// rel_error = 0.031%) should NOT be compressed to FP16 to avoid precision degradation
-// that cascades through deep networks.
-TEST_F(TransformationTestsF, CompressConstants_skip_scalar_with_high_f16_error) {
- // Model: Parameter -> Multiply(input, Const(2.7725887)) -> Result
- // Scalar 2.7725887 has rel_error > 1e-4 when rounded to FP16 -> stays in FP32.
+struct RelativeThresholdTestParams {
+ // Input shape for Multiply.
+ ov::Shape input_shape;
+ // Constant shape controls whether scalar or tensor relative threshold is used.
+ ov::Shape constant_shape;
+ // Repeated value used to build constant tensor.
+ float value;
+ // Expected transform result: FP16+Convert when true, keep FP32 when false.
+ bool expect_compressed;
+ // Stable, readable gtest suffix.
+ std::string test_name;
+};
+
+class CompressConstantsRelativeThresholdTest : public TransformationTestsF,
+ public testing::WithParamInterface<RelativeThresholdTestParams> {
+public:
+ static std::string get_test_name(const testing::TestParamInfo<RelativeThresholdTestParams>& obj) {
+ return obj.param.test_name;
+ }
+};
+
+TEST_P(CompressConstantsRelativeThresholdTest, HandlesRelativeThresholdByConstantShape) {
+ const auto& param = GetParam();
+ const auto values = std::vector<float>(shape_size(param.constant_shape), param.value);
+
{
- auto input = std::make_shared<ov::opset8::Parameter>(ov::element::f32, ov::Shape{1, 3, 4});
- auto scale = v0::Constant::create(ov::element::f32, ov::Shape{1}, {2.7725887f});
+ auto input = std::make_shared<ov::opset8::Parameter>(ov::element::f32, param.input_shape);
+ auto scale = v0::Constant::create(ov::element::f32, param.constant_shape, values);
auto mul = std::make_shared<ov::opset8::Multiply>(input, scale);
model = std::make_shared<ov::Model>(ov::OutputVector{mul}, ov::ParameterVector{input});
@@ -622,15 +641,55 @@ TEST_F(TransformationTestsF, CompressConstants_skip_scalar_with_high_f16_error)
}
{
- auto input = std::make_shared<ov::opset8::Parameter>(ov::element::f32, ov::Shape{1, 3, 4});
- // Constant stays in FP32 — FP16 relative error exceeds threshold
- auto scale = v0::Constant::create(ov::element::f32, ov::Shape{1}, {2.7725887f});
- auto mul = std::make_shared<ov::opset8::Multiply>(input, scale);
+ auto input = std::make_shared<ov::opset8::Parameter>(ov::element::f32, param.input_shape);
+ auto element_type = param.expect_compressed ? ov::element::f16 : ov::element::f32;
+ auto scale = v0::Constant::create(element_type, param.constant_shape, values);
+ std::shared_ptr<ov::Node> scale_node = scale;
+ if (param.expect_compressed) {
+ scale_node = std::make_shared<v0::Convert>(scale, ov::element::f32);
+ }
+ auto mul = std::make_shared<ov::opset8::Multiply>(input, scale_node);
model_ref = std::make_shared<ov::Model>(ov::OutputVector{mul}, ov::ParameterVector{input});
}
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}
+INSTANTIATE_TEST_SUITE_P(
+ CompressConstantsRelativeThreshold,
+ CompressConstantsRelativeThresholdTest,
+ testing::Values(
+ // Scalar constant with high FP16 relative rounding error (e.g., log(16) = 2.7725887,
+ // rel_error = 0.031%) should NOT be compressed to FP16.
+ RelativeThresholdTestParams{ov::Shape{1, 3, 4}, ov::Shape{1}, 2.7725887f, false, "ScalarRejectsTightThreshold"},
+ // Negative counterpart for scalar rejection.
+ RelativeThresholdTestParams{ov::Shape{1, 3, 4},
+ ov::Shape{1},
+ -2.7725887f,
+ false,
+ "ScalarNegativeRejectsTightThreshold"},
+ // Non-scalar uses looser tensor relative threshold and should be compressed for this value.
+ RelativeThresholdTestParams{ov::Shape{1, 4}, ov::Shape{4}, 2.7725887f, true, "TensorAcceptsLooserThreshold"},
+ // Negative counterpart for tensor acceptance.
+ RelativeThresholdTestParams{ov::Shape{1, 4},
+ ov::Shape{4},
+ -2.7725887f,
+ true,
+ "TensorNegativeAcceptsLooserThreshold"},
+ // Non-scalar with high per-element relative error should stay FP32.
+ // The keep-threshold comparison is inclusive (>= f16_compression_keep_threshold).
+ RelativeThresholdTestParams{ov::Shape{1, 4},
+ ov::Shape{4},
+ 1.0e-5f,
+ false,
+ "TensorRejectsWhenAboveTensorThreshold"},
+ // Negative counterpart for tensor rejection.
+ RelativeThresholdTestParams{ov::Shape{1, 4},
+ ov::Shape{4},
+ -1.0e-5f,
+ false,
+ "TensorNegativeRejectsWhenAboveTensorThreshold"}),
+ CompressConstantsRelativeThresholdTest::get_test_name);
+
// Scalar constant exactly representable in FP16 (e.g., 8.0) should be compressed normally.
TEST_F(TransformationTestsF, CompressConstants_compress_scalar_exact_f16) {
// Model: Parameter -> Multiply(input, Const(8.0)) -> Result
@@ -656,32 +715,6 @@ TEST_F(TransformationTestsF, CompressConstants_compress_scalar_exact_f16) {
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}
-TEST_F(TransformationTestsF, CompressConstants_skip_non_scalar_with_high_error_at_least_75) {
- // Model: Parameter -> Multiply(input, Const({2.7725887, 2.7725887, 2.7725887, 2.7725887})) -> Result
- // Non-scalar (numel=4) with high per-element error in at least 75% of elements -> skipped (not compressed).
- // The keep-threshold comparison is inclusive (>= f16_compression_keep_threshold).
- {
- auto input = std::make_shared<ov::opset8::Parameter>(ov::element::f32, ov::Shape{1, 4});
- auto scale =
- v0::Constant::create(ov::element::f32, ov::Shape{4}, {2.7725887f, 2.7725887f, 2.7725887f, 2.7725887f});
- auto mul = std::make_shared<ov::opset8::Multiply>(input, scale);
- model = std::make_shared<ov::Model>(ov::OutputVector{mul}, ov::ParameterVector{input});
-
- manager.register_pass<ov::pass::MarkPrecisionSensitiveConstants>();
- manager.register_pass<ov::pass::CompressFloatConstants>();
- }
-
- {
- auto input = std::make_shared<ov::opset8::Parameter>(ov::element::f32, ov::Shape{1, 4});
- // Stays FP32: relative round-trip error exceeds threshold for all 4 elements (>75%).
- auto scale =
- v0::Constant::create(ov::element::f32, ov::Shape{4}, {2.7725887f, 2.7725887f, 2.7725887f, 2.7725887f});
- auto mul = std::make_shared<ov::opset8::Multiply>(input, scale);
- model_ref = std::make_shared<ov::Model>(ov::OutputVector{mul}, ov::ParameterVector{input});
- }
- comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
-}
-
// Non-scalar constant with high absolute FP16 error (e.g. RoPE frequency table)
// should NOT be compressed — value 5002.0 has FP16 abs error = 2.0 (ULP=4 in [4096,8192) range).
TEST_F(TransformationTestsF, CompressConstants_skip_non_scalar_with_high_abs_error) {
diff --git a/src/core/reference/include/openvino/reference/convert.hpp b/src/core/reference/include/openvino/reference/convert.hpp
index dc823b54628..68ec1c72630 100644
--- a/src/core/reference/include/openvino/reference/convert.hpp
+++ b/src/core/reference/include/openvino/reference/convert.hpp
@@ -82,27 +82,33 @@ void convert<bfloat16, float>(const bfloat16* arg, float* out, size_t count);
template <>
void convert<int32_t, float16>(const int32_t* arg, float16* out, size_t count);
-/// Maximum tolerated |abs(src - round_trip_f16(src))| for an in-range element.
+/// @brief Maximum tolerated |abs(src - round_trip_f16(src))| for an in-range element.
/// If any element exceeds this threshold, the whole tensor is kept in its
/// original precision (FP32 / FP64 — acts as a tensor-wide veto in
/// check_f16_compression()).
inline constexpr double f16_compression_max_abs_error = 1.0;
-/// Maximum tolerated relative round-trip error abs_diff / |src| for an
-/// in-range element. Elements above this threshold are accumulated into the
-/// combined rejection count used together with f16_compression_keep_threshold.
-inline constexpr double f16_compression_max_rel_error = 1e-4;
-/// Proportion of combined rejections (out-of-FP16-range + high relative-error)
+/// @brief Maximum tolerated relative round-trip error abs_diff / |src| for an in-range element when compressing a
+/// single scalar value. Used by the f16 scalar compression path (e.g. for RoPE constants), which is more sensitive to
+/// precision
+inline constexpr double f16_scalar_compression_max_rel_error = 1e-4;
+/// @brief Maximum tolerated relative round-trip error abs_diff / |src| for an in-range element when compressing a
+/// whole tensor. Elements above this threshold are accumulated into the combined rejection count used together with
+/// f16_compression_keep_threshold. This is a looser threshold than f16_scalar_compression_max_rel_error to allow more
+/// aggressive compression at the tensor level, where individual high-relative-error elements may be "diluted" by many
+/// low-relative-error elements.
+inline constexpr double f16_tensor_compression_max_rel_error = 1e-3;
+/// @brief Proportion of combined rejections (out-of-FP16-range + high relative-error)
/// at which the Constant is kept in its original precision (FP32 / FP64), i.e.
/// no FP16 compression is applied.
inline constexpr float f16_compression_keep_threshold = 0.75f;
-/// Result of a single-pass FP16-compression feasibility check produced by
+/// @brief Result of a single-pass FP16-compression feasibility check produced by
/// check_f16_compression(). Used by CompressFloatConstantsImpl to decide
/// whether a floating-point Constant may be safely compressed to FP16.
struct CompressionCheckResult {
/// Combined count of rejected elements: values outside the finite FP16
/// range PLUS in-range values whose FP16 conversion exceeds
- /// f16_compression_max_rel_error. Compared against
+ /// f16_tensor_compression_max_rel_error. Compared against
/// f16_compression_keep_threshold to keep the Constant in its original
/// precision. For a pure out-of-range count, use count_out_of_f16_range().
size_t rejected_count;
@@ -119,8 +125,12 @@ struct CompressionCheckResult {
/// CompressionCheckResult::has_lossy. Otherwise accumulates a combined
/// rejection count (out-of-FP16-range + high relative-error) in
/// CompressionCheckResult::rejected_count, to be compared against
-/// f16_compression_keep_threshold. JIT/AVX-512 (or AVX2+F16C) accelerated on
-/// x86; falls back to a scalar loop elsewhere.
+/// f16_compression_keep_threshold. The relative-error threshold is selected
+/// internally from the input size: single-element inputs use
+/// f16_scalar_compression_max_rel_error, while larger tensors use
+/// f16_tensor_compression_max_rel_error.
+/// JIT/AVX-512 (or AVX2+F16C) accelerated on x86 for tensors; single-element
+/// inputs fall back to a scalar loop.
CompressionCheckResult check_f16_compression(const float* arg, size_t count);
/// Counts elements in `arg` that fall outside the finite FP16 range (subnormal
diff --git a/src/core/reference/src/op/convert.cpp b/src/core/reference/src/op/convert.cpp
index 7a87da29a69..91b23ee85f4 100644
--- a/src/core/reference/src/op/convert.cpp
+++ b/src/core/reference/src/op/convert.cpp
@@ -37,8 +37,7 @@ inline const float kF16MaxPos = std::numeric_limits<ov::float16>::max();
inline const float kF16MaxNeg = std::numeric_limits<ov::float16>::lowest();
inline const float kF16MinPos = ov::float16::from_bits(0x0001);
inline const float kF16MinNeg = -ov::float16::from_bits(0x0001);
-inline constexpr float kF16CompressionAbsErrVal = static_cast<float>(ov::reference::f16_compression_max_abs_error);
-inline constexpr float kF16CompressionRelErrVal = static_cast<float>(ov::reference::f16_compression_max_rel_error);
+
inline constexpr uint32_t kAbsMaskVal = 0x7FFFFFFFu;
template <typename src_t, typename dst_t, bool clamp = false>
@@ -412,6 +411,8 @@ public:
void* rejected_dst; // size_t* — output: combined out-of-range + high-relative-error count
void* lossy_dst; // size_t* — output: lossy count (0 or non-zero)
const size_t count;
+ float max_relative_error;
+ float max_abs_error;
} args_t;
typedef void (*fn_t)(const args_t*);
@@ -483,8 +484,6 @@ private:
bcast(f16_max_neg_vec, &kF16MaxNeg);
bcast(f16_min_pos_vec, &kF16MinPos);
bcast(f16_min_neg_vec, &kF16MinNeg);
- bcast(abs_err_vec, &kF16CompressionAbsErrVal);
- bcast(rel_err_vec, &kF16CompressionRelErrVal);
bcast(abs_mask_vec, &kAbsMaskVal);
vpxorq(zero_vec, zero_vec, zero_vec);
@@ -493,6 +492,8 @@ private:
mov(reg_rejected_dst, ptr[param + offsetof(args_t, rejected_dst)]);
mov(reg_lossy_dst, ptr[param + offsetof(args_t, lossy_dst)]);
mov(reg_count, ptr[param + offsetof(args_t, count)]);
+ vbroadcastss(rel_err_vec, dword[param + offsetof(args_t, max_relative_error)]);
+ vbroadcastss(abs_err_vec, dword[param + offsetof(args_t, max_abs_error)]);
constexpr uint8_t cmp_lt_os = 1;
constexpr uint8_t cmp_neq_uq = 4;
@@ -537,7 +538,7 @@ private:
kortestw(k_lossy, k_lossy);
jnz(lossy_exit, T_NEAR);
- // === Relative-error check: (abs_diff > |data| * 1e-4) AND NOT k_oor ===
+ // === Relative-error check: (abs_diff > |data| * max_relative_error) AND NOT k_oor ===
vpandd(abs_data_vec, data_vec, abs_mask_vec);
vmulps(rel_thresh_vec, abs_data_vec, rel_err_vec);
vcmpps(k_rel, diff_vec, rel_thresh_vec, cmp_gt_os);
@@ -631,6 +632,8 @@ public:
void* rejected_dst; // size_t* — output: combined out-of-range + high-relative-error count
void* lossy_dst; // size_t* — output: lossy count (0 or non-zero)
const size_t count;
+ float max_relative_error;
+ float max_abs_error;
} args_t;
typedef void (*fn_t)(const args_t*);
@@ -688,7 +691,7 @@ private:
mov(reg_saved_rsp, rsp); // save rsp for lossy exit stack cleanup
// --- Load constants ---
- // 256-bit (8-lane) broadcast arrays for AVX2 (no vbroadcastss on ymm here; we use vmovdqu).
+ // 256-bit (8-lane) broadcast arrays for AVX2.
// Arrays filled from FP16-derived constants (non-constexpr — see kF16* above) are static const;
// arrays filled from literal/constexpr thresholds are static constexpr.
static const float max_pos_bounds[8] =
@@ -700,22 +703,6 @@ private:
static const float min_neg_bounds[8] =
{kF16MinNeg, kF16MinNeg, kF16MinNeg, kF16MinNeg, kF16MinNeg, kF16MinNeg, kF16MinNeg, kF16MinNeg};
static constexpr int32_t i32_ones[8] = {1, 1, 1, 1, 1, 1, 1, 1};
- static constexpr float abs_errs[8] = {kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal,
- kF16CompressionAbsErrVal};
- static constexpr float rel_errs[8] = {kF16CompressionRelErrVal,
- kF16CompressionRelErrVal,
- kF16CompressionRelErrVal,
- kF16CompressionRelErrVal,
- kF16CompressionRelErrVal,
- kF16CompressionRelErrVal,
- kF16CompressionRelErrVal,
- kF16CompressionRelErrVal};
static constexpr uint32_t abs_masks[8] =
{kAbsMaskVal, kAbsMaskVal, kAbsMaskVal, kAbsMaskVal, kAbsMaskVal, kAbsMaskVal, kAbsMaskVal, kAbsMaskVal};
@@ -729,8 +716,6 @@ private:
load_vec(f16_min_pos_vec, reinterpret_cast<size_t>(min_pos_bounds));
load_vec(f16_min_neg_vec, reinterpret_cast<size_t>(min_neg_bounds));
load_vec(i32_ones_vec, reinterpret_cast<size_t>(i32_ones));
- load_vec(abs_err_vec, reinterpret_cast<size_t>(abs_errs));
- load_vec(rel_err_vec, reinterpret_cast<size_t>(rel_errs));
load_vec(abs_mask_vec, reinterpret_cast<size_t>(abs_masks));
vxorps(f16_zero_vec, f16_zero_vec, f16_zero_vec);
vxorps(rejected_accum, rejected_accum, rejected_accum);
@@ -740,6 +725,8 @@ private:
mov(reg_rejected_dst, ptr[param + offsetof(args_t, rejected_dst)]);
mov(reg_lossy_dst, ptr[param + offsetof(args_t, lossy_dst)]);
mov(reg_sz, ptr[param + offsetof(args_t, count)]);
+ vbroadcastss(rel_err_vec, dword[param + offsetof(args_t, max_relative_error)]);
+ vbroadcastss(abs_err_vec, dword[param + offsetof(args_t, max_abs_error)]);
constexpr uint8_t cmp_lt_os = 1;
constexpr uint8_t cmp_neq_uq = 4;
@@ -784,11 +771,11 @@ private:
vtestps(tmp_vec, tmp_vec); // ZF=1 if all zeros
jnz(exit_lossy, T_NEAR); // bail if any lossy
- // === Relative error check: count elements where |diff| > |value| * 1e-4 ===
- // (equivalent to abs_diff / |value| > 1e-4, but avoids division)
+ // === Relative error check: count elements where |diff| > |value| * max_relative_error ===
+ // (equivalent to abs_diff / |value| > max_relative_error, but avoids division)
vandps(tmp_vec, data_vec, abs_mask_vec); // tmp_vec = |data|
- vmulps(tmp_vec, tmp_vec, rel_err_vec); // tmp_vec = |data| * 1e-4
- vcmpps(tmp_vec, diff_vec, tmp_vec, cmp_gt_os); // 1 where |diff| > |data|*1e-4
+ vmulps(tmp_vec, tmp_vec, rel_err_vec); // tmp_vec = |data| * max_relative_error
+ vcmpps(tmp_vec, diff_vec, tmp_vec, cmp_gt_os); // 1 where |diff| > |data| * max_relative_error
vandnps(tmp_vec, mask_vec, tmp_vec); // exclude OOR (avoid double-count)
vorps(mask_vec, mask_vec, tmp_vec); // combine OOR + relative-error
@@ -950,10 +937,14 @@ namespace {
// marshal its two size_t outputs into a CompressionCheckResult. Shared by
// both JIT dispatches in check_f16_compression().
template <typename Jit>
-CompressionCheckResult run_check_f16_compression_jit(typename Jit::fn_t fn, const float* arg, size_t count) {
+CompressionCheckResult run_check_f16_compression_jit(typename Jit::fn_t fn,
+ const float* arg,
+ size_t count,
+ float max_relative_error,
+ float max_abs_error) {
CompressionCheckResult result{0, false};
size_t lossy_count = 0;
- typename Jit::args_t args = {arg, &result.rejected_count, &lossy_count, count};
+ typename Jit::args_t args = {arg, &result.rejected_count, &lossy_count, count, max_relative_error, max_abs_error};
fn(&args);
result.has_lossy = lossy_count > 0;
return result;
@@ -962,13 +953,25 @@ CompressionCheckResult run_check_f16_compression_jit(typename Jit::fn_t fn, cons
#endif // OV_CORE_USE_XBYAK_JIT
CompressionCheckResult check_f16_compression(const float* arg, size_t count) {
+ const double max_relative_error =
+ count == 1 ? f16_scalar_compression_max_rel_error : f16_tensor_compression_max_rel_error;
+ const double max_abs_error = f16_compression_max_abs_error;
#ifdef OV_CORE_USE_XBYAK_JIT
if (util::may_i_use_dynamic_code()) {
if (auto fn = jit_check_f16_compression_avx512::get()) {
- return run_check_f16_compression_jit<jit_check_f16_compression_avx512>(fn, arg, count);
+ return run_check_f16_compression_jit<jit_check_f16_compression_avx512>(
+ fn,
+ arg,
+ count,
+ static_cast<float>(max_relative_error),
+ static_cast<float>(max_abs_error));
}
if (auto fn = jit_check_f16_compression::get()) {
- return run_check_f16_compression_jit<jit_check_f16_compression>(fn, arg, count);
+ return run_check_f16_compression_jit<jit_check_f16_compression>(fn,
+ arg,
+ count,
+ static_cast<float>(max_relative_error),
+ static_cast<float>(max_abs_error));
}
}
#endif // OV_CORE_USE_XBYAK_JIT
@@ -980,10 +983,10 @@ CompressionCheckResult check_f16_compression(const float* arg, size_t count) {
} else {
const double roundtripped = static_cast<double>(static_cast<float>(static_cast<float16>(v)));
const double abs_diff = std::abs(v - roundtripped);
- if (abs_diff > f16_compression_max_abs_error) {
+ if (abs_diff > max_abs_error) {
return {0, true};
}
- if (v != 0.0f && abs_diff / std::abs(v) > f16_compression_max_rel_error) {
+ if (v != 0.0f && abs_diff / std::abs(v) > max_relative_error) {
++result.rejected_count;
}
}