Skip to content

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

[GPU] Fix activation zero-points of oneDNN convolution with reused impls (#39902)

Jade Chobea795b42b26
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/convolution.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/convolution.hpp
index 75eb1d923f9..e40ef06d201 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/convolution.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/convolution.hpp
@@ -207,6 +207,8 @@ struct convolution : public primitive_base<convolution> {
input_info activations_zero_points;
/// @brief Primitive id containing compensation.
input_info compensation;
+ /// @brief All activations zero-point values are equal. Selects the oneDNN zero-point mask, so it is part of the impl cache key.
+ bool activations_zero_point_per_tensor = false;
size_t hash() const override {
size_t seed = primitive::hash();
@@ -227,6 +229,7 @@ struct convolution : public primitive_base<convolution> {
seed = hash_combine(seed, !weights_zero_points.is_valid());
seed = hash_combine(seed, !activations_zero_points.is_valid());
seed = hash_combine(seed, !compensation.is_valid());
+ seed = hash_combine(seed, activations_zero_point_per_tensor);
return seed;
}
@@ -236,6 +239,9 @@ struct convolution : public primitive_base<convolution> {
}
auto rhs_casted = downcast<const convolution>(rhs);
+ if (activations_zero_point_per_tensor != rhs_casted.activations_zero_point_per_tensor) {
+ return false;
+ }
#define cmp_fields(name) name == rhs_casted.name
return cmp_fields(stride) &&
@@ -277,6 +283,7 @@ struct convolution : public primitive_base<convolution> {
ob << weights_zero_points;
ob << activations_zero_points;
ob << compensation;
+ ob << activations_zero_point_per_tensor;
}
void load(BinaryInputBuffer& ib) override {
@@ -298,6 +305,7 @@ struct convolution : public primitive_base<convolution> {
ib >> weights_zero_points;
ib >> activations_zero_points;
ib >> compensation;
+ ib >> activations_zero_point_per_tensor;
}
protected:
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp
index 8cb2d4e5553..f64c9156f72 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_primitive_fusing.cpp
@@ -490,6 +490,7 @@ void prepare_primitive_fusing::fuse_bias(program &p) {
// Copy transposed flag to new prim as convolution node might be produced by deconv -> conv replacement before this pass
conv_with_bias_prim->transposed = desc->transposed;
+ conv_with_bias_prim->activations_zero_point_per_tensor = desc->activations_zero_point_per_tensor;
auto& new_conv_node = p.get_or_create(conv_with_bias_prim);
fuse_bias_f(conv, new_conv_node, bias_node, eltw_node);
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp
index 813515c9e5a..5c526e1e15b 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp
@@ -148,6 +148,9 @@ struct convolution_onednn : typed_primitive_onednn_impl<convolution> {
private:
int _zero_point_mask;
dnnl::memory::data_type _wzp_data_type = dnnl::memory::data_type::undef;
+ // s32 copy of the activations zero-points of this impl's node, and the memory it was converted from
+ mutable memory::ptr _azp_s32;
+ mutable memory::ptr _azp_s32_src;
protected:
std::unique_ptr<primitive_impl> clone() const override {
@@ -178,6 +181,15 @@ protected:
const auto& conv_node = instance.get_node().as<convolution>();
auto& a_zp_node = conv_node.activations_zero_points().as<data>();
a_zp = a_zp_node.get_attached_memory_ptr();
+ // create() converts only the zero-points of its own node. Keep the s32 copy in this impl: the data node is shared
+ if (a_zp->get_layout().data_type != data_types::i32) {
+ if (_azp_s32_src != a_zp) {
+ _azp_s32 = onednn::convert_zp_data_to_s32(a_zp);
+ OPENVINO_ASSERT(_azp_s32 != nullptr, "[GPU] Unsupported activations zero-points type for oneDNN convolution ", instance.id());
+ _azp_s32_src = a_zp;
+ }
+ a_zp = _azp_s32;
+ }
}
dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(a_zp->get_layout(), dnnl::memory::format_tag::a);
@@ -207,14 +219,13 @@ protected:
_wzp_data_type = data_type;
}
- template <typename T>
static void set_activation_zero_points_attr(const std::shared_ptr<dnnl::primitive_attr>& attrs,
- cldnn::data_node& node, int& zero_point_mask) {
- int32_t zp_val = DNNL_RUNTIME_S32_VAL;
- bool is_per_tensor = onednn::is_per_tensor<T>(node, zp_val);
+ cldnn::data_node& node,
+ bool per_tensor,
+ int& zero_point_mask) {
memory::ptr s32_mem = onednn::convert_zp_data_to_s32(node.get_attached_memory_ptr());
node.attach_memory(s32_mem, false);
- zero_point_mask = is_per_tensor ? 0 : 2;
+ zero_point_mask = per_tensor ? 0 : 2;
attrs->set_zero_points_mask(DNNL_ARG_SRC, zero_point_mask);
}
@@ -238,13 +249,9 @@ protected:
throw std::runtime_error("Unsupported data type for activations zero points for oneDNN convolution");
}
- if (a_zp_dtype == data_types::i8) {
- set_activation_zero_points_attr<ov::element_type_traits<data_types::i8>::value_type>(attrs, a_zp.as<data>(), zero_point_mask);
- } else if (a_zp_dtype == data_types::u8) {
- set_activation_zero_points_attr<ov::element_type_traits<data_types::u8>::value_type>(attrs, a_zp.as<data>(), zero_point_mask);
- } else if (a_zp_dtype == data_types::i32) {
- set_activation_zero_points_attr<ov::element_type_traits<data_types::i32>::value_type>(attrs, a_zp.as<data>(), zero_point_mask);
- }
+ // The mask is part of the cached impl: it must depend only on the impl cache key (the flag and the zero-points layout)
+ const bool per_tensor = arg.get_primitive()->activations_zero_point_per_tensor || a_zp.get_output_layout().count() == 1;
+ set_activation_zero_points_attr(attrs, a_zp.as<data>(), per_tensor, zero_point_mask);
}
if (arg.weights_zero_points_term()) {
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp
index 0a18859839c..7db1214111e 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp
@@ -849,29 +849,6 @@ dnnl::algorithm convert_activation_func(cldnn::activation_func func) {
}
}
-template <typename T>
-bool is_per_tensor(cldnn::data_node& node, int32_t& zp_val) {
- auto ptr = node.get_attached_memory_ptr();
- auto* engine = ptr->get_engine();
- auto& stream = engine->get_service_stream();
- auto num_elems = node.get_output_layout().count();
- mem_lock<T, mem_lock_type::read> old_data{ptr, stream};
- auto val = old_data[0];
- for (size_t i = 1; i < num_elems; i++) {
- if (val != old_data[i]) {
- zp_val = DNNL_RUNTIME_S32_VAL;
- return false;
- }
- }
-
- zp_val = val;
- return true;
-}
-
-template bool is_per_tensor<int8_t>(cldnn::data_node& node, int32_t& zp_val);
-template bool is_per_tensor<uint8_t>(cldnn::data_node& node, int32_t& zp_val);
-template bool is_per_tensor<int32_t>(cldnn::data_node& node, int32_t& zp_val);
-
static std::string get_external_order(const std::vector<size_t>& order, bool is_weights, bool is_grouped) {
cldnn::format default_fmt = format::get_default_format(order.size(), is_weights, is_grouped);
const auto& default_order = default_fmt.order();
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp
index 6ec6ccb13fc..fb0dc3a16fc 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp
@@ -86,10 +86,6 @@ size_t get_post_ops_count(const program_node& node);
bool is_supported_post_ops(const program_node& node);
bool is_supported_pad(const layout& layout);
-// Check if data node is per-tensor
-template <typename T>
-bool is_per_tensor(cldnn::data_node& node, int32_t& zp_val);
-
int get_prelu_mask_from_layouts(const std::function<layout()>& get_output_layout,
const std::function<layout(int32_t)>& get_input_layout,
int32_t slope_input_idx);
diff --git a/src/plugins/intel_gpu/src/plugin/ops/convolution.cpp b/src/plugins/intel_gpu/src/plugin/ops/convolution.cpp
index e703766cd70..f0218bd5aef 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/convolution.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/convolution.cpp
@@ -15,6 +15,8 @@
#include "intel_gpu/primitives/deconvolution.hpp"
#include "intel_gpu/primitives/permute.hpp"
+#include <algorithm>
+
namespace ov {
namespace op {
namespace internal {
@@ -26,6 +28,18 @@ using Convolution = ov::intel_gpu::op::Convolution;
namespace ov::intel_gpu {
+// True if the zero-point is a Constant whose elements are all equal
+static bool is_uniform_zero_point(const std::shared_ptr<ov::Node>& node) {
+ auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(node);
+ if (!zp_const || ov::shape_size(zp_const->get_shape()) == 0) {
+ return false;
+ }
+ const auto values = zp_const->cast_vector<double>();
+ return std::all_of(values.begin(), values.end(), [&](double v) {
+ return v == values[0];
+ });
+}
+
static void CreateConvolutionOp(ProgramBuilder& p, const std::shared_ptr<ov::intel_gpu::op::Convolution>& op) {
validate_inputs_count(op, {3, 6});
auto inputs = p.GetInputInfo(op);
@@ -74,6 +88,7 @@ static void CreateConvolutionOp(ProgramBuilder& p, const std::shared_ptr<ov::int
op->get_output_element_type(0),
auto_pad,
filter_rank);
+ prim->activations_zero_point_per_tensor = is_uniform_zero_point(op->get_input_node_shared_ptr(op::Convolution::Args::AZP));
} else {
prim = std::make_shared<cldnn::convolution>(layerName,
inputs[op::Convolution::Args::INPUT],
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
index 92d7198894e..e74260d807b 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
@@ -13883,3 +13883,89 @@ TEST(convolution_1d_small_ic_gemm_gate, rejects_input_features_above_max) {
ASSERT_ANY_THROW(run_conv_1d(c, c.dt, "convolution_gpu_1d_small_ic_gemm"))
<< "kernel accepted IC = " << c.in_features;
}
+
+namespace {
+// Two dynamic oneDNN convolutions with equal shapes: the second one reuses the impl created for the first one
+void test_onednn_conv_dynamic_azp_impl_reuse(bool per_channel0, bool per_channel1, bool set_per_tensor_flag = true) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ const int64_t ic = 16, oc = 16, h = 6, w = 6, k = 3, oh = h - k + 1, ow = w - k + 1;
+ tests::random_generator rg(GET_SUITE_NAME);
+ std::vector<uint8_t> input_data(ic * h * w);
+ for (auto& v : input_data)
+ v = static_cast<uint8_t>(rg.generate_random_val<int>(0, 40, 1));
+
+ std::array<std::vector<int>, 2> wv, zp;
+ for (size_t i = 0; i < 2; ++i) {
+ wv[i].resize(oc * ic * k * k);
+ for (auto& v : wv[i])
+ v = rg.generate_random_val<int>(-10, 10, 1);
+ zp[i].resize(ic);
+ for (int64_t c = 0; c < ic; ++c)
+ zp[i][c] = (i == 0 ? per_channel0 : per_channel1) ? static_cast<int>(3 + 7 * i + c) : static_cast<int>(3 + 14 * i);
+ }
+
+ auto input_mem = engine.allocate_memory({ov::PartialShape{1, ic, h, w}, data_types::u8, format::bfyx});
+ set_values(input_mem, input_data);
+ topology topology(input_layout("input", layout{ov::PartialShape{-1, ic, -1, -1}, data_types::u8, format::bfyx}));
+ for (size_t i = 0; i < 2; ++i) {
+ const auto idx = std::to_string(i);
+ auto weights_mem = engine.allocate_memory({ov::PartialShape{oc, ic, k, k}, data_types::i8, format::bfyx});
+ set_values(weights_mem, std::vector<int8_t>(wv[i].begin(), wv[i].end()));
+ auto azp_mem = engine.allocate_memory({ov::PartialShape{1, ic, 1, 1}, data_types::u8, format::bfyx});
+ set_values(azp_mem, std::vector<uint8_t>(zp[i].begin(), zp[i].end()));
+ topology.add(data("weights" + idx, weights_mem), data("azp" + idx, azp_mem));
+ auto conv_prim =
+ convolution("conv" + idx, input_info("input"), "weights" + idx, "", "", "azp" + idx, "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false, data_types::f32);
+ // Set by the plugin (ops/convolution.cpp) when the OV Constant is converted to the primitive
+ if (set_per_tensor_flag)
+ conv_prim.activations_zero_point_per_tensor = !(i == 0 ? per_channel0 : per_channel1);
+ topology.add(conv_prim);
+ topology.add(reorder("out" + idx, input_info("conv" + idx), format::bfyx, data_types::f32));
+ }
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ network network(engine, topology, config);
+ network.set_input_data("input", input_mem);
+ auto outputs = network.execute();
+
+ for (size_t i = 0; i < 2; ++i) {
+ mem_lock<float, mem_lock_type::read> out(outputs.at("out" + std::to_string(i)).get_memory(), get_test_stream());
+ ASSERT_EQ(out.size(), static_cast<size_t>(oc * oh * ow));
+ for (int64_t o = 0; o < oc; ++o)
+ for (int64_t y = 0; y < oh; ++y)
+ for (int64_t x = 0; x < ow; ++x) {
+ int32_t acc = 0;
+ for (int64_t c = 0; c < ic; ++c)
+ for (int64_t ky = 0; ky < k; ++ky)
+ for (int64_t kx = 0; kx < k; ++kx)
+ acc += (static_cast<int32_t>(input_data[(c * h + y + ky) * w + x + kx]) - zp[i][c]) *
+ wv[i][((o * ic + c) * k + ky) * k + kx];
+ ASSERT_EQ(static_cast<float>(acc), out[(o * oh + y) * ow + x]) << "conv" << i << " o=" << o << " y=" << y << " x=" << x;
+ }
+ }
+}
+} // namespace
+
+TEST(convolution_onednn_dynamic_azp, impl_reuse_per_tensor) {
+ test_onednn_conv_dynamic_azp_impl_reuse(false, false);
+}
+TEST(convolution_onednn_dynamic_azp, impl_reuse_per_channel) {
+ test_onednn_conv_dynamic_azp_impl_reuse(true, true);
+}
+// Same shapes, different zero-point kinds: the impl must not be shared between a per-tensor and a per-channel conv.
+// conv1 is executed before conv0 and creates the impl that conv0 reuses
+TEST(convolution_onednn_dynamic_azp, impl_reuse_mixed_conv0_tensor_conv1_channel) {
+ test_onednn_conv_dynamic_azp_impl_reuse(false, true);
+}
+TEST(convolution_onednn_dynamic_azp, impl_reuse_mixed_conv0_channel_conv1_tensor) {
+ test_onednn_conv_dynamic_azp_impl_reuse(true, false);
+}
+// The per-tensor flag is not set (e.g. the zero-point is not a Constant): uniform data must not make the impl per-tensor
+TEST(convolution_onednn_dynamic_azp, impl_reuse_no_flag_conv0_channel_conv1_uniform) {
+ test_onednn_conv_dynamic_azp_impl_reuse(true, false, false);
+}