swapLast2Axes
Applicable Products
Ascend 950PR&950DT products : Not 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
API function:
swapLast2AxesGetWorkspaceSize: computes the workspace size required by theswapLast2Axesoperator.swapLast2Axes: swaps the last two dimensions of the tensor.Formula:
$$ outTensor_{bij} = inTensor_{bji}\\ $$Where: b is the batch number of the data, i is the row number of the input data, and j is the column number of the input data. Examples:
- Example 1:
The input
inTensoris:
[[[1.+0.j, 2.+0.j, 3.+0.j]]]
After theswapLast2Axesoperator is called, the outputoutTensoris:
[[[1.+0.j], [2.+0.j], [3.+0.j]]] - Example 2:
The input
inTensoris:
[[[ 0.+0.j, 1.+0.j, 2.+0.j],
[ 3.+0.j, 4.+0.j, 5.+0.j]],
[[ 6.+0.j, 7.+0.j, 8.+0.j],
[ 9.+0.j, 10.+0.j, 11.+0.j]]]
After theswapLast2Axesoperator is called, the outputoutTensoris:
[[[ 0.+0.j, 3.+0.j],
[ 1.+0.j, 4.+0.j],
[ 2.+0.j, 5.+0.j]],
[[ 6.+0.j, 9.+0.j],
[ 7.+0.j, 10.+0.j],
[ 8.+0.j, 11.+0.j]]]
- Example 1:
The input
Function Prototype
To use the swapLast2Axes operator, the swapLast2AxesGetWorkspaceSize API must be called first to obtain the input parameters and compute the required workspace size based on the computation process, and then the swapLast2Axes API is called to perform the computation.
AsdSip::AspbStatus swapLast2AxesGetWorkspaceSize(
size_t *size)AsdSip::AspbStatus swapLast2Axes(
const aclTensor * inTensor,
aclTensor * outTensor,
void * stream,
void * workspace = nullptr)Return value:
For details about the return values, see SiP Return Codes.
swapLast2AxesGetWorkspaceSize
Parameter description:
Parameter Input/Output Description size (size_t *) Input/Output Workspace required by the swapLast2Axesoperator.Return value:
For details about the return values, see SiP Return Codes.
swapLast2Axes
Parameter description:
Parameter Input/Output Description inTensor (aclTensor *) Input - Input tensor data, corresponding to "inTensor" in the formula.
- The maximum number of input elements is 3600000000 (within [60000, 60000]).
- Only
COMPLEX64is supported. Data format:ND. - The input
dimis limited to 2 or 3.
outTensor (aclTensor *) Output - Output tensor data, corresponding to "outTensor" in the formula.
- Only
COMPLEX64is supported. The data type must be consistent with that ofinTensor. - If the shape of
inTensoris [k,x,y], the shape ofoutTensoris [k,y,x]. - Data format:
ND.
workspace (void *) Input Workspace required by the swapLast2Axesoperator.stream (void *) Input NPU execution stream. Return value:
For details about the return values, see SiP Return Codes.
Constraints
During actual computation, the operator does not support high-dimensional ND operations (dimensions > 3).
Calling Example
The example code is as follows. This sample is intended to provide a minimal implementation for quick start, development, and debugging of the operator. Its core goal is to demonstrate the core functionality of the operator using the simplest code, rather than providing production-grade security assurance. Users are advised not to directly use the example code for business purposes. If users apply the example code in their own real business scenarios and security issues occur, the users shall bear the consequences themselves.
#include <iostream>
#include <vector>
#include "asdsip.h"
#include "acl/acl.h"
#include "acl_meta.h"
using namespace AsdSip;
#define ASD_STATUS_CHECK(err) \
do { \
AsdSip::AspbStatus err_ = (err); \
if (err_ != AsdSip::ErrorType::ACL_SUCCESS) { \
std::cout << "Execute failed." << std::endl; \
exit(-1); \
} \
} while (0)
#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: Initialize ACL.
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) * 2;
// Call aclrtMalloc to allocate device-side 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-side data to device-side 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);
// Compute 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 aclTensor.
*tensor = aclCreateTensor(shape.data(),
shape.size(),
dataType,
strides.data(),
0,
aclFormat::ACL_FORMAT_ND,
shape.data(),
shape.size(),
*deviceAddr);
return 0;
}
void printTensor(const float *tensorData, size_t row, size_t col)
{
for (size_t r = 0; r < row; ++r) {
for (size_t c = 0; c < col; ++c) {
size_t index = (r * col + c) * 2;
std::cout << "(" << int(tensorData[index]) << ", " << int(tensorData[index + 1]) << ") ";
}
std::cout << "\n";
}
}
int main(int argc, char **argv)
{
int deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
int64_t row = 3;
int64_t col = 2;
const int64_t tensorSize = row * col * 2;
std::vector<float> tensorInData;
tensorInData.resize(tensorSize);
for (int64_t i = 0; i < tensorSize; i++) {
tensorInData[i] = 0.0 + i;
}
std::vector<float> tensorOutData;
tensorOutData.resize(tensorSize);
std::vector<int64_t> inShape = {row, col};
std::vector<int64_t> outShape = {col, row};
aclTensor *input = nullptr;
aclTensor *output = nullptr;
void *inputDeviceAddr = nullptr;
void *outputDeviceAddr = nullptr;
ret = CreateAclTensor(tensorInData, inShape, &inputDeviceAddr, aclDataType::ACL_COMPLEX64, &input);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(tensorOutData, outShape, &outputDeviceAddr, aclDataType::ACL_COMPLEX64, &output);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
void *workspace = nullptr;
size_t lwork = 0;
swapLast2AxesGetWorkspaceSize(lwork);
std::cout << "lwork = " << lwork << std::endl;
if (lwork > 0) {
ret = aclrtMalloc(&workspace, static_cast<int64_t>(lwork), ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
ASD_STATUS_CHECK(swapLast2Axes(input, output, stream, workspace));
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
ret = aclrtMemcpy(tensorOutData.data(),
tensorSize * sizeof(float),
outputDeviceAddr,
tensorSize * sizeof(float),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy output tensor from device to host failed. ERROR: %d\n", ret); return ret);
std::cout << "row = " << row << ", col = " << col << std::endl;
std::cout << "------- Input ------- " << std::endl;
printTensor(tensorInData.data(), row, col);
std::cout << "------- Output -------" << std::endl;
printTensor(tensorOutData.data(), col, row);
std::cout << "Execute successfully." << std::endl;
aclrtFree(inputDeviceAddr);
aclrtFree(outputDeviceAddr);
aclDestroyTensor(input);
aclDestroyTensor(output);
if (lwork > 0) {
aclrtFree(workspace);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}