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
[GPU] Fix accuracy issue by disable fp16 compression for Sin/Cos (#31589)
Paul Youngsoo Ahn8ba85e1ea3c0
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/disable_fp16_comp_for_periodic_funcs.cpp b/src/plugins/intel_gpu/src/plugin/transformations/disable_fp16_comp_for_periodic_funcs.cppindex 785d8b81949..68acbbf1335 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/disable_fp16_comp_for_periodic_funcs.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/disable_fp16_comp_for_periodic_funcs.cpp@@ -3,6 +3,8 @@//#include <memory>+#include <queue>+#include <unordered_set>#include "openvino/pass/pattern/op/or.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"@@ -10,44 +12,51 @@#include "openvino/op/sin.hpp"#include "openvino/op/cos.hpp"#include "openvino/op/reshape.hpp"-#include "openvino/op/transpose.hpp"-#include "openvino/op/util/scatter_base.hpp"-#include "openvino/op/util/scatter_nd_base.hpp"-#include "openvino/op/util/scatter_elements_update_base.hpp"#include "openvino/op/slice.hpp"-#include "openvino/op/broadcast.hpp"-#include "openvino/op/concat.hpp"-#include "openvino/op/split.hpp"#include "openvino/op/strided_slice.hpp"-#include "openvino/op/tile.hpp"-#include "openvino/op/identity.hpp"-#include "openvino/op/pad.hpp"-#include "openvino/op/util/gather_base.hpp"-#include "openvino/op/util/gather_nd_base.hpp"-#include "openvino/op/squeeze.hpp"+#include "openvino/op/split.hpp"+#include "openvino/op/concat.hpp"#include "openvino/op/unsqueeze.hpp"+#include "openvino/op/squeeze.hpp"+#include "openvino/op/tile.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/util/broadcast_base.hpp"+#include "openvino/op/variadic_split.hpp"+#include "openvino/op/util/binary_elementwise_arithmetic.hpp"+#include "openvino/op/util/unary_elementwise_arithmetic.hpp"+#include "openvino/op/mvn.hpp"+#include "openvino/op/normalize_l2.hpp"+#include "openvino/op/sqrt.hpp"+#include "openvino/op/reduce_sum.hpp"+#include "openvino/op/reduce_mean.hpp"#include "transformations/utils/utils.hpp"#include "disable_fp16_comp_for_periodic_funcs.hpp"-static bool is_non_value_modifying_node(const std::shared_ptr<ov::Node>& node) {- return ov::is_type<ov::op::v1::Reshape>(node) ||- ov::is_type<ov::op::v1::Transpose>(node) ||- ov::is_type<ov::op::util::ScatterBase>(node) ||- ov::is_type<ov::op::util::ScatterNDBase>(node) ||- ov::is_type<ov::op::util::ScatterElementsUpdateBase>(node) ||+// Uses propagate_through_ops whitelist from mark_subgraph_to_keep_in_mixed_precision in convert precision pass+// to ensure consistent behavior with OpenVINO's existing mixed precision logic.+static bool is_propagate_through_node(const std::shared_ptr<ov::Node>& node) {+ return ov::is_type<ov::op::v0::Squeeze>(node) ||+ ov::is_type<ov::op::v0::Unsqueeze>(node) ||+ ov::is_type<ov::op::v1::Reshape>(node) ||+ ov::is_type<ov::op::util::BroadcastBase>(node) ||+ ov::is_type<ov::op::util::BinaryElementwiseArithmetic>(node) ||+ ov::is_type<ov::op::util::UnaryElementwiseArithmetic>(node) ||+ ov::is_type<ov::op::v6::MVN>(node) ||+ ov::is_type<ov::op::v0::MVN>(node) ||+ ov::is_type<ov::op::v0::NormalizeL2>(node) ||+ ov::is_type<ov::op::v0::Sqrt>(node) ||+ ov::is_type<ov::op::v1::StridedSlice>(node) ||+ ov::is_type<ov::op::v1::ReduceSum>(node) ||+ ov::is_type<ov::op::v1::ReduceMean>(node) ||ov::is_type<ov::op::v8::Slice>(node) ||- ov::is_type<ov::op::v1::Broadcast>(node) ||- ov::is_type<ov::op::v0::Concat>(node) ||+ ov::is_type<ov::op::v1::VariadicSplit>(node) ||ov::is_type<ov::op::v1::Split>(node) ||- ov::is_type<ov::op::v1::StridedSlice>(node) ||- ov::is_type<ov::op::v0::Tile>(node) ||- ov::is_type<ov::op::v16::Identity>(node) ||- ov::is_type<ov::op::v1::Pad>(node) ||- ov::is_type<ov::op::util::GatherNDBase>(node) ||- ov::is_type<ov::op::util::GatherBase>(node) ||- ov::is_type<ov::op::v15::Squeeze>(node) ||- ov::is_type<ov::op::v0::Unsqueeze>(node);+ ov::is_type<ov::op::v0::Concat>(node) ||+ ov::is_type<ov::op::v0::Convert>(node) ||+ ov::is_type<ov::op::v0::Constant>(node) ||+ ov::is_type<ov::op::v0::Tile>(node);}ov::intel_gpu::DisableFP16CompressionForPeriodicFuncs::DisableFP16CompressionForPeriodicFuncs()@@ -60,41 +69,65 @@ ov::intel_gpu::DisableFP16CompressionForPeriodicFuncs::DisableFP16CompressionForov::matcher_pass_callback callback = [&](ov::pass::pattern::Matcher& m) {auto node = m.get_match_root();+ // Only disable FP16 compression for FP32 data type,+ // because sin/cos with fp64 should be converted to sin/cos with fp32.+ if (node->get_output_element_type(0) != ov::element::f32) {+ return false;+ }++ // Check if sin/cos is followed by propagate-through operations.+ // If so, skip disable_fp16_compression because convert_precision pass will insert+ // Convert nodes at the end of the propagate-through chain (before the first+ // non-propagate-through node), not immediately after sin/cos.+ // In this case, do not apply disable_fp16_compression.+ for (auto& user : node->get_users()) {+ if (is_propagate_through_node(user)) {+ return false;+ }+ }- // Disable FP16 compression for the current node+ // Disable FP16 compression for the current sin/cos nodeov::disable_fp16_compression(node);- auto current_node = node;-- OPENVINO_ASSERT(current_node && !current_node->inputs().empty(),- "current_node should not be null and have inputs");-- // Traverse the input chain to find the first value-modifying node- while (current_node) {- /*- * Move to the first input node, the reason that we only traverse first input:- * 1. Sin/Cos operations have only one input containing the actual data- * 2. For non-value-modifying operations (reshape, transpose, etc.), input[0] contains the actual data- * while other inputs (input[1], input[2]...) are metadata for:- * - Shape information (target shape for reshape)- * - Index information (axes for transpose, gather indices)- * - Broadcasting parameters (broadcast shape)- * These metadata inputs don't affect the actual data values, only how the data is arranged or accessed- * 3. Since we're tracking the data flow that affects numerical precision,- * we only need to follow the path of actual data (input[0])- */- current_node = current_node->get_input_node_shared_ptr(0); // Follow only the first input- if (!current_node || current_node->inputs().empty()) {- break;+ // Traverse the input chain to find non-propagate-through nodes+ // and apply disable_fp16_compression to prevent fp16 precision data loss+ std::queue<std::shared_ptr<ov::Node>> nodes_to_process;+ std::unordered_set<std::shared_ptr<ov::Node>> visited;++ nodes_to_process.push(node);+ visited.insert(node);++ /*+ * Use BFS to traverse input chain and find non-propagate-through nodes+ * to apply disable_fp16_compression, preventing fp16 precision data loss.+ */+ while (!nodes_to_process.empty()) {+ auto current_node = nodes_to_process.front();+ nodes_to_process.pop();++ OPENVINO_ASSERT(current_node, "current_node should not be null");++ if (current_node->inputs().empty()) {+ continue;}- if (ov::fp16_compression_is_disabled(current_node)) {- break;+ if ((node != current_node)+ && ov::fp16_compression_is_disabled(current_node)) {+ continue;}- if (!is_non_value_modifying_node(current_node)) {+ if (!is_propagate_through_node(current_node)) {ov::disable_fp16_compression(current_node);- break;+ continue;+ }++ // For propagate-through nodes, add all input nodes to processing queue+ for (size_t i = 0; i < current_node->get_input_size(); ++i) {+ auto input_node = current_node->get_input_node_shared_ptr(i);+ if (input_node && visited.find(input_node) == visited.end()) {+ nodes_to_process.push(input_node);+ visited.insert(input_node);+ }}}diff --git a/src/plugins/intel_gpu/tests/unit/transformations/disable_fp16_compression_periodic_func_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/disable_fp16_compression_periodic_func_test.cppindex 211fedfaf70..26d3270a4b1 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/disable_fp16_compression_periodic_func_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/disable_fp16_compression_periodic_func_test.cpp@@ -24,6 +24,8 @@#include "openvino/op/cos.hpp"#include "openvino/op/sin.hpp"#include "openvino/op/concat.hpp"+#include "openvino/op/matmul.hpp"+#include "openvino/op/gather.hpp"using namespace testing;using namespace ov::intel_gpu;@@ -42,9 +44,46 @@ static bool fuse_type_to_convert(const std::shared_ptr<ov::Node>& node, const pr}static const std::string name_add = "add_1";+static const std::string name_mul = "mul_1";static const std::string name_sin = "sin";static const std::string name_cos = "cos";+static std::shared_ptr<ov::Model> create_model_with_periodic_func_with_matmul() {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{ -1, -1, 3 });+ auto constant_1 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, { -1 });+ auto unsqueeze_1 = std::make_shared<ov::op::v0::Unsqueeze>(input, constant_1 );++ auto constant_2_compressed = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{ 1,1,341 }, { 3.14062f });+ auto constant_2 = std::make_shared<ov::op::v0::Convert>(constant_2_compressed, ov::element::f32);++ auto matmul_weight = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{ 1, 341 }, { 1.0f });+ auto matmul_1 = std::make_shared<ov::op::v0::MatMul>(unsqueeze_1, matmul_weight);++ auto multiply_1 = std::make_shared<ov::op::v1::Multiply>(matmul_1, constant_2);++ auto constant_3_compressed = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{ 1,1,341 }, { -1.57031f });+ auto constant_3 = std::make_shared<ov::op::v0::Convert>(constant_3_compressed, ov::element::f32);+ auto constant_4 = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{ 4 }, { 0, 1, 3, 2 });++ auto add_1 = std::make_shared<ov::op::v1::Add>(multiply_1, constant_3);+ add_1->set_friendly_name(name_add);+ auto transpose_1 = std::make_shared<ov::op::v1::Transpose>(add_1, constant_4);+ auto reshape_1 = std::make_shared<ov::op::v1::Reshape>(transpose_1, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{ 3 }, { 0, 0, 1023 }), false);++ auto sin = std::make_shared<ov::op::v0::Sin>(reshape_1);+ sin->set_friendly_name(name_sin);+ auto cos = std::make_shared<ov::op::v0::Cos>(reshape_1);+ cos->set_friendly_name(name_cos);+ auto constant_5 = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{ 2046 }, { 0 });+ auto constant_6 = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{ }, { 0 });+ auto gather_sin = std::make_shared<ov::op::v8::Gather>(sin, constant_5, constant_6);+ auto gather_cos = std::make_shared<ov::op::v8::Gather>(cos, constant_5, constant_6);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{gather_sin, gather_cos}, 2);++ return std::make_shared<ov::Model>(ov::OutputVector{concat}, ov::ParameterVector{input});+}++static std::shared_ptr<ov::Model> create_model_with_periodic_func() {auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{ -1, -1, 3 });auto constant_1 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, { -1 });@@ -68,7 +107,11 @@ static std::shared_ptr<ov::Model> create_model_with_periodic_func() {sin->set_friendly_name(name_sin);auto cos = std::make_shared<ov::op::v0::Cos>(reshape_1);cos->set_friendly_name(name_cos);- auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{sin, cos}, 2);+ auto constant_5 = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{ 2046 }, { 0 });+ auto constant_6 = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{ }, { 0 });+ auto gather_sin = std::make_shared<ov::op::v8::Gather>(sin, constant_5, constant_6);+ auto gather_cos = std::make_shared<ov::op::v8::Gather>(cos, constant_5, constant_6);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{gather_sin, gather_cos}, 2);return std::make_shared<ov::Model>(ov::OutputVector{concat}, ov::ParameterVector{input});}@@ -82,12 +125,29 @@ static std::shared_ptr<ov::Model> create_simple_model_with_periodic_func() {auto cos = std::make_shared<ov::op::v0::Cos>(input);cos->set_friendly_name(name_cos);- auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{sin, cos}, 1);+ return std::make_shared<ov::Model>(ov::OutputVector{sin, cos}, ov::ParameterVector{input});+}- return std::make_shared<ov::Model>(ov::OutputVector{concat}, ov::ParameterVector{input});+static std::shared_ptr<ov::Model> create_no_available_disabled_fp16_model() {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 1});+ auto input2 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 256});+ auto constant_1 = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{ 1, 128 }, { 1.f });+ auto matmul = std::make_shared<ov::op::v0::MatMul>(input, constant_1);+ matmul->set_friendly_name(name_mul);++ auto sin = std::make_shared<ov::op::v0::Sin>(matmul);+ sin->set_friendly_name(name_sin);++ auto cos = std::make_shared<ov::op::v0::Cos>(matmul);+ cos->set_friendly_name(name_cos);++ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{sin,cos},1);++ return std::make_shared<ov::Model>(ov::OutputVector{concat}, ov::ParameterVector{input, input2});}-static void run_disable_fp16_compression_for_periodic_func_test(std::shared_ptr<ov::Model> model, const std::unordered_set<std::string>& required_nodes) {+static void run_disable_fp16_compression_for_periodic_func_test(std::shared_ptr<ov::Model> model,+ const std::unordered_set<std::string>& required_nodes, bool is_expected_disabled_fp16 = true) {ov::pass::Manager manager;manager.register_pass<DisableFP16CompressionForPeriodicFuncs>();@@ -125,15 +185,19 @@ static void run_disable_fp16_compression_for_periodic_func_test(std::shared_ptr<keep_precision_sensitive_in_fp32_2,convert_input_output_precision);manager.run_passes(model);-std::unordered_set<std::string> found_nodes; // Set of found node namesfor (auto& op : model->get_ops()) {if (required_nodes.count(op->get_friendly_name())) {found_nodes.insert(op->get_friendly_name()); // Add the found node name to the set- ASSERT_TRUE(ov::fp16_compression_is_disabled(op))- << "FP16 compression is not disabled for node: " << op->get_friendly_name();+ if (is_expected_disabled_fp16) {+ ASSERT_TRUE(ov::fp16_compression_is_disabled(op))+ << "FP16 compression is not disabled for node: " << op->get_friendly_name();+ } else {+ ASSERT_FALSE(ov::fp16_compression_is_disabled(op))+ << "FP16 compression is unexpectedly disabled for node: " << op->get_friendly_name();+ }}}@@ -147,8 +211,21 @@ TEST(TransformationTests, DisableFP16CompressionForPeriodicFuncsTest) {run_disable_fp16_compression_for_periodic_func_test(model, required_nodes);}+TEST(TransformationTests, DisableFP16CompressionForPeriodicFuncsTestWithMatMul) {+ std::unordered_set<std::string> required_nodes = {name_sin, name_cos, name_add};+ auto model = create_model_with_periodic_func_with_matmul();+ run_disable_fp16_compression_for_periodic_func_test(model, required_nodes);+}++TEST(TransformationTests, DisableFP16CompressionForPeriodicFuncsTestSimple) {std::unordered_set<std::string> required_nodes = {name_sin, name_cos};auto model = create_simple_model_with_periodic_func();run_disable_fp16_compression_for_periodic_func_test(model, required_nodes);}++TEST(TransformationTests, DisableFP16CompressionForPeriodicFuncsTestNoAvailableDisabledFP16) {+ std::unordered_set<std::string> required_nodes = {name_mul, name_sin, name_cos};+ auto model = create_no_available_disabled_fp16_model();+ run_disable_fp16_compression_for_periodic_func_test(model, required_nodes, false);+}\ No newline at end of file