aclStft
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Computes the Fourier transform of the input within a sliding window.
Formula:
When normalized=False:
When normalized=True:
Where:
- is a frequency point of the FFT.
- is the index of the sliding window.
- is a 1D or 2D Tensor. When is 1D, it is a time-series sampling sequence. When is 2D, it contains multiple time-series sampling sequences.
- is the sliding window size.
- is a 1D Tensor. It is the window function of the STFT (for example, hann_window), and its length is .
- is the twiddle factor.
Function Prototype
Each operator uses a two-phase API. First call the "aclStftGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the operator computation process, and then call the "aclStft" API to perform the computation.
aclnnStatus aclStftGetWorkspaceSize(
const aclTensor *self,
const aclTensor *windowOptional,
aclTensor *out,
int64_t nFft,
int64_t hopLength,
int64_t winLength,
bool normalized,
bool onesided,
bool returnComplex,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclStft(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclStftGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self Input Input to be computed, corresponding to `self` in the formula. - Empty tensors are not supported.
- shape is [L]/[B, L], where L is the length of the time-series sampling sequence and B is the number of time-series sampling sequences.
- Inf, -Inf, and NaN inputs are not supported.
FLOAT32, DOUBLE, COMPLEX64, COMPLEX128 ND 1-2 × windowOptional Input Must be a 1D tensor, corresponding to `window` in the formula. - Empty tensors are not supported.
- The data type is the same as that of `self`.
- The shape is [winLength], where winLength is the length of the STFT window function.
FLOAT32, DOUBLE, COMPLEX64, COMPLEX128 ND 1 × out Output Fourier transform result of `self` within the window, which is `X` in the corresponding formula. - Empty tensors are not supported.
- If returnComplex=True, `out` is a complex tensor with shape [N, T] or [B, N, T].
- If returnComplex=False, `out` is a real tensor with shape [N, T, 2] or [B, N, T, 2].
FLOAT32, DOUBLE, COMPLEX64, COMPLEX128 ND 3-4 × nFft Input Number of FFT points (greater than 0), corresponding to `nFft` in the corresponding formula. - INT64 - - - hopLength Input Represents the interval between sliding windows (greater than 0), corresponding to `hopLength` in the corresponding formula. - INT64 - - - winLength Input Indicates the window size (greater than 0), corresponding to `winLength` in the corresponding formula. - INT64 - - - normalized Input Indicates whether to normalize the Fourier transform result. - BOOL - - - onesided Input Specifies whether to return the full result or half of the result. If the data type of input `self` is COMPLEX64 or COMPLEX128, only False is supported. BOOL - - - returnComplex Input Specifies whether the return value is a complex Tensor or separate Tensors for the real and imaginary parts. - BOOL - - - workspaceSize Output Returns the size of the workspace to be requested on the Device. - - - - - executor Output Returns the op executor containing the operator computation flow. - - - - - Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input self or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data format of self is not supported. The data types of self and windowOptional are inconsistent. The data types of self, windowOptional, or out are not supported by the platform. Invalid values are input for nFft, hopLength, or winLength. The dimensions of self, windowOptional, or out are not within the supported range. When the data type of input self is COMPLEX64 or COMPLEX128, the value of onesided is True.
aclStft
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device. workspaceSize Input Size of the workspace requested on the Device. Obtained by calling the first-phase API aclStftGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Difference in the input self from the PyTorch API: the input self of the PyTorch API is the original input, while the input self of aclStftGetWorkspaceSize is obtained by padding the original input in the PyTorch frontend.
When the shape of the input self is [B, L] and the result of the following formula is large, the computation of this API may time out.
nFft <= L.
winLength <= nFft.
When normalized=True,
The mapping between the input and output data types of self, windowOptional, returnComplex, and out is as follows:
| Self | windowOptional | returnComplex | Out |
|---|---|---|---|
| FLOAT32 | FLOAT32 | True | COMPLEX64 |
| DOUBLE | DOUBLE | True | COMPLEX128 |
| COMPLEX64 | COMPLEX64 | True | COMPLEX64 |
| COMPLEX128 | COMPLEX128 | True | COMPLEX128 |
| FLOAT32 | FLOAT32 | False | FLOAT32 |
| DOUBLE | DOUBLE | False | DOUBLE |
| COMPLEX64 | COMPLEX64 | False | FLOAT32 |
| COMPLEX128 | COMPLEX128 | False | DOUBLE |
- Deterministic computation:
- aclStft defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/acl_stft.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape)
{
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream)
{
// Boilerplate for resource initialization.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(
const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType,
aclTensor** tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate device memory.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy host data to device memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(
shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(),
*deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) Initialize the device/stream. For details, see the ACL public API list.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check as required.
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> selfShape = {5};
std::vector<int64_t> windowShape = {4};
std::vector<int64_t> outShape = {3, 1, 2};
void* selfDeviceAddr = nullptr;
void* windowDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* window = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {1, 6, 8, 5, 7};
std::vector<float> windowHostData = {1, 1, 1, 1};
std::vector<float> outHostData = {0, 0, 0, 0, 0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the window aclTensor.
ret = CreateAclTensor(windowHostData, windowShape, &windowDeviceAddr, aclDataType::ACL_FLOAT, &window);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
int n_fft = 4;
int hop_length = 2;
int win_length = 4;
bool normalized = false;
bool onesided = true;
bool returnComplex = false;
// 3. Call the CANN operator library API, and change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclStft.
ret = aclStftGetWorkspaceSize(
self, window, out, n_fft, hop_length, win_length, normalized, onesided, returnComplex, &workspaceSize,
&executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclStftGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclStft.
ret = aclStft(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclStft failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value, and copy the result from device memory to the host. Modify the code based on the interface definition of the specific API.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(resultData[0]),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release the aclTensor. Modify the code based on the interface definition of the specific API.
aclDestroyTensor(self);
aclDestroyTensor(window);
aclDestroyTensor(out);
// 7. Release device resources. Modify the code based on the interface definition of the specific API.
aclrtFree(selfDeviceAddr);
aclrtFree(windowDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}