Skip to content

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

[GPU] Enable 3d compressed FC (#31690)

Taylor Yeonbok Lee0a49c3257ff7
diff --git a/src/core/src/op/transpose.cpp b/src/core/src/op/transpose.cpp
index e6f979fca0c..a19944fd013 100644
--- a/src/core/src/op/transpose.cpp
+++ b/src/core/src/op/transpose.cpp
@@ -95,20 +95,44 @@ bool Transpose::evaluate(TensorVector& outputs, const TensorVector& inputs) cons
int4_extract_t m_half;
};
- auto out_ptr = int4_iterator(static_cast<uint8_t*>(out.data()));
- // The int4_iterator not supports const pointer but these data are not modified
- auto in_ptr = int4_iterator(static_cast<uint8_t*>(const_cast<void*>(arg.data())));
- if ((arg_type == ov::element::i4 || arg_type == ov::element::u4) && arg.get_shape().size() == 2) {
- const auto out_shape_d0 = out_shape[0];
- const auto out_shape_d1 = out_shape[1];
-
- for (size_t i = 0; i < out_shape_d0; i++) {
- size_t off = i;
- for (size_t j = 0; j < out_shape_d1; j++) {
- out_ptr.copy_from(in_ptr + off);
- ++out_ptr;
- off += out_shape_d0;
+ if (arg_type == ov::element::i4 || arg_type == ov::element::u4) {
+ // The int4_iterator not supports const pointer but these data are not modified
+ auto transpose_xy =
+ [](int4_iterator& out_ptr, int4_iterator& in_ptr, size_t out_shape_d0, size_t out_shape_d1) {
+ for (size_t i = 0; i < out_shape_d0; i++) {
+ size_t off = i;
+ for (size_t j = 0; j < out_shape_d1; j++) {
+ out_ptr.copy_from(in_ptr + off);
+ ++out_ptr;
+ off += out_shape_d0;
+ }
+ }
+ };
+ if (arg.get_shape().size() == 2) {
+ auto out_ptr = int4_iterator(static_cast<uint8_t*>(out.data()));
+ auto in_ptr = int4_iterator(static_cast<uint8_t*>(const_cast<void*>(arg.data())));
+ const auto out_shape_d0 = out_shape[0];
+ const auto out_shape_d1 = out_shape[1];
+ transpose_xy(out_ptr, in_ptr, out_shape_d0, out_shape_d1);
+ } else if (arg.get_shape().size() == 3) {
+ OPENVINO_ASSERT(axes_order[0] == 0 && axes_order[1] == 2 && axes_order[2] == 1,
+ "Unsupported transpose order for i4/u4 type");
+ const auto out_batch = out_shape[0];
+ const auto out_shape_d0 = out_shape[1];
+ const auto out_shape_d1 = out_shape[2];
+ OPENVINO_ASSERT((out_shape_d0 * out_shape_d1) % 2 == 0,
+ "Only supports even number of i4/u4 data in each batch for transposing");
+ size_t batch_offset = 0;
+ for (size_t b = 0; b < out_batch; b++) {
+ uint8_t* out_batch_base = static_cast<uint8_t*>(out.data()) + batch_offset / 2;
+ uint8_t* in_batch_base = (static_cast<uint8_t*>(const_cast<void*>(arg.data()))) + batch_offset / 2;
+ auto out_ptr = int4_iterator(out_batch_base);
+ auto in_ptr = int4_iterator(in_batch_base);
+ transpose_xy(out_ptr, in_ptr, out_shape_d0, out_shape_d1);
+ batch_offset += out_shape_d0 * out_shape_d1;
}
+ } else {
+ OPENVINO_THROW("Transpose for i4/u4 dtype is supported only for ndims <= 3");
}
} else {
reference::transpose(static_cast<const char*>(arg.data()),
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 9f67ec1d2f9..18714e88705 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
@@ -533,29 +533,41 @@ static void optimize_weights_decompression_parameters(fully_connected_node& fc_n
fc_node.get_dependency(dep_id).recalc_output_layout(false);
};
- auto need_reorder = [&](size_t dep_id) {
+ auto need_reorder = [&](size_t dep_id, size_t weight_rank) {
auto dep_layout = fc_node.get_input_layout(dep_id);
auto dep_pshape = dep_layout.get_partial_shape();
- if (dep_pshape.size() == 0) {
+ auto dep_rank = dep_pshape.size();
+ if (dep_rank == 0) {
// ConvertU4WeightsZeroPointToScalar pass generates scalar const layer
return false;
}
- // Group for scale_idx is always 1, whereas zero_point_idx is 0.
- auto groups_idx = (dep_pshape.size() > 1) ? 1 : 0;
+ auto groups_idx = dep_rank == 1 ? 0 : weight_rank - 1;
auto groups_count = dep_pshape[groups_idx].get_length();
-
return groups_count > 1;
};
+ // possible cases
+ // legacy [K, N, 1, 1] => crop padded dims
+ // new shape [1, K, N] => preserve
+ auto weights_shape = fc_node.get_input_layout(1).get_partial_shape();
+ auto weight_rank = weights_shape.size();
+ if (weight_rank >= 3 && weights_shape[0] != 1) {
+ // legacy case
+ weight_rank = std::count_if(weights_shape.begin(), weights_shape.end(), [](ov::Dimension d) {
+ return d.get_length() > 1;
+ });
+
+ weight_rank = std::max(static_cast<size_t>(2), weight_rank);
+ }
auto decompression_scale_idx = !fc_node.bias_term() ? 2 : 3;
- if (need_reorder(decompression_scale_idx)) {
+ if (need_reorder(decompression_scale_idx, weight_rank)) {
reorder_bfyx_to_fbyx(decompression_scale_idx);
}
if (fc_prim->decompression_zero_point.is_valid()) {
auto decompression_zp_idx = decompression_scale_idx + 1;
- if (need_reorder(decompression_zp_idx)) {
+ if (need_reorder(decompression_zp_idx, weight_rank)) {
reorder_bfyx_to_fbyx(decompression_zp_idx);
}
}
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
index f709d7a2aac..4729d9ce527 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
@@ -22,6 +22,7 @@ struct fully_connected_onednn : typed_primitive_onednn_impl<fully_connected> {
using parent::parent;
static constexpr int COMMON = 0;
static constexpr int PER_OC = 2;
+ static constexpr int PER_TENSOR = 7;
static constexpr int GROUPED = 3;
DECLARE_OBJECT_TYPE_SERIALIZATION(cldnn::onednn::fully_connected_onednn)
@@ -374,6 +375,10 @@ public:
}
auto weights_layout = impl_params.get_input_layout(1);
+ auto weight_shape = weights_layout.get_partial_shape();
+ auto weight_rank = std::count_if(weight_shape.begin(), weight_shape.end(), [](ov::Dimension d) { return d.get_length() > 1; });
+ weight_rank = std::max(static_cast<int64_t>(2), weight_rank);
+ OPENVINO_ASSERT(weight_rank <= 3, "Currently only weights with equal to or less than 3D is supported");
auto shift_size = std::max<size_t>(prim->input_size - 2, 0);
int per_oc = PER_OC << shift_size;
int grouped = GROUPED | (1 << (prim->input_size - 1));
@@ -382,17 +387,20 @@ public:
auto decompression_scale_idx = ++idx;
auto scale_layout = arg.get_dependency(decompression_scale_idx).get_output_layout();
ds_data_type = convert_data_type(scale_layout.data_type);
- auto ifm = arg.get_dependency(1).get_output_layout().get_dim(1);
- auto ngroups = scale_layout.get_dim(1);
+ auto ifm = arg.get_dependency(1).get_output_layout().get_dim(weight_rank - 1);
+ auto ngroups = scale_layout.get_dim(weight_rank - 1);
group_size = ifm / ngroups;
OPENVINO_ASSERT((group_size == 1 || ngroups == 1 || group_size % 32 == 0),
"[GPU] group_size should be aligned to 32 if it is not a single scale group or the group_size is not one.");
if (scale_layout.count() == 1) {
attr->set_scales(DNNL_ARG_WEIGHTS, COMMON, dnnl::memory::dims{}, ds_data_type);
- } else if (ngroups == 1) {
+ } else if (ngroups == 1 && weight_rank <= 2) {
attr->set_scales(DNNL_ARG_WEIGHTS, per_oc, dnnl::memory::dims{}, ds_data_type);
} else {
- // OneDNN does not support scalar zero-point for s4 and u8 type. Need to broadcast it.
+ // should use {K, 1} for the group size + per tensor mask for 3d
+ // Example:
+ // input[32, 6, 2088], W_t[32, 5760, 2088], scale[32, 1, 5760]
+ // set scale group as [32, 2088, 1]
attr->set_scales(DNNL_ARG_WEIGHTS, grouped, {group_size, 1}, ds_data_type);
}
}
@@ -405,10 +413,13 @@ public:
if (dzp_layout.count() == 1) {
attr->set_zero_points(DNNL_ARG_WEIGHTS, COMMON, dnnl::memory::dims{}, dzp_data_type);
} else {
- auto ngroups = dzp_layout.get_dim(1);
- if (ngroups == 1) {
+ size_t rank = dzp_layout.get_partial_shape().size();
+ OPENVINO_ASSERT(rank <= 3, "rank > 3d not supported");
+ auto ngroups = dzp_layout.get_dim(rank - 1);
+ if (ngroups == 1 && rank <= 2) {
attr->set_zero_points(DNNL_ARG_WEIGHTS, per_oc, dnnl::memory::dims{}, dzp_data_type);
} else {
+ // should use {K, 1} for the group size + per tensor mask for 3d
attr->set_zero_points(DNNL_ARG_WEIGHTS, grouped, {group_size, 1}, dzp_data_type);
}
}
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
index 5ceed2d0541..3f89361a390 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
@@ -35,10 +35,11 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
output.get_element_type() == ov::element::i4);
};
- auto reshape_3d_to_2d = [](const ov::Output<ov::Node>& output) {
+ auto reshape_squeeze = [](const ov::Output<ov::Node>& output) {
auto in_ps = output.get_node()->get_input_partial_shape(0);
auto out_ps = output.get_node()->get_output_partial_shape(0);
- return in_ps.rank().is_static() && out_ps.rank().is_static() && in_ps.size() == 3 && out_ps.size() == 2;
+ return in_ps.rank().is_static() && out_ps.rank().is_static() &&
+ ((in_ps.size() == 3 && out_ps.size() == 2) || (in_ps.size() == 4 && out_ps.size() == 3));
};
auto weights_m = wrap_type<ov::op::v0::Constant>(compressed_constant);
@@ -56,7 +57,8 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
auto mul_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{mul_with_sub_m, mul_no_sub_m});
auto reshape_const_m = wrap_type<ov::op::v0::Constant>();
- auto reshape_m = wrap_type<ov::op::v1::Reshape>({mul_m, reshape_const_m}, reshape_3d_to_2d);
+ auto reshape_m = wrap_type<ov::op::v1::Reshape>({mul_m, reshape_const_m}, reshape_squeeze);
+ auto convert_reshape_m = wrap_type<ov::op::v0::Convert>({reshape_m});
auto mul2_const_m = wrap_type<ov::op::v0::Constant>();
auto mul2_m = wrap_type<ov::op::v1::Multiply>({reshape_m, mul2_const_m});
@@ -67,7 +69,8 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
auto data_m = any_input();
auto bias_m = any_input();
- auto weights_input_m = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{reshape_m, transpose_m, mul_m, mul2_m});
+ auto weights_input_m = std::make_shared<ov::pass::pattern::op::Or>(
+ ov::OutputVector{reshape_m, convert_reshape_m, transpose_m, mul_m, mul2_m});
auto fully_connected_m = wrap_type<op::FullyConnected>({data_m, weights_input_m, bias_m});
ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
@@ -81,29 +84,38 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
if (!fc || transformation_callback(fc)) {
return false;
}
-
bool has_transpose = pattern_map.count(transpose_m);
auto scale_shape = pattern_map.at(mul_const_m).get_shape();
bool grouped = std::count_if(scale_shape.begin(), scale_shape.end(), [](size_t d) { return d > 1; }) > 1;
bool sub_with_convert = (pattern_map.count(sub_with_convert_m) > 0) ? true : false;
+ auto weight_shape = fc->get_input_shape(1);
+ bool is_weight_3d = (std::count_if(weight_shape.begin(), weight_shape.end(), [](size_t d) { return d > 1; }) == 3);
+
auto weight_ptr = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(weights_m).get_node_shared_ptr());
bool weight_u8 = false;
if (weight_ptr->get_element_type() == ov::element::u8 || weight_ptr->get_element_type() == ov::element::i8)
weight_u8 = true;
- auto reshape_const_to_2d = [has_transpose, grouped](std::shared_ptr<ov::Node> node) {
+ auto reshape_const = [has_transpose, grouped, is_weight_3d](std::shared_ptr<ov::Node> node) {
auto constant = ov::as_type_ptr<ov::op::v0::Constant>(node);
OPENVINO_ASSERT(constant != nullptr);
ov::Shape current_shape = constant->get_shape();
if (current_shape.size() <= 2)
return constant;
- OPENVINO_ASSERT(current_shape.size() == 3);
-
- auto new_shape = (has_transpose || !grouped) ? ov::Shape{current_shape[0] * current_shape[1], current_shape[2]}
- : ov::Shape{current_shape[0], current_shape[1] * current_shape[2]};
-
+ ov::Shape new_shape;
+ if (current_shape.size() == 3) {
+ if (is_weight_3d)
+ return constant;
+ else
+ new_shape = (has_transpose || !grouped) ? ov::Shape{current_shape[0] * current_shape[1], current_shape[2]}
+ : ov::Shape{current_shape[0], current_shape[1] * current_shape[2]};
+ } else {
+ OPENVINO_ASSERT(current_shape.size() == 4 && is_weight_3d);
+ new_shape = (has_transpose || !grouped) ? ov::Shape{current_shape[0], current_shape[1] * current_shape[2], current_shape[3]}
+ : ov::Shape{current_shape[0], current_shape[1], current_shape[3] * current_shape[1]};
+ }
auto new_constant = std::make_shared<ov::op::v0::Constant>(*constant, new_shape);
ov::copy_weightless_cache_attr(constant, new_constant);
@@ -129,15 +141,15 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
const ov::Output<Node>& fc_input_a = fc->input(0).get_source_output();
- const auto& scale = reshape_const_to_2d(pattern_map.at(mul_const_m).get_node_shared_ptr());
+ const auto& scale = reshape_const(pattern_map.at(mul_const_m).get_node_shared_ptr());
std::shared_ptr<ov::Node> optional_zero_point = nullptr;
const bool with_zero_point = pattern_map.count(sub_no_convert_m) > 0 || pattern_map.count(sub_with_convert_m) > 0;
if (with_zero_point) {
- optional_zero_point = convert_const_to_u8(reshape_const_to_2d(pattern_map.at(sub_const_m).get_node_shared_ptr()));
+ optional_zero_point = convert_const_to_u8(reshape_const(pattern_map.at(sub_const_m).get_node_shared_ptr()));
}
- std::shared_ptr<ov::Node> fc_input_b = reshape_const_to_2d(pattern_map.at(weights_m).get_node_shared_ptr());
+ std::shared_ptr<ov::Node> fc_input_b = reshape_const(pattern_map.at(weights_m).get_node_shared_ptr());
std::shared_ptr<ov::Node> fc_input_scale = scale;
std::shared_ptr<ov::Node> fc_input_zp = optional_zero_point;
std::shared_ptr<ov::Node> fc_input_bias = pattern_map.at(bias_m).get_node_shared_ptr();
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
index 45d1e3106e5..befd1a52dc6 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
@@ -13,10 +13,13 @@
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "transformations/utils/utils.hpp"
#include "openvino/core/graph_util.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/multiply.hpp"
namespace ov::intel_gpu {
-ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected() {
+ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad) {
+ using namespace ov::pass::pattern;
auto static_rank_gt_1 = [](const ov::Output<ov::Node>& output) {
const auto& r = output.get_partial_shape().rank();
return r.is_static() && r.get_length() > 1;
@@ -25,13 +28,58 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected() {
const auto& pshape = output.get_partial_shape();
return ov::op::util::is_on_constant_path(output) &&
static_rank_gt_1(output) &&
- pshape.is_static() &&
- std::count_if(pshape.begin(), pshape.end(), [](const ov::Dimension& x) { return x != 1; }) <= 2;
+ pshape.is_static();
};
+ // compressed path
+ auto compressed_constant = [](const ov::Output<ov::Node>& output) {
+ return (output.get_element_type() == ov::element::u8 ||
+ output.get_element_type() == ov::element::i8 ||
+ output.get_element_type() == ov::element::u4 ||
+ output.get_element_type() == ov::element::i4);
+ };
+
+ auto reshape_squeeze = [](const ov::Output<ov::Node>& output) {
+ auto in_ps = output.get_node()->get_input_partial_shape(0);
+ auto out_ps = output.get_node()->get_output_partial_shape(0);
+ return in_ps.rank().is_static() && out_ps.rank().is_static() &&
+ ((in_ps.size() == 3 && out_ps.size() == 2) || (in_ps.size() == 4 && out_ps.size() == 3));
+ };
+
+ auto compressed_weights_m = wrap_type<ov::op::v0::Constant>(compressed_constant);
+ auto convert_m = wrap_type<ov::op::v0::Convert>({compressed_weights_m});
+
+ auto sub_const_m = wrap_type<ov::op::v0::Constant>();
+ auto sub_convert_const_m = wrap_type<ov::op::v0::Convert>({sub_const_m});
+ auto sub_with_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_convert_const_m});
+ auto sub_no_convert_m = wrap_type<ov::op::v1::Subtract>({convert_m, sub_const_m});
+ auto subtract_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{sub_with_convert_m, sub_no_convert_m});
+
+ auto mul_const_m = wrap_type<ov::op::v0::Constant>();
+ auto mul_with_sub_m = wrap_type<ov::op::v1::Multiply>({subtract_m, mul_const_m});
+ auto mul_no_sub_m = wrap_type<ov::op::v1::Multiply>({convert_m, mul_const_m});
+ auto mul_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{mul_with_sub_m, mul_no_sub_m});
+
+ auto reshape_const_m = wrap_type<ov::op::v0::Constant>();
+ auto reshape_m = wrap_type<ov::op::v1::Reshape>({mul_m, reshape_const_m}, reshape_squeeze);
+ auto convert_reshape_m = wrap_type<ov::op::v0::Convert>({reshape_m});
+
+ auto mul2_const_m = wrap_type<ov::op::v0::Constant>();
+ auto mul2_m = wrap_type<ov::op::v1::Multiply>({reshape_m, mul2_const_m});
+
+ auto transpose_input = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{reshape_m, mul_m});
+ auto transpose_const_m = wrap_type<ov::op::v0::Constant>();
+ auto transpose_m = wrap_type<ov::op::v1::Transpose>({transpose_input, transpose_const_m});
auto activations_m = ov::pass::pattern::any_input(static_rank_gt_1);
- auto weights_m = ov::pass::pattern::any_input(weights_path);
- auto matmul_m = ov::pass::pattern::wrap_type<ov::op::v0::MatMul>({ activations_m, weights_m }, ov::pass::pattern::has_static_rank());
+ auto general_weights_m = ov::pass::pattern::any_input(weights_path);
+
+ auto compressed_weights_input_m = std::make_shared<ov::pass::pattern::op::Or>(
+ ov::OutputVector{reshape_m, convert_reshape_m, transpose_m, mul_m, mul2_m, general_weights_m});
+
+ auto weights_m = std::make_shared<ov::pass::pattern::op::Or>(
+ ov::OutputVector{compressed_weights_input_m, general_weights_m});
+ auto matmul_m =
+ ov::pass::pattern::wrap_type<ov::op::v0::MatMul>({activations_m, weights_m}, ov::pass::pattern::has_static_rank());
ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
const auto& pattern_map = m.get_pattern_value_map();
@@ -86,7 +134,8 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected() {
* for example: [2, 32, 64] [3, 64, 64] it will raise an exception.
*/
- auto get_aligned_shapes = [&shape_a, &shape_b, &rank_a, &rank_b, &matmul]() -> std::tuple<bool, ov::PartialShape, ov::PartialShape> {
+ auto get_aligned_shapes = [&shape_a, &shape_b, &rank_a, &rank_b, &matmul, &supports_immad](const bool is_compressed_weight)
+ -> std::tuple<bool, ov::PartialShape, ov::PartialShape> {
ov::PartialShape shape_a_aligned(shape_a);
ov::PartialShape shape_b_aligned(shape_b);
size_t max_size = std::max(rank_a, rank_b);
@@ -108,7 +157,7 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected() {
for (size_t i = 0; i < max_size - 2; ++i) {
if (shape_b_aligned[i] == 1) {
shape_b_aligned[i] = shape_a_aligned[i];
- } else {
+ } else if (!is_compressed_weight || !supports_immad) {
return std::make_tuple(false, std::move(shape_a_aligned), std::move(shape_b_aligned));
}
}
@@ -141,10 +190,11 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected() {
return transpose;
};
+ bool is_compressed_weight = (pattern_map.at(compressed_weights_input_m).get_node_shared_ptr() != nullptr);
bool success = true;
ov::PartialShape shape_a_aligned;
ov::PartialShape shape_b_aligned;
- std::tie(success, shape_a_aligned, shape_b_aligned) = get_aligned_shapes();
+ std::tie(success, shape_a_aligned, shape_b_aligned) = get_aligned_shapes(is_compressed_weight);
if (!success) {
return false;
}
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.hpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.hpp
index bf110fe7866..2669237d8cc 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.hpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.hpp
@@ -11,7 +11,7 @@ namespace ov::intel_gpu {
class ConvertMatMulToFullyConnected: public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("ConvertMatMulToFullyConnected");
- ConvertMatMulToFullyConnected();
+ ConvertMatMulToFullyConnected(bool supports_immad = false);
};
} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 5fd1236b7d8..65b045971b3 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -1195,7 +1195,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
manager.set_per_pass_validation(false);
manager.register_pass<ov::intel_gpu::ClampFP16Output>();
- manager.register_pass<ov::intel_gpu::ConvertMatMulToFullyConnected>();
+ manager.register_pass<ov::intel_gpu::ConvertMatMulToFullyConnected>(device_info.supports_immad);
manager.register_pass<ov::intel_gpu::MoveFCReshapeToWeights>();
manager.register_pass<ov::intel_gpu::ConvertFullyConnectedToFullyConnectedCompressed>();
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 22205451844..26b34f045da 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -250,6 +250,9 @@ std::vector<std::string> disabledTestPatterns() {
returnVal.push_back(R"(.*smoke_MatmulWeightsDecompressionQuantizeConvolution_basic.*)");
returnVal.push_back(R"(.*smoke_Nms9LayerTest/Nms9LayerTest.Inference/num_batches=2_num_boxes=50.*)");
returnVal.push_back(R"(.*smoke_ScaledAttnDynamic4D_GPU/ScaledAttnLayerGPUTest.CompareWithRefs/n.*)");
+ } else {
+ // CVS-172342
+ returnVal.push_back(R"(.*smoke_MatMulCompressedWeights_3D_weight.*)");
}
return returnVal;
}
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/matmul_weights_decompression.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/matmul_weights_decompression.cpp
index 17f5c3fd086..efb4ae259ea 100644
--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/matmul_weights_decompression.cpp
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/matmul_weights_decompression.cpp
@@ -148,15 +148,16 @@ protected:
// N - number of groups
// G - group size
auto transformed_weights_shape = transpose_if_necessary(weights_shape);
+ auto weights_rank = weights_shape.size();
if (group_decompression) {
- OPENVINO_ASSERT(weights_shape[0] % group_size == 0,
+ OPENVINO_ASSERT(weights_shape[weights_rank - 2] % group_size == 0,
"Weights output channels count (",
- weights_shape[0],
+ weights_shape[weights_rank - 2],
") must be divisible by decompression group size (",
group_size,
").");
auto in_channel_idx = transpose_weights ? transformed_weights_shape.size() - 1 : transformed_weights_shape.size() - 2;
- transformed_weights_shape[in_channel_idx] = weights_shape[0] / group_size;
+ transformed_weights_shape[in_channel_idx] = weights_shape[weights_rank - 2] / group_size;
transformed_weights_shape.insert(transformed_weights_shape.begin() + in_channel_idx + 1, group_size);
}
auto weights_tensor = ov::test::utils::create_and_fill_tensor(weights_precision, transformed_weights_shape);
@@ -171,7 +172,9 @@ protected:
// Ordinary decompression: [O, 1]
// Group decompression: [O, N, 1]
ov::Shape scaleshift_target_shape{output_channels};
- scaleshift_target_shape.insert(scaleshift_target_shape.begin(), group_decompression ? weights_shape[0] / group_size : 1);
+ scaleshift_target_shape.insert(scaleshift_target_shape.begin(), group_decompression ? weights_shape[weights_rank - 2] / group_size : 1);
+ if (weights_shape.size() == 3 && std::count_if(weights_shape.begin(), weights_shape.end(), [](size_t dim) { return dim != 1; }) == 3)
+ scaleshift_target_shape.insert(scaleshift_target_shape.begin(), weights_shape[0]);
scaleshift_target_shape = transpose_if_necessary(scaleshift_target_shape);
if (group_decompression) {
auto in_channel_idx = transpose_weights ? scaleshift_target_shape.size() - 1 : scaleshift_target_shape.size() - 2;
@@ -217,8 +220,12 @@ protected:
std::shared_ptr<ov::Node> last_node = std::make_shared<ov::op::v1::Multiply>(mul_parent, scale_const);
if (group_decompression) {
- auto reshape_target_shape = transpose_weights ? std::vector<int>{-1, static_cast<int>(weights_shape[0])}
- : std::vector<int>{static_cast<int>(weights_shape[0]), -1};
+ auto weight_hidden_idx = weights_shape.size() - 2;
+ auto reshape_target_shape = transpose_weights ? std::vector<int>{-1, static_cast<int>(weights_shape[weight_hidden_idx])}
+ : std::vector<int>{static_cast<int>(weights_shape[weight_hidden_idx]), -1};
+ for (size_t i = 0; i < weight_hidden_idx; ++i) {
+ reshape_target_shape.insert(reshape_target_shape.begin() + i, weights_shape[i]);
+ }
auto target_shape_node = ov::op::v0::Constant::create(ov::element::i32, {reshape_target_shape.size()}, reshape_target_shape);
last_node = std::make_shared<ov::op::v1::Reshape>(last_node, target_shape_node, false);
}
@@ -440,5 +447,20 @@ INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeights_dyn_quan_no_slm,
::testing::Values(2.0f)), // Note: this is because of potential cldnn accuracy issue
MatmulWeightsDecompression::get_test_case_name);
+INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeights_3D_weight,
+ MatmulWeightsDecompression,
+ ::testing::Combine(::testing::Values(ShapeParams{{{-1, -1, 32}, {{32, 128, 32}}},
+ {32, 32, 1024}, 32}),
+ ::testing::Values(ov::element::u4),
+ ::testing::Values(ov::element::f16),
+ ::testing::Values(false),
+ ::testing::Values(false),
+ ::testing::Values(false),
+ ::testing::Values(false),
+ ::testing::Values(true),
+ ::testing::Values(0),
+ ::testing::Values(2.0f)),
+ MatmulWeightsDecompression::get_test_case_name);
+
} // namespace
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
index c9aa85c5fea..00e9494244a 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
@@ -432,7 +432,40 @@ TEST_F(TransformationTestsF, ConvertMatMulToFullyConnectedTest_compressed_u8_wei
auto transpose_const = ov::opset1::Constant::create(ov::element::i32, {3}, {0, 2, 1});
auto transpose = std::make_shared<ov::opset1::Transpose>(mul, transpose_const);
- auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto matmul = std::make_shared<op::FullyConnected>(data, transpose, no_bias);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{data});
+ }
+}
+
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnectedTest_compressed_u4_weights_3D) {
+ {
+ auto data = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{3, 2, 2});
+ auto weights = ov::opset1::Constant::create(ov::element::u4, ov::Shape{3, 2, 2}, {1});
+ auto convert = std::make_shared<ov::opset1::Convert>(weights, ov::element::f16);
+ auto sub_const = ov::opset1::Constant::create(ov::element::f16, ov::Shape{3, 1, 2}, {1});
+ auto sub = std::make_shared<ov::opset1::Subtract>(convert, sub_const);
+ auto mul_const = ov::opset1::Constant::create(ov::element::f16, ov::Shape{3, 1, 2}, {1});
+ auto mul = std::make_shared<ov::opset1::Multiply>(sub, mul_const);
+ auto matmul = std::make_shared<ov::opset1::MatMul>(data, mul);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{data});
+ bool support_immad = true;
+ manager.register_pass<ConvertMatMulToFullyConnected>(support_immad);
+ }
+ {
+ auto data = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{3, 2, 2});
+ auto weights = ov::opset1::Constant::create(ov::element::u4, ov::Shape{3, 2, 2}, {1});
+ auto convert = std::make_shared<ov::opset1::Convert>(weights, ov::element::f16);
+ auto sub_const = ov::opset1::Constant::create(ov::element::f16, ov::Shape{3, 1, 2}, {1});
+ auto sub = std::make_shared<ov::opset1::Subtract>(convert, sub_const);
+ auto mul_const = ov::opset1::Constant::create(ov::element::f16, ov::Shape{3, 1, 2}, {1});
+ auto mul = std::make_shared<ov::opset1::Multiply>(sub, mul_const);
+
+ auto transpose_const = ov::opset1::Constant::create(ov::element::i32, {3}, {0, 2, 1});
+ auto transpose = std::make_shared<ov::opset1::Transpose>(mul, transpose_const);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
auto matmul = std::make_shared<op::FullyConnected>(data, transpose, no_bias);
model_ref = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{data});