Skip to content

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

[CPU] Fix bf16 saturation handling for EltwisePowerStatic parameters (#32558)

Bo Liucac315b38192
diff --git a/src/plugins/intel_cpu/src/nodes/eltwise.cpp b/src/plugins/intel_cpu/src/nodes/eltwise.cpp
index cea2abc0226..239e27d79d1 100644
--- a/src/plugins/intel_cpu/src/nodes/eltwise.cpp
+++ b/src/plugins/intel_cpu/src/nodes/eltwise.cpp
@@ -546,23 +546,25 @@ bool Eltwise::isWithBroadcast() {
}
void Eltwise::init() {
- // Bf16 saturation handling for gamma parameter when input precision is bf16 to make sure it stays within the valid
- // range for bfloat16.
+ // Bf16 saturation handling for PowerStatic parameters
+ // to make sure they stay within the valid range for bfloat16.
if (m_attrs.data.algo == Algorithm::EltwisePowerStatic && getOriginalInputPrecisionAtPort(0) == ov::element::bf16) {
- const float lowest = static_cast<float>(std::numeric_limits<ov::bfloat16>::lowest());
- const float max = static_cast<float>(std::numeric_limits<ov::bfloat16>::max());
- auto& gamma = m_attrs.data.gamma;
+ const float bf16_lowest = static_cast<float>(std::numeric_limits<ov::bfloat16>::lowest());
+ const float bf16_max = static_cast<float>(std::numeric_limits<ov::bfloat16>::max());
- if (gamma < lowest) {
- gamma = lowest;
- }
+ // Helper lambda to clamp parameter values within bf16 range
+ auto clampBf16Parameter = [&](auto& param) {
+ if (std::isfinite(param)) {
+ param = std::clamp(static_cast<float>(param), bf16_lowest, bf16_max);
+ }
+ };
- if (gamma > max) {
- gamma = max;
- }
+ // Clamp all PowerStatic parameters
+ clampBf16Parameter(m_attrs.data.alpha);
+ clampBf16Parameter(m_attrs.data.beta);
+ clampBf16Parameter(m_attrs.data.gamma);
}
}
-
void Eltwise::getSupportedDescriptors() {
CPU_NODE_ASSERT(!getParentEdges().empty(), "Incorrect number of input edges");
CPU_NODE_ASSERT(!getChildEdges().empty(), "Incorrect number of output edges");
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/powerstatic_bf16_saturation.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/powerstatic_bf16_saturation.cpp
index 402463eee4f..4189d133cf2 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/powerstatic_bf16_saturation.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/powerstatic_bf16_saturation.cpp
@@ -6,8 +6,10 @@
#include "internal_properties.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/concat.hpp"
+#include "openvino/op/convert.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/multiply.hpp"
+#include "openvino/op/subtract.hpp"
#include "shared_test_classes/base/ov_subgraph.hpp"
#include "utils/fusing_test_utils.hpp"
@@ -29,6 +31,9 @@ namespace test {
|
|
result
+
+ This pattern creates a PowerStatic node with gamma parameter = -3.40282e+38,
+ which exceeds bf16 range and requires saturation handling.
*/
class PowerStaticBF16Saturation : virtual public SubgraphBaseTest, public CpuTestWithFusing {
@@ -46,18 +51,17 @@ protected:
parameters.push_back(param0);
parameters.push_back(param1);
- const auto constMatmul0 = std::make_shared<ov::op::v0::Constant>(ov::element::f32,
- ov::Shape{1, 1},
- std::vector<double>({3.40282e+38}));
- const auto constMatmul1 = std::make_shared<ov::op::v0::Constant>(ov::element::f32,
- ov::Shape{1, 1},
- std::vector<double>({3.40282e+38}));
- const auto matMul0 = std::make_shared<ov::op::v0::MatMul>(parameters[0], constMatmul0, false, false);
- const auto matMul1 = std::make_shared<ov::op::v0::MatMul>(parameters[1], constMatmul1, false, false);
+ // Constants that will create large values exceeding bf16 range
+ const auto const_large_positive = std::make_shared<ov::op::v0::Constant>(ov::element::f32,
+ ov::Shape{1, 1},
+ std::vector<float>({3.40282e+38f}));
+ const auto matMul0 = std::make_shared<ov::op::v0::MatMul>(parameters[0], const_large_positive, false, false);
+ const auto matMul1 = std::make_shared<ov::op::v0::MatMul>(parameters[1], const_large_positive, false, false);
const auto concat = std::make_shared<ov::op::v0::Concat>(OutputVector{matMul0, matMul1}, 0);
- const auto constAdd = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{}, -3.40282e+38);
- const auto add = std::make_shared<ov::op::v1::Add>(concat, constAdd);
+ const auto const_large_negative =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{}, -3.40282e+38f);
+ const auto add = std::make_shared<ov::op::v1::Add>(concat, const_large_negative);
function = makeNgraphFunction(ElementType::f32, parameters, add, "PowerStaticBF16Saturation");
configuration.insert({ov::hint::inference_precision(ov::element::bf16)});
@@ -71,15 +75,13 @@ protected:
in_data.start_from = 1;
in_data.range = 1;
in_data.resolution = 1;
- auto tensor0 = ov::test::utils::create_and_fill_tensor(modelInputs[0].get_element_type(),
- targetInputStaticShapes[0],
- in_data);
- auto tensor1 = ov::test::utils::create_and_fill_tensor(modelInputs[1].get_element_type(),
- targetInputStaticShapes[1],
- in_data);
-
- inputs.insert({modelInputs[0].get_node_shared_ptr(), tensor0});
- inputs.insert({modelInputs[1].get_node_shared_ptr(), tensor1});
+
+ for (size_t i = 0; i < modelInputs.size(); ++i) {
+ auto tensor = ov::test::utils::create_and_fill_tensor(modelInputs[i].get_element_type(),
+ targetInputStaticShapes[i],
+ in_data);
+ inputs.insert({modelInputs[i].get_node_shared_ptr(), tensor});
+ }
}
};
@@ -87,5 +89,89 @@ TEST_F(PowerStaticBF16Saturation, CompareWithRefs) {
run();
}
+/* Bf16 saturation handling for the PowerStatic beta parameter when input precision is bf16
+
+ Param0 Param1
+ | |
+ matmul0 matmul1
+ \ /
+ \ /
+ concat
+ |
+ multiply(1.0)
+ |
+ subtract(1.0 - multiply_result)
+ |
+ multiply(-3.40282e+38)
+ |
+ result
+
+ This pattern gets fused into a PowerStatic node with beta parameter = -3.40282e+38,
+ which exceeds bf16 range and requires saturation handling.
+ The matmul operations ensure the input precision is bf16 through inference_precision setting.
+*/
+
+class PowerStaticBetaBF16Saturation : virtual public SubgraphBaseTest, public CpuTestWithFusing {
+protected:
+ void SetUp() override {
+ abs_threshold = 0;
+ targetDevice = ov::test::utils::DEVICE_CPU;
+ InputShape input0 = {{-1, -1}, {{1, 1}}};
+ InputShape input1 = {{-1, -1}, {{1, 1}}};
+
+ init_input_shapes({input0, input1});
+ ov::ParameterVector parameters;
+ auto param0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, -1});
+ auto param1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, -1});
+ parameters.push_back(param0);
+ parameters.push_back(param1);
+
+ // Constants that create a PowerStatic node with beta parameter exceeding bf16 range
+ const auto const_one =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1, 1}, std::vector<float>{1.0f});
+
+ const auto const_large_negative =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32,
+ ov::Shape{1, 1},
+ std::vector<float>{-3.40282346638528859812e+38f});
+
+ const auto matMul0 = std::make_shared<ov::op::v0::MatMul>(parameters[0], const_one, false, false);
+ const auto matMul1 = std::make_shared<ov::op::v0::MatMul>(parameters[1], const_one, false, false);
+ const auto concat = std::make_shared<ov::op::v0::Concat>(OutputVector{matMul0, matMul1}, 0);
+
+ // Create computation pattern:
+ // Step 1: concat_result * 1.0 = concat_result (bf16 after precision conversion)
+ // Step 2: 1.0 - multiply_result
+ // Step 3: (1.0 - multiply_result) * (-3.40282e+38)
+ // This pattern will be fused into PowerStatic node with beta = -3.40282e+38, exceeding bf16 range
+ auto multiply_step = std::make_shared<ov::op::v1::Multiply>(concat, const_one);
+ auto subtract_step = std::make_shared<ov::op::v1::Subtract>(const_one, multiply_step);
+ auto final_multiply = std::make_shared<ov::op::v1::Multiply>(subtract_step, const_large_negative);
+
+ function = makeNgraphFunction(ElementType::f32, parameters, final_multiply, "PowerStaticBetaBF16Saturation");
+ configuration.insert({ov::hint::inference_precision(ov::element::bf16)});
+ configuration.insert(ov::intel_cpu::snippets_mode(ov::intel_cpu::SnippetsMode::DISABLE));
+ }
+ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {
+ inputs.clear();
+ const auto& modelInputs = function->inputs();
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = 1;
+ in_data.range = 1;
+ in_data.resolution = 1;
+
+ for (size_t i = 0; i < modelInputs.size(); ++i) {
+ auto tensor = ov::test::utils::create_and_fill_tensor(modelInputs[i].get_element_type(),
+ targetInputStaticShapes[i],
+ in_data);
+ inputs.insert({modelInputs[i].get_node_shared_ptr(), tensor});
+ }
+ }
+};
+
+TEST_F(PowerStaticBetaBF16Saturation, CompareWithRefs) {
+ run();
+}
+
} // namespace test
} // namespace ov