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
Add TransposeFuse to the frontend normalize pass (#36591)
Katarzyna Mitrus1094fbfc4109
diff --git a/src/frontends/pytorch/src/frontend.cpp b/src/frontends/pytorch/src/frontend.cppindex c9341b44df1..06e36e3ca12 100644--- a/src/frontends/pytorch/src/frontend.cpp+++ b/src/frontends/pytorch/src/frontend.cpp@@ -16,6 +16,7 @@#include "transformations/common_optimizations/push_constant_to_subgraph.hpp"#include "transformations/common_optimizations/remove_multi_subgraph_op_dangling_params.hpp"#include "transformations/common_optimizations/reverse_shape_and_type_infer.hpp"+#include "transformations/common_optimizations/transpose_sinking.hpp"#include "transformations/control_flow/unroll_if.hpp"#include "transformations/fp16_compression/mark_decompression_convert_constant_folding.hpp"#include "transformations/op_conversions/convert_convertlike.hpp"@@ -228,6 +229,7 @@ void FrontEnd::normalize(const std::shared_ptr<ov::Model>& model) const {manager.register_pass<ov::pass::MarkCompressedFloatConstants>();manager.register_pass<ov::pass::ConvertConvertPromoteTypes>();+ manager.register_pass<ov::pass::TransposeFuse>(); // Required for fusion of Transpose for grouped_mmmanager.register_pass<ov::pass::PushConstantToSubgraph>();manager.register_pass<ov::frontend::pytorch::pass::TupleUnpackInBodyReplacer>();manager.register_pass<ov::frontend::pass::SequenceConcatReplacer>();diff --git a/src/frontends/pytorch/src/op/grouped_mm.cpp b/src/frontends/pytorch/src/op/grouped_mm.cppindex 00bc7f75a87..3783b328112 100644--- a/src/frontends/pytorch/src/op/grouped_mm.cpp+++ b/src/frontends/pytorch/src/op/grouped_mm.cpp@@ -37,23 +37,15 @@ OutputVector translate_grouped_mm(const NodeContext& context) {// PyTorch provides mat_b as [G, K, N] (3D) or [total_tokens, N] (2D).// GroupedMatMul-17 expects [G, N, K] (3D) — N-first layout.- // If b is already a Transpose with the expected permutation, fold it by taking- // the input of that existing Transpose instead of stacking another one.const auto b_rank = b.get_partial_shape().rank();PYTORCH_OP_CONVERSION_CHECK(!b_rank.is_static() || b_rank.get_length() != 2,"grouped_mm: 2D × 2D (weight gradient) case is not supported.");if (b_rank.is_static() && b_rank.get_length() == 3) {- const std::vector<int32_t> expected_perm{0, 2, 1};- auto existing_transpose = ov::as_type_ptr<v1::Transpose>(b.get_node_shared_ptr());- auto existing_perm =- existing_transpose ? ov::as_type_ptr<v0::Constant>(existing_transpose->input_value(1).get_node_shared_ptr())- : nullptr;- if (existing_perm && existing_perm->cast_vector<int32_t>() == expected_perm) {- b = existing_transpose->input_value(0);- } else {- auto perm = context.mark_node(v0::Constant::create(element::i32, Shape{3}, expected_perm));- b = context.mark_node(std::make_shared<v1::Transpose>(b, perm));- }+ // If the source graph already transposes the weights,+ // the resulting sequence of Transposes is removed+ // by the TransposeFuse pass during the frontend normalize stage.+ auto perm = context.mark_node(v0::Constant::create(element::i32, Shape{3}, {0, 2, 1}));+ b = context.mark_node(std::make_shared<v1::Transpose>(b, perm));}Output<Node> result;