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
[ONNX] Added RotaryEmbedding from com.microsoft domain (#30988)
bopeng1234c6dea091c0bf
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.mdindex 41ca6785528..d8cff1bc8e9 100644--- a/src/frontends/onnx/docs/supported_ops.md+++ b/src/frontends/onnx/docs/supported_ops.md@@ -298,7 +298,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)|com.microsoft |RemovePadding | |1 | ||com.microsoft |RestorePadding | |1 | ||com.microsoft |Rfft | |1 | |-|com.microsoft |RotaryEmbedding | |1 | |+|com.microsoft |RotaryEmbedding |1 |1 | ||com.microsoft |SampleOp | |1 | ||com.microsoft |Sampling | |1 | ||com.microsoft |SimplifiedLayerNormalization |1 |1 | |diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cppnew file mode 100644index 00000000000..d82ef4dd7fe--- /dev/null+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cpp@@ -0,0 +1,143 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "cmath"+#include "core/operator_set.hpp"+#include "exceptions.hpp"+#include "openvino/core/graph_util.hpp"+#include "openvino/core/rt_info.hpp"+#include "openvino/frontend/exception.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/broadcast.hpp"+#include "openvino/op/concat.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/gather.hpp"+#include "openvino/op/greater.hpp"+#include "openvino/op/greater_eq.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/range.hpp"+#include "openvino/op/reshape.hpp"+#include "openvino/op/scaled_dot_product_attention.hpp"+#include "openvino/op/select.hpp"+#include "openvino/op/shape_of.hpp"+#include "openvino/op/slice.hpp"+#include "openvino/op/split.hpp"+#include "openvino/op/squeeze.hpp"+#include "openvino/op/subtract.hpp"+#include "openvino/op/transpose.hpp"+#include "openvino/op/unsqueeze.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"+#include "utils/common.hpp"+#include "utils/reshape.hpp"++using namespace ov::op;++namespace ov {+namespace frontend {+namespace onnx {+namespace com_microsoft {+namespace opset_1 {++ov::OutputVector make_split(const ov::Output<ov::Node>& value, int64_t num_splits, int64_t axis) {+ using namespace ov::op;+ const auto axis_node = v0::Constant::create(ov::element::i64, ov::Shape{}, {axis});+ const auto split = std::make_shared<v1::Split>(value, axis_node, num_splits);++ return split->outputs();+}++std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape, const std::vector<int>& dims) {+ static const auto zero = v0::Constant::create(ov::element::i32, ov::Shape{}, {0});+ const auto dims_const = v0::Constant::create(ov::element::i32, ov::Shape{dims.size()}, dims);+ return std::make_shared<v8::Gather>(shape, dims_const, zero);+}++ov::OutputVector rotary_embedding(const ov::frontend::onnx::Node& node) {+ common::default_op_checks(node, 4);+ // Original documentation:+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.RotaryEmbedding+ const auto inputs = node.get_ov_inputs();+ const auto& input = inputs[0]; // [bs,seqlen,hidden] or [bs,num_heads,seqlen,headsize]+ const auto& position_ids = inputs[1]; // [seqlen] or [bs, seqlen]+ const auto& cos_cache = inputs[2]; // [max_seqlen, head_size/2]+ const auto& sin_cache = inputs[3]; // [max_seqlen, head_size/2]++ const auto interleaved = node.get_attribute_value<int64_t>("interleaved"); // required+ const auto minus_one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});+ const auto zero = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});+ const auto one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});+ const auto two = v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});++ const auto cos = std::make_shared<v8::Gather>(cos_cache,+ position_ids,+ zero); // [seqlen, head_size/2] or [bs, seqlen, head_size/2]+ const auto sin = std::make_shared<v8::Gather>(sin_cache,+ position_ids,+ zero); // [seqlen, head_size/2] or [bs, seqlen, head_size/2]++ const auto input_shape = std::make_shared<v3::ShapeOf>(input);+ const bool input_is_3d = input.get_partial_shape().rank().get_length() == 3;+ const auto perm = v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});++ ov::Output<ov::Node> input_4d = input;+ if (input_is_3d) {+ const auto headsize = v0::Constant::create(ov::element::i64, ov::Shape{1}, {cos_cache.get_shape()[-1] * 2});+ const auto input_shape_prev_2 = get_dimensions(input_shape, {0, 1});+ auto new_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{input_shape_prev_2, minus_one, headsize}, 0);+ auto input_reshaped =+ std::make_shared<v1::Reshape>(input, new_input_shape, false); // [bs,seqlen,num_heads,headsize]+ input_4d = std::make_shared<v1::Transpose>(input_reshaped, perm); // [bs,num_heads,seqlen,headsize]+ }++ ov::Output<ov::Node> output;+ if (interleaved) {+ auto input_4d_shape = std::make_shared<v3::ShapeOf>(input_4d);+ auto dim_bns = get_dimensions(input_4d_shape, {0, 1, 2});+ auto half_head_size = v0::Constant::create(ov::element::i64, ov::Shape{1}, {cos_cache.get_shape()[-1]});+ auto split_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size, two}, 0);+ auto reshaped_input = std::make_shared<v1::Reshape>(input_4d, split_input_shape, false);++ auto in_split = make_split(reshaped_input, 2, -1);+ split_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size}, 0);+ auto in_split_0 = std::make_shared<v1::Reshape>(in_split[0], split_input_shape, false);+ auto in_split_1 = std::make_shared<v1::Reshape>(in_split[1], split_input_shape, false);++ auto res_0 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(in_split_0, cos),+ std::make_shared<v1::Multiply>(in_split_1, sin));+ auto res_1 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(in_split_0, sin),+ std::make_shared<v1::Multiply>(in_split_1, cos));++ split_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size, one}, 0);+ auto res_0_5d = std::make_shared<v1::Reshape>(res_0, split_input_shape, false);+ auto res_1_5d = std::make_shared<v1::Reshape>(res_1, split_input_shape, false);++ auto concat_ret = std::make_shared<v0::Concat>(ov::NodeVector{res_0_5d, res_1_5d}, -1);+ output = std::make_shared<v1::Reshape>(concat_ret, input_4d_shape,+ false); // [bs,num_heads,seqlen,headsize]+ } else {+ auto in_split = make_split(input_4d, 2, -1); // [bs,num_heads,seqlen,headsize/2]+ auto res_0 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(in_split[0], cos),+ std::make_shared<v1::Multiply>(in_split[1], sin));+ auto res_1 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(in_split[0], sin),+ std::make_shared<v1::Multiply>(in_split[1], cos));+ output = std::make_shared<v0::Concat>(ov::NodeVector{res_0, res_1}, -1); // [bs,num_heads,seqlen,headsize]+ }++ if (input_is_3d) {+ output = std::make_shared<v1::Transpose>(output, perm); // [bs,seqlen,num_heads,headsize]+ output = std::make_shared<v1::Reshape>(output, input_shape,+ false); // [bs,seqlen,hidden]+ }++ return {output};+}++ONNX_OP("RotaryEmbedding", OPSET_SINCE(1), com_microsoft::opset_1::rotary_embedding, MICROSOFT_DOMAIN);++} // namespace opset_1+} // namespace com_microsoft+} // namespace onnx+} // namespace frontend+} // namespace ov\ No newline at end of filediff --git a/src/frontends/onnx/tests/models/com.microsoft/rotary_embedding.prototxt b/src/frontends/onnx/tests/models/com.microsoft/rotary_embedding.prototxtnew file mode 100644index 00000000000..8fd275810ad--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/rotary_embedding.prototxt@@ -0,0 +1,98 @@+ir_version: 1+graph {+ node {+ input: "input"+ input: "position_ids"+ input: "cos_cache"+ input: "sin_cache"+ output: "output"+ name: "RotaryEmbedding_0"+ op_type: "RotaryEmbedding"+ attribute {+ name: "interleaved"+ i: 0+ type: INT+ }+ attribute {+ name: "num_heads"+ i: 0+ type: INT+ }+ attribute {+ name: "rotary_embedding_dim"+ i: 0+ type: INT+ }+ domain: "com.microsoft"+ }+ name: "RotaryEmbedding_Graph"+ input {+ name: "input"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_value: 1 }+ dim { dim_value: 2 }+ dim { dim_value: 16 }+ }+ }+ }+ }+ input {+ name: "position_ids"+ type {+ tensor_type {+ elem_type: 7+ shape {+ dim { dim_value: 1 }+ dim { dim_value: 2 }+ }+ }+ }+ }+ input {+ name: "cos_cache"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_value: 2 }+ dim { dim_value: 4 }+ }+ }+ }+ }+ input {+ name: "sin_cache"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_value: 2 }+ dim { dim_value: 4 }+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_value: 1 }+ dim { dim_value: 2 }+ dim { dim_value: 16 }+ }+ }+ }+ }+}+opset_import {+ version: 11+}+opset_import {+ domain: "com.microsoft"+ version: 1+}\ No newline at end of filediff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cppindex e4f8bbfebc0..9e722d1d4a8 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -1805,6 +1805,50 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_bias_add) {test_case.run();}+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_rotary_embedding) {+ const auto model = convert_model("com.microsoft/rotary_embedding.onnx");++ std::vector<float> input = {+ -1.1258f, -1.1524f, -0.2506f, -0.4339f, 0.8487f, 0.6920f, -0.3160f, -2.1152f, 0.3223f, -1.2633f, 0.3500f,+ 0.3081f, 0.1198f, 1.2377f, 1.1168f, -0.2473f, -1.3527f, -1.6959f, 0.5667f, 0.7935f, 0.5988f, -1.5551f,+ -0.3414f, 1.8530f, 0.7502f, -0.5855f, -0.1734f, 0.1835f, 1.3894f, 1.5863f, 0.9463f, -0.8437f,+ };+ std::vector<int64_t> position_ids = {0, 1};+ std::vector<float> cos_cache = {+ 0.8437f,+ -0.7849f,+ -0.7829f,+ 0.4581f,+ -0.9870f,+ 0.6273f,+ -0.9483f,+ -0.9962f,+ };+ std::vector<float> sin_cache = {+ 0.5368f,+ 0.6196f,+ -0.6222f,+ 0.8889f,+ 0.1605f,+ -0.7788f,+ 0.3174f,+ -0.0872f,+ };++ std::vector<float> expected_output = {-1.4054f, 0.4758f, -0.0004f, 1.6814f, 0.1117f, -1.2572f, 0.4033f, -1.3547f,+ 0.2076f, 0.2247f, 0.4209f, 0.361f, 0.2741f, -1.7542f, -1.0921f, 0.1606f,+ 1.239f, -2.275f, -0.429f, -0.6289f, -0.8081f, 0.3453f, 0.5036f, -1.9152f,+ -0.9634f, 0.8681f, -0.1359f, -0.2564f, -1.2509f, 1.4511f, -0.9524f, 0.8245f};++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>(Shape{1, 2, 16}, input);+ test_case.add_input<int64_t>(Shape{1, 2}, position_ids);+ test_case.add_input<float>(Shape{2, 4}, cos_cache);+ test_case.add_input<float>(Shape{2, 4}, sin_cache);+ test_case.add_expected_output<float>(Shape{1, 2, 16}, expected_output);+ test_case.run_with_tolerance_as_fp(0.01f);+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_past_0_input_1_rotary) {const auto model = convert_model("com.microsoft/gqa_rotary.onnx");