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
[Transformations] Fix ConvertFCToCompressed apply_transpose for rank-1 scale/zp (#36044)
Maxim Vafin5f62a559de0c
diff --git a/src/common/transformations/src/transformations/op_conversions/convert_fc_to_compressed.cpp b/src/common/transformations/src/transformations/op_conversions/convert_fc_to_compressed.cppindex 54a7a57ed7f..096b61bd952 100644--- a/src/common/transformations/src/transformations/op_conversions/convert_fc_to_compressed.cpp+++ b/src/common/transformations/src/transformations/op_conversions/convert_fc_to_compressed.cpp@@ -16,6 +16,7 @@#include "openvino/op/reshape.hpp"#include "openvino/op/subtract.hpp"#include "openvino/op/transpose.hpp"+#include "openvino/op/unsqueeze.hpp"#include "openvino/pass/pattern/op/or.hpp"#include "openvino/pass/pattern/op/pattern.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"@@ -98,23 +99,58 @@ ConvertFullyConnectedToFullyConnectedCompressed::process_compressed_weights(const auto& transpose = weights_block->get_anchor("transpose", pattern_map).value().get_node_shared_ptr();std::shared_ptr<ov::Node> transpose_const =weights_block->get_anchor("transpose_const", pattern_map).value().get_node_shared_ptr();- if (ov::shape_size(transpose_const->get_shape()) != fc_input_b->get_output_partial_shape(0).size()) {- std::vector<int32_t> new_order(fc_input_b->get_output_partial_shape(0).size());- std::iota(new_order.begin(), new_order.end(), 0);- std::swap(new_order[new_order.size() - 1], new_order[new_order.size() - 2]);- transpose_const = std::make_shared<v0::Constant>(ov::element::i32, ov::Shape{new_order.size()}, new_order);- }- fc_input_b = transpose->clone_with_new_inputs({fc_input_b->output(0), transpose_const});- ov::disable_constant_folding(fc_input_b);- result_nodes.push_back(fc_input_b);- fc_input_scale = transpose->clone_with_new_inputs({scale->output(0), transpose_const});- ov::disable_constant_folding(fc_input_scale);- result_nodes.push_back(fc_input_scale);+ // The matched `transpose_const` was authored for the weights tensor and may have a+ // different rank than `scale` / `zero_point` (which can be per-channel rank-1+ // constants while the weights are rank-2). Align each input's rank, then build a+ // perm that matches it; rank-1 per-channel constants are unsqueezed to rank-2+ // [N, 1] so that downstream consumers (e.g. DnnlPostOpsComposer in the CPU plugin)+ // which require rank-2/3 decompression params can prepack them. Inputs with a+ // single element are left as-is.+ // All inputs reaching this lambda are Constants (optionally wrapped in a Convert+ // injected by `convert_u4const_to_u8`), so their shapes are always static.+ auto align_and_transpose = [&](const ov::Output<ov::Node>& in) -> std::shared_ptr<ov::Node> {+ const auto& in_shape = in.get_shape();+ const auto in_rank = in_shape.size();+ if (in_rank == 0 || ov::shape_size(in_shape) == 1) {+ return in.get_node_shared_ptr();+ }+ std::shared_ptr<ov::Node> node = in.get_node_shared_ptr();+ if (in_rank == 1) {+ // Promote rank-1 per-channel constant to rank-2 [N, 1] via Unsqueeze.+ // Peel a wrapping Convert (injected by convert_u4const_to_u8 for u4 ZP)+ // so make_try_fold can collapse Unsqueeze on the underlying Constant,+ // then re-apply the Convert on the folded result.+ auto wrapping_convert = ov::as_type_ptr<v0::Convert>(node);+ auto inner = wrapping_convert ? wrapping_convert->get_input_node_shared_ptr(0) : node;+ auto axis = v0::Constant::create(ov::element::i32, ov::Shape{}, {1});+ auto unsqueezed = ov::op::util::make_try_fold<v0::Unsqueeze>(inner, axis);+ result_nodes.push_back(unsqueezed);+ if (wrapping_convert) {+ auto rewrapped =+ std::make_shared<v0::Convert>(unsqueezed, wrapping_convert->get_destination_type());+ result_nodes.push_back(rewrapped);+ return rewrapped;+ }+ return unsqueezed;+ }+ std::shared_ptr<ov::Node> perm = transpose_const;+ if (ov::shape_size(perm->get_shape()) != in_rank) {+ std::vector<int32_t> new_order(in_rank);+ std::iota(new_order.begin(), new_order.end(), 0);+ std::swap(new_order[in_rank - 1], new_order[in_rank - 2]);+ perm = std::make_shared<v0::Constant>(ov::element::i32, ov::Shape{in_rank}, new_order);+ }+ auto transposed = transpose->clone_with_new_inputs({node->output(0), perm});+ ov::disable_constant_folding(transposed);+ result_nodes.push_back(transposed);+ return transposed;+ };++ fc_input_b = align_and_transpose(fc_input_b->output(0));+ fc_input_scale = align_and_transpose(scale->output(0));if (with_zero_point && ov::shape_size(optional_zero_point->output(0).get_shape()) > 1) {- fc_input_zp = transpose->clone_with_new_inputs({optional_zero_point->output(0), transpose_const});- ov::disable_constant_folding(fc_input_zp);- result_nodes.push_back(fc_input_zp);+ fc_input_zp = align_and_transpose(optional_zero_point->output(0));}}diff --git a/src/common/transformations/tests/op_conversions/convert_fc_to_compressed_test.cpp b/src/common/transformations/tests/op_conversions/convert_fc_to_compressed_test.cppindex 798b8dcd095..56c0ca5ed0f 100644--- a/src/common/transformations/tests/op_conversions/convert_fc_to_compressed_test.cpp+++ b/src/common/transformations/tests/op_conversions/convert_fc_to_compressed_test.cpp@@ -14,6 +14,7 @@#include "openvino/op/parameter.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/subtract.hpp"+#include "openvino/op/transpose.hpp"#include "openvino/pass/manager.hpp"#include "ov_ops/fully_connected.hpp"#include "ov_ops/fully_connected_compressed.hpp"@@ -131,3 +132,101 @@ const auto params = std::vector<ConvertFCToCompressedParams>{} // namespaceINSTANTIATE_TEST_SUITE_P(TransformationTests, ConvertFCToCompressed, ::testing::ValuesIn(params));++// Regression test: when the matched Transpose acts on the weights tensor and+// scale / zero-point are rank-1 per-output-channel Constants, the old code+// path tried to apply the rank-2 weight perm to a rank-1 input, which+// crashes Transpose shape inference. The fixed `apply_transpose` lambda+// promotes rank-1 per-channel Constants to rank-2 [N, 1] without inserting a+// runtime op, while still transposing the rank-2 weights.+TEST_F(TransformationTestsF, ConvertFCToCompressedRank1ScaleZpWithTranspose) {+ const size_t OC = 5;+ const size_t IC = 2048;+ const auto compressed_type = ov::element::u8;+ const std::vector<ov::element::Type> supported_activation_types{ov::element::f32};+ const std::vector<ov::element::Type> supported_weights_types{compressed_type};++ manager.register_pass<ov::pass::ConvertFullyConnectedToFullyConnectedCompressed>(supported_activation_types,+ supported_weights_types);+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{10, IC});+ // Weights laid out as [IC, OC] so that a Transpose([1, 0]) yields [OC, IC]+ // (matching the FullyConnected input layout convention).+ auto weights_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{IC, OC}, {1});+ auto wei_convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32);+ // Rank-1 per-output-channel zero-point.+ auto zp_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{OC}, {1});+ auto zp_convert = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::f32);+ auto wei_sub = std::make_shared<ov::op::v1::Subtract>(wei_convert, zp_convert);+ // Rank-1 per-output-channel scale.+ auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{OC}, {1});+ auto wei_scale = std::make_shared<ov::op::v1::Multiply>(wei_sub, scale_const);+ auto perm = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{2}, std::vector<int32_t>{1, 0});+ auto wei_t = std::make_shared<ov::op::v1::Transpose>(wei_scale, perm);+ auto bias = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{0});+ auto fc = std::make_shared<ov::op::internal::FullyConnected>(input, wei_t, bias);++ model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});+ }+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{10, IC});+ // Weights Constant stays in its original [IC, OC] layout; the transformation+ // re-clones the rank-2 Transpose with the same perm.+ auto weights_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{IC, OC}, {1});+ auto perm = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{2}, std::vector<int32_t>{1, 0});+ auto weights_t = std::make_shared<ov::op::v1::Transpose>(weights_const, perm);+ // Rank-1 [OC] -> rank-2 [OC, 1] Constant reshape baked in (no runtime op).+ auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{OC, 1}, {1});+ auto zp_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{OC, 1}, {1});+ auto bias = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{0});+ auto fc_compressed =+ std::make_shared<ov::op::internal::FullyConnectedCompressed>(input, weights_t, bias, scale_const, zp_const);+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc_compressed}, ov::ParameterVector{input});+ }+}++// Regression test: rank-0 (scalar / per-tensor) scale and zero-point with weights+// Transpose. The old code path applied the rank-2 weight perm to a rank-0 input+// which crashes Transpose shape inference. The fixed `apply_transpose` returns+// rank-0 Constants unchanged while still transposing the rank-2 weights.+TEST_F(TransformationTestsF, ConvertFCToCompressedRank0ScaleZpWithTranspose) {+ const size_t OC = 5;+ const size_t IC = 2048;+ const auto compressed_type = ov::element::u8;+ const std::vector<ov::element::Type> supported_activation_types{ov::element::f32};+ const std::vector<ov::element::Type> supported_weights_types{compressed_type};++ manager.register_pass<ov::pass::ConvertFullyConnectedToFullyConnectedCompressed>(supported_activation_types,+ supported_weights_types);+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{10, IC});+ auto weights_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{IC, OC}, {1});+ auto wei_convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32);+ // Rank-0 (scalar) per-tensor zero-point.+ auto zp_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{}, {1});+ auto zp_convert = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::f32);+ auto wei_sub = std::make_shared<ov::op::v1::Subtract>(wei_convert, zp_convert);+ // Rank-0 (scalar) per-tensor scale.+ auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {1});+ auto wei_scale = std::make_shared<ov::op::v1::Multiply>(wei_sub, scale_const);+ auto perm = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{2}, std::vector<int32_t>{1, 0});+ auto wei_t = std::make_shared<ov::op::v1::Transpose>(wei_scale, perm);+ auto bias = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{0});+ auto fc = std::make_shared<ov::op::internal::FullyConnected>(input, wei_t, bias);++ model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input});+ }+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{10, IC});+ auto weights_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{IC, OC}, {1});+ auto perm = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{2}, std::vector<int32_t>{1, 0});+ auto weights_t = std::make_shared<ov::op::v1::Transpose>(weights_const, perm);+ // Rank-0 scale / zp pass through apply_transpose unchanged.+ auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{}, {1});+ auto zp_const = ov::op::v0::Constant::create(compressed_type, ov::Shape{}, {1});+ auto bias = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{0});+ auto fc_compressed =+ std::make_shared<ov::op::internal::FullyConnectedCompressed>(input, weights_t, bias, scale_const, zp_const);+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc_compressed}, ov::ParameterVector{input});+ }+}