HCmatinvBatched
Security statement:
This example provides a minimal implementation for quickly developing and debugging operators. It focuses on demonstrating core functionality through simple code rather than providing production-ready security.
This sample code is for demonstration purposes only. You assume all responsibility for security risks if this code is implemented in a live service environment.
HCmatinvBatched
The following is an example of calling the HCmatinvBatched operator:
#include <iostream>
#include <vector>
#include <complex>
#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); \
} else { \
std::cout << "Execute successfully." << std::endl; \
} \
} while (0)
void printTensor(const std::complex<op::fp16_t> *tensorData, int64_t batch, int64_t rows, int64_t cols)
{
for (int64_t b = 0; b < batch; b++) {
for (int64_t i = 0; i < rows; i++) {
for (int64_t j = 0; j < cols; j++) {
auto data = tensorData[b * rows * cols + i * cols + j];
std::cout << "(" << (float)data.real() << "," << (float)data.imag() << ")" << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
#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)
{
// Initialize ACL. This code is written in a fixed format.
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 memory on the device.
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 the data on the host to the memory on the device.
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 an ACL tensor.
*tensor = aclCreateTensor(shape.data(),
shape.size(),
dataType,
strides.data(),
0,
aclFormat::ACL_FORMAT_ND,
shape.data(),
shape.size(),
*deviceAddr);
return 0;
}
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 batchSize = 3;
int64_t n = 4;
int64_t tensorASize = batchSize * n * n;
std::vector<std::complex<op::fp16_t>> tensorInAData;
std::vector<std::complex<op::fp16_t>> tensorInAinvData;
std::vector<int32_t> tensorInInfoData;
tensorInAData.reserve(tensorASize);
tensorInAinvData.reserve(tensorASize);
tensorInInfoData.reserve(batchSize);
for (int32_t batchIdx = 0; batchIdx < batchSize; batchIdx++) {
for (int32_t i = 0; i < n; i++) {
for (int32_t j = 0; j < n; j++) {
if (i == j) {
tensorInAData[n * n * batchIdx + n * i + j] = std::complex<op::fp16_t>(2.0f + batchIdx, -2.0f - batchIdx);
} else {
tensorInAData[n * n * batchIdx + n * i + j] = std::complex<op::fp16_t>(1.0f, -1.0f);
}
}
}
}
for (int32_t batchIdx = 0; batchIdx < batchSize; batchIdx++) {
for (int32_t i = 0; i < n; i++) {
for (int32_t j = 0; j < n; j++) {
tensorInAinvData[n * n * batchIdx + n * i + j] = std::complex<op::fp16_t>(-1.0f, -1.0f);
}
}
}
for (int32_t batchIdx = 0; batchIdx < batchSize; batchIdx++) {
tensorInInfoData[batchIdx] = 0;
}
std::cout << "------- input TensorInA -------" << std::endl;
printTensor(tensorInAData.data(), batchSize, n, n);
std::cout << "------- input TensorInAinv -------" << std::endl;
printTensor(tensorInAinvData.data(), batchSize, n, n);
std::vector<int64_t> aShape = {batchSize, n, n};
std::vector<int64_t> ainvShape = {batchSize, n, n};
std::vector<int64_t> infoShape = {batchSize};
aclTensor *inputA = nullptr;
aclTensor *inputAinv = nullptr;
aclTensor *inputInfo = nullptr;
void *inputADeviceAddr = nullptr;
void *inputAinvDeviceAddr = nullptr;
void *inputInfoDeviceAddr = nullptr;
ret = CreateAclTensor(tensorInAData, aShape, &inputADeviceAddr, aclDataType::ACL_COMPLEX32, &inputA);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(tensorInAinvData, ainvShape, &inputAinvDeviceAddr, aclDataType::ACL_COMPLEX32, &inputAinv);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(tensorInInfoData, infoShape, &inputInfoDeviceAddr, aclDataType::ACL_INT32, &inputInfo);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
asdBlasHandle handle;
asdBlasCreate(handle);
size_t lwork = 0;
void *buffer = nullptr;
asdBlasMakeHCmatinvBatchedPlan(handle, n, batchSize);
asdBlasGetWorkspaceSize(handle, lwork);
std::cout << "lwork = " << lwork << std::endl;
if (lwork > 0) {
ret = aclrtMalloc(&buffer, 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);
}
asdBlasSetWorkspace(handle, buffer);
asdBlasSetStream(handle, stream);
asdBlasSynchronize(handle);
ASD_STATUS_CHECK(asdBlasHCmatinvBatched(handle, n, inputA, n, inputAinv, n, inputInfo, batchSize));
asdBlasSynchronize(handle);
asdBlasDestroy(handle);
ret = aclrtMemcpy(tensorInAinvData.data(),
tensorASize * sizeof(std::complex<op::fp16_t>),
inputAinvDeviceAddr,
tensorASize * sizeof(std::complex<op::fp16_t>),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy Ainv from device to host failed. ERROR: %d\n", ret); return ret);
std::cout << "------- output TensorInAinv -------" << std::endl;
printTensor(tensorInAinvData.data(), batchSize, n, n);
aclDestroyTensor(inputA);
aclDestroyTensor(inputAinv);
aclDestroyTensor(inputInfo);
aclrtFree(inputADeviceAddr);
aclrtFree(inputAinvDeviceAddr);
aclrtFree(inputInfoDeviceAddr);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}
Parent topic: BLAS