HCmatinvBatched
安全声明:
该样例旨在提供快速上手、开发和调试算子的最小化实现,其核心目标是使用最精简的代码展示算子的核心功能,而非提供生产级的安全保障。
不推荐用户直接将示例代码作为业务代码,若用户将示例代码应用在自身的真实业务场景中且发生了安全问题,则需用户自行承担。
HCmatinvBatched
HCmatinvBatched算子调用示例:
#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)
{
// 固定写法,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);
// 调用aclrtMalloc申请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);
// 调用aclrtMemcpy将host侧数据复制到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);
// 计算连续tensor的strides
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];
}
// 调用aclCreateTensor接口创建aclTensor
*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;
}
父主题: BLAS