-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[CPU][TRANSFORMATIONS][SNIPPETS] Keep Sin/Cos argument computation in f32 under bf16/f16 inference precision #36719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
goyaladitya05
wants to merge
1
commit into
openvinotoolkit:master
Choose a base branch
from
goyaladitya05:fix/cpu-sincos-args-f32
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...n/transformations/include/transformations/fp16_compression/disable_fp16_comp_ltx_rope.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/pass/matcher_pass.hpp" | ||
| #include "transformations_visibility.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| /** | ||
| * @ingroup ov_transformation_common_api | ||
| * @brief Keeps the decomposed LTX-Video RoPE angle chain (Multiply -> Add -> Transpose -> Reshape -> | ||
| * Sin/Cos) in f32: the angles reach ~1.6e4 rad, which f16/bf16 quantize in steps larger than 2*pi. | ||
| * | ||
| * The fused RoPE variant is handled on GPU by IncreasePositionIdsPrecisionForLtxVideo. | ||
| */ | ||
| class TRANSFORMATIONS_API DisableFP16CompForLtxVideoRopePattern : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_MATCHER_PASS_RTTI("DisableFP16CompForLtxVideoRopePattern"); | ||
| DisableFP16CompForLtxVideoRopePattern(); | ||
| }; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov |
51 changes: 51 additions & 0 deletions
51
...ommon/transformations/src/transformations/fp16_compression/disable_fp16_comp_ltx_rope.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "transformations/fp16_compression/disable_fp16_comp_ltx_rope.hpp" | ||
|
|
||
| #include "itt.hpp" | ||
| #include "openvino/op/add.hpp" | ||
| #include "openvino/op/constant.hpp" | ||
| #include "openvino/op/cos.hpp" | ||
| #include "openvino/op/multiply.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/sin.hpp" | ||
| #include "openvino/op/transpose.hpp" | ||
| #include "openvino/pass/pattern/op/or.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/rt_info/disable_precision_conversion.hpp" | ||
|
|
||
| namespace v0 = ov::op::v0; | ||
| namespace v1 = ov::op::v1; | ||
|
|
||
| namespace ov::pass { | ||
|
|
||
| DisableFP16CompForLtxVideoRopePattern::DisableFP16CompForLtxVideoRopePattern() { | ||
| MATCHER_SCOPE(DisableFP16CompForLtxVideoRopePattern); | ||
| using namespace ov::pass::pattern; | ||
|
|
||
| // grid values are small; the large magnitude appears only from the frequency Multiply onwards | ||
| auto mul = wrap_type<v1::Multiply>({any_input(), any_input()}); | ||
| auto add_constant = wrap_type<v0::Constant>(); | ||
| auto add = wrap_type<v1::Add>({mul, add_constant}); | ||
| auto transpose = wrap_type<v1::Transpose>({add, any_input()}); | ||
| auto reshape = wrap_type<v1::Reshape>({transpose, any_input()}); | ||
| auto sin = wrap_type<v0::Sin>({reshape}); | ||
| auto cos = wrap_type<v0::Cos>({reshape}); | ||
| auto sin_or_cos = std::make_shared<pattern::op::Or>(OutputVector{sin, cos}); | ||
|
|
||
| matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](Matcher& m) { | ||
| const auto& pattern_map = m.get_pattern_value_map(); | ||
| for (const auto& node : {mul, add_constant, add, transpose, reshape}) { | ||
| ov::disable_conversion(pattern_map.at(node).get_node_shared_ptr(), element::f16); | ||
| } | ||
| ov::disable_conversion(m.get_match_root(), element::f16); | ||
| return false; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<Matcher>(sin_or_cos, matcher_name); | ||
| this->register_matcher(m, callback); | ||
| } | ||
|
|
||
| } // namespace ov::pass |
78 changes: 78 additions & 0 deletions
78
src/common/transformations/tests/fp16_compression/disable_fp16_comp_ltx_rope_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| #include "transformations/fp16_compression/disable_fp16_comp_ltx_rope.hpp" | ||
|
|
||
| #include "common_test_utils/ov_test_utils.hpp" | ||
| #include "openvino/op/add.hpp" | ||
| #include "openvino/op/constant.hpp" | ||
| #include "openvino/op/cos.hpp" | ||
| #include "openvino/op/multiply.hpp" | ||
| #include "openvino/op/parameter.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/sin.hpp" | ||
| #include "openvino/op/transpose.hpp" | ||
| #include "transformations/rt_info/disable_precision_conversion.hpp" | ||
|
|
||
| namespace v0 = ov::op::v0; | ||
| namespace v1 = ov::op::v1; | ||
|
|
||
| namespace { | ||
| // Multiply -> Add(Constant) -> Transpose -> Reshape -> Sin/Cos, the decomposed LTX-Video rope angles. | ||
| struct RopeChain { | ||
| std::shared_ptr<v0::Parameter> grid; | ||
| std::shared_ptr<v1::Multiply> mul; | ||
| std::shared_ptr<v0::Constant> add_const; | ||
| std::shared_ptr<v1::Add> add; | ||
| std::shared_ptr<v1::Transpose> transpose; | ||
| std::shared_ptr<v1::Reshape> reshape; | ||
| std::shared_ptr<v0::Sin> sin; | ||
| std::shared_ptr<v0::Cos> cos; | ||
| std::shared_ptr<ov::Model> model(const std::string& name) const { | ||
| return std::make_shared<ov::Model>(ov::OutputVector{sin, cos}, ov::ParameterVector{grid}, name); | ||
| } | ||
| }; | ||
|
|
||
| RopeChain make_rope_chain() { | ||
| RopeChain c; | ||
| c.grid = std::make_shared<v0::Parameter>(ov::element::f32, ov::Shape{1, 8, 1}); | ||
| auto freqs = v0::Constant::create(ov::element::f32, ov::Shape{1, 1, 16}, std::vector<float>(16, 1000.0f)); | ||
| c.mul = std::make_shared<v1::Multiply>(c.grid, freqs); | ||
| c.add_const = v0::Constant::create(ov::element::f32, ov::Shape{}, {-1.0f}); | ||
| c.add = std::make_shared<v1::Add>(c.mul, c.add_const); | ||
| auto order = v0::Constant::create(ov::element::i32, ov::Shape{3}, {0, 2, 1}); | ||
| c.transpose = std::make_shared<v1::Transpose>(c.add, order); | ||
| auto shape = v0::Constant::create(ov::element::i32, ov::Shape{2}, {1, 128}); | ||
| c.reshape = std::make_shared<v1::Reshape>(c.transpose, shape, false); | ||
| c.sin = std::make_shared<v0::Sin>(c.reshape); | ||
| c.cos = std::make_shared<v0::Cos>(c.reshape); | ||
| return c; | ||
| } | ||
| } // namespace | ||
|
|
||
| // The whole angle chain from the frequency Multiply up to Sin/Cos is kept in f32. | ||
| TEST_F(TransformationTestsF, DisableFP16CompForLtxVideoRopeMarksAngleChain) { | ||
| model = make_rope_chain().model("model"); | ||
|
|
||
| manager.register_pass<ov::pass::DisableFP16CompForLtxVideoRopePattern>(); | ||
|
|
||
| { | ||
| auto c = make_rope_chain(); | ||
| for (const auto& node : | ||
| std::vector<std::shared_ptr<ov::Node>>{c.mul, c.add_const, c.add, c.transpose, c.reshape, c.sin, c.cos}) { | ||
| disable_conversion(node, ov::element::f16); | ||
| } | ||
| model_ref = c.model("model_ref"); | ||
| } | ||
| comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
| comparator.enable(FunctionsComparator::CmpValues::RUNTIME_KEYS); | ||
| } | ||
|
|
||
| // Sin/Cos outside the rope pattern are left untouched, so unrelated subgraphs keep low precision. | ||
| TEST_F(TransformationTestsF, DisableFP16CompForLtxVideoRopeSkipsUnrelatedSinCos) { | ||
| auto x = std::make_shared<v0::Parameter>(ov::element::f32, ov::Shape{1, 8}); | ||
| auto sin = std::make_shared<v0::Sin>(x); | ||
| model = std::make_shared<ov::Model>(ov::OutputVector{sin}, ov::ParameterVector{x}, "model"); | ||
|
|
||
| manager.register_pass<ov::pass::DisableFP16CompForLtxVideoRopePattern>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/rope_table_precision.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "common_test_utils/node_builders/constant.hpp" | ||
| #include "common_test_utils/ov_tensor_utils.hpp" | ||
| #include "openvino/op/add.hpp" | ||
| #include "openvino/op/cos.hpp" | ||
| #include "openvino/op/matmul.hpp" | ||
| #include "openvino/op/multiply.hpp" | ||
| #include "openvino/op/parameter.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/sin.hpp" | ||
| #include "openvino/op/transpose.hpp" | ||
| #include "openvino/runtime/system_conf.hpp" | ||
| #include "shared_test_classes/base/ov_subgraph.hpp" | ||
|
|
||
| namespace ov::test { | ||
|
|
||
| // Decomposed LTX-Video rope table applied to a projection: the angle chain must stay f32 under bf16 | ||
| // enforcement, MatMuls stay bf16. The static case also exercises Snippets tokenization. | ||
| class RopeTablePrecisionCPUTest : public testing::WithParamInterface<InputShape>, public SubgraphBaseTest { | ||
| public: | ||
| static std::string getTestCaseName(const testing::TestParamInfo<InputShape>& obj) { | ||
| std::ostringstream result; | ||
| result << (obj.param.first.is_dynamic() ? "dynamic" : "static"); | ||
| result << "_IS=" << ov::test::utils::partialShape2str({obj.param.first}); | ||
| return result.str(); | ||
| } | ||
|
|
||
| protected: | ||
| void SetUp() override { | ||
| targetDevice = utils::DEVICE_CPU; | ||
| configuration.insert({ov::hint::inference_precision.name(), ov::element::bf16}); | ||
| // bf16-computed angles give O(1) errors; honest bf16 rounding stays well under this | ||
| rel_threshold = 0.05; | ||
| abs_threshold = 0.5; | ||
|
|
||
| // the rope table size is fixed; only the hidden states shape varies | ||
| init_input_shapes({InputShape{ov::PartialShape{1, GRID, 1}, {ov::Shape{1, GRID, 1}}}, GetParam()}); | ||
|
|
||
| ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(ov::element::f32, inputDynamicShapes[0]), | ||
| std::make_shared<ov::op::v0::Parameter>(ov::element::f32, inputDynamicShapes[1])}; | ||
|
|
||
| // ~500 rad max after the freq multiply: fatal for bf16 (~2 rad steps), exact for f32 sin/cos | ||
| auto freqs = utils::make_constant(ov::element::f32, ov::Shape{1, 1, BANDS}, std::vector<float>{1, 2, 4, 8}); | ||
| auto angles = std::make_shared<ov::op::v1::Multiply>(params[0], freqs); | ||
| auto shifted = std::make_shared<ov::op::v1::Add>( | ||
| angles, | ||
| utils::make_constant(ov::element::f32, ov::Shape{}, std::vector<float>{-1.0F})); | ||
| auto order = utils::make_constant(ov::element::i32, ov::Shape{3}, std::vector<int>{0, 2, 1}); | ||
| auto transpose = std::make_shared<ov::op::v1::Transpose>(shifted, order); | ||
| auto target_shape = | ||
| utils::make_constant(ov::element::i32, ov::Shape{2}, std::vector<int>{1, static_cast<int>(HIDDEN_SIZE)}); | ||
| auto reshape = std::make_shared<ov::op::v1::Reshape>(transpose, target_shape, false); | ||
| auto cos = std::make_shared<ov::op::v0::Cos>(reshape); | ||
| auto sin = std::make_shared<ov::op::v0::Sin>(reshape); | ||
|
|
||
| ov::test::utils::InputGenerateData weights_data(-1, 2, 256); | ||
| auto proj_weights = utils::make_constant(ov::element::f32, ov::Shape{HIDDEN_SIZE, HIDDEN_SIZE}, weights_data); | ||
| auto q = std::make_shared<ov::op::v0::MatMul>(params[1], proj_weights); | ||
|
|
||
| auto q_cos = std::make_shared<ov::op::v1::Multiply>(q, cos); | ||
| auto q_sin = std::make_shared<ov::op::v1::Multiply>(q, sin); | ||
| auto rotated = std::make_shared<ov::op::v1::Add>(q_cos, q_sin); | ||
|
|
||
| auto out_weights = utils::make_constant(ov::element::f32, ov::Shape{HIDDEN_SIZE, HIDDEN_SIZE}, weights_data); | ||
| auto matmul = std::make_shared<ov::op::v0::MatMul>(rotated, out_weights); | ||
|
|
||
| function = std::make_shared<ov::Model>(ov::OutputVector{std::make_shared<ov::op::v0::Result>(matmul)}, | ||
| params, | ||
| "RopeTablePrecision"); | ||
| } | ||
|
|
||
| void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override { | ||
| inputs.clear(); | ||
| const auto& funcInputs = function->inputs(); | ||
|
|
||
| ov::Tensor grid{ov::element::f32, targetInputStaticShapes[0]}; | ||
| auto* grid_data = grid.data<float>(); | ||
| for (size_t i = 0; i < grid.get_size(); ++i) { | ||
| grid_data[i] = static_cast<float>(i); | ||
| } | ||
| inputs.insert({funcInputs[0].get_node_shared_ptr(), grid}); | ||
|
|
||
| utils::InputGenerateData in_data; | ||
| in_data.start_from = -1; | ||
| in_data.range = 2; | ||
| in_data.resolution = 256; | ||
| auto hidden = | ||
| utils::create_and_fill_tensor(funcInputs[1].get_element_type(), targetInputStaticShapes[1], in_data); | ||
| inputs.insert({funcInputs[1].get_node_shared_ptr(), hidden}); | ||
| } | ||
|
|
||
| static constexpr size_t GRID = 64; | ||
| static constexpr size_t BANDS = 4; | ||
| static constexpr size_t HIDDEN_SIZE = GRID * BANDS; | ||
| }; | ||
|
|
||
| TEST_P(RopeTablePrecisionCPUTest, CompareWithRefs) { | ||
| if (!ov::with_cpu_x86_bfloat16()) { | ||
| GTEST_SKIP() << "No bf16 hardware support"; | ||
| } | ||
| run(); | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(smoke_RopeTablePrecision, | ||
| RopeTablePrecisionCPUTest, | ||
| testing::Values(InputShape{ov::PartialShape{-1, 256}, | ||
| {ov::Shape{64, 256}, ov::Shape{32, 256}}}, | ||
| InputShape{ov::PartialShape{64, 256}, {ov::Shape{64, 256}}}), | ||
| RopeTablePrecisionCPUTest::getTestCaseName); | ||
|
|
||
| } // namespace ov::test |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean we should make this attribute copyable? (Not a request for you)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would work, and it'd let us drop both workarounds here (the ConvertToPowerStatic re-apply and the snippets propagation), but it's global. every copy_runtime_info from a marked node would propagate "keep in fp32", including decomposition/fusion passes, so there's an over-marking risk (e.g. a fusion pulling the mark onto a MatMul).
If that's acceptable, I'll set flipping is_copyable() to true and removing the manual re-applies to see what happens, but preferably as a separate PR.