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] Support non-transposed INT4 compressed weights in FullyConnected for shared weights scenario (#36845)
Kamil Wieloch5dc350efd3c0
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.cppindex 20f014c9840..d5a4d480710 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@@ -266,6 +266,12 @@ public:const kernel_impl_params* impl_params = reinterpret_cast<kernel_impl_params*>(ib.getKernelImplParams());auto prim = impl_params->typed_desc<fully_connected>();auto weights_layout = impl_params->get_input_layout(1);+ const 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);+ const auto ifm_dim_idx = prim->weights_transposed ? (weight_rank - 1) : (weight_rank - 2);+auto shift_size = std::max<size_t>(prim->input_size - 2, 0);auto& arg = impl_params->get_program().get_node(impl_params->desc->id).as<fully_connected>();int idx = !arg.bias_term() ? 1 : 2;@@ -279,7 +285,7 @@ public:auto decompression_scale_idx = ++idx;auto scale_layout = arg.get_dependency(decompression_scale_idx).get_output_layout();- auto ngroups = scale_layout.get_dim(1);+ const auto ngroups = scale_layout.get_dim(ifm_dim_idx);if (scale_layout.count() == 1) {_attrs->set_scales(DNNL_ARG_WEIGHTS, COMMON, dnnl::memory::dims{}, _ds_data_type);} else if (ngroups == 1) {@@ -299,7 +305,7 @@ public:if (dzp_layout.count() == 1) {_attrs->set_zero_points(DNNL_ARG_WEIGHTS, COMMON, dnnl::memory::dims{}, _dzp_data_type);} else {- auto ngroups = dzp_layout.get_dim(1);+ auto ngroups = dzp_layout.get_dim(ifm_dim_idx);if (ngroups == 1) {_attrs->set_zero_points(DNNL_ARG_WEIGHTS, per_oc, dnnl::memory::dims{}, _dzp_data_type);} else {@@ -380,8 +386,10 @@ 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(weight_rank - 1);- auto ngroups = scale_layout.get_dim(weight_rank - 1);+ // IFM (K) dimension position depends on weight layout orientation.+ const auto ifm_dim_idx = prim->weights_transposed ? (weight_rank - 1) : (weight_rank - 2);+ const auto ifm = arg.get_dependency(1).get_output_layout().get_dim(ifm_dim_idx);+ const auto ngroups = scale_layout.get_dim(ifm_dim_idx);group_size = static_cast<int>(ifm / ngroups);OPENVINO_ASSERT((group_size == 1 || ngroups == 1 || group_size % 16 == 0),"[GPU] group_size should be aligned to 16 if it is not a single scale group or the group_size is not one.");@@ -410,7 +418,8 @@ public:auto dzp_rank = std::count_if(dzp_shape.begin(), dzp_shape.end(), [](ov::Dimension d) { return d.get_length() > 1; });dzp_rank = std::max(static_cast<int64_t>(2), dzp_rank);- auto ngroups = dzp_layout.get_dim(dzp_rank - 1);+ auto dzp_ifm_dim_idx = prim->weights_transposed ? (dzp_rank - 1) : (dzp_rank - 2);+ auto ngroups = dzp_layout.get_dim(dzp_ifm_dim_idx);if (ngroups == 1 && dzp_rank <= 2) {attr->set_zero_points(DNNL_ARG_WEIGHTS, per_oc, dnnl::memory::dims{}, dzp_data_type);} else {diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cppindex 8751f2e6213..6683f425ff0 100644--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp@@ -139,7 +139,9 @@ static void validate_and_check_shapes(const std::shared_ptr<const ov::ITensor>&" != dst: ",dst->get_element_type(),")");- OPENVINO_ASSERT(src->get_element_type().bitwidth() >= 8, "[GPU] Unsupported element type for copying: ", src->get_element_type());+ // Sub-byte types require contiguous (non-ROI) copies+ OPENVINO_ASSERT(src->get_element_type().bitwidth() >= 8 || roi_shape.empty(),+ "[GPU] ROI copy is not supported for sub-byte element type: ", src->get_element_type());// If it's a simple copy_to/copy_from call, then change dst shapeif (roi_shape.empty()) {@@ -215,6 +217,22 @@ void RemoteTensorImpl::copy_to(const std::shared_ptr<ov::ITensor>& dst,auto dst_remote_tensor = std::dynamic_pointer_cast<RemoteTensorImpl>(dst);auto shape = roi_shape.empty() ? get_shape() : roi_shape;+ // Sub-byte types use flat contiguous copy+ if (m_element_type.bitwidth() < 8) {+ const auto byte_size = get_byte_size();+ if (dst_remote_tensor != nullptr) {+ auto src_mem = MemWrapper(stream, get_memory(), nullptr);+ auto dst_mem = MemWrapper(stream, dst_remote_tensor->get_memory(), nullptr);+ src_mem.copy_to(dst_mem, src_offset, dst_offset, byte_size);+ } else {+ OPENVINO_ASSERT(!std::dynamic_pointer_cast<ov::IRemoteTensor>(dst), "[GPU] Unsupported Remote Tensor type");+ auto src_mem = MemWrapper(stream, get_memory(), nullptr);+ auto dst_mem = MemWrapper(stream, nullptr, dst->data());+ src_mem.copy_to(dst_mem, src_offset, dst_offset, byte_size);+ }+ return;+ }+ov::Strides roi_strides = calculate_strides(shape, m_element_type);if (dst_remote_tensor != nullptr) {GPU_DEBUG_TRACE_DETAIL << "Copying from RemoteTensor (" << get_memory()->get_allocation_type() << ") to RemoteTensor ("@@ -249,6 +267,22 @@ void RemoteTensorImpl::copy_from(const std::shared_ptr<const ov::ITensor>& src,auto& stream = m_context->get_engine().get_service_stream();auto src_remote_tensor = std::dynamic_pointer_cast<const RemoteTensorImpl>(src);+ // Sub-byte types use flat contiguous copy+ if (m_element_type.bitwidth() < 8) {+ const auto byte_size = get_byte_size();+ if (src_remote_tensor != nullptr) {+ auto src_mem = MemWrapper(stream, src_remote_tensor->get_memory(), nullptr);+ auto dst_mem = MemWrapper(stream, get_memory(), nullptr);+ src_mem.copy_to(dst_mem, src_offset, dst_offset, byte_size);+ } else {+ OPENVINO_ASSERT(!std::dynamic_pointer_cast<const ov::IRemoteTensor>(src), "[GPU] Unsupported Remote Tensor type");+ auto src_mem = MemWrapper(stream, nullptr, const_cast<void*>(src->data()));+ auto dst_mem = MemWrapper(stream, get_memory(), nullptr);+ src_mem.copy_to(dst_mem, src_offset, dst_offset, byte_size);+ }+ return;+ }+ov::Strides roi_strides = calculate_strides(shape, m_element_type);if (src_remote_tensor != nullptr) {GPU_DEBUG_TRACE_DETAIL << "Copying from RemoteTensor (" << src_remote_tensor->get_memory()->get_allocation_type() << ") to RemoteTensor ("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.cppindex 540fa326fe6..eb8f7bb83a5 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@@ -224,7 +224,20 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad// Weights normalizationbool is_small_matmul = true;- if (supports_immad && shape_a.is_static() && shape_b.is_static()) {++ // Sub-byte Parameter weights can't be transposed at runtime (permute kernel doesn't support them).+ // Use non-transposed FC layout instead; requires XMX for the oneDNN path.+ const bool is_parameter_compressed_weight = supports_immad &&+ is_compressed_weight &&+ !transpose_node &&+ !matmul->get_transpose_b() &&+ pattern_map.count(weights_param_m) != 0 &&+ pattern_map.at(weights_param_m).get_node_shared_ptr() != nullptr &&+ pattern_map.at(weights_param_m).get_element_type().bitwidth() < 8;++ if (is_parameter_compressed_weight) {+ is_small_matmul = false;+ } else if (supports_immad && shape_a.is_static() && shape_b.is_static()) {auto output_shape = matmul->get_output_shape(0);size_t k = 0;if (matmul->get_transpose_a())@@ -283,7 +296,7 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad}} else {if (!matmul->get_transpose_b()) {- if (can_reuse_transpose(fc_input_b)) {+ if (!is_parameter_compressed_weight && can_reuse_transpose(fc_input_b)) {fc_input_b = transpose_node;}} else {diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cppindex 82b6e1a33e7..ff3f6f7e3d0 100644--- a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp@@ -2787,6 +2787,119 @@ INSTANTIATE_TEST_SUITE_P(copy_tests,ov::Coordinate{0, 1, 1, 0}, ov::Coordinate{4, 2, 2, 3}})));+namespace {+// Sub-byte (i4) tensors can't be indexed per element, so fill/compare at byte granularity.+void fill_bytes(ov::Tensor& tensor) {+ auto* ptr = static_cast<uint8_t*>(tensor.data());+ for (size_t i = 0; i < tensor.get_byte_size(); ++i) {+ ptr[i] = static_cast<uint8_t>((i * 37 + 11) & 0xFF);+ }+}++// Sub-byte copy is a flat whole-byte copy, so the padded tail nibble must round-trip byte-for-byte too.+void expect_bytes_eq(const ov::Tensor& expected, const ov::Tensor& actual) {+ ASSERT_EQ(expected.get_byte_size(), actual.get_byte_size());+ const auto* e = static_cast<const uint8_t*>(expected.data());+ const auto* a = static_cast<const uint8_t*>(actual.data());+ for (size_t i = 0; i < expected.get_byte_size(); ++i) {+ ASSERT_EQ(e[i], a[i]) << "byte mismatch at offset " << i;+ }+}+} // namespace++using RemoteTensorSubByteParams = std::tuple<ov::element::Type, RemoteTensorSharingType, ov::Shape>;++struct RemoteTensorSubByte : ::testing::TestWithParam<RemoteTensorSubByteParams> {+ static std::string getTestCaseName(const testing::TestParamInfo<RemoteTensorSubByteParams>& obj) {+ const auto& [type, sharing_type, shape] = obj.param;+ std::ostringstream result;+ result << "type=" << type.get_type_name() << "_sharing=" << sharing_type << "_shape=";+ for (size_t i = 0; i < shape.size(); ++i) {+ result << (i ? "x" : "") << shape[i];+ }+ return result.str();+ }+};++TEST_P(RemoteTensorSubByte, smoke_CopyFrom) {+#if defined(ANDROID)+ GTEST_SKIP();+#endif+ const auto& [type, sharing_type, shape] = GetParam();++ auto core = ov::Core();+ auto remote_context = core.get_default_context(ov::test::utils::DEVICE_GPU);+ auto gpu_context = remote_context.as<ov::intel_gpu::ocl::ClContext>();++ auto host_ref = ov::Tensor(type, shape);+ fill_bytes(host_ref);++ // host -> remote, then read back remote -> host+ auto remote_dst = create_tensor(gpu_context, sharing_type, type, shape);+ remote_dst.copy_from(host_ref);+ ASSERT_EQ(remote_dst.get_shape(), shape);++ auto host_out = ov::Tensor(type, shape);+ remote_dst.copy_to(host_out);+ expect_bytes_eq(host_ref, host_out);++ // remote -> remote+ auto remote_src = create_tensor(gpu_context, sharing_type, type, shape);+ remote_src.copy_from(host_ref);++ auto remote_dst2 = create_tensor(gpu_context, sharing_type, type, shape);+ remote_dst2.copy_from(remote_src);+ ASSERT_EQ(remote_dst2.get_shape(), shape);++ auto host_out2 = ov::Tensor(type, shape);+ remote_dst2.copy_to(host_out2);+ expect_bytes_eq(host_ref, host_out2);+}++TEST_P(RemoteTensorSubByte, smoke_CopyTo) {+#if defined(ANDROID)+ GTEST_SKIP();+#endif+ const auto& [type, sharing_type, shape] = GetParam();++ auto core = ov::Core();+ auto remote_context = core.get_default_context(ov::test::utils::DEVICE_GPU);+ auto gpu_context = remote_context.as<ov::intel_gpu::ocl::ClContext>();++ auto host_ref = ov::Tensor(type, shape);+ fill_bytes(host_ref);++ // remote -> remote via copy_to+ auto remote_src = create_tensor(gpu_context, sharing_type, type, shape);+ remote_src.copy_from(host_ref);++ auto remote_dst = create_tensor(gpu_context, sharing_type, type, shape);+ remote_src.copy_to(remote_dst);+ ASSERT_EQ(remote_dst.get_shape(), shape);++ auto host_out = ov::Tensor(type, shape);+ remote_dst.copy_to(host_out);+ expect_bytes_eq(host_ref, host_out);++ // host -> remote via host.copy_to(remote)+ auto remote_dst2 = create_tensor(gpu_context, sharing_type, type, shape);+ host_ref.copy_to(remote_dst2);++ auto host_out2 = ov::Tensor(type, shape);+ remote_dst2.copy_to(host_out2);+ expect_bytes_eq(host_ref, host_out2);+}++INSTANTIATE_TEST_SUITE_P(smoke_copy_sub_byte,+ RemoteTensorSubByte,+ ::testing::Combine(::testing::Values(ov::element::i4, ov::element::u2),+ ::testing::Values(RemoteTensorSharingType::PLUGIN_CL_TENSOR,+ RemoteTensorSharingType::PLUGIN_USM_DEVICE_TENSOR,+ RemoteTensorSharingType::PLUGIN_HOST_TENSOR),+ ::testing::Values(ov::Shape{4, 16},+ ov::Shape{3, 5})),+ RemoteTensorSubByte::getTestCaseName);+TEST(RemoteTensor, smoke_CanSetRoiRemoteTensor) {#if defined(ANDROID)GTEST_SKIP();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.cppindex 5c83c5242fd..1db56f21745 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@@ -9,6 +9,7 @@#include "intel_gpu/runtime/internal_properties.hpp"#include "intel_gpu/runtime/utils.hpp"#include "openvino/op/constant.hpp"+#include "openvino/runtime/intel_gpu/properties.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/matmul.hpp"#include "openvino/op/multiply.hpp"@@ -249,9 +250,15 @@ TEST_P(MatmulWeightsDecompression, Inference) {param_weights,dyn_quan_group_size,abs_threshold_f16] = GetParam();- // Skip tests for 4-bit parameter weights because 4-bit transpose is not supported- if (param_weights && weights_precision != ov::element::u8) {- GTEST_SKIP();+ // Sub-byte parameter weights need the non-transposed FC path which requires XMX+ if (param_weights && weights_precision.bitwidth() < 8) {+ if (transpose_weights) {+ GTEST_SKIP() << "Sub-byte parameter weights with transposed layout need runtime transpose, which GPU can't do";+ }+ const auto caps = core->get_property(targetDevice, ov::device::capabilities);+ if (std::find(caps.begin(), caps.end(), ov::intel_gpu::capability::HW_MATMUL) == caps.end()) {+ GTEST_SKIP() << "Non-transposed sub-byte parameter weights require XMX (HW_MATMUL capability)";+ }}SKIP_IF_CURRENT_TEST_IS_DISABLED(); // This is necessary because of check_resultsrun();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.cppindex 9b66f1b3cff..8548cb934d2 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@@ -1081,3 +1081,30 @@ TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_LargeF16_NoXMX_Transpmodel_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});}}++// Verify that Parameter INT4 compressed weights skip the Transpose and produce FC with transpose_b=false+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnected_ParameterCompressedWeights_NoTranspose) {+ {+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{128, 2048});+ auto weights = std::make_shared<ov::opset1::Parameter>(ov::element::u4, ov::Shape{2048, 8192});+ auto convert = std::make_shared<ov::opset1::Convert>(weights, ov::element::f32);+ auto scale = ov::opset1::Constant::create(ov::element::f32, ov::Shape{1, 8192}, {0.1f});+ auto mul = std::make_shared<ov::opset1::Multiply>(convert, scale);+ auto matmul = std::make_shared<ov::opset1::MatMul>(input, mul, false, false);++ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{input, weights});+ manager.register_pass<ConvertMatMulToFullyConnected>(true);+ }+ {+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{128, 2048});+ auto weights = std::make_shared<ov::opset1::Parameter>(ov::element::u4, ov::Shape{2048, 8192});+ auto convert = std::make_shared<ov::opset1::Convert>(weights, ov::element::f32);+ auto scale = ov::opset1::Constant::create(ov::element::f32, ov::Shape{1, 8192}, {0.1f});+ auto mul = std::make_shared<ov::opset1::Multiply>(convert, scale);+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();+ // transpose_b=false: weights stay in [K, N] layout, no Transpose inserted+ auto fc = std::make_shared<op::FullyConnected>(input, mul, no_bias, ov::element::f32, /*transpose_b=*/false);++ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input, weights});+ }+}