Skip to content

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

[CPU] Round integer Clamp bounds with ceil and floor in JIT emitters (#39711)

Arshil Ahmed53c97bf388a2
diff --git a/src/plugins/intel_cpu/src/emitters/utils.cpp b/src/plugins/intel_cpu/src/emitters/utils.cpp
index d7fcb0cbea0..4bfa703b9d3 100644
--- a/src/plugins/intel_cpu/src/emitters/utils.cpp
+++ b/src/plugins/intel_cpu/src/emitters/utils.cpp
@@ -5,6 +5,7 @@
#include "utils.hpp"
#include <algorithm>
+#include <cmath>
#include <common/utils.hpp>
#include <cstddef>
#include <cstdint>
@@ -83,10 +84,11 @@ std::pair<int32_t, int32_t> get_clamp_min_max(double alpha, double beta, const o
case ov::element::i32: {
// Clamp alpha/beta to the int32_t range in double space before casting:
// casting an out-of-range double to an integral type is undefined behavior.
+ // For integral types the spec converts min with ceil and max with floor.
constexpr auto i32_min = static_cast<double>(std::numeric_limits<int32_t>::min());
constexpr auto i32_max = static_cast<double>(std::numeric_limits<int32_t>::max());
- minimum = static_cast<int32_t>(std::clamp(alpha, i32_min, i32_max));
- maximum = static_cast<int32_t>(std::clamp(beta, i32_min, i32_max));
+ minimum = static_cast<int32_t>(std::clamp(std::ceil(alpha), i32_min, i32_max));
+ maximum = static_cast<int32_t>(std::clamp(std::floor(beta), i32_min, i32_max));
break;
}
case ov::element::f32:
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp
index 694358b70c7..78d359495b1 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include <algorithm>
#include <cstdint>
#include <limits>
#include <vector>
@@ -35,6 +36,13 @@ const std::vector<std::pair<double, double>> intervals_unsigned = {
{10.6, 20.6}
};
+// Fractional bounds within the generated input range [0, 15]: integral types round min up and max down
+const std::vector<std::pair<double, double>> intervals_8bit = {
+ {2.5, 10.5},
+ {0.4, 7.6},
+ {3.0, 12.0}
+};
+
const std::vector<ov::element::Type> model_type = {
ov::element::f32,
ov::element::f16,
@@ -64,9 +72,17 @@ const auto test_Clamp_i64 = ::testing::Combine(
::testing::Values(ov::test::utils::DEVICE_CPU)
);
+const auto test_Clamp_8bit = ::testing::Combine(
+ ::testing::ValuesIn(ov::test::static_shapes_to_test_representation(input_shapes_static)),
+ ::testing::ValuesIn(intervals_8bit),
+ ::testing::Values(ov::element::i8, ov::element::u8),
+ ::testing::Values(ov::test::utils::DEVICE_CPU)
+);
+
INSTANTIATE_TEST_SUITE_P(smoke_TestsClamp_signed, ClampLayerTest, test_Clamp_signed, ClampLayerTest::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_TestsClamp_unsigned, ClampLayerTest, test_Clamp_unsigned, ClampLayerTest::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_TestsClamp_i64, ClampLayerTest, test_Clamp_i64, ClampLayerTest::getTestCaseName);
+INSTANTIATE_TEST_SUITE_P(smoke_TestsClamp_8bit, ClampLayerTest, test_Clamp_8bit, ClampLayerTest::getTestCaseName);
TEST(smoke_Clamp, Int32MaxMinusOneBoundIsPreserved) {
const auto maximum = std::numeric_limits<int32_t>::max();
@@ -88,4 +104,23 @@ TEST(smoke_Clamp, Int32MaxMinusOneBoundIsPreserved) {
ASSERT_EQ(output.data<const int32_t>()[0], bound);
ASSERT_EQ(output.data<const int32_t>()[1], bound);
}
+
+TEST(smoke_Clamp, Int8NegativeFractionalBoundsAreRounded) {
+ const std::vector<int8_t> values = {-6, -5, -4, -2, -1, 0, 3};
+ const std::vector<int8_t> expected = {-4, -4, -4, -2, -2, -2, -2}; // min = ceil(-4.5), max = floor(-1.5)
+ const auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::i8, ov::Shape{values.size()});
+ const auto clamp = std::make_shared<ov::op::v0::Clamp>(input, -4.5, -1.5);
+ const auto model = std::make_shared<ov::Model>(clamp, ov::ParameterVector{input});
+
+ ov::Core core;
+ auto compiledModel = core.compile_model(model, ov::test::utils::DEVICE_CPU);
+ auto inferRequest = compiledModel.create_infer_request();
+ ov::Tensor inputTensor(ov::element::i8, ov::Shape{values.size()});
+ std::copy(values.begin(), values.end(), inputTensor.data<int8_t>());
+ inferRequest.set_input_tensor(inputTensor);
+ inferRequest.infer();
+
+ const auto output = inferRequest.get_output_tensor();
+ ASSERT_EQ(std::vector<int8_t>(output.data<const int8_t>(), output.data<const int8_t>() + values.size()), expected);
+}
} // namespace