Skip to content

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

[GPU] Fix FakeQuantize scale/shift precompute for 5D+ ranges (#39638)

Jade Choaf765e256192
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp
index 291d4794d37..9817f9fc451 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp
@@ -90,14 +90,14 @@ void prepare_quantization::prepare_scale_shift_opt(program &p, quantize_node& qu
auto mem_output_scale = p.get_engine().allocate_memory(scales_layout, false);
auto mem_output_shift = p.get_engine().allocate_memory(scales_layout, false);
- auto get_offset_safe = [](const layout& l, const tensor& idx) -> tensor::value_type {
- auto sizes = l.get_tensor();
- auto pitches = l.get_pitches();
-
- return (idx.batch[0] % sizes.batch[0])*pitches[0]
- + (idx.feature[0] % sizes.feature[0])*pitches[1]
- + (idx.spatial[1] % sizes.spatial[1])*pitches[2 + 0] // y
- + (idx.spatial[0] % sizes.spatial[0])*pitches[2 + 1]; // x
+ // Returns the linear offset of idx in layout l, broadcasting dimensions of size 1.
+ auto get_offset_safe = [](const layout& l, const tensor& idx) -> size_t {
+ const auto sizes = l.get_tensor();
+ tensor safe_idx(0);
+ for (size_t i = 0; i < safe_idx.raw.size(); i++) {
+ safe_idx.raw[i] = sizes.raw[i] > 0 ? idx.raw[i] % sizes.raw[i] : 0;
+ }
+ return l.get_linear_offset(safe_idx);
};
auto lock_memory = [&stream] (memory::ptr memory, std::function<void(std::size_t, float)>& set_data,
@@ -185,34 +185,42 @@ void prepare_quantization::prepare_scale_shift_opt(program &p, quantize_node& qu
auto primitive = quantize_node.get_primitive();
int levels = primitive->levels;
- for (int b = 0; b < scales_layout.batch(); b++) {
- for (int f = 0; f < scales_layout.feature(); f++) {
- for (int y = 0; y < scales_layout.spatial(1); y++) {
- for (int x = 0; x < scales_layout.spatial(0); x++) {
- auto idx = cldnn::tensor(format::bfyx, {b, f, y, x}, 0);
- auto s_offset = scales_layout.get_linear_offset(idx);
- float in_lo = get_data_input_low(get_offset_safe(mem_input_low->get_layout(), idx));
- float in_hi = get_data_input_high(get_offset_safe(mem_input_high->get_layout(), idx));
-
- float out_lo = get_data_output_low(get_offset_safe(mem_output_low->get_layout(), idx));
- float out_hi = get_data_output_high(get_offset_safe(mem_output_high->get_layout(), idx));
- float in_shift_basic = (static_cast<float>(levels) - 1.f) / (in_hi - in_lo);
- set_data_input_scale(s_offset, in_shift_basic);
- set_data_input_shift(s_offset, -in_lo * in_shift_basic);
- set_data_output_scale(s_offset, (out_hi - out_lo) / (static_cast<float>(levels) - 1.f));
- set_data_output_shift(s_offset, out_lo);
-
- if (get_data_output_scale(s_offset) != 1.0f) {
- need_post_scale = true;
- }
- if (get_data_output_shift(s_offset) != 0.0f) {
- need_post_shift = true;
- }
- if (get_data_input_scale(s_offset) < 0.0f) {
- has_negative_scales = true;
- }
- }
- }
+ // Visit every element of the scales tensor regardless of its rank (b, f and all spatial dimensions).
+ const auto scales_size = scales_layout.get_tensor();
+ const auto in_lo_layout = mem_input_low->get_layout();
+ const auto in_hi_layout = mem_input_high->get_layout();
+ const auto out_lo_layout = mem_output_low->get_layout();
+ const auto out_hi_layout = mem_output_high->get_layout();
+ const size_t scales_count = scales_size.count();
+ for (size_t i = 0; i < scales_count; i++) {
+ tensor idx(0);
+ size_t rem = i;
+ for (size_t d = idx.raw.size(); d-- > 0;) {
+ const auto dim = static_cast<size_t>(std::max<tensor::value_type>(scales_size.raw[d], 1));
+ idx.raw[d] = static_cast<tensor::value_type>(rem % dim);
+ rem /= dim;
+ }
+
+ auto s_offset = scales_layout.get_linear_offset(idx);
+ float in_lo = get_data_input_low(get_offset_safe(in_lo_layout, idx));
+ float in_hi = get_data_input_high(get_offset_safe(in_hi_layout, idx));
+
+ float out_lo = get_data_output_low(get_offset_safe(out_lo_layout, idx));
+ float out_hi = get_data_output_high(get_offset_safe(out_hi_layout, idx));
+ float in_shift_basic = (static_cast<float>(levels) - 1.f) / (in_hi - in_lo);
+ set_data_input_scale(s_offset, in_shift_basic);
+ set_data_input_shift(s_offset, -in_lo * in_shift_basic);
+ set_data_output_scale(s_offset, (out_hi - out_lo) / (static_cast<float>(levels) - 1.f));
+ set_data_output_shift(s_offset, out_lo);
+
+ if (get_data_output_scale(s_offset) != 1.0f) {
+ need_post_scale = true;
+ }
+ if (get_data_output_shift(s_offset) != 0.0f) {
+ need_post_shift = true;
+ }
+ if (get_data_input_scale(s_offset) < 0.0f) {
+ has_negative_scales = true;
}
}
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp
index 540eeb95ae4..a8934bbc7be 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp
@@ -10,6 +10,7 @@
#include <intel_gpu/primitives/data.hpp>
#include "quantize_inst.h"
+#include <cmath>
#include <cstddef>
using namespace cldnn;
@@ -1284,3 +1285,119 @@ INSTANTIATE_TEST_SUITE_P(quantize_smoke,
quantize_random_test_params{ data_types::f32, data_types::u8, {1, 16, 10, 10}, format::bs_fs_yx_bsv32_fsv32, format::bs_fs_yx_bsv32_fsv32, 5}
));
#endif
+
+// Ranges of rank >= 5 that vary along spatial dimensions (e.g. weight FakeQuantize ranges [C,1,1,1,KW]
+// produced by NNCF for depthwise GroupConvolution) must be broadcast per dimension when
+// prepare_quantization precomputes scales and shifts.
+struct quantize_nd_ranges_params {
+ ov::Shape data_shape;
+ ov::Shape range_shape;
+};
+
+class quantize_nd_ranges_test : public ::testing::TestWithParam<quantize_nd_ranges_params> {};
+
+TEST_P(quantize_nd_ranges_test, per_element_ranges) {
+ const auto& p = GetParam();
+ auto& engine = get_test_engine();
+ const int levels = 255;
+ const auto fmt = format::get_default_format(p.data_shape.size());
+
+ const size_t data_count = ov::shape_size(p.data_shape);
+ const size_t range_count = ov::shape_size(p.range_shape);
+ const auto rank = p.data_shape.size();
+
+ std::vector<float> in_lo(range_count), in_hi(range_count), out_lo(range_count), out_hi(range_count);
+ for (size_t i = 0; i < range_count; i++) {
+ in_hi[i] = 1.0f + 0.5f * static_cast<float>(i);
+ in_lo[i] = -in_hi[i];
+ out_lo[i] = -2.0f * in_hi[i];
+ out_hi[i] = 3.0f * in_hi[i];
+ }
+
+ // Maps a data element to the broadcast range element (numpy broadcasting, same rank).
+ auto range_index = [&](size_t data_idx) {
+ size_t r_idx = 0;
+ size_t r_pitch = 1;
+ for (size_t d = rank; d-- > 0;) {
+ const size_t coord = data_idx % p.data_shape[d];
+ data_idx /= p.data_shape[d];
+ if (p.range_shape[d] != 1)
+ r_idx += coord * r_pitch;
+ r_pitch *= p.range_shape[d];
+ }
+ return r_idx;
+ };
+
+ // Input values are placed at a quarter step between quantization levels (no rounding ties),
+ // with some of them outside of [in_lo, in_hi] to exercise clamping.
+ std::vector<float> input_data(data_count), ref(data_count);
+ for (size_t i = 0; i < data_count; i++) {
+ const size_t r = range_index(i);
+ const float step = (in_hi[r] - in_lo[r]) / static_cast<float>(levels - 1);
+ const int k = static_cast<int>((i * 37) % (levels + 10)) - 5;
+ input_data[i] = in_lo[r] + (static_cast<float>(k) + 0.25f) * step;
+
+ const float x = input_data[i];
+ if (x <= in_lo[r]) {
+ ref[i] = out_lo[r];
+ } else if (x > in_hi[r]) {
+ ref[i] = out_hi[r];
+ } else {
+ const float q = std::round((x - in_lo[r]) / (in_hi[r] - in_lo[r]) * static_cast<float>(levels - 1));
+ ref[i] = q / static_cast<float>(levels - 1) * (out_hi[r] - out_lo[r]) + out_lo[r];
+ }
+ }
+
+ auto input = engine.allocate_memory({ov::PartialShape(p.data_shape), data_types::f32, fmt});
+ auto make_range = [&](const std::vector<float>& values) {
+ auto mem = engine.allocate_memory({ov::PartialShape(p.range_shape), data_types::f32, fmt});
+ set_values(mem, values);
+ return mem;
+ };
+ set_values(input, input_data);
+
+ topology topology;
+ topology.add(input_layout("input", input->get_layout()),
+ data("input_low", make_range(in_lo)),
+ data("input_high", make_range(in_hi)),
+ data("output_low", make_range(out_lo)),
+ data("output_high", make_range(out_hi)),
+ quantize("quantize",
+ input_info("input"),
+ input_info("input_low"),
+ input_info("input_high"),
+ input_info("output_low"),
+ input_info("output_high"),
+ levels,
+ data_types::f32),
+ reorder("out", input_info("quantize"), fmt, data_types::f32));
+
+ // optimize_data is required to run prepare_quantization (scale/shift precomputation).
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ network network(engine, topology, config);
+ network.set_input_data("input", input);
+ auto outputs = network.execute();
+
+ auto output = outputs.at("out").get_memory();
+ cldnn::mem_lock<float, mem_lock_type::read> output_ptr(output, get_test_stream());
+ ASSERT_EQ(output->count(), data_count);
+ for (size_t i = 0; i < data_count; i++) {
+ ASSERT_NEAR(output_ptr[i], ref[i], 1e-4f * std::max(1.0f, std::fabs(ref[i]))) << " index = " << i;
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(quantize_smoke,
+ quantize_nd_ranges_test,
+ testing::Values(
+ // 5D: depthwise GroupConvolution weights [G,O,I,KH,KW]
+ quantize_nd_ranges_params{{16, 1, 1, 3, 3}, {16, 1, 1, 1, 3}},
+ quantize_nd_ranges_params{{16, 1, 1, 3, 3}, {16, 1, 1, 3, 1}},
+ quantize_nd_ranges_params{{16, 1, 1, 3, 3}, {16, 1, 1, 3, 3}},
+ quantize_nd_ranges_params{{16, 1, 1, 3, 3}, {1, 1, 1, 1, 3}},
+ quantize_nd_ranges_params{{4, 2, 3, 3, 3}, {4, 1, 3, 1, 1}},
+ quantize_nd_ranges_params{{2, 8, 4, 5, 6}, {1, 8, 4, 1, 6}},
+ // 6D
+ quantize_nd_ranges_params{{2, 3, 4, 2, 3, 5}, {1, 3, 4, 1, 1, 5}},
+ // 4D (regression check for the previously supported path)
+ quantize_nd_ranges_params{{16, 1, 3, 3}, {16, 1, 1, 3}}));