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
[Snippets] Add softmax axis normalization helper (#32107)
Arseniy Obolenskiyed150d6c3e77
diff --git a/src/common/snippets/include/snippets/utils/utils.hpp b/src/common/snippets/include/snippets/utils/utils.hppindex 883d40bd9cd..442fa3c06fd 100644--- a/src/common/snippets/include/snippets/utils/utils.hpp+++ b/src/common/snippets/include/snippets/utils/utils.hpp@@ -16,6 +16,7 @@#include <limits>#include <memory>#include <numeric>+#include <optional>#include <sstream>#include <string>#include <type_traits>@@ -85,6 +86,10 @@ inline auto normalize_rank(int32_t allocation_rank, const size_t shape_rank) ->return allocation_rank < 0 ? allocation_rank + static_cast<int32_t>(shape_rank) + 1 : allocation_rank;}+// Returns the normalized Softmax axis when the node is a Softmax with a static rank.+// Returns nullopt if the node is null, has a dynamic rank, or is not a supported Softmax version.+std::optional<int64_t> get_softmax_axis(const std::shared_ptr<const ov::Node>& node);+template <typename T, typename... Args>constexpr bool any_of(T val, Args... items) {static_assert(sizeof...(Args) > 0, "'any_of' requires at least one item to compare against.");diff --git a/src/common/snippets/src/pass/collapse_subgraph.cpp b/src/common/snippets/src/pass/collapse_subgraph.cppindex 2b07787ffbb..37c553c4990 100644--- a/src/common/snippets/src/pass/collapse_subgraph.cpp+++ b/src/common/snippets/src/pass/collapse_subgraph.cpp@@ -62,7 +62,6 @@#include "openvino/op/round.hpp"#include "openvino/op/select.hpp"#include "openvino/op/sigmoid.hpp"-#include "openvino/op/softmax.hpp"#include "openvino/op/sqrt.hpp"#include "openvino/op/squared_difference.hpp"#include "openvino/op/subtract.hpp"@@ -191,21 +190,15 @@ auto is_supported_op(const std::shared_ptr<const Node>& n) -> bool {};auto is_supported_softmax = [](const std::shared_ptr<const Node>& n) -> bool {- if (n->get_input_size() != 1 || n->get_input_partial_shape(0).rank().is_dynamic()) {+ if (n->get_input_size() != 1) {return false;}- int64_t axis = -1;- const auto rank = n->get_input_partial_shape(0).rank();- if (const auto softmax_v8 = ov::as_type_ptr<const ov::op::v8::Softmax>(n)) {- if (rank.is_static()) {- axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), rank, *n);- }- } else if (const auto softmax_v1 = ov::as_type_ptr<const ov::op::v1::Softmax>(n)) {- axis = softmax_v1->get_axis();- } else {+ const auto axis = ov::snippets::utils::get_softmax_axis(n);+ if (!axis) {return false;}- return axis >= 0 && axis == (rank.get_length() - 1);+ const auto rank = static_cast<int64_t>(n->get_input_partial_shape(0).rank().get_length());+ return *axis == (rank - 1);};auto is_supported_broadcast_op = [](const std::shared_ptr<const Node>& n) -> bool {diff --git a/src/common/snippets/src/pass/mha_tokenization.cpp b/src/common/snippets/src/pass/mha_tokenization.cppindex 4254b0a1f03..452f1f674b9 100644--- a/src/common/snippets/src/pass/mha_tokenization.cpp+++ b/src/common/snippets/src/pass/mha_tokenization.cpp@@ -21,11 +21,9 @@#include "openvino/core/node_vector.hpp"#include "openvino/core/type.hpp"#include "openvino/core/type/element_type.hpp"-#include "openvino/core/validation_util.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/fake_quantize.hpp"#include "openvino/op/select.hpp"-#include "openvino/op/softmax.hpp"#include "openvino/op/util/attr_types.hpp"#include "openvino/op/util/binary_elementwise_arithmetic.hpp"#include "openvino/op/util/unary_elementwise_arithmetic.hpp"@@ -309,17 +307,9 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const Config& confireturn false;}- int64_t axis = 0;- const auto rank = interm_op->get_input_partial_shape(0).rank();- if (const auto softmax_v8 = ov::as_type_ptr<ov::op::v8::Softmax>(interm_op)) {- axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), rank, *interm_op);- } else if (const auto softmax_v1 = ov::as_type_ptr<ov::op::v1::Softmax>(interm_op)) {- axis = softmax_v1->get_axis();- } else {- return false;- }-- if (axis != rank.get_length() - 1 || interm_op->get_output_target_inputs(0).size() != 1) {+ const auto axis = ov::snippets::utils::get_softmax_axis(interm_op);+ const auto rank = static_cast<int64_t>(interm_op->get_input_partial_shape(0).rank().get_length());+ if (!axis || *axis != (rank - 1) || interm_op->get_output_target_inputs(0).size() != 1) {return false;}diff --git a/src/common/snippets/src/pass/mlp_seq_tokenization.cpp b/src/common/snippets/src/pass/mlp_seq_tokenization.cppindex e0b2f84bd49..9a0803537d9 100644--- a/src/common/snippets/src/pass/mlp_seq_tokenization.cpp+++ b/src/common/snippets/src/pass/mlp_seq_tokenization.cpp@@ -14,11 +14,9 @@#include "openvino/core/node_vector.hpp"#include "openvino/core/type.hpp"#include "openvino/core/type/element_type.hpp"-#include "openvino/core/validation_util.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/fake_quantize.hpp"#include "openvino/op/matmul.hpp"-#include "openvino/op/softmax.hpp"#include "openvino/op/util/binary_elementwise_arithmetic.hpp"#include "openvino/op/util/unary_elementwise_arithmetic.hpp"#include "openvino/opsets/opset1.hpp"@@ -62,16 +60,12 @@ bool TokenizeMLPSeqSnippets::is_matmul_supported(const std::shared_ptr<ov::Node>}bool TokenizeMLPSeqSnippets::is_supported_softmax(const std::shared_ptr<ov::Node>& node) {- int64_t axis = 0;- const auto rank = node->get_input_partial_shape(0).rank();- if (const auto softmax_v8 = ov::as_type_ptr<ov::op::v8::Softmax>(node)) {- axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), rank, *node);- } else if (const auto softmax_v1 = ov::as_type_ptr<ov::op::v1::Softmax>(node)) {- axis = softmax_v1->get_axis();- } else {+ const auto axis = ov::snippets::utils::get_softmax_axis(node);+ if (!axis) {return false;}- return is_tensor_supported(node->get_input_tensor(0)) && axis == (rank.get_length() - 1);+ const auto rank = static_cast<int64_t>(node->get_input_partial_shape(0).rank().get_length());+ return is_tensor_supported(node->get_input_tensor(0)) && *axis == (rank - 1);}bool TokenizeMLPSeqSnippets::is_supported_intermediate_op(const std::shared_ptr<ov::Node>& node) {diff --git a/src/common/snippets/src/pass/softmax_decomposition.cpp b/src/common/snippets/src/pass/softmax_decomposition.cppindex 7bf5940b254..69dbe05b271 100644--- a/src/common/snippets/src/pass/softmax_decomposition.cpp+++ b/src/common/snippets/src/pass/softmax_decomposition.cpp@@ -12,8 +12,6 @@#include "openvino/core/except.hpp"#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"-#include "openvino/core/type.hpp"-#include "openvino/core/validation_util.hpp"#include "openvino/op/exp.hpp"#include "openvino/op/multiply.hpp"#include "openvino/op/softmax.hpp"@@ -45,29 +43,26 @@ SoftmaxDecomposition::SoftmaxDecomposition() {OPENVINO_ASSERT(!pshape.rank().is_dynamic(), "SoftmaxDecomposition doesn't support dynamic ranks");const auto rank = pshape.size();- size_t axis = 0;- if (const auto softmax_v8 = ov::as_type_ptr<ov::op::v8::Softmax>(softmax)) {- axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), rank, *softmax);- } else if (const auto softmax_v1 = ov::as_type_ptr<ov::op::v1::Softmax>(softmax)) {- axis = softmax_v1->get_axis();- } else {+ const auto axis = ov::snippets::utils::get_softmax_axis(softmax);+ if (!axis) {OPENVINO_THROW("Unexpected node matched");}+ const auto normalized_axis = static_cast<size_t>(*axis);const auto& softmax_input = softmax->input_value(0);- const auto reduce_max = std::make_shared<ov::snippets::op::ReduceMax>(softmax_input, axis);+ const auto reduce_max = std::make_shared<ov::snippets::op::ReduceMax>(softmax_input, normalized_axis);ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_max);const auto subtract = std::make_shared<ov::op::v1::Subtract>(softmax_input, reduce_max);const auto exp = std::make_shared<ov::op::v0::Exp>(subtract);- const auto reduce_sum = std::make_shared<ov::snippets::op::ReduceSum>(exp, axis);+ const auto reduce_sum = std::make_shared<ov::snippets::op::ReduceSum>(exp, normalized_axis);ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_sum);const auto power = std::make_shared<ov::snippets::op::PowerStatic>(reduce_sum, -1.F);const auto multiply = std::make_shared<ov::op::v1::Multiply>(exp, power);- OPENVINO_ASSERT(axis < rank, "Softmax has incorrect axis");+ OPENVINO_ASSERT(normalized_axis < rank, "Softmax has incorrect axis");std::vector<size_t> subtensor(rank, 1);- for (size_t i = axis; i < rank; ++i) {+ for (size_t i = normalized_axis; i < rank; ++i) {subtensor[i] = utils::get_full_dim_value();}diff --git a/src/common/snippets/src/pass/softmax_reshape_elimination.cpp b/src/common/snippets/src/pass/softmax_reshape_elimination.cppindex 5b79879c5df..1627de451d7 100644--- a/src/common/snippets/src/pass/softmax_reshape_elimination.cpp+++ b/src/common/snippets/src/pass/softmax_reshape_elimination.cpp@@ -10,7 +10,6 @@#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"#include "openvino/core/type.hpp"-#include "openvino/core/validation_util.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/softmax.hpp"@@ -19,6 +18,7 @@#include "openvino/pass/pattern/op/pattern.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "snippets/itt.hpp"+#include "snippets/utils/utils.hpp"ov::snippets::pass::SoftmaxReshapeElimination::SoftmaxReshapeElimination() {MATCHER_SCOPE(SoftmaxReshapeElimination);@@ -48,17 +48,9 @@ ov::snippets::pass::SoftmaxReshapeElimination::SoftmaxReshapeElimination() {}const auto softmax_rank = softmax_shape.rank();- int64_t axis = 0;- if (const auto softmax_v8 = ov::as_type_ptr<const ov::op::v8::Softmax>(softmax)) {- axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), softmax_rank, *softmax);- } else if (const auto softmax_v1 = ov::as_type_ptr<const ov::op::v1::Softmax>(softmax)) {- axis = softmax_v1->get_axis();- } else {- return false;- }-+ const auto axis = ov::snippets::utils::get_softmax_axis(softmax);// Supports only last axis- if (axis != softmax_rank.get_length() - 1) {+ if (!axis || *axis != static_cast<int64_t>(softmax_rank.get_length()) - 1) {return false;}diff --git a/src/common/snippets/src/pass/validate.cpp b/src/common/snippets/src/pass/validate.cppindex 5cd286f97b3..79fafb93b50 100644--- a/src/common/snippets/src/pass/validate.cpp+++ b/src/common/snippets/src/pass/validate.cpp@@ -15,7 +15,6 @@#include "openvino/core/node_input.hpp"#include "openvino/core/shape.hpp"#include "openvino/core/type.hpp"-#include "openvino/core/validation_util.hpp"#include "openvino/op/broadcast.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"@@ -69,16 +68,12 @@ bool Validate::is_supported_matmul(const std::shared_ptr<const ov::Node>& op) {bool Validate::is_supported_softmax(const std::shared_ptr<const ov::Node>& op) {// Softmax is supported only with axis by last dim- const auto softmax_rank = op->get_input_partial_shape(0).rank();- int64_t axis = 0;- if (const auto softmax_v8 = ov::as_type_ptr<const ov::op::v8::Softmax>(op)) {- axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), softmax_rank, *softmax_v8);- } else if (const auto softmax_v1 = ov::as_type_ptr<const ov::op::v1::Softmax>(op)) {- axis = softmax_v1->get_axis();- } else {+ const auto axis = utils::get_softmax_axis(op);+ if (!axis) {return false;}- return axis == softmax_rank.get_length() - 1;+ const auto softmax_rank = static_cast<int64_t>(op->get_input_partial_shape(0).rank().get_length());+ return *axis == (softmax_rank - 1);}bool Validate::is_supported_fq([[maybe_unused]] const std::shared_ptr<const ov::Node>& node) {diff --git a/src/common/snippets/src/utils/utils.cpp b/src/common/snippets/src/utils/utils.cppindex 7594fb2213b..c946e1ed7b6 100644--- a/src/common/snippets/src/utils/utils.cpp+++ b/src/common/snippets/src/utils/utils.cpp@@ -11,6 +11,7 @@#include <functional>#include <memory>#include <numeric>+#include <optional>#include <set>#include <sstream>#include <string>@@ -25,7 +26,9 @@#include "openvino/core/partial_shape.hpp"#include "openvino/core/shape.hpp"#include "openvino/core/type.hpp"+#include "openvino/core/validation_util.hpp"#include "openvino/op/fake_quantize.hpp"+#include "openvino/op/softmax.hpp"#include "snippets/lowered/expression.hpp"#include "snippets/lowered/expression_port.hpp"#include "snippets/lowered/port_descriptor.hpp"@@ -150,6 +153,27 @@ auto get_non_scalar_constant_count_for_fq(const std::shared_ptr<ov::op::v0::Fakereturn 0;}+std::optional<int64_t> get_softmax_axis(const std::shared_ptr<const ov::Node>& node) {+ if (!node) {+ return {};+ }+ const auto rank = node->get_input_partial_shape(0).rank();+ if (rank.is_dynamic()) {+ return {};+ }+ if (const auto softmax_v8 = ov::as_type_ptr<const ov::op::v8::Softmax>(node)) {+ return static_cast<int64_t>(ov::util::try_normalize_axis(softmax_v8->get_axis(), rank, *softmax_v8));+ }+ if (const auto softmax_v1 = ov::as_type_ptr<const ov::op::v1::Softmax>(node)) {+ const auto axis = softmax_v1->get_axis();+ if (static_cast<int64_t>(axis) >= rank.get_length()) {+ return {};+ }+ return static_cast<int64_t>(axis);+ }+ return {};+}+bool broadcast_merge_dim(size_t& dst, const size_t& d1, const size_t& d2) {if (d1 == d2 || d1 == 1 || (is_dynamic_value(d1) && d2 != 1)) {dst = d2;