Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ONNX] Add support for ONNX STFT with 2D signal input - use ov::STFT instead of decomposition (#34818)

Katarzyna Mitrus0a9686ff089d
diff --git a/src/frontends/onnx/frontend/src/op/stft.cpp b/src/frontends/onnx/frontend/src/op/stft.cpp
index 2e3977835db..6f92bc8f247 100644
--- a/src/frontends/onnx/frontend/src/op/stft.cpp
+++ b/src/frontends/onnx/frontend/src/op/stft.cpp
@@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "openvino/op/stft.hpp"
+
#include "core/null_node.hpp"
#include "core/operator_set.hpp"
#include "exceptions.hpp"
@@ -26,10 +28,32 @@ namespace ai_onnx {
namespace opset_17 {
ov::OutputVector stft(const ov::frontend::onnx::Node& node) {
+ common::default_op_checks(node, 2, 4);
+
const ov::OutputVector ov_inputs{node.get_ov_inputs()};
auto signal = ov_inputs.at(0);
+ const auto signal_param_shape = signal.get_partial_shape();
+ const auto window_node_provided = ov_inputs.size() > 2 && !ov::op::util::is_null(ov_inputs[2]);
const auto dft_length_provided = ov_inputs.size() > 3 && !ov::op::util::is_null(ov_inputs[3]);
const auto onesided = node.get_attribute_value<int64_t>("onesided", 1);
+
+ // Use ov::op::v15::STFT for 2D signal - all inputs are required, output is onesided
+ // Even if 2D input is not described in onnx spec, it is allowed by onnx model checker,
+ // 2D input can be seen in real models and is supported by frameworks (including OpenVINO STFT)
+ // The 2D shape is compatible with 3D case [batch, signal_length, 1] -> [batch, signal_length]
+ const auto is_2d_signal = signal_param_shape.rank().is_static() && signal_param_shape.size() == 2;
+ if (is_2d_signal && onesided && window_node_provided && dft_length_provided) {
+ // ONNX STFT inputs: signal, frame_step, window (optional), frame_length (optional)
+ // ONNX STFT output shape: [batch_size, num_frames, fft_length, 2]
+ // OpenVINO v15::STFT inputs: data, window, frame_size, frame_step
+ // OpenVINO v15::STFT output shape: [batch_size, num_frames, fft_length, 2] (transpose_frames=false)
+ constexpr bool transpose_frames = false;
+ return {std::make_shared<v15::STFT>(signal, ov_inputs[2], ov_inputs[3], ov_inputs[1], transpose_frames)};
+ }
+ // Use DFT-based decomposition for 3D signal or optional inputs
+ CHECK_VALID_NODE(node,
+ signal_param_shape.is_static() && signal_param_shape.size() == 3,
+ "Shape of signal input must be static with the rank equal to 3.");
const int64_t axis = 1;
const auto& frame_step_node = ov_inputs.at(1);
@@ -43,10 +67,6 @@ ov::OutputVector stft(const ov::frontend::onnx::Node& node) {
frame_step > 0,
"Provided frame_step input value must be greater than zero. Got: ",
frame_step);
- const auto signal_param_shape = signal.get_partial_shape();
- CHECK_VALID_NODE(node,
- signal_param_shape.is_static() && signal_param_shape.size() == 3,
- "Shape of signal input must be static with the rank equal to 3.");
int64_t frame_length = signal_param_shape[axis].get_length() / frame_step; // default value
if (dft_length_provided) {
@@ -59,7 +79,6 @@ ov::OutputVector stft(const ov::frontend::onnx::Node& node) {
ov::as_type_ptr<v0::Constant>(frame_length_node.get_node_shared_ptr())->cast_vector<int64_t>()[0];
}
- const auto window_node_provided = ov_inputs.size() > 2 && !ov::op::util::is_null(ov_inputs[2]);
if (window_node_provided) { // window input provided
if (ov_inputs[2].get_partial_shape().rank().is_static()) {
CHECK_VALID_NODE(node,
diff --git a/src/frontends/onnx/tests/models/stft_2D_onesided_real_input_given_window_no_default_length.prototxt b/src/frontends/onnx/tests/models/stft_2D_onesided_real_input_given_window_no_default_length.prototxt
new file mode 100644
index 00000000000..ba2c2970db4
--- /dev/null
+++ b/src/frontends/onnx/tests/models/stft_2D_onesided_real_input_given_window_no_default_length.prototxt
@@ -0,0 +1,97 @@
+ir_version: 8
+graph {
+ node {
+ output: "frame_step"
+ name: "frame_step_const"
+ op_type: "Constant"
+ attribute {
+ name: "value"
+ t {
+ dims: 0
+ data_type: 7
+ int64_data: 8
+ name: "frame_step_tensor"
+ }
+ type: TENSOR
+ }
+ }
+ node {
+ output: "frame_length"
+ name: "frame_length_const"
+ op_type: "Constant"
+ attribute {
+ name: "value"
+ t {
+ dims: 0
+ data_type: 7
+ int64_data: 16
+ name: "frame_length_tensor"
+ }
+ type: TENSOR
+ }
+ }
+ node {
+ input: "signal"
+ input: "frame_step"
+ input: "window"
+ input: "frame_length"
+ output: "Y"
+ op_type: "STFT"
+ attribute {
+ name: "onesided"
+ i: 1
+ type: INT
+ }
+ }
+ input {
+ name: "signal"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 32
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "window"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 16
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ }
+ dim {
+ }
+ dim {
+ }
+ dim {
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 17
+}
diff --git a/src/frontends/onnx/tests/onnx_import_signal.in.cpp b/src/frontends/onnx/tests/onnx_import_signal.in.cpp
index e803e7a54b6..a3f30798bb5 100644
--- a/src/frontends/onnx/tests/onnx_import_signal.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_signal.in.cpp
@@ -1493,6 +1493,48 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_stft_onesided_real_input_given_window_
-7.999382f, 1.6240807f, -7.99942f, 0.0f});
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_stft_2D_onesided_real_input_given_window_no_default_length) {
+ auto model = convert_model("stft_2D_onesided_real_input_given_window_no_default_length.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+ const Shape signal_shape{2, 32};
+ std::vector<float> signal(ov::shape_size(signal_shape));
+ std::iota(std::begin(signal), std::end(signal), 0.f);
+ test_case.add_input<float>(signal_shape, signal);
+ test_case.add_input<float>(Shape{16},
+ {1.0f,
+ 0.96194196f,
+ 0.8535616f,
+ 0.6913578f,
+ 0.5000232f,
+ 0.308685f,
+ 0.1464712f,
+ 0.038075745f,
+ 0.0f,
+ 0.03804031f,
+ 0.1464057f,
+ 0.3085994f,
+ 0.49993038f,
+ 0.6912722f,
+ 0.853496f,
+ 0.96190655f});
+ test_case.add_expected_output<float>(
+ Shape{2, 3, 9, 2},
+ {55.996273f, 0.0f, 23.999104f, 24.93398f, -7.99869f, 22.70421f, -7.999475f, 12.814739f,
+ -7.999694f, 8.329526f, -7.999781f, 5.5011215f, -7.999824f, 3.3910275f, -7.9998474f, 1.6240749f,
+ -7.999859f, 0.0f, 119.99441f, 0.0f, 55.998657f, 24.931015f, -7.9980354f, 22.704208f,
+ -7.9992137f, 12.814741f, -7.999542f, 8.329527f, -7.9996705f, 5.5011196f, -7.999737f, 3.3910284f,
+ -7.999769f, 1.6240768f, -7.9997787f, 0.0f, 183.99254f, 0.0f, 87.998215f, 24.928047f,
+ -7.9973803f, 22.70421f, -7.9989514f, 12.814744f, -7.9993896f, 8.329525f, -7.999561f, 5.5011206f,
+ -7.999648f, 3.3910275f, -7.9996986f, 1.6240759f, -7.99971f, 0.0f, 311.98883f, 0.0f,
+ 151.99731f, 24.922113f, -7.9960704f, 22.704214f, -7.998431f, 12.814743f, -7.9990845f, 8.329521f,
+ -7.999341f, 5.5011144f, -7.999474f, 3.3910275f, -7.999542f, 1.6240788f, -7.9995728f, 0.0f,
+ 375.98694f, 0.0f, 183.99686f, 24.919151f, -7.9954133f, 22.704203f, -7.9981623f, 12.814751f,
+ -7.998932f, 8.329529f, -7.9992285f, 5.501122f, -7.9993834f, 3.3910313f, -7.9994736f, 1.6240854f,
+ -7.9994965f, 0.0f, 439.98508f, 0.0f, 215.9964f, 24.916183f, -7.99476f, 22.704206f,
+ -7.9979024f, 12.814749f, -7.9987793f, 8.329529f, -7.999122f, 5.5011153f, -7.9992967f, 3.391035f,
+ -7.999382f, 1.6240807f, -7.99942f, 0.0f});
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_stft_proper_exception_if_non_const_frame_step) {
try {
convert_model("stft_non_const_frame_step.onnx");