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
[OV CPU/GPU] modify pass ConvertWeightCompressedConv1x1ToMatmul for both CPU/GPU, add i4 reorder kernel output support to GPU (#32983)
bopeng1234013fc42f2c88
diff --git a/src/common/transformations/include/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.hpp b/src/common/transformations/include/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.hppindex bd5bcd47760..460f7afd3d6 100644--- a/src/common/transformations/include/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.hpp+++ b/src/common/transformations/include/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.hpp@@ -43,6 +43,10 @@ class TRANSFORMATIONS_API ConvertWeightCompressedConv1x1ToMatmul;* | Reshape | | Multiply |<--------------+* +-------------+ +----------+* | |+ * | *-----------++ * | | Reshape | (optional)+ * | +-----------++ * | |* | v* | +-----------+* +----------------->|Convolution|@@ -93,6 +97,11 @@ class TRANSFORMATIONS_API ConvertWeightCompressedConv1x1ToMatmul;* | +----------+* | |* | v+ * | *----------++ * | | Reshape | (optional)+ * | +----------++ * | |+ * | v* | +--------+* +-------------> | MatMul |* +--------+diff --git a/src/common/transformations/src/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.cpp b/src/common/transformations/src/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.cppindex bcd3d7cc6c6..582a4f3f6b5 100644--- a/src/common/transformations/src/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.cpp+++ b/src/common/transformations/src/transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.cpp@@ -36,27 +36,20 @@ using ov::pass::pattern::op::Or;ov::pass::ConvertWeightCompressedConv1x1ToMatmul::ConvertWeightCompressedConv1x1ToMatmul() {MATCHER_SCOPE(ConvertWeightCompressedConv1x1ToMatmul);- auto filter1x1_path = [](const ov::Output<ov::Node>& output) {- const auto& pshape = output.get_partial_shape();- return ov::op::util::is_on_path<ov::op::v0::Constant, ov::op::v0::Parameter>(output) && pshape.is_static() &&- pshape[-1] == 1 && pshape[-2] == 1;- };-- auto bias_path = [](const ov::Output<ov::Node>& output) {- const auto& pshape = output.get_partial_shape();- return ov::op::util::is_on_path<ov::op::v0::Constant>(output) && pshape.is_static() && pshape[0] == 1 &&- pshape[2] == 1 && pshape[3] == 1;- };auto first_input_m = ov::pass::pattern::any_input();auto a_order_m = ov::pass::pattern::wrap_type<ov::op::v0::Constant>();auto transpose_activations_m = ov::pass::pattern::wrap_type<ov::op::v1::Transpose>({first_input_m, a_order_m});- auto reshape_activations_m = ov::pass::pattern::wrap_type<ov::op::v1::Reshape>({first_input_m, a_order_m});+ auto reshape_activations_m =+ ov::pass::pattern::wrap_type<ov::op::v1::Reshape>({first_input_m, a_order_m},+ pattern::shape_matches("[?, hidden_in, 1, 1]"));auto a_m =std::make_shared<ov::pass::pattern::op::Or>(OutputVector{transpose_activations_m, reshape_activations_m});- auto weights_const_m = wrap_type<ov::op::v0::Constant>(rank_equals(4) && has_static_rank() && filter1x1_path);- auto weights_param_m = wrap_type<ov::op::v0::Parameter>(rank_equals(4) && has_static_rank() && filter1x1_path);+ auto weights_const_m = wrap_type<ov::op::v0::Constant>(pattern::shape_matches("[?, ?, 1, 1]") ||+ pattern::shape_matches("[?, ?, ?, 1, 1]"));+ auto weights_param_m = wrap_type<ov::op::v0::Parameter>(pattern::shape_matches("[?, ?, 1, 1]") ||+ pattern::shape_matches("[?, ?, ?, 1, 1]"));auto weights_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{weights_const_m, weights_param_m});auto weight_convert_m = ov::pass::pattern::wrap_type<ov::op::v0::Convert>({weights_m});auto weights_scales_m = ov::pass::pattern::any_input();@@ -68,10 +61,13 @@ ov::pass::ConvertWeightCompressedConv1x1ToMatmul::ConvertWeightCompressedConv1x1auto weight_dequantized_m =std::make_shared<ov::pass::pattern::op::Or>(OutputVector{weight_convert_m, weight_subtract_m});auto weight_mult_m = ov::pass::pattern::wrap_type<ov::op::v1::Multiply>({weight_dequantized_m, weights_scales_m});- auto conv1x1_m = ov::pass::pattern::wrap_type<ov::op::v1::Convolution>({a_m, weight_mult_m});+ auto weight_reshape_m =+ ov::pass::pattern::wrap_type<ov::op::v1::Reshape>({weight_mult_m, ov::pass::pattern::any_input()});+ auto weight_input_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{weight_mult_m, weight_reshape_m});+ auto conv1x1_m = ov::pass::pattern::wrap_type<ov::op::v1::Convolution>({a_m, weight_input_m});// Optional bias- auto bias_const_m = wrap_type<ov::op::v0::Constant>(rank_equals(4) && has_static_rank() && bias_path);+ auto bias_const_m = wrap_type<ov::op::v0::Constant>(pattern::shape_matches("[1, ?, 1, 1]"));auto bias_m = ov::pass::pattern::wrap_type<ov::op::v1::Add>({conv1x1_m, bias_const_m});auto bias_out_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{conv1x1_m, bias_m});@@ -93,6 +89,9 @@ ov::pass::ConvertWeightCompressedConv1x1ToMatmul::ConvertWeightCompressedConv1x1? pattern_map.at(weight_subtract_m).get_node_shared_ptr(): nullptr;auto weight_mult = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(weight_mult_m).get_node_shared_ptr());+ auto weight_reshape = (pattern_map.count(weight_reshape_m) > 0)+ ? pattern_map.at(weight_reshape_m).get_node_shared_ptr()+ : nullptr;auto bias_out = (pattern_map.count(bias_m) > 0) ? pattern_map.at(bias_m).get_node_shared_ptr() : nullptr;auto bias_const =(pattern_map.count(bias_const_m) > 0) ? pattern_map.at(bias_const_m).get_node_shared_ptr() : nullptr;@@ -125,14 +124,23 @@ ov::pass::ConvertWeightCompressedConv1x1ToMatmul::ConvertWeightCompressedConv1x1ov::copy_weightless_cache_attr(constant, new_constant);return new_constant;- } else {- OPENVINO_ASSERT(current_shape.size() == 4);+ } else if (current_shape.size() == 4) {OPENVINO_ASSERT(current_shape[2] == 1 && current_shape[3] == 1);auto new_shape = ov::Shape{current_shape[0], current_shape[1]};auto new_constant = std::make_shared<ov::op::v0::Constant>(*constant, new_shape);+ ov::copy_weightless_cache_attr(constant, new_constant);+ return new_constant;+ } else {+ OPENVINO_ASSERT(current_shape.size() == 5);+ OPENVINO_ASSERT(current_shape[3] == 1 && current_shape[4] == 1);++ auto new_shape = ov::Shape{current_shape[0], current_shape[1], current_shape[2]};++ auto new_constant = std::make_shared<ov::op::v0::Constant>(*constant, new_shape);+ov::copy_weightless_cache_attr(constant, new_constant);return new_constant;}@@ -152,19 +160,18 @@ ov::pass::ConvertWeightCompressedConv1x1ToMatmul::ConvertWeightCompressedConv1x1OPENVINO_ASSERT(param != nullptr);std::vector<int> values_reshape_b;auto shape_b = param->get_output_partial_shape(0);- for (size_t i = 0; i < shape_b.size(); i++)- if (shape_b.to_shape()[i] != 1) {- values_reshape_b.push_back(static_cast<int>(shape_b.to_shape()[i]));- }+ // removed last two dimensions, 1x1+ for (size_t i = 0; i < (shape_b.size() - 2); i++)+ values_reshape_b.push_back(static_cast<int>(shape_b.to_shape()[i]));- auto reshape_weight_const = ov::op::v0::Constant::create(element::i32, Shape{2}, values_reshape_b);+ auto reshape_weight_const =+ ov::op::v0::Constant::create(element::i32, Shape{values_reshape_b.size()}, values_reshape_b);auto Reshape_weight = std::make_shared<ov::op::v1::Reshape>(param, reshape_weight_const, false);MatcherPass::register_new_node(Reshape_weight);Reshape_weight->set_friendly_name(param->get_friendly_name() + "_Reshape_weight");weight_squeezed_convert =ov::as_type_ptr<ov::op::v0::Convert>(weight_convert->clone_with_new_inputs({Reshape_weight}));- ov::copy_runtime_info(weight_convert, weight_squeezed_convert);- ov::copy_runtime_info(weight_convert, Reshape_weight);+ ov::copy_runtime_info(weight_convert, {weight_squeezed_convert, Reshape_weight});}ov::disable_constant_folding(weight_squeezed_convert);@@ -193,6 +200,39 @@ ov::pass::ConvertWeightCompressedConv1x1ToMatmul::ConvertWeightCompressedConv1x1ov::copy_runtime_info(weight_mult, scaled_weight);ov::disable_constant_folding(scaled_weight);+ if (weight_reshape) {+ const auto& shape = scaled_weight->get_output_partial_shape(0);+ OPENVINO_ASSERT(shape.rank().get_length() == 3, "Expected 3 Dim weights for block decompression case");+ auto shape_const = std::make_shared<ov::op::v0::Constant>(+ ov::element::i64,+ ov::Shape{2},+ std::vector<int64_t>{shape[0].get_length(), shape[1].get_length() * shape[2].get_length()});+ auto final_weight_reshape = std::make_shared<ov::op::v1::Reshape>(scaled_weight, shape_const, false);+ ov::copy_runtime_info(weight_reshape, final_weight_reshape);+ final_weight_reshape->set_friendly_name(weight_reshape->get_friendly_name() + "_reshape_weight");+ scaled_weight = final_weight_reshape;+ }++ // When activation is reshaped to [?, hidden_in, 1, 1], two possible cases:+ // 1. reshape from [..., hidden_in]+ // direct use it in matmul.+ // 2. reshape from [..., num_head, head_dim]+ // can't use it directly, need reshape it to [..., hidden_in], then use in matmul.+ if (pattern_map.count(reshape_activations_m)) {+ auto reshape_activations = pattern_map.at(reshape_activations_m).get_node_shared_ptr();+ auto shape_in = reshape_activations->get_input_partial_shape(0);+ auto shape_out = reshape_activations->get_output_partial_shape(0);+ if (shape_in[-1].is_dynamic() || shape_in[-1].get_length() != shape_out[1].get_length()) {+ auto reshape_const =+ std::make_shared<ov::op::v0::Constant>(ov::element::i64,+ ov::Shape{4},+ std::vector<int64_t>{1, 1, -1, shape_out[1].get_length()});+ auto reshape_activations_new = std::make_shared<ov::op::v1::Reshape>(activation, reshape_const, false);+ ov::copy_runtime_info(reshape_activations, reshape_activations_new);+ activation = reshape_activations_new;+ }+ }+auto matmul = std::make_shared<ov::op::v0::MatMul>(activation, scaled_weight, false, true);ov::copy_runtime_info(conv1x1, matmul);std::shared_ptr<Node> matmul_out;diff --git a/src/common/transformations/tests/op_conversions/convert_weight_compressed_conv1x1_to_matmul_test.cpp b/src/common/transformations/tests/op_conversions/convert_weight_compressed_conv1x1_to_matmul_test.cppindex d8325f41fe9..20344930baf 100644--- a/src/common/transformations/tests/op_conversions/convert_weight_compressed_conv1x1_to_matmul_test.cpp+++ b/src/common/transformations/tests/op_conversions/convert_weight_compressed_conv1x1_to_matmul_test.cpp@@ -7,14 +7,10 @@#include <gtest/gtest.h>#include <memory>-#include <openvino/core/model.hpp>-#include <openvino/pass/manager.hpp>-#include <ov_ops/type_relaxed.hpp>#include <string>-#include <transformations/init_node_info.hpp>-#include <transformations/utils/utils.hpp>#include "common_test_utils/ov_test_utils.hpp"+#include "openvino/core/model.hpp"#include "openvino/op/add.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/convolution.hpp"@@ -27,52 +23,78 @@#include "openvino/opsets/opset1_decl.hpp"#include "openvino/opsets/opset3_decl.hpp"#include "openvino/opsets/opset7_decl.hpp"+#include "openvino/pass/manager.hpp"+#include "ov_ops/type_relaxed.hpp"+#include "transformations/init_node_info.hpp"#include "transformations/rt_info/decompression.hpp"+#include "transformations/utils/utils.hpp"++namespace ov::test {-using namespace ov;using namespace testing;namespace {struct Conv1x1ToMatmulTestParams {+ bool with_group_quant;bool with_zp;bool with_bias;bool with_convert;- bool weights_as_param;+ bool with_param_weight;+ bool with_act_new_reshape;std::string activation_op_type;};std::shared_ptr<ov::Model> gen_model(const Conv1x1ToMatmulTestParams& p) {- auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{1, 1, 2, 10});+ auto input = std::make_shared<ov::opset1::Parameter>(+ ov::element::f16,+ (p.activation_op_type == "Reshape" && p.with_act_new_reshape) ? ov::Shape{1, 1, 2, 5} : ov::Shape{1, 1, 1, 10});+std::shared_ptr<ov::Node> act_node;if (p.activation_op_type == "Transpose") {auto transpose_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {0, 3, 1, 2});act_node = std::make_shared<ov::opset1::Transpose>(input, transpose_const);} else {- auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {1, 10, 1, 2});+ auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {1, 10, 1, 1});act_node = std::make_shared<ov::opset1::Reshape>(input, reshape_const, false);}std::shared_ptr<ov::Node> weights_node;ov::ParameterVector params = {input};- if (p.weights_as_param) {- auto weights_param = std::make_shared<ov::opset1::Parameter>(ov::element::i4, ov::Shape{15, 10, 1, 1});+ if (p.with_param_weight) {+ auto weights_param = std::make_shared<ov::opset1::Parameter>(+ ov::element::i4,+ p.with_group_quant ? ov::Shape{15, 5, 2, 1, 1} : ov::Shape{15, 10, 1, 1});weights_node = weights_param;params.push_back(weights_param);} else {- weights_node = ov::opset1::Constant::create(ov::element::i4, {15, 10, 1, 1}, {1});+ weights_node =+ ov::opset1::Constant::create(ov::element::i4,+ p.with_group_quant ? ov::Shape{15, 5, 2, 1, 1} : ov::Shape{15, 10, 1, 1},+ {1});}auto weights_convert = std::make_shared<ov::op::v0::Convert>(weights_node, ov::element::f16);std::shared_ptr<ov::Node> current_node = weights_convert;if (p.with_zp) {- auto zp_const = ov::opset1::Constant::create(ov::element::i4, {15, 10, 1, 1}, {1});+ auto zp_const =+ ov::opset1::Constant::create(ov::element::i4,+ p.with_group_quant ? ov::Shape{15, 5, 1, 1, 1} : ov::Shape{15, 1, 1, 1},+ {1});auto zp_convert = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::f16);current_node = std::make_shared<ov::opset1::Subtract>(weights_convert, zp_convert);}- auto scale_const = ov::opset1::Constant::create(ov::element::f16, {15, 10, 1, 1}, {1});- auto mul = std::make_shared<ov::opset1::Multiply>(current_node, scale_const);+ auto scale_const =+ ov::opset1::Constant::create(ov::element::f16,+ p.with_group_quant ? ov::Shape{15, 5, 1, 1, 1} : ov::Shape{15, 1, 1, 1},+ {1});+ std::shared_ptr<ov::Node> mul = std::make_shared<ov::opset1::Multiply>(current_node, scale_const);++ if (p.with_group_quant) {+ auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {15, 10, 1, 1});+ mul = std::make_shared<ov::opset1::Reshape>(mul, reshape_const, false);+ }auto conv = std::make_shared<ov::opset1::Convolution>(act_node,mul,@@ -96,7 +118,7 @@ std::shared_ptr<ov::Model> gen_model(const Conv1x1ToMatmulTestParams& p) {auto transpose_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {0, 2, 3, 1});out_node = std::make_shared<ov::opset1::Transpose>(current_node, transpose_const);} else {- auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {1, 1, 2, 15});+ auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {1, 1, 1, 15});out_node = std::make_shared<ov::opset1::Reshape>(current_node, reshape_const, false);}@@ -104,32 +126,54 @@ std::shared_ptr<ov::Model> gen_model(const Conv1x1ToMatmulTestParams& p) {}std::shared_ptr<ov::Model> gen_model_ref(const Conv1x1ToMatmulTestParams& p) {- auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f16, ov::Shape{1, 1, 2, 10});+ auto input = std::make_shared<ov::opset1::Parameter>(+ ov::element::f16,+ (p.activation_op_type == "Reshape" && p.with_act_new_reshape) ? ov::Shape{1, 1, 2, 5} : ov::Shape{1, 1, 1, 10});std::shared_ptr<ov::Node> weights_node;ov::ParameterVector params = {input};- if (p.weights_as_param) {- auto weights_param = std::make_shared<ov::opset1::Parameter>(ov::element::i4, ov::Shape{15, 10, 1, 1});- auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{2}, {15, 10});+ if (p.with_param_weight) {+ auto weights_param = std::make_shared<ov::opset1::Parameter>(+ ov::element::i4,+ p.with_group_quant ? ov::Shape{15, 5, 2, 1, 1} : ov::Shape{15, 10, 1, 1});+ auto reshape_const = ov::opset1::Constant::create(ov::element::i32,+ p.with_group_quant ? ov::Shape{3} : ov::Shape{2},+ p.with_group_quant ? ov::Shape{15, 5, 2} : ov::Shape{15, 10});weights_node = std::make_shared<ov::opset1::Reshape>(weights_param, reshape_const, false);params.push_back(weights_param);} else {- weights_node = ov::opset1::Constant::create(ov::element::i4, {15, 10}, {1});+ weights_node = ov::opset1::Constant::create(ov::element::i4,+ p.with_group_quant ? ov::Shape{15, 5, 2} : ov::Shape{15, 10},+ {1});}auto weights_convert = std::make_shared<ov::op::v0::Convert>(weights_node, ov::element::f16);std::shared_ptr<ov::Node> current_node = weights_convert;if (p.with_zp) {- auto zp_const = ov::opset1::Constant::create(ov::element::i4, {15, 10}, {1});+ auto zp_const = ov::opset1::Constant::create(ov::element::i4,+ p.with_group_quant ? ov::Shape{15, 5, 1} : ov::Shape{15, 1},+ {1});auto zp_convert = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::f16);current_node = std::make_shared<ov::opset1::Subtract>(weights_convert, zp_convert);}- auto scale_const = ov::opset1::Constant::create(ov::element::f16, {15, 10}, {1});- auto mul = std::make_shared<ov::opset1::Multiply>(current_node, scale_const);+ auto scale_const = ov::opset1::Constant::create(ov::element::f16,+ p.with_group_quant ? ov::Shape{15, 5, 1} : ov::Shape{15, 1},+ {1});+ std::shared_ptr<ov::Node> mul = std::make_shared<ov::opset1::Multiply>(current_node, scale_const);++ if (p.with_group_quant) {+ auto reshape_const = ov::opset1::Constant::create(ov::element::i64, ov::Shape{2}, {15, 10});+ mul = std::make_shared<ov::opset1::Reshape>(mul, reshape_const, false);+ }- auto matmul = std::make_shared<ov::op::v0::MatMul>(input, mul, false, true);+ std::shared_ptr<ov::Node> act_node = input;+ if (p.activation_op_type == "Reshape" && p.with_act_new_reshape) {+ auto reshape_const = ov::opset1::Constant::create(ov::element::i64, ov::Shape{4}, {1, 1, 1, 10});+ act_node = std::make_shared<ov::opset1::Reshape>(input, reshape_const, false);+ }+ auto matmul = std::make_shared<ov::op::v0::MatMul>(act_node, mul, false, true);current_node = matmul;if (p.with_bias) {@@ -142,7 +186,7 @@ std::shared_ptr<ov::Model> gen_model_ref(const Conv1x1ToMatmulTestParams& p) {std::shared_ptr<ov::Node> out_node;if (p.activation_op_type == "Reshape") {- auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {1, 1, 2, 15});+ auto reshape_const = ov::opset1::Constant::create(ov::element::i32, ov::Shape{4}, {1, 1, 1, 15});out_node = std::make_shared<ov::opset1::Reshape>(current_node, reshape_const, false);} else {out_node = current_node;@@ -154,19 +198,25 @@ std::shared_ptr<ov::Model> gen_model_ref(const Conv1x1ToMatmulTestParams& p) {class ConvertWeightCompressedConv1x1ToMatmulTest: public TransformationTestsF,- public WithParamInterface<std::tuple<bool, bool, bool, bool, std::string>> {+ public WithParamInterface<std::tuple<bool, bool, bool, bool, bool, bool, std::string>> {public:static std::string get_test_case_name(- const testing::TestParamInfo<std::tuple<bool, bool, bool, bool, std::string>>& obj) {- bool with_zp, with_bias, with_convert, weights_as_param;- std::string activation_op_type;- std::tie(with_zp, with_bias, with_convert, weights_as_param, activation_op_type) = obj.param;+ const testing::TestParamInfo<std::tuple<bool, bool, bool, bool, bool, bool, std::string>>& obj) {+ const auto& [with_group_quant,+ with_zp,+ with_bias,+ with_convert,+ with_param_weight,+ with_act_new_reshape,+ activation_op_type] = obj.param;std::ostringstream result;+ result << "with_group_quant=" << with_group_quant << "_";result << "with_zp=" << with_zp << "_";result << "with_bias=" << with_bias << "_";result << "with_convert=" << with_convert << "_";- result << "weights_as_param=" << weights_as_param << "_";+ result << "with_param_weight=" << with_param_weight << "_";+ result << "with_act_new_reshape=" << with_act_new_reshape << "_";result << "activation_op_type=" << activation_op_type;return result.str();}@@ -174,10 +224,20 @@ public:protected:void SetUp() override {TransformationTestsF::SetUp();- bool with_zp, with_bias, with_convert, weights_as_param;- std::string activation_op_type;- std::tie(with_zp, with_bias, with_convert, weights_as_param, activation_op_type) = GetParam();- Conv1x1ToMatmulTestParams params{with_zp, with_bias, with_convert, weights_as_param, activation_op_type};+ const auto& [with_group_quant,+ with_zp,+ with_bias,+ with_convert,+ with_param_weight,+ with_act_new_reshape,+ activation_op_type] = GetParam();+ Conv1x1ToMatmulTestParams params{with_group_quant,+ with_zp,+ with_bias,+ with_convert,+ with_param_weight,+ with_act_new_reshape,+ activation_op_type};model = gen_model(params);model_ref = gen_model_ref(params);manager.register_pass<ov::pass::ConvertWeightCompressedConv1x1ToMatmul>();@@ -189,6 +249,8 @@ TEST_P(ConvertWeightCompressedConv1x1ToMatmulTest, CompareFunctions) {}INSTANTIATE_TEST_SUITE_P(TransformationTests,ConvertWeightCompressedConv1x1ToMatmulTest,::testing::Combine(::testing::Bool(),+ ::testing::Bool(),+ ::testing::Bool(),::testing::Bool(),::testing::Bool(),::testing::Bool(),@@ -196,8 +258,15 @@ INSTANTIATE_TEST_SUITE_P(TransformationTests,ConvertWeightCompressedConv1x1ToMatmulTest::get_test_case_name);// Checked blocked cases-TEST(TransformationTests, ConvertWeightCompressedConv1x1ToMatmulExceptionTest_conv3x3) {- auto CreateConv = [&]() {+class ConvertWeightCompressedConv1x1ToMatmulTest_Blocked : public TransformationTestsF {+public:+ ConvertWeightCompressedConv1x1ToMatmulTest_Blocked() {+ manager.register_pass<ov::pass::ConvertWeightCompressedConv1x1ToMatmul>();+ }+};++TEST_F(ConvertWeightCompressedConv1x1ToMatmulTest_Blocked, conv3x3) {+ auto CreateConv3x3 = [&]() {ov::Strides strides{1, 1};ov::Strides dilations{1, 1};ov::CoordinateDiff pads_begin{1, 1};@@ -222,22 +291,6 @@ TEST(TransformationTests, ConvertWeightCompressedConv1x1ToMatmulExceptionTest_coauto model = std::make_shared<ov::Model>(ov::OutputVector{transpose2}, ov::ParameterVector{input1});return model;};-- ov::pass::Manager manager;- manager.set_per_pass_validation(false);- manager.register_pass<ov::pass::ConvertWeightCompressedConv1x1ToMatmul>();-- auto func = CreateConv();-- manager.run_passes(func);-- bool success = false;- for (auto& ops : func->get_ops()) {- std::string type_name(ops->get_type_name());- if (type_name.find("MatMul") != std::string::npos) {- success = true;- break;- }- }- ASSERT_TRUE(success == false);+ model = CreateConv3x3();}+} // namespace ov::testdiff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cppindex ec42cdf519c..4fd2454c215 100644--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp@@ -18,6 +18,7 @@#include "low_precision/common/precisions_restriction.hpp"#include "low_precision/common/quantization_granularity_restriction.hpp"#include "low_precision/layer_transformation.hpp"+#include "low_precision/qdq_stripping.hpp"#include "low_precision/quantization_details.hpp"#include "nodes/fullyconnected.h"#include "openvino/core/descriptor/tensor.hpp"@@ -105,6 +106,7 @@#include "transformations/op_conversions/convert_space_to_depth.hpp"#include "transformations/op_conversions/convert_ti_to_sequences.hpp"#include "transformations/op_conversions/convert_topk11_downgrade.hpp"+#include "transformations/op_conversions/convert_weight_compressed_conv1x1_to_matmul.hpp"#include "transformations/op_conversions/detection_output_downgrade.hpp"#include "transformations/op_conversions/detection_output_upgrade.hpp"#include "transformations/op_conversions/eye_decomposition.hpp"@@ -294,14 +296,32 @@ namespace ov::intel_cpu {using const_node_ptr = const std::shared_ptr<const ov::Node>;bool Transformations::is_decompression_multiply(const_node_ptr& node) {- auto all_has_type = [](const std::set<ov::Input<ov::Node>>& consumers, const ov::DiscreteTypeInfo& type) {- return std::all_of(consumers.begin(), consumers.end(), [&type](const ov::Input<ov::Node>& input) {- return input.get_node()->get_type_info() == type;+ auto is_1x1_conv = [](const ov::Node* node) {+ const auto* conv = ov::as_type<const ov::op::v1::Convolution>(node);+ if (!conv) {+ return false;+ }+ const auto& weights_pshape = conv->get_input_partial_shape(1);+ return weights_pshape.is_static() && weights_pshape.rank().get_length() == 4 &&+ weights_pshape[2].get_length() == 1 && weights_pshape[3].get_length() == 1;+ };++ auto all_has_type = [&](const std::set<ov::Input<ov::Node>>& consumers, const ov::DiscreteTypeInfo& type) {+ return std::all_of(consumers.begin(), consumers.end(), [&type, &is_1x1_conv](const ov::Input<ov::Node>& input) {+ auto* consumer_node = input.get_node();+ if (consumer_node->get_type_info() != type) {+ return false;+ }+ if (type == ov::op::v1::Convolution::get_type_info_static()) {+ return is_1x1_conv(consumer_node);+ }+ return true;});};const auto consumers = node->get_output_target_inputs(0);- if (all_has_type(consumers, ov::op::v0::MatMul::get_type_info_static())) {+ if (all_has_type(consumers, ov::op::v0::MatMul::get_type_info_static()) ||+ all_has_type(consumers, ov::op::v1::Convolution::get_type_info_static())) {return true;}@@ -311,7 +331,8 @@ bool Transformations::is_decompression_multiply(const_node_ptr& node) {}return std::all_of(consumers.begin(), consumers.end(), [&all_has_type](const ov::Input<ov::Node>& consumer) {const auto child_consumers = consumer.get_node()->get_output_target_inputs(0);- return all_has_type(child_consumers, ov::op::v0::MatMul::get_type_info_static());+ return all_has_type(child_consumers, ov::op::v0::MatMul::get_type_info_static()) ||+ all_has_type(child_consumers, ov::op::v1::Convolution::get_type_info_static());});};@@ -319,6 +340,7 @@ bool Transformations::is_decompression_multiply(const_node_ptr& node) {for (const auto& consumer : consumers) {const auto child_consumers = consumer.get_node()->get_output_target_inputs(0);if (all_has_type(child_consumers, ov::op::v0::MatMul::get_type_info_static()) ||+ all_has_type(child_consumers, ov::op::v1::Convolution::get_type_info_static()) ||are_converts_from_decompression(child_consumers)) {return true;}@@ -491,6 +513,23 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecisov::pass::ConvertGatherToGatherCompressed);decompression_handling_manager.run_passes(model);+ {+ using namespace ov::pass::low_precision;+ const auto enableQDQStripping = LowPrecision::isFunctionQuantized(model, std::set<levels>{levels::int16});+ if (enableQDQStripping) {+ ov::pass::Manager qdq_stripping_manager("Plugin:CPU:QDQ_Stripping");+ using namespace ov::element;+ // QDQ stripping pipeline+ // 1. Fuse FQ->Convert->DQ to a single FQ+ qdq_stripping_manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16},+ TypeVector{f32},+ true);+ // 2. Strip FQ layers with unsupported levels+ qdq_stripping_manager.register_pass<FQStrippingTransformation>(std::set<size_t>{levels::int16}, false);+ qdq_stripping_manager.run_passes(model);+ }+ }+ov::pass::Manager manager("Plugin:CPU");manager.set_per_pass_validation(false);if (useLpt) {@@ -1063,6 +1102,7 @@ void Transformations::PostLpt() {ov::pass::Manager postLPTPassManager("CPU:PostLPT");postLPTPassManager.set_per_pass_validation(false);+ CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::ConvertWeightCompressedConv1x1ToMatmul);CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::ConvertBroadcast3);CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::ReshapePRelu);CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::MoveEltwiseUpThroughDataMov);diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.clindex f709f4c0868..6dd09d08dc6 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl@@ -231,6 +231,27 @@ KERNEL (reorder_data)(res = __TO_OUTPUT_REORDER_TYPE(res);FUSED_OPS;output[output_idx] = FUSED_OPS_RESULT;+ #elif defined(INT4_OUTPUT) || defined(UINT4_OUTPUT)+ OUTPUT_TYPE val_char = __TO_OUTPUT_REORDER_TYPE(res);+ int val_i32 = convert_int(val_char);++ #if !CONVERT_TRUNCATE+ #if defined(INT4_OUTPUT)+ val_i32 = clamp(val_i32, -8, 7);+ #else+ val_i32 = clamp(val_i32, 0, 15);+ #endif+ #endif++ uint val_u32 = (uint)(val_i32 & 0x0F);++ volatile __global uint* output_u32 = (volatile __global uint*)output;+ uint main_idx = output_idx / 8;+ uint sub_idx = output_idx % 8;+ uint shift = sub_idx * 4;++ atomic_and(&output_u32[main_idx], ~(0x0F << shift));+ atomic_or(&output_u32[main_idx], (val_u32 << shift));#elseoutput[output_idx] = ACTIVATION_TYPED(OUTPUT_REORDER, __TO_OUTPUT_REORDER_TYPE(res), ACTIVATION_PARAMS_TYPED);#endifdiff --git a/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp b/src/plugins/intel_gpu/src/kernel_selector/jitter.cppindex 71dae60f437..9344d805717 100644--- a/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp@@ -1516,11 +1516,15 @@ JitConstants MakeTypeJitConstants(Datatype dataType, const std::string& macroNambreak;case Datatype::INT4:type = "char";+ to_type = "convert_char(v)";+ to_type_sat = "convert_char_sat(v)";type_size = "0.5f";is_fp = false;break;case Datatype::UINT4:type = "uchar";+ to_type = "convert_uchar(v)";+ to_type_sat = "convert_uchar_sat(v)";type_size = "0.5f";is_fp = false;break;diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cppindex eafdd3148bb..50ef5198d87 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp@@ -29,6 +29,8 @@ ParamsKey ReorderKernelRef::GetSupportedKey() const {k.EnableOutputDataType(Datatype::UINT8);k.EnableOutputDataType(Datatype::UINT16);k.EnableOutputDataType(Datatype::UINT32);+ k.EnableOutputDataType(Datatype::UINT4);+ k.EnableOutputDataType(Datatype::INT4);k.EnableOutputDataType(Datatype::BF16);k.EnableSurfaceInputSupport();k.EnableDifferentTypes();@@ -74,6 +76,14 @@ JitConstants ReorderKernelRef::GetJitConstants(const reorder_params& params) conjit.AddConstant(MakeJitConstant("UINT4_INPUT", true));}+ if ( params.outputs[0].GetDType() == Datatype::UINT4 ) {+ jit.AddConstant(MakeJitConstant("UINT4_OUTPUT", true));+ }++ if ( params.outputs[0].GetDType() == Datatype::INT4 ) {+ jit.AddConstant(MakeJitConstant("INT4_OUTPUT", true));+ }+return jit;}diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cppindex 40608553654..f38162dbb6f 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp@@ -3884,3 +3884,51 @@ TEST(reorder_gpu_i4, basic_int4){run_reorder_int4({32, 1, 1, 1});}++template <typename T>+void run_reorder_test_i4(data_types input_type, data_types output_type, const std::vector<T>& input_data, const std::vector<uint8_t>& expected) {+ auto& engine = get_test_engine();++ layout in_layout({ov::Shape{1, 1, 2, 2}, input_type, format::bfyx});+ layout out_layout({ov::Shape{1, 1, 2, 2}, output_type, format::bfyx});++ memory::ptr input_mem = engine.allocate_memory(in_layout);+ set_values(input_mem, input_data);++ topology topology(input_layout("input", in_layout), reorder("reorder", input_info("input"), out_layout));++ auto 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();+ auto output_mem = outputs.at("reorder").get_memory();+ cldnn::mem_lock<uint8_t> output_ptr(output_mem, get_test_stream());++ for (size_t i = 0; i < expected.size(); ++i) {+ ASSERT_EQ(expected[i], output_ptr[i]);+ }+}++TEST(reorder_gpu_i4, i4_identity) {+ std::vector<uint8_t> input_data = {0x78, 0x60};+ run_reorder_test_i4(data_types::i4, data_types::i4, input_data, {0x78, 0x60});+}++TEST(reorder_gpu_i4, u4_identity) {+ std::vector<uint8_t> input_data = {0x78, 0x60};+ run_reorder_test_i4(data_types::u4, data_types::u4, input_data, {0x78, 0x60});+}++TEST(reorder_gpu_i4, fp16_to_i4) {+ std::vector<ov::float16> input_data = {ov::float16(-8.5f), ov::float16(7.2f), ov::float16(0.0f), ov::float16(6.0f)};+ run_reorder_test_i4(data_types::f16, data_types::i4, input_data, {0x78, 0x60});+}++TEST(reorder_gpu_i4, fp16_to_u4) {+ std::vector<ov::float16> input_data = {ov::float16(-8.5f), ov::float16(7.2f), ov::float16(0.0f), ov::float16(6.0f)};+ run_reorder_test_i4(data_types::f16, data_types::u4, input_data, {0x70, 0x60});+}