|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "openvino/op/istft.hpp" |
| 6 | + |
| 7 | +#include "openvino/frontend/complex_type_mark.hpp" |
| 8 | +#include "openvino/frontend/pytorch/node_context.hpp" |
| 9 | +#include "openvino/op/broadcast.hpp" |
| 10 | +#include "openvino/op/constant.hpp" |
| 11 | +#include "openvino/op/convert_like.hpp" |
| 12 | +#include "openvino/op/divide.hpp" |
| 13 | +#include "openvino/op/unsqueeze.hpp" |
| 14 | +#include "utils.hpp" |
| 15 | + |
| 16 | +namespace ov { |
| 17 | +namespace frontend { |
| 18 | +namespace pytorch { |
| 19 | +namespace op { |
| 20 | + |
| 21 | +using namespace ov::op; |
| 22 | + |
| 23 | +OutputVector translate_istft(const NodeContext& context) { |
| 24 | + // aten::istft(Tensor self, int n_fft, int? hop_length=None, int? win_length=None, Tensor? window=None, bool |
| 25 | + // center=True, bool normalized=False, bool? onesided=None, int? length=None, bool return_complex=False) |
| 26 | + num_inputs_check(context, 2, 10, true); |
| 27 | + |
| 28 | + auto input = context.get_input(0); |
| 29 | + auto complex_type_mark = as_type_ptr<ComplexTypeMark>(input.get_node_shared_ptr()); |
| 30 | + if (complex_type_mark) { |
| 31 | + input = complex_type_mark->input_value(0); |
| 32 | + } |
| 33 | + |
| 34 | + auto n_fft = context.get_input(1); |
| 35 | + |
| 36 | + ov::Output<ov::Node> hop_length; |
| 37 | + if (!context.input_is_none(2)) { |
| 38 | + hop_length = context.get_input(2); |
| 39 | + } else { |
| 40 | + // Defualt floor(n_fft / 4) |
| 41 | + const auto four = context.mark_node(std::make_shared<ov::op::v0::Constant>(ov::element::i32, Shape{}, 4)); |
| 42 | + const auto four_cast = context.mark_node(std::make_shared<ov::op::v1::ConvertLike>(four, n_fft)); |
| 43 | + hop_length = context.mark_node(std::make_shared<ov::op::v1::Divide>(n_fft, four_cast)); |
| 44 | + } |
| 45 | + |
| 46 | + ov::Output<ov::Node> win_length; |
| 47 | + if (!context.input_is_none(3)) { |
| 48 | + win_length = context.get_input(3); |
| 49 | + } else { |
| 50 | + win_length = n_fft; |
| 51 | + } |
| 52 | + |
| 53 | + ov::Output<ov::Node> window; |
| 54 | + if (!context.input_is_none(4)) { |
| 55 | + window = context.get_input(4); |
| 56 | + } else { |
| 57 | + const auto one = context.mark_node(std::make_shared<ov::op::v0::Constant>(ov::element::i32, Shape{}, 1)); |
| 58 | + const auto one_cast = context.mark_node(std::make_shared<ov::op::v1::ConvertLike>(one, input)); |
| 59 | + const auto zero = context.mark_node(std::make_shared<ov::op::v0::Constant>(ov::element::i32, Shape{1}, 0)); |
| 60 | + const auto win_length_cast = |
| 61 | + context.mark_node(std::make_shared<ov::op::v0::Convert>(win_length, ov::element::i64)); |
| 62 | + const auto win_len_vec = context.mark_node(std::make_shared<ov::op::v0::Unsqueeze>(win_length_cast, zero)); |
| 63 | + window = context.mark_node(std::make_shared<ov::op::v3::Broadcast>(one_cast, win_len_vec)); |
| 64 | + } |
| 65 | + |
| 66 | + bool center = true; |
| 67 | + if (!context.input_is_none(5)) { |
| 68 | + center = context.const_input<bool>(5); |
| 69 | + } |
| 70 | + |
| 71 | + bool normalized = false; |
| 72 | + if (!context.input_is_none(6)) { |
| 73 | + normalized = context.const_input<bool>(6); |
| 74 | + } |
| 75 | + |
| 76 | + bool onesided = true; |
| 77 | + if (!context.input_is_none(7)) { |
| 78 | + onesided = context.const_input<bool>(7); |
| 79 | + } |
| 80 | + PYTORCH_OP_CONVERSION_CHECK(onesided, "aten::istft conversion is currently supported with onesided=True only."); |
| 81 | + |
| 82 | + bool return_complex = false; |
| 83 | + if (!context.input_is_none(9)) { |
| 84 | + return_complex = context.const_input<bool>(9); |
| 85 | + } |
| 86 | + |
| 87 | + // Perform ISTFT |
| 88 | + ov::Output<ov::Node> istft; |
| 89 | + if (context.input_is_none(8)) { |
| 90 | + istft = context.mark_node(std::make_shared<v16::ISTFT>(input, window, n_fft, hop_length, center, normalized)); |
| 91 | + } else { |
| 92 | + auto signal_length = context.get_input(8); |
| 93 | + istft = context.mark_node( |
| 94 | + std::make_shared<v16::ISTFT>(input, window, n_fft, hop_length, signal_length, center, normalized)); |
| 95 | + } |
| 96 | + |
| 97 | + if (return_complex) { |
| 98 | + return {context.mark_node(std::make_shared<ComplexTypeMark>(istft, istft.get_element_type()))}; |
| 99 | + } else { |
| 100 | + return {istft}; |
| 101 | + } |
| 102 | +}; |
| 103 | +} // namespace op |
| 104 | +} // namespace pytorch |
| 105 | +} // namespace frontend |
| 106 | +} // namespace ov |
0 commit comments