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
[GGUF] Keep MatMul boundaries in SimplifyActivationShapes (#39653)
Maxim Vafinccb7ccc4022f
diff --git a/src/frontends/gguf/src/pass/simplify_activation_shapes.cpp b/src/frontends/gguf/src/pass/simplify_activation_shapes.cppindex 2acfa9fa4ca..07658396a2c 100644--- a/src/frontends/gguf/src/pass/simplify_activation_shapes.cpp+++ b/src/frontends/gguf/src/pass/simplify_activation_shapes.cpp@@ -5,6 +5,7 @@#include "pass/simplify_activation_shapes.hpp"#include <algorithm>+#include <map>#include <unordered_set>#include "openvino/core/graph_util.hpp"@@ -12,6 +13,7 @@#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/gather.hpp"+#include "openvino/op/matmul.hpp"#include "openvino/op/normalize_l2.hpp"#include "openvino/op/reduce_mean.hpp"#include "openvino/op/reduce_sum.hpp"@@ -56,6 +58,22 @@ ov::Output<ov::Node> unwrapped(const ov::Output<ov::Node>& value) {return value;}+// MatMul activation boundaries keep their original rank: plugins pick FullyConnected kernels+// (e.g. GPU dynamic quantization) and fuse residual eltwise ops based on that layout.+bool feeds_matmul(const ov::Node& node) {+ const auto targets = node.output(0).get_target_inputs();+ return std::any_of(targets.begin(), targets.end(), [](const ov::Input<ov::Node>& target) {+ return ov::is_type<ov::op::v0::MatMul>(target.get_node());+ });+}++bool consumes_matmul(const ov::Node& node) {+ const auto inputs = node.input_values();+ return std::any_of(inputs.begin(), inputs.end(), [](const ov::Output<ov::Node>& input) {+ return ov::is_type<ov::op::v0::MatMul>(input.get_node()) && activation_shape(input.get_partial_shape());+ });+}+} // namespacebool SimplifyActivationShapes::run_on_model(const std::shared_ptr<ov::Model>& model) {@@ -65,6 +83,7 @@ bool SimplifyActivationShapes::run_on_model(const std::shared_ptr<ov::Model>& mosymbols.run_passes(model);const auto nodes = model->get_ordered_ops();std::unordered_set<const ov::Node*> constants;+ std::map<ov::Output<ov::Node>, ov::Output<ov::Node>> squeezed;for (const auto& node : nodes) {const auto inputs = node->inputs();if (ov::is_type<ov::op::v0::Constant>(node) ||@@ -83,6 +102,8 @@ bool SimplifyActivationShapes::run_on_model(const std::shared_ptr<ov::Model>& mo}const auto shape = node->get_output_partial_shape(0);const auto reshape = ov::as_type_ptr<ov::op::v1::Reshape>(node);+ if ((reshape && feeds_matmul(*node)) || consumes_matmul(*node))+ continue;if (reshape && shape.rank() == 3) {const auto value = unwrapped(reshape->input_value(0));if (same_shape(value.get_partial_shape(), shape)) {@@ -100,9 +121,13 @@ bool SimplifyActivationShapes::run_on_model(const std::shared_ptr<ov::Model>& moconst auto value = unwrapped(input);if (value != input)return value;- const auto squeezed = ov::op::util::make_try_fold<ov::op::v0::Squeeze>(input, axis);- created.push_back(squeezed);- return squeezed;+ const auto cached = squeezed.find(input);+ if (cached != squeezed.end())+ return cached->second;+ const auto squeeze = ov::op::util::make_try_fold<ov::op::v0::Squeeze>(input, axis);+ created.push_back(squeeze);+ squeezed.emplace(input, squeeze);+ return squeeze;};if (reshape) {const auto value = unwrapped(reshape->input_value(0));diff --git a/src/frontends/gguf/tests/test_simplify_activation_shapes.cpp b/src/frontends/gguf/tests/test_simplify_activation_shapes.cppindex 26bd5c4716a..67ab35c18d5 100644--- a/src/frontends/gguf/tests/test_simplify_activation_shapes.cpp+++ b/src/frontends/gguf/tests/test_simplify_activation_shapes.cpp@@ -5,11 +5,13 @@#include "common_test_utils/ov_test_utils.hpp"#include "openvino/op/add.hpp"#include "openvino/op/constant.hpp"+#include "openvino/op/exp.hpp"#include "openvino/op/gather.hpp"#include "openvino/op/matmul.hpp"#include "openvino/op/parameter.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/shape_of.hpp"+#include "openvino/op/squeeze.hpp"#include "openvino/op/swish.hpp"#include "openvino/op/unsqueeze.hpp"#include "openvino/op/variadic_split.hpp"@@ -52,6 +54,44 @@ TEST_F(GGUFActivationShapes, KeepsResidualPathAtRankThree) {model_ref = residual_model(true);}+TEST_F(GGUFActivationShapes, KeepsReshapeFeedingMatMul) {+ const auto x = std::make_shared<v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 8});+ const auto target = v0::Constant::create(ov::element::i64, {4}, {2, 1, -1, 8});+ const auto reshape = std::make_shared<v1::Reshape>(std::make_shared<v4::Swish>(x), target, false);+ const auto w = v0::Constant::create(ov::element::f32, {4, 8}, {0.25f});+ const auto fc = std::make_shared<v0::MatMul>(reshape, w, false, true);+ model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{x});+}++std::shared_ptr<ov::Model> get_matmul_output_consumers_model() {+ const auto x = std::make_shared<v0::Parameter>(ov::element::f32, ov::PartialShape{2, 1, -1, 8});+ const auto w = v0::Constant::create(ov::element::f32, {8, 8}, {0.25f});+ const auto fc = std::make_shared<v0::MatMul>(x, w, false, true);+ const auto add = std::make_shared<v1::Add>(fc, x);+ return std::make_shared<ov::Model>(+ ov::OutputVector{std::make_shared<v4::Swish>(add), std::make_shared<v0::Exp>(add)},+ ov::ParameterVector{x});+}++std::shared_ptr<ov::Model> get_matmul_output_consumers_model_ref() {+ const auto x = std::make_shared<v0::Parameter>(ov::element::f32, ov::PartialShape{2, 1, -1, 8});+ const auto w = v0::Constant::create(ov::element::f32, {8, 8}, {0.25f});+ const auto fc = std::make_shared<v0::MatMul>(x, w, false, true);+ const auto add = std::make_shared<v1::Add>(fc, x);+ const auto axis = v0::Constant::create(ov::element::i64, {1}, {1});+ const auto squeezed = std::make_shared<v0::Squeeze>(add, axis);+ return std::make_shared<ov::Model>(+ ov::OutputVector{std::make_shared<v0::Unsqueeze>(std::make_shared<v4::Swish>(squeezed), axis),+ std::make_shared<v0::Unsqueeze>(std::make_shared<v0::Exp>(squeezed), axis)},+ ov::ParameterVector{x});+}++TEST_F(GGUFActivationShapes, KeepsMatMulOutputConsumersAtRankFour) {+ model = get_matmul_output_consumers_model();+ model_ref = get_matmul_output_consumers_model_ref();+ comparator.enable(FunctionsComparator::CONSUMERS_COUNT);+}+TEST_F(GGUFActivationShapes, PreservesOtherOutputsOfReshapeProducer) {const auto make_model = [](bool compact) {const auto x = std::make_shared<v0::Parameter>(ov::element::f32, ov::Shape{1, 5, 8});