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] GroupedMatMul: fixed decompression layout (#38400)
Vladislav Golubev43f0b9307f9d
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.cppindex 1e42a7d6f1e..3ee667dbc8f 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@@ -4,6 +4,7 @@#include "fully_connected_inst.h"#include "gather_matmul_inst.h"+#include "grouped_matmul_inst.h"#include "moe_3gemm_fused_inst.h"#include "moe_gemm_inst.h"#include "impls/ocl_v2/moe/moe_3gemm_base.hpp"@@ -638,6 +639,18 @@ static void optimize_gather_matmul_decompression_parameters(gather_matmul_node&reorder_decompression_param_to_byfx(node, gather_matmul::WEIGHT_ZP, p);}+// onednn grouped matmul reads group-wise scales/zp as [G, K/gs, N] (N innermost), while the graph provides+// [G, N, K/gs]. The reorder to byfx is constant-folded at compile time.+static void optimize_grouped_matmul_decompression_parameters(grouped_matmul_node& node, program& p) {+ auto prim = node.get_primitive();+ if (!prim->compressed_weights)+ return;+ size_t idx = grouped_matmul::GroupedMatmulInputIdx::OFFSETS + 1;+ reorder_decompression_param_to_byfx(node, idx++, p);+ if (prim->decompression_zero_point.is_valid())+ reorder_decompression_param_to_byfx(node, idx, p);+}+static void optimize_moe_gemm_decompression_parameters(moe_gemm_node& node, program& p) {auto prim = node.get_primitive();// Production has bias; tests may not.@@ -698,6 +711,8 @@ void prepare_quantization::run(program& p) {optimize_weights_decompression_parameters(node->as<fully_connected>(), p);} else if (node->is_type<gather_matmul>()) {optimize_gather_matmul_decompression_parameters(node->as<gather_matmul>(), p);+ } else if (node->is_type<grouped_matmul>()) {+ optimize_grouped_matmul_decompression_parameters(node->as<grouped_matmul>(), p);} else if (node->is_type<moe_gemm>()) {optimize_moe_gemm_decompression_parameters(node->as<moe_gemm>(), p);} else if (node->is_type<moe_3gemm_fused_compressed>()) {diff --git a/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp b/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cppindex c8f21cba0ae..b07cabbe0aa 100644--- a/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp+++ b/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp@@ -5,10 +5,13 @@#include "common_test_utils/subgraph_builders/weights_decompression_builders.hpp"#include <algorithm>+#include <array>#include <limits>#include "common_test_utils/node_builders/constant.hpp"#include "openvino/core/type/element_iterator.hpp"+#include "openvino/core/validation_util.hpp"+#include "openvino/op/concat.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/multiply.hpp"#include "openvino/op/parameter.hpp"@@ -240,6 +243,9 @@ std::shared_ptr<ov::Node> initMatMulDecompressionSubgraphQuantization(// G - group sizeauto transformed_weights_shape = transpose_if_necessary(weights_shape);const auto IC = *(weights_shape.rbegin() + 1);+ const float fp32_min = -0.1f;+ const float fp32_max = 0.1f;+ std::shared_ptr<ov::op::v0::Constant> fp32_weights;if (group_decompression) {OPENVINO_ASSERT(IC % group_size == 0,"Weights output channels count (",@@ -249,18 +255,41 @@ std::shared_ptr<ov::Node> initMatMulDecompressionSubgraphQuantization(").");auto in_channel_idx =transpose_weights ? transformed_weights_shape.size() - 1 : transformed_weights_shape.size() - 2;- transformed_weights_shape[in_channel_idx] = IC / group_size;+ const size_t n_groups = IC / group_size;+ transformed_weights_shape[in_channel_idx] = n_groups;transformed_weights_shape.insert(transformed_weights_shape.begin() + in_channel_idx + 1, group_size);- }- // mt19937 distribution: gtest's LCG repeats every k_range, collapsing per-group min/max diversity.- const float fp32_min = -0.1f;- const float fp32_max = 0.1f;- auto fp32_weights_tensor = ov::test::utils::create_and_fill_tensor_real_distribution(ov::element::f32,- transformed_weights_shape,- fp32_min,- fp32_max,- static_cast<int>(seed));+ // With a single [fp32_min, fp32_max) distribution, all groups get nearly the same min/max, i.e. near-identical+ // scales and zero points, so a plugin that reads the decompression constants+ // in a wrong layout (e.g. misses a [N, K/gs] -> [K/gs, N] reorder) may still not cause the test failure.+ // So every slice along the groups axis is generated from its own sub-range, and the slices are concatenated.+ constexpr std::array<float, 3> width_ratios{1.0f, 0.5f, 0.25f};+ constexpr std::array<float, 5> position_ratios{0.0f, 1.0f, 0.5f, 0.25f, 0.75f};+ const float full_width = fp32_max - fp32_min;+ auto slice_shape = transformed_weights_shape;+ slice_shape[in_channel_idx] = 1;+ ov::OutputVector slices;+ for (size_t g = 0; g < n_groups; ++g) {+ const float width = full_width * width_ratios[g % width_ratios.size()];+ const float slice_min = fp32_min + position_ratios[g % position_ratios.size()] * (full_width - width);+ auto slice_tensor = ov::test::utils::create_and_fill_tensor_real_distribution(ov::element::f32,+ slice_shape,+ slice_min,+ slice_min + width,+ static_cast<int>(seed + g));+ slices.push_back(std::make_shared<ov::op::v0::Constant>(slice_tensor));+ }+ const auto concat = std::make_shared<ov::op::v0::Concat>(slices, static_cast<int64_t>(in_channel_idx));+ fp32_weights = ov::util::get_constant_from_source(concat);+ OPENVINO_ASSERT(fp32_weights, "Failed to constant-fold the concatenated FP32 weights");+ } else {+ fp32_weights = std::make_shared<ov::op::v0::Constant>(+ ov::test::utils::create_and_fill_tensor_real_distribution(ov::element::f32,+ transformed_weights_shape,+ fp32_min,+ fp32_max,+ static_cast<int>(seed)));+ }// Calculate quantization parametersconst auto qmin = weights_precision == ov::element::u2 ? 0.0f@@ -274,7 +303,7 @@ std::shared_ptr<ov::Node> initMatMulDecompressionSubgraphQuantization(: weights_precision.is_signed() ? 127.0f: 255.0f;- auto* fp32_data = fp32_weights_tensor.data<float>();+ const auto* fp32_data = fp32_weights->get_data_ptr<float>();const size_t total_size = ov::shape_size(transformed_weights_shape);const auto OC = weights_shape.back();