Skip to content

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

[GPU] Disable FakeQuantize decomposition in activations scaling LPT (#39127)

Andrew Kwangwoong Park7dd23acd6389
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index d9d6ce1c253..8ac9f5eb906 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -27,6 +27,7 @@
#include "low_precision/concat.hpp"
#include "low_precision/convolution.hpp"
#include "low_precision/convolution_backprop_data.hpp"
+#include "low_precision/fake_quantize_decomposition.hpp"
#include "low_precision/fold_convert.hpp"
#include "low_precision/fuse_convert.hpp"
#include "low_precision/group_convolution.hpp"
@@ -1679,6 +1680,10 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
pass_config->disable<GroupConvolutionTransformation>();
pass_config->disable<MatMulTransformation>();
pass_config->disable<MVNTransformation>();
+ // This LPT run only moves scalar Multiply layers. FakeQuantize layers left by the INT8 LPT run (e.g. Q/K
+ // quantization before SDPA) must not be decomposed again: the f16 DataPrecision interval (+/-1e15)
+ // overflows f16 and turns the quantized activations into inf/NaN.
+ pass_config->disable<FakeQuantizeDecompositionTransformation>();
pass_config->set_callback<FoldConvertTransformation>(
[](const std::shared_ptr<const ov::Node> &node) -> bool {
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/activations_scaling.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/activations_scaling.cpp
index 0af723d58f2..807428f6e46 100644
--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/activations_scaling.cpp
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/activations_scaling.cpp
@@ -15,11 +15,14 @@
#include "openvino/op/add.hpp"
#include "openvino/op/sqrt.hpp"
#include "openvino/op/divide.hpp"
+#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/power.hpp"
#include "openvino/op/reduce_mean.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/reshape.hpp"
+#include "openvino/op/scaled_dot_product_attention.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/variadic_split.hpp"
@@ -272,4 +275,112 @@ TEST_F(ActivationsScalingFeatureAxisRMS, Inference) {
core->set_property(targetDevice, ov::hint::activations_scale_factor(8.f));
run();
}
+
+// FakeQuantize layers kept by the INT8 LPT run (Q/K quantization in front of SDPA, as in INT8 hybrid quantized
+// diffusion transformers) must keep their intervals when activations scaling runs LPT again. Decomposing them with
+// the f16 DataPrecision interval overflows f16 in the standalone quantize primitive and SDPA returns NaN.
+//
+// input
+// |
+// +--------------+--------------+
+// | | |
+// MatMul MatMul MatMul
+// | | |
+// Reshape Reshape Reshape
+// | | |
+// RoPE RoPE |
+// | | |
+// FakeQuantize FakeQuantize |
+// | | |
+// Transpose Transpose Transpose
+// | Q | K | V
+// +--------------+--------------+
+// |
+// SDPA
+// |
+// Transpose
+// |
+// Reshape
+// |
+// MatMul
+class ActivationsScalingFakeQuantizeSDPA : public ov::test::SubgraphBaseTest {
+protected:
+ static constexpr int64_t num_heads = 2;
+ static constexpr int64_t head_size = 64;
+ static constexpr int64_t hidden_size = num_heads * head_size;
+
+ void SetUp() override {
+ targetDevice = ov::test::utils::DEVICE_GPU;
+ init_input_shapes({{{-1, -1, hidden_size}, {{1, 32, hidden_size}}},
+ {{1, -1, 1, head_size}, {{1, 32, 1, head_size}}},
+ {{1, -1, 1, head_size}, {{1, 32, 1, head_size}}}});
+ inType = outType = ov::element::f32;
+ abs_threshold = 0.01f;
+ // Activations scaling applies to f16 inference only, while core_configuration() selects f32 for f32 parameters.
+ configuration.insert(ov::hint::inference_precision(ov::element::f16));
+
+ ov::ParameterVector params;
+ for (const auto& shape : inputDynamicShapes) {
+ params.push_back(std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape));
+ }
+ const auto& input = params[0];
+ const auto& cos = params[1];
+ const auto& sin = params[2];
+
+ int seed = 1;
+ auto linear = [&](const ov::Output<ov::Node>& data) {
+ const auto weights =
+ ov::test::utils::create_and_fill_tensor_real_distribution(ov::element::f32, ov::Shape{hidden_size, hidden_size}, -0.1f, 0.1f, seed++);
+ return std::make_shared<ov::op::v0::MatMul>(data, std::make_shared<ov::op::v0::Constant>(weights), false, true);
+ };
+ auto split_heads = [](const ov::Output<ov::Node>& data) {
+ const auto shape = ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{0, 0, num_heads, head_size});
+ return std::make_shared<ov::op::v1::Reshape>(data, shape, true);
+ };
+ auto slice_last_axis = [](const ov::Output<ov::Node>& data, int64_t start, int64_t stop) {
+ return std::make_shared<ov::op::v8::Slice>(data,
+ ov::op::v0::Constant::create(ov::element::i64, {1}, {start}),
+ ov::op::v0::Constant::create(ov::element::i64, {1}, {stop}),
+ ov::op::v0::Constant::create(ov::element::i64, {1}, {1}),
+ ov::op::v0::Constant::create(ov::element::i64, {1}, {3}));
+ };
+ auto rope = [&](const ov::Output<ov::Node>& data) {
+ const auto second_half = slice_last_axis(data, head_size / 2, std::numeric_limits<int64_t>::max());
+ const auto negated = std::make_shared<ov::op::v1::Multiply>(second_half, ov::op::v0::Constant::create(ov::element::f32, {}, {-1.f}));
+ const auto rotated = std::make_shared<ov::op::v0::Concat>(ov::NodeVector{negated, slice_last_axis(data, 0, head_size / 2)}, -1);
+ return std::make_shared<ov::op::v1::Add>(std::make_shared<ov::op::v1::Multiply>(data, cos), std::make_shared<ov::op::v1::Multiply>(rotated, sin));
+ };
+ auto fake_quantize = [](const ov::Output<ov::Node>& data) {
+ const auto low = ov::op::v0::Constant::create(ov::element::f32, {}, {-4.f});
+ const auto high = ov::op::v0::Constant::create(ov::element::f32, {}, {4.f});
+ return std::make_shared<ov::op::v0::FakeQuantize>(data, low, high, low, high, 256);
+ };
+ const auto order = ov::op::v0::Constant::create(ov::element::i64, {4}, {0, 2, 1, 3});
+
+ const auto q = std::make_shared<ov::op::v1::Transpose>(fake_quantize(rope(split_heads(linear(input)))), order);
+ const auto k = std::make_shared<ov::op::v1::Transpose>(fake_quantize(rope(split_heads(linear(input)))), order);
+ const auto v = std::make_shared<ov::op::v1::Transpose>(split_heads(linear(input)), order);
+ const auto sdpa = std::make_shared<ov::op::v13::ScaledDotProductAttention>(q, k, v, false);
+
+ const auto merged = std::make_shared<ov::op::v1::Reshape>(std::make_shared<ov::op::v1::Transpose>(sdpa, order),
+ ov::op::v0::Constant::create(ov::element::i64, {3}, std::vector<int64_t>{0, 0, hidden_size}),
+ true);
+ function = std::make_shared<ov::Model>(ov::OutputVector{linear(merged)}, params, "ActivationsScalingFakeQuantizeSDPA");
+ }
+
+ void generate_inputs(const std::vector<ov::Shape>& target_input_static_shapes) override {
+ inputs.clear();
+ const auto& model_inputs = function->inputs();
+ for (size_t i = 0; i < model_inputs.size(); ++i) {
+ const auto tensor =
+ ov::test::utils::create_and_fill_tensor_real_distribution(ov::element::f32, target_input_static_shapes[i], -1.f, 1.f, static_cast<int>(i) + 1);
+ inputs.insert({model_inputs[i].get_node_shared_ptr(), tensor});
+ }
+ }
+};
+
+TEST_F(ActivationsScalingFakeQuantizeSDPA, smoke_Inference) {
+ core->set_property(targetDevice, ov::hint::activations_scale_factor(8.f));
+ run();
+}
} // namespace