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
[CPU]Add bf16saturation for powerStatic gamma (#31432)
Mang Guo936f0e17535a
diff --git a/src/plugins/intel_cpu/src/nodes/eltwise.cpp b/src/plugins/intel_cpu/src/nodes/eltwise.cppindex 43736aeea51..68344424a44 100644--- a/src/plugins/intel_cpu/src/nodes/eltwise.cpp+++ b/src/plugins/intel_cpu/src/nodes/eltwise.cpp@@ -11,6 +11,7 @@#include <cassert>#include <cmath>#include <cstddef>+#include <limits>#include <map>#include <memory>#include <oneapi/dnnl/dnnl.hpp>@@ -45,6 +46,7 @@#include "openvino/core/node.hpp"#include "openvino/core/shape.hpp"#include "openvino/core/type.hpp"+#include "openvino/core/type/bfloat16.hpp"#include "openvino/core/type/element_type.hpp"#include "openvino/op/abs.hpp"#include "openvino/op/add.hpp"@@ -543,6 +545,24 @@ bool Eltwise::isWithBroadcast() {return false;}+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.+ 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;++ if (gamma < lowest) {+ gamma = lowest;+ }++ if (gamma > max) {+ gamma = max;+ }+ }+}+void Eltwise::getSupportedDescriptors() {CPU_NODE_ASSERT(!getParentEdges().empty(), "Incorrect number of input edges");CPU_NODE_ASSERT(!getChildEdges().empty(), "Incorrect number of output edges");@@ -619,6 +639,7 @@ void Eltwise::initSupportedPrimitiveDescriptors() {m_attrs.data.algo = getAlgorithm();m_attrs.postOps = getPostOps(fusedWith, ov::element::dynamic);m_attrs.opsList = {getType()};+// Create memory descriptorsstd::vector<MemoryDescPtr> srcDescs;// Select preferred layout for memory descriptorsdiff --git a/src/plugins/intel_cpu/src/nodes/eltwise.h b/src/plugins/intel_cpu/src/nodes/eltwise.hindex 9bad7d799c7..75f80f92be6 100644--- a/src/plugins/intel_cpu/src/nodes/eltwise.h+++ b/src/plugins/intel_cpu/src/nodes/eltwise.h@@ -105,6 +105,7 @@ private:static EltwiseBroadcastingPolicy determineBroadcastingPolicy(const std::shared_ptr<ov::Node>& op);size_t getOpInputsNum() const;+ void init() override;void appendMemory(const std::vector<float>& data, MemoryPtr& memPtr, std::vector<MemoryPtr>& postOpsMem);static void appendMemory(const std::vector<float>& data,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.cppnew file mode 100644index 00000000000..402463eee4f--- /dev/null+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/powerstatic_bf16_saturation.cpp@@ -0,0 +1,91 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "common_test_utils/ov_tensor_utils.hpp"+#include "internal_properties.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/concat.hpp"+#include "openvino/op/matmul.hpp"+#include "openvino/op/multiply.hpp"+#include "shared_test_classes/base/ov_subgraph.hpp"+#include "utils/fusing_test_utils.hpp"++using namespace CPUTestUtils;+namespace ov {+namespace test {++/* Bf16 saturation handling for the PowerStatic gamma parameter when input precision is bf16++ Param0 Param1+ | |+ matmul0 matmul1+ \ /+ \ /+ concat+ |+ | /const(-3.40282e+38)+ add /+ |+ |+ result+*/++class PowerStaticBF16Saturation : 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);++ 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);+ 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);++ function = makeNgraphFunction(ElementType::f32, parameters, add, "PowerStaticBF16Saturation");+ 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;+ 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});+ }+};++TEST_F(PowerStaticBF16Saturation, CompareWithRefs) {+ run();+}++} // namespace test+} // namespace ov