Skip to content

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

[Op][PT FE] Enable ISTFT for Pytorch Frontend (#28743)

Katarzyna Mitrusd6147c2f95e5
diff --git a/src/core/reference/src/op/istft.cpp b/src/core/reference/src/op/istft.cpp
index 254c7216396..fe4eaf0bf93 100644
--- a/src/core/reference/src/op/istft.cpp
+++ b/src/core/reference/src/op/istft.cpp
@@ -33,7 +33,8 @@ void istft(const float* in_data,
const auto num_frames = data_shape[frames_axis];
const auto signal_length = (num_frames - 1) * frame_step + frame_size;
- const int64_t final_signal_length = length > 0 ? length : (center ? (signal_length - frame_size) : signal_length);
+ const int64_t final_signal_length =
+ length > 0 ? length : (center ? (signal_length - (frame_size & ~1)) : signal_length);
std::fill(final_result, final_result + batch_size * final_signal_length, 0.f);
std::vector<float> mid_result(batch_size * signal_length, 0.f);
diff --git a/src/core/shape_inference/include/istft_shape_inference.hpp b/src/core/shape_inference/include/istft_shape_inference.hpp
index 39a3c3cff9b..327cc8c9322 100644
--- a/src/core/shape_inference/include/istft_shape_inference.hpp
+++ b/src/core/shape_inference/include/istft_shape_inference.hpp
@@ -111,9 +111,9 @@ std::vector<TRShape> shape_infer(const ISTFT* op,
const int64_t frames_axis = 1 + (is_data_3D ? 0 : 1);
const TDim& num_frames_dim = data_shape[frames_axis];
- TDim signal_length = (num_frames_dim - 1) * frame_step_val;
- if (!op->get_center()) {
- signal_length += frame_size_val;
+ TDim signal_length = (num_frames_dim - 1) * frame_step_val + frame_size_val;
+ if (op->get_center()) {
+ signal_length = signal_length - (frame_size_val & ~1);
}
output_shapes[0][0] = std::move(signal_length);
}
diff --git a/src/core/tests/type_prop/istft.cpp b/src/core/tests/type_prop/istft.cpp
index 6516aef42ba..ed9fcfe2861 100644
--- a/src/core/tests/type_prop/istft.cpp
+++ b/src/core/tests/type_prop/istft.cpp
@@ -135,7 +135,7 @@ INSTANTIATE_TEST_SUITE_P(
std::make_tuple(PartialShape{1, 48}, PartialShape{16}, 16, 16, false, PartialShape{1, 9, 3, 2}),
std::make_tuple(PartialShape{2, 48}, PartialShape{8}, 16, 4, false, PartialShape{2, 9, 9, 2}),
std::make_tuple(PartialShape{2, 9}, PartialShape{5}, 9, 100, false, PartialShape{2, 5, 1, 2}),
- std::make_tuple(PartialShape{2, 0}, PartialShape{5}, 9, 100, true, PartialShape{2, 5, 1, 2}),
+ std::make_tuple(PartialShape{2, 1}, PartialShape{5}, 9, 100, true, PartialShape{2, 5, 1, 2}),
std::make_tuple(PartialShape{4, 47},
PartialShape{7},
11,
@@ -151,7 +151,7 @@ INSTANTIATE_TEST_SUITE_P(
3,
false,
PartialShape{{2, 4}, 6, {1, -1}, 2}),
- std::make_tuple(PartialShape{{2, 4}, {-1, -1}},
+ std::make_tuple(PartialShape{{2, 4}, {1, -1}},
PartialShape{7},
11,
3,
diff --git a/src/frontends/pytorch/src/op/istft.cpp b/src/frontends/pytorch/src/op/istft.cpp
new file mode 100644
index 00000000000..5483d491001
--- /dev/null
+++ b/src/frontends/pytorch/src/op/istft.cpp
@@ -0,0 +1,106 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/istft.hpp"
+
+#include "openvino/frontend/complex_type_mark.hpp"
+#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/broadcast.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert_like.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace frontend {
+namespace pytorch {
+namespace op {
+
+using namespace ov::op;
+
+OutputVector translate_istft(const NodeContext& context) {
+ // aten::istft(Tensor self, int n_fft, int? hop_length=None, int? win_length=None, Tensor? window=None, bool
+ // center=True, bool normalized=False, bool? onesided=None, int? length=None, bool return_complex=False)
+ num_inputs_check(context, 2, 10, true);
+
+ auto input = context.get_input(0);
+ auto complex_type_mark = as_type_ptr<ComplexTypeMark>(input.get_node_shared_ptr());
+ if (complex_type_mark) {
+ input = complex_type_mark->input_value(0);
+ }
+
+ auto n_fft = context.get_input(1);
+
+ ov::Output<ov::Node> hop_length;
+ if (!context.input_is_none(2)) {
+ hop_length = context.get_input(2);
+ } else {
+ // Defualt floor(n_fft / 4)
+ const auto four = context.mark_node(std::make_shared<ov::op::v0::Constant>(ov::element::i32, Shape{}, 4));
+ const auto four_cast = context.mark_node(std::make_shared<ov::op::v1::ConvertLike>(four, n_fft));
+ hop_length = context.mark_node(std::make_shared<ov::op::v1::Divide>(n_fft, four_cast));
+ }
+
+ ov::Output<ov::Node> win_length;
+ if (!context.input_is_none(3)) {
+ win_length = context.get_input(3);
+ } else {
+ win_length = n_fft;
+ }
+
+ ov::Output<ov::Node> window;
+ if (!context.input_is_none(4)) {
+ window = context.get_input(4);
+ } else {
+ const auto one = context.mark_node(std::make_shared<ov::op::v0::Constant>(ov::element::i32, Shape{}, 1));
+ const auto one_cast = context.mark_node(std::make_shared<ov::op::v1::ConvertLike>(one, input));
+ const auto zero = context.mark_node(std::make_shared<ov::op::v0::Constant>(ov::element::i32, Shape{1}, 0));
+ const auto win_length_cast =
+ context.mark_node(std::make_shared<ov::op::v0::Convert>(win_length, ov::element::i64));
+ const auto win_len_vec = context.mark_node(std::make_shared<ov::op::v0::Unsqueeze>(win_length_cast, zero));
+ window = context.mark_node(std::make_shared<ov::op::v3::Broadcast>(one_cast, win_len_vec));
+ }
+
+ bool center = true;
+ if (!context.input_is_none(5)) {
+ center = context.const_input<bool>(5);
+ }
+
+ bool normalized = false;
+ if (!context.input_is_none(6)) {
+ normalized = context.const_input<bool>(6);
+ }
+
+ bool onesided = true;
+ if (!context.input_is_none(7)) {
+ onesided = context.const_input<bool>(7);
+ }
+ PYTORCH_OP_CONVERSION_CHECK(onesided, "aten::istft conversion is currently supported with onesided=True only.");
+
+ bool return_complex = false;
+ if (!context.input_is_none(9)) {
+ return_complex = context.const_input<bool>(9);
+ }
+
+ // Perform ISTFT
+ ov::Output<ov::Node> istft;
+ if (context.input_is_none(8)) {
+ istft = context.mark_node(std::make_shared<v16::ISTFT>(input, window, n_fft, hop_length, center, normalized));
+ } else {
+ auto signal_length = context.get_input(8);
+ istft = context.mark_node(
+ std::make_shared<v16::ISTFT>(input, window, n_fft, hop_length, signal_length, center, normalized));
+ }
+
+ if (return_complex) {
+ return {context.mark_node(std::make_shared<ComplexTypeMark>(istft, istft.get_element_type()))};
+ } else {
+ return {istft};
+ }
+};
+} // namespace op
+} // namespace pytorch
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index aeffe0c2a3d..ef2919855d3 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -125,6 +125,7 @@ OP_CONVERTER(translate_index_select);
OP_CONVERTER(translate_instance_norm);
OP_CONVERTER(translate_int);
OP_CONVERTER(translate_inverse);
+OP_CONVERTER(translate_istft);
OP_CONVERTER(translate_is_nonzero);
OP_CONVERTER(translate_layer_norm);
OP_CONVERTER(translate_len);
@@ -523,6 +524,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::Int", op::translate_int},
{"aten::IntImplicit", op::translate_int},
{"aten::is_grad_enabled", op::return_false_scalar},
+ {"aten::istft", op::translate_istft},
{"aten::is_nonzero", op::translate_is_nonzero},
{"aten::isfinite", op::translate_1to1_match_1_inputs<opset10::IsFinite>},
{"aten::isinf", op::translate_1to1_match_1_inputs<opset10::IsInf>},
diff --git a/tests/layer_tests/pytorch_tests/test_istft.py b/tests/layer_tests/pytorch_tests/test_istft.py
new file mode 100644
index 00000000000..2f0eb4be031
--- /dev/null
+++ b/tests/layer_tests/pytorch_tests/test_istft.py
@@ -0,0 +1,253 @@
+# Copyright (C) 2018-2025 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import pytest
+import torch
+
+from pytorch_layer_test_class import PytorchLayerTest
+
+
+class TestISTFT(PytorchLayerTest):
+ def _prepare_input(self, n_fft, hop_length, win_length, center, normalized, signal_shape, signal_length = None, rand_data=False, out_dtype="float32"):
+ import numpy as np
+
+ if rand_data:
+ signal = np.random.randn(*signal_shape).astype(out_dtype)
+ else:
+ num_samples = signal_shape[-1]
+ half_idx = num_samples // 2
+ t = np.linspace(0, 1, num_samples)
+ signal = np.sin(2 * np.pi * 5 * t)
+ signal[half_idx:] += np.sin(2 * np.pi * 10 * t[half_idx:])
+ signal = np.broadcast_to(signal, signal_shape).astype(out_dtype)
+ signal = torch.from_numpy(signal)
+
+ if win_length is None:
+ window_size = n_fft
+ else:
+ window_size = win_length
+
+ if center and hop_length and hop_length != n_fft:
+ window = np.hanning(window_size)
+ else: # If 'center' is false, the window can't contain zeros at the beginning
+ window = np.hamming(window_size)
+
+ window = window.astype(out_dtype)
+ window_tensor = torch.from_numpy(window)
+
+ stft_out = torch.stft(
+ signal,
+ n_fft,
+ hop_length=hop_length,
+ win_length=win_length,
+ window=window_tensor,
+ center=center,
+ pad_mode="reflect",
+ normalized=normalized,
+ onesided=True,
+ return_complex=True,
+ )
+
+ if (signal_length is not None):
+ signal_length_input = np.array(signal_length)
+ return (torch.view_as_real(stft_out).numpy().astype(out_dtype), window, signal_length_input)
+ else:
+ return (torch.view_as_real(stft_out).numpy().astype(out_dtype), window)
+
+ def create_model(self, n_fft, hop_length, win_length, normalized, center):
+
+ class aten_istft(torch.nn.Module):
+
+ def __init__(self, n_fft, hop_length, win_length, normalized, center):
+ super(aten_istft, self).__init__()
+ self.n_fft = n_fft
+ self.hop_length = hop_length
+ self.win_length = win_length
+ self.normalized = normalized
+ self.center = center
+
+
+ def forward(self, x, window):
+ return torch.istft(
+ torch.view_as_complex(x),
+ self.n_fft,
+ hop_length=self.hop_length,
+ win_length=self.win_length,
+ window=window,
+ center=self.center,
+ normalized=self.normalized,
+ onesided=True,
+ return_complex=False,
+ length = None
+ )
+
+ ref_net = None
+
+ return aten_istft(n_fft, hop_length, win_length, normalized, center), ref_net, "aten::istft"
+
+ def create_model_with_sig_len(self, n_fft, hop_length, win_length, normalized, center):
+
+ class aten_istft(torch.nn.Module):
+
+ def __init__(self, n_fft, hop_length, win_length, normalized, center):
+ super(aten_istft, self).__init__()
+ self.n_fft = n_fft
+ self.hop_length = hop_length
+ self.win_length = win_length
+ self.normalized = normalized
+ self.center = center
+
+
+ def forward(self, x, window, sig_length):
+ return torch.istft(
+ torch.view_as_complex(x),
+ self.n_fft,
+ hop_length=self.hop_length,
+ win_length=self.win_length,
+ window=window,
+ center=self.center,
+ normalized=self.normalized,
+ onesided=True,
+ return_complex=False,
+ length = sig_length.item()
+ )
+
+ ref_net = None
+
+ return aten_istft(n_fft, hop_length, win_length, normalized, center), ref_net, "aten::istft"
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.parametrize(("trace_model"), [False, True])
+ @pytest.mark.parametrize(("signal_shape", "n_fft", "hop_length", "window_size"), [
+ [(1, 48), 16, 16, 16],
+ [(1, 48), 16, 8, 16],
+ [(1, 48), 16, 4, 16],
+ [(1, 48), 16, 1, 16],
+ [(1, 48), 16, 4, 16],
+ [(2, 48), 16, 4, 16],
+ [(3, 48), 16, 4, 16],
+ [(4, 48), 16, 4, 16],
+ [(1, 256), 32, 16, 32],
+ [(2, 256), 32, 16, 32],
+ [(1, 256), 24, 16, 20],
+ [(1, 256), 128, 128, 128],
+ [(1, 256), 256, 256, 256],
+ [(1, 47), 17, 5, 17],
+ [(1, 47), 17, 5, 13],
+ [(1, 256), 133, 77, 133],
+ [(1, 48), 16, None, 16],
+ [(1, 48), 16, 8, None],
+ [(1, 48), 16, None, None],
+ ])
+ @pytest.mark.parametrize(("normalized"), [True, False])
+ @pytest.mark.parametrize(("center"), [True, False])
+ def test_istft(self, n_fft, hop_length, window_size, signal_shape, normalized, center, ie_device, precision, ir_version, trace_model):
+ if ie_device == "GPU":
+ pytest.xfail(reason="ISTFT op is not supported on GPU yet")
+
+ if center is False and window_size and window_size < n_fft:
+ pytest.skip(
+ reason="torch istft doesn't allow for zeros padding in window, when `center` is false ")
+
+ self._test(*self.create_model(n_fft, hop_length, window_size, normalized, center), ie_device, precision,
+ ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length" : hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)
+
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.parametrize(("trace_model"), [False, True])
+ @pytest.mark.parametrize(("signal_shape", "n_fft", "hop_length", "window_size", "signal_length"), [
+ [(3, 48), 16, 8, 16, 48],
+ [(3, 48), 16, 8, 16, 32],
+ [(3, 48), 16, 8, 16, 55],
+ ])
+ @pytest.mark.parametrize(("normalized"), [True, False])
+ @pytest.mark.parametrize(("center"), [True, False])
+ def test_istft_with_sig_len(self, n_fft, hop_length, window_size, signal_shape, normalized, center, signal_length, ie_device, precision, ir_version, trace_model):
+ if ie_device == "GPU":
+ pytest.xfail(reason="ISTFT op is not supported on GPU yet")
+
+ self._test(*self.create_model_with_sig_len(n_fft, hop_length, window_size, normalized, center), ie_device, precision,
+ ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length" : hop_length, "center": center, "normalized": normalized, "signal_length": signal_length}, trace_model=trace_model)
+
+
+class TestISTFTDefaultParams(PytorchLayerTest):
+ def _prepare_input(self, n_fft, hop_length, win_length, center, normalized, signal_shape, out_dtype="float32"):
+ import numpy as np
+
+ signal = np.random.randn(*signal_shape).astype(out_dtype)
+ signal = torch.from_numpy(signal)
+
+ stft_kwargs = {
+ 'n_fft': n_fft,
+ 'return_complex': True
+ }
+
+ if hop_length is not None:
+ stft_kwargs['hop_length'] = hop_length
+ if win_length is not None:
+ stft_kwargs['win_length'] = win_length
+ if center is not None:
+ stft_kwargs['center'] = center
+ if normalized is not None:
+ stft_kwargs['normalized'] = normalized
+
+ stft_out = torch.stft(
+ signal,
+ **stft_kwargs
+ )
+
+ return (torch.view_as_real(stft_out).numpy().astype(out_dtype))
+
+ def create_model(self, n_fft, hop_length, win_length, normalized, center):
+
+ class aten_istft(torch.nn.Module):
+
+ def __init__(self, n_fft, hop_length, win_length, normalized, center):
+ super(aten_istft, self).__init__()
+ self.n_fft = n_fft
+ self.hop_length = hop_length
+ self.win_length = win_length
+ self.normalized = normalized
+ self.center = center
+
+ def forward(self, x):
+ istft_kwargs = {
+ 'n_fft': self.n_fft,
+ 'onesided': True,
+ 'return_complex': False,
+ }
+
+ if self.hop_length is not None:
+ istft_kwargs['hop_length'] = self.hop_length
+ if self.win_length is not None:
+ istft_kwargs['win_length'] = self.win_length
+ if self.center is not None:
+ istft_kwargs['center'] = self.center
+ if self.normalized is not None:
+ istft_kwargs['normalized'] = self.normalized
+
+ return torch.istft(torch.view_as_complex(x), **istft_kwargs)
+
+ ref_net = None
+
+ return aten_istft(n_fft, hop_length, win_length, normalized, center), ref_net, "aten::istft"
+
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.parametrize(("trace_model"), [True])
+ @pytest.mark.parametrize(("signal_shape", "n_fft", "hop_length", "window_size"), [
+ [(1, 48), 16, None, 16],
+ [(1, 48), 16, 8, None],
+ [(1, 48), 16, None, None],
+ ])
+ @pytest.mark.parametrize(("normalized"), [True, None])
+ @pytest.mark.parametrize(("center"), [False, None])
+ def test_istft(self, n_fft, hop_length, window_size, signal_shape, normalized, center, ie_device, precision, ir_version, trace_model):
+ if ie_device == "GPU":
+ pytest.xfail(reason="ISTFT op is not supported on GPU yet")
+
+ self._test(*self.create_model(n_fft, hop_length, window_size, normalized, center), ie_device, precision,
+ ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length" : hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)