* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file stft_infershape.cpp
* \brief
*/
#include "register/op_impl_registry.h"
#include "log/log.h"
#include <string>
using namespace ge;
namespace ops {
static constexpr size_t STFT_IDX_IN_NUM = 0;
static constexpr size_t STFT_IDX_IN_THREE = 2;
static constexpr size_t STFT_IDX_OUT_NUM = 0;
static constexpr size_t COMPLEX_IDX = 4;
static constexpr size_t ONE_SIDED_IDX = 3;
static constexpr size_t N_FFT_IDX = 5;
static constexpr size_t HOP_LENGTH_IDX = 0;
static constexpr size_t WIN_LENGTH_IDX = 1;
static constexpr size_t SUPPORTED_DIM_TWO = 2;
static constexpr size_t SUPPORTED_DIM_THREE = 3;
static constexpr size_t SUPPORTED_DIM_FOUR = 4;
static constexpr size_t DIM_NUM_ZERO = 0;
static constexpr size_t DIM_NUM_ONE = 1;
static constexpr size_t DIM_NUM_TWO = 2;
static constexpr size_t DIM_NUM_THREE = 3;
static ge::graphStatus InferShape4STFT(gert::InferShapeContext* context)
{
OP_LOGD(context->GetNodeName(), "----------- enter InferShape4STFT. -----------");
auto x_desc = context->GetInputTensor(STFT_IDX_IN_NUM);
OP_CHECK_NULL_WITH_CONTEXT(context, x_desc);
auto x_shape = context->GetInputShape(STFT_IDX_IN_NUM);
OP_CHECK_NULL_WITH_CONTEXT(context, x_shape);
int64_t dims = x_shape->GetDimNum();
if (dims != DIM_NUM_ONE && dims != DIM_NUM_TWO) {
OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "x", std::to_string(dims) + "D", "1D or 2D");
return ge::GRAPH_FAILED;
}
int64_t len = (dims == 1) ? x_shape->GetDim(0) : x_shape->GetDim(1);
int64_t batch = (dims == 1) ? 1 : x_shape->GetDim(0);
auto attrs = context->GetAttrs();
OP_CHECK_NULL_WITH_CONTEXT(context, attrs);
const bool* complex_attr = attrs->GetAttrPointer<bool>(COMPLEX_IDX);
bool complex = true;
if (complex_attr != nullptr && *complex_attr == false) {
complex = false;
}
const bool* one_sided_attr = attrs->GetAttrPointer<bool>(ONE_SIDED_IDX);
bool one_sided = true;
if (one_sided_attr != nullptr && *one_sided_attr == false) {
one_sided = false;
}
const int64_t* n_fft_attr = attrs->GetAttrPointer<int64_t>(N_FFT_IDX);
if (n_fft_attr == nullptr) {
OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "n_fft", "Required attribute is missing");
return GRAPH_FAILED;
}
int64_t n_fft = *n_fft_attr;
if (n_fft <= 0) {
OP_LOGE_FOR_INVALID_VALUE(context->GetNodeName(), "n_fft", std::to_string(n_fft), "greater than 0");
return GRAPH_FAILED;
}
if (len != ge::UNKNOWN_DIM && len < n_fft) {
OP_LOGE_FOR_INVALID_VALUE(context->GetNodeName(), "n_fft", std::to_string(n_fft),
"less than or equal to input length " + std::to_string(len));
return GRAPH_FAILED;
}
const int64_t* hop_length_attr = attrs->GetAttrPointer<int64_t>(HOP_LENGTH_IDX);
int64_t hop_length = (n_fft >> 2);
if (hop_length_attr != nullptr && *hop_length_attr != 0) {
hop_length = *hop_length_attr;
}
if (hop_length <= 0) {
OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(context->GetNodeName(), "hop_length", std::to_string(hop_length),
"Resolved hop_length must be greater than 0. The default is n_fft / 4, "
"with n_fft = " +
std::to_string(n_fft));
return GRAPH_FAILED;
}
const int64_t* win_length_attr = attrs->GetAttrPointer<int64_t>(WIN_LENGTH_IDX);
int64_t win_length = n_fft;
if (win_length_attr != nullptr && *win_length_attr != 0) {
win_length = *win_length_attr;
}
if (win_length <= 0 || win_length > n_fft) {
OP_LOGE_FOR_INVALID_VALUE(context->GetNodeName(), "win_length", std::to_string(win_length),
"in range (0, " + std::to_string(n_fft) + "] after applying the default value");
return GRAPH_FAILED;
}
auto window = context->GetInputDesc(STFT_IDX_IN_THREE);
if (window != nullptr) {
auto win_shape = context->GetInputShape(STFT_IDX_IN_THREE);
OP_CHECK_NULL_WITH_CONTEXT(context, win_shape);
int64_t win_dim = win_shape->GetDimNum();
if (win_dim != 1) {
OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "window", std::to_string(win_dim) + "D", "1D");
return GRAPH_FAILED;
}
if (win_shape->GetDim(0) != win_length) {
OP_LOGE_FOR_INVALID_SHAPE(context->GetNodeName(), "window", Ops::Base::ToString(*win_shape),
"[" + std::to_string(win_length) + "]");
return GRAPH_FAILED;
}
}
int64_t n_frames = (len == ge::UNKNOWN_DIM) ? ge::UNKNOWN_DIM : 1 + (len - n_fft) / hop_length;
auto out_shape = context->GetOutputShape(STFT_IDX_OUT_NUM);
OP_CHECK_NULL_WITH_CONTEXT(context, out_shape);
int64_t n_dim = (!one_sided) ? n_fft : (n_fft / 2 + 1);
if (batch == 1 && dims == 1 && complex) {
out_shape->SetDimNum(SUPPORTED_DIM_TWO);
out_shape->SetDim(0, n_dim);
out_shape->SetDim(1, n_frames);
} else if (batch == 1 && dims == 1 && !complex) {
out_shape->SetDimNum(SUPPORTED_DIM_THREE);
out_shape->SetDim(0, n_dim);
out_shape->SetDim(1, n_frames);
out_shape->SetDim(DIM_NUM_TWO, DIM_NUM_TWO);
} else if (complex) {
out_shape->SetDimNum(SUPPORTED_DIM_THREE);
out_shape->SetDim(0, batch);
out_shape->SetDim(1, n_dim);
out_shape->SetDim(DIM_NUM_TWO, n_frames);
} else {
out_shape->SetDimNum(SUPPORTED_DIM_FOUR);
out_shape->SetDim(0, batch);
out_shape->SetDim(1, n_dim);
out_shape->SetDim(DIM_NUM_TWO, n_frames);
out_shape->SetDim(DIM_NUM_THREE, DIM_NUM_TWO);
}
return GRAPH_SUCCESS;
}
static graphStatus InferDataType4STFT(gert::InferDataTypeContext* context)
{
auto x_type = context->GetInputDataType(STFT_IDX_IN_NUM);
if (!(x_type == DT_COMPLEX64 || x_type == DT_COMPLEX128 || x_type == DT_DOUBLE || x_type == DT_FLOAT)) {
OP_LOGE_FOR_INVALID_DTYPE(context->GetNodeName(), "x", Ops::Base::ToString(x_type),
"float, double, complex64 or complex128");
return ge::GRAPH_FAILED;
}
const gert::RuntimeAttrs* attrs = context->GetAttrs();
const bool* complex_attr = attrs->GetAttrPointer<bool>(COMPLEX_IDX);
bool complex = true;
if (complex_attr != nullptr && *complex_attr == false) {
complex = false;
}
auto window = context->GetInputDesc(STFT_IDX_IN_THREE);
DataType type = DT_FLOAT;
if (window != nullptr) {
auto window_type = context->GetInputDataType(STFT_IDX_IN_THREE);
if (!(window_type == DT_COMPLEX64 || window_type == DT_COMPLEX128 || window_type == DT_DOUBLE ||
window_type == DT_FLOAT)) {
OP_LOGE_FOR_INVALID_DTYPE(context->GetNodeName(), "window", Ops::Base::ToString(window_type),
"float, double, complex64 or complex128");
return GRAPH_FAILED;
}
if (x_type == DT_DOUBLE || x_type == DT_COMPLEX128) {
type = complex ? DT_COMPLEX128 : DT_DOUBLE;
} else if (x_type == DT_FLOAT || x_type == DT_COMPLEX64) {
if (window_type == DT_FLOAT || window_type == DT_COMPLEX64) {
type = complex ? DT_COMPLEX64 : DT_FLOAT;
} else if (window_type == DT_DOUBLE || window_type == DT_COMPLEX128) {
type = complex ? DT_COMPLEX128 : DT_DOUBLE;
}
}
} else {
if (x_type == DT_FLOAT || x_type == DT_COMPLEX64) {
type = complex ? DT_COMPLEX64 : DT_FLOAT;
} else if (x_type == DT_DOUBLE || x_type == DT_COMPLEX128) {
type = complex ? DT_COMPLEX128 : DT_DOUBLE;
}
}
context->SetOutputDataType(STFT_IDX_OUT_NUM, type);
return GRAPH_SUCCESS;
}
IMPL_OP_INFERSHAPE(STFT).InferShape(InferShape4STFT).InferDataType(InferDataType4STFT);
}