aclnnLayerNorm&aclnnLayerNormWithImplMode
支持的产品型号
- Atlas 训练系列产品。
- Atlas A2训练系列产品/Atlas 800I A2推理产品。
- Atlas 200/500 A2推理产品。
接口原型
每个算子分为两段式接口,必须先调用“aclnnLayerNormGetWorkspaceSize”或者“aclnnLayerNormWithImplModeGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnLayerNorm”或者“aclnnLayerNormWithImplMode”接口执行计算。
aclnnStatus aclnnLayerNormGetWorkspaceSize(const aclTensor *input, const aclIntArray *normalizedShape, const aclTensor *weightOptional, const aclTensor *biasOptional, double eps, aclTensor *out, aclTensor *meanOutOptional, aclTensor *rstdOutOptional, uint64_t *workspaceSize, aclOpExecutor **executor)
aclnnStatus aclnnLayerNorm(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)
aclnnStatus aclnnLayerNormWithImplModeGetWorkspaceSize(const aclTensor *input, const aclIntArray *normalizedShape, const aclTensor *weightOptional, const aclTensor *biasOptional, double eps, aclTensor *out, aclTensor *meanOutOptional, aclTensor *rstdOutOptional, int32_t implMode, uint64_t *workspaceSize, aclOpExecutor **executor)
aclnnStatus aclnnLayerNormWithImplMode(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)
功能描述
- 算子功能:对指定层进行均值为0、标准差为1的归一化计算。
- 计算公式: 其中,E[input]表示输入的均值,Var[input]表示输入的方差。
aclnnLayerNormGetWorkspaceSize
参数说明:
input(aclTensor*,计算输入):公式中的输入
x
,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持)。shape为[A1,...,Ai,R1,...,Rj],其中A1至Ai表示无需norm的维度,R1至Rj表示需norm的维度。支持非连续的Tensor,数据格式支持ND。normalizedShape(aclIntArray*,计算输入):表示需要进行norm计算的维度,数据类型支持INT64,shape为[R1,...,Rj], 长度小于等于输入input的长度,不支持为空。
weightOptional(aclTensor*,计算输入):公式中的输入
w
,可选参数。weightOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持),数据类型与输入input一致或为FLOAT类型,且当biasOptional存在时weightOptional与biasOptional的数据类型相同。shape与normalizedShape相等,为[R1,...,Rj]。支持非连续的Tensor,数据格式支持ND。weightOptional为空时,接口内部会构造一个shape为[R1,...,Rj],数据全为1的tensor,当biasOptional存在时weightOptional与biasOptional的数据类型相同,biasOptional不存在时weightOptional与输入input的数据类型相同。biasOptional(aclTensor*,计算输入):公式中的输入
b
,可选参数。biasOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持),数据类型与输入input一致或为FLOAT类型,且当weightOptional存在时biasOptional与weightOptional的数据类型相同。shape与normalizedShape相等,为[R1,...,Rj]。支持非连续的Tensor,数据格式支持ND。biasOptional为空时,接口内部会构造一个shape为[R1,...,Rj],数据全为0的tensor,当weightOptional存在时biasOptional与weightOptional的数据类型相同,weightOptional不存在时biasOptional与输入input的数据类型相同。eps(double,计算输入):公式中的输入
eps
,用于规避除零计算,数据类型支持DOUBLE, 且需要可转换成与input相同的数据类型。out(aclTensor*,计算输出):公式中的输出
out
,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持)。shape需要与input的shape相等,为[A1,...,Ai,R1,...,Rj]。支持非连续的Tensor,数据格式支持ND。meanOutOptional(aclTensor*,计算输出):可选参数,meanOutOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持)。当rstdOutOptional存在时与rstdOutOptional的数据类型和shape相同,shape为[A1,...,Ai,1,...,1],Ai后共有j个1,与需要norm的轴长度保持相同。支持非连续的Tensor,数据格式支持ND。
rstdOutOptional(aclTensor*,计算输出):可选参数,rstdOutOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持)。当meanOutOptional存在时与meanOutOptional的数据类型和shape相同,shape为[A1,...,Ai,1,...,1],Ai后共有j个1,与需要norm的轴长度保持相同。支持非连续的Tensor,数据格式支持ND。
workspaceSize(uint64_t*, 出参):返回需要在Device侧申请的workspace大小。
executor(aclOpExecutor**, 出参):返回op执行器,包含了算子计算流程。
返回值:
aclnnStatus:返回状态码,具体参见aclnn返回码。
161001 (ACLNN_ERR_PARAM_NULLPTR): 1. 传入的input、normalizedShape或out为空指针。
161002 (ACLNN_ERR_PARAM_INVALID): 1. input、normalizedShape、weightOptional(非空时)、biasOptional(非空时)、out、meanOutOptional(非空时)、rstdOutOptional(非空时),shape的维度超过8维。
2. input、weightOptional(非空时)、biasOptional(非空时)、out、meanOutOptional(非空时)、rstdOutOptional(非空时),数据类型不在支持的范围内。
3. normalizedShape维度小于1维。
4. weightOptional非空且shape与normalizedShape不相等。
5. biasOptional非空且shape与normalizedShape不相等。
6. input的维度小于normalizedShape的维度。
7. input的shape与normalizedShape右对齐时对应维度shape不相等。
aclnnLayerNorm
参数说明:
workspace(void*, 入参):在Device侧申请的workspace内存地址。
workspaceSize(uint64_t, 入参):在Device侧申请的workspace大小,由第一段接口aclnnLayerNormGetWorkspaceSize获取。
executor(aclOpExecutor*, 入参):op执行器,包含了算子计算流程。
stream(aclrtStream, 入参):指定执行任务的AscendCL Stream流。
返回值:
aclnnStatus:返回状态码,具体参见aclnn返回码。
aclnnLayerNormWithImplModeGetWorkspaceSize
参数说明:
input(aclTensor*,计算输入):公式中的输入
x
,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持)。shape为[A1,...,Ai,R1,...,Rj],其中A1至Ai表示无需norm的维度,R1至Rj表示需norm的维度。支持非连续的Tensor,数据格式支持ND。normalizedShape(aclIntArray*,计算输入):表示需要进行norm计算的维度,数据类型支持INT64,shape为[R1,...,Rj], 长度小于等于输入input的长度,不支持为空。
weightOptional(aclTensor*,计算输入):公式中的输入
w
,可选参数。weightOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持),数据类型与输入input一致或为FLOAT类型,且当biasOptional存在时与biasOptional的数据类型相同。shape与normalizedShape相等,为[R1,...,Rj]。支持非连续的Tensor,数据格式支持ND。weightOptional为空时,接口内部会构造一个shape为[R1,...,Rj],数据全为1的tensor,当biasOptional存在时与biasOptional的数据类型相同,biasOptional不存在时与输入input的数据类型相同。biasOptional(aclTensor*,计算输入):公式中的输入
b
,可选参数。biasOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持),数据类型与输入input一致或为FLOAT类型,且当weightOptional存在时与weightOptional的数据类型相同。shape与normalizedShape相等,为[R1,...,Rj]。支持非连续的Tensor,数据格式支持ND。biasOptional为空时,接口内部会构造一个shape为[R1,...,Rj],数据全为0的tensor,当weightOptional存在时与weightOptional的数据类型相同,weightOptional不存在时与输入input的数据类型相同。eps(double,计算输入):公式中的输入
eps
,用于规避除零计算,数据类型支持DOUBLE, 且需要可转换成与input相同的数据类型。out(aclTensor*,计算输出):公式中的输出
out
,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持)。shape需要与input的shape相等,为[A1,...,Ai,R1,...,Rj]。支持非连续的Tensor,数据格式支持ND。meanOutOptional(aclTensor*,计算输出):可选参数,meanOutOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持)。当rstdOutOptional存在时与rstdOutOptional的数据类型和shape相同,shape为[A1,...,Ai,1,...,1],Ai后共有j个1,与需要norm的轴长度保持相同。支持非连续的Tensor,数据格式支持ND。
rstdOutOptional(aclTensor*,计算输出):可选参数,rstdOutOptional非空时,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品、Atlas 200/500 A2推理产品支持)。当meanOutOptional存在时与meanOutOptional的数据类型和shape相同,shape为[A1,...,Ai,1,...,1],Ai后共有j个1,与需要norm的轴长度保持相同。支持非连续的Tensor,数据格式支持ND。
implMode(int32_t, 计算输入):精度模式,用于指定kernel选择对应的计算模式,默认实现为高精度模式(0), 高性能模式分为高性能模式(1)/保持FLOAT16计算模式(2),高性能模式谨慎使用,有精度损失,保持FLOAT16计算模式仅支持所有输入同时为FLOAT16,且计算精度最低。
workspaceSize(uint64_t*, 出参):返回需要在Device侧申请的workspace大小。
executor(aclOpExecutor**, 出参):返回op执行器,包含了算子计算流程。
返回值:
aclnnStatus:返回状态码,具体参见aclnn返回码。
161001 (ACLNN_ERR_PARAM_NULLPTR): 1. 传入的input、normalizedShape或out为空指针。
161002 (ACLNN_ERR_PARAM_INVALID): 1. input、normalizedShape、weightOptional非空、biasOptional非空、out、meanOutOptional非空或rstdOutOptional非空时的shape超过8维。
2. input、weightOptional非空、biasOptional非空、out、meanOut非空或rstdOut非空时的数据类型不在支持的范围内。
3. normalizedShape维度小于1维。
4. weightOptional非空且shape与normalizedShape不相等。
5. biasOptional非空且shape与normalizedShape不相等。
6. input的维度小于normalizedShape的维度。
7. input的shape与normalizedShape右对齐时对应维度shape不相等。
8. implMode的取值不在0,1和2范围内。
9. implMode的取值为2且输入的数据类型不全部为FLOAT16。
aclnnLayerNormWithImplMode
参数说明:
workspace(void*, 入参):在Device侧申请的workspace内存地址。
workspaceSize(uint64_t, 入参):在Device侧申请的workspace大小,由第一段接口aclnnLayerNormWithImplModeGetWorkspaceSize获取。
executor(aclOpExecutor*, 入参):op执行器,包含了算子计算流程。
stream(aclrtStream, 入参):指定执行任务的AscendCL Stream流。
返回值:
aclnnStatus:返回状态码,具体参见aclnn返回码。
约束与限制
无
调用示例
示例代码如下,仅供参考,具体编译和执行过程请参考编译与运行样例。
aclnnLayerNorm示例:
#include <iostream> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_layer_norm.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) { // 固定写法,AscendCL初始化 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 CreateAclTensorMem(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr) { auto size = GetShapeSize(shape) * sizeof(T); 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); 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); return 0; } template <typename T> void aclCreateTensorP(const std::vector<T>& shape, void** deviceAddr, aclDataType dataType, aclTensor** 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]; } *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); } template <typename T> void aclCreateIntArrayP(const std::vector<T>& hostData, aclIntArray** intArray) { *intArray = aclCreateIntArray(hostData.data(), hostData.size()); } int main() { // 1.(固定写法)device/stream初始化,参考AscendCL对外接口列表 int32_t 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); // 2. 构造输入与输出,需要根据API的接口自定义构造 std::vector<int64_t> xShape = {1, 2, 32}; std::vector<int64_t> normShape = {32}; std::vector<int64_t> meanShape = {1, 2, 1}; void* xDeviceAddr = nullptr; void* weightDeviceAddr = nullptr; void* biasDeviceAddr = nullptr; void* outDeviceAddr = nullptr; void* meanDeviceAddr = nullptr; void* rstdDeviceAddr = nullptr; aclTensor* x = nullptr; aclIntArray* norm = nullptr; aclTensor* weight = nullptr; aclTensor* bias = nullptr; aclTensor* out = nullptr; aclTensor* mean = nullptr; aclTensor* rstd = nullptr; std::vector<float> xHostData(64, 2.0); std::vector<int64_t> normData = {32}; std::vector<float> weightHostData(32, 1.0); std::vector<float> biasHostData(32, 0.0); std::vector<float> outHostData(64, 0.0); std::vector<float> meanHostData(2, 0.0); std::vector<float> rstdHostData(2, 0.0); double eps = 1e-5; ret = CreateAclTensorMem(xHostData, xShape, &xDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensorMem(weightHostData, normShape, &weightDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensorMem(biasHostData, normShape, &biasDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensorMem(outHostData, xShape, &outDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensorMem(meanHostData, meanShape, &meanDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensorMem(rstdHostData, meanShape, &rstdDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); aclCreateTensorP(xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x); aclCreateIntArrayP(normData, &norm); aclCreateTensorP(normShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight); aclCreateTensorP(normShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias); aclCreateTensorP(xShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); aclCreateTensorP(meanShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean); aclCreateTensorP(meanShape, &rstdDeviceAddr, aclDataType::ACL_FLOAT, &rstd); // 3. 调用CANN算子库API,需要修改为具体的Api名称 uint64_t workspaceSize = 0; aclOpExecutor* executor; // 调用aclnnLayerNorm第一段接口 ret = aclnnLayerNormGetWorkspaceSize(x, norm, weight, bias, eps, out, mean, rstd, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLayerNormGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); 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); } // 调用aclnnLayerNorm第二段接口 ret = aclnnLayerNorm(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLayerNorm failed. ERROR: %d\n", ret); return ret); // 4.(固定写法)同步等待任务执行结束 ret = aclrtSynchronizeStream(stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 auto size = GetShapeSize(xShape); 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 first result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("out result[%ld] is: %f\n", i, resultData[i]); } auto size1 = GetShapeSize(meanShape); std::vector<float> resultData1(size1, 0); ret = aclrtMemcpy(resultData1.data(), resultData1.size() * sizeof(resultData1[0]), meanDeviceAddr, size1 * sizeof(resultData1[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy second result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size1; i++) { LOG_PRINT("mean result[%ld] is: %f\n", i, resultData1[i]); } auto size2 = GetShapeSize(meanShape); std::vector<float> resultData2(size2, 0); ret = aclrtMemcpy(resultData2.data(), resultData2.size() * sizeof(resultData2[0]), rstdDeviceAddr, size2 * sizeof(resultData2[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy last result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size2; i++) { LOG_PRINT("rstd result[%ld] is: %f\n", i, resultData2[i]); } // 6. 释放aclTensor和aclIntArray,需要根据具体API的接口定义修改 aclDestroyTensor(x); aclDestroyIntArray(norm); aclDestroyTensor(weight); aclDestroyTensor(bias); aclDestroyTensor(out); aclDestroyTensor(mean); aclDestroyTensor(rstd); // 7. 释放device资源 aclrtFree(xDeviceAddr); aclrtFree(weightDeviceAddr); aclrtFree(biasDeviceAddr); aclrtFree(outDeviceAddr); aclrtFree(meanDeviceAddr); aclrtFree(rstdDeviceAddr); if (workspaceSize > 0) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }
aclnnLayerNormWithImplMode示例:
#include <iostream> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_layer_norm.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) { // 固定写法,AscendCL初始化 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 CreateAclTensorMem(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr) { 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); return 0; } template <typename T> void aclCreateTensorP(const std::vector<T>& shape, void** deviceAddr, aclDataType dataType, aclTensor** tensor) { // 计算连续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); } template <typename T> void aclCreateIntArrayP(const std::vector<T>& hostData, aclIntArray** intArray) { // 调用接口创建aclIntArray *intArray = aclCreateIntArray(hostData.data(), hostData.size()); } int main() { // 1.(固定写法)device/stream初始化,参考AscendCL对外接口列表 // 根据自己的实际device填写deviceId int32_t 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); // 2. 构造输入与输出,需要根据API的接口自定义构造 std::vector<int64_t> xShape = {1, 2, 32}; std::vector<int64_t> normShape = {32}; std::vector<int64_t> meanShape = {1, 2, 1}; void* xDeviceAddr = nullptr; void* weightDeviceAddr = nullptr; void* biasDeviceAddr = nullptr; void* outDeviceAddr = nullptr; void* meanDeviceAddr = nullptr; void* rstdDeviceAddr = nullptr; aclTensor* x = nullptr; aclIntArray* norm = nullptr; aclTensor* weight = nullptr; aclTensor* bias = nullptr; aclTensor* out = nullptr; aclTensor* mean = nullptr; aclTensor* rstd = nullptr; std::vector<float> xHostData(64, 2.0); std::vector<int64_t> normData = {32}; std::vector<float> weightHostData(32, 1.0); std::vector<float> biasHostData(32, 0.0); std::vector<float> outHostData(64, 0.0); std::vector<float> meanHostData(2, 0.0); std::vector<float> rstdHostData(2, 0.0); double eps = 1e-5; int32_t implMode = 0; // 创建x aclTensor ret = CreateAclTensorMem(xHostData, xShape, &xDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); // 创建weight aclTensor ret = CreateAclTensorMem(weightHostData, normShape, &weightDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); // 创建bias aclTensor ret = CreateAclTensorMem(biasHostData, normShape, &biasDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); // 创建out aclTensor ret = CreateAclTensorMem(outHostData, xShape, &outDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); // 创建mean aclTensor ret = CreateAclTensorMem(meanHostData, meanShape, &meanDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); // 创建rstd aclTensor ret = CreateAclTensorMem(rstdHostData, meanShape, &rstdDeviceAddr); CHECK_RET(ret == ACL_SUCCESS, return ret); aclCreateTensorP(xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x); aclCreateIntArrayP(normData, &norm); aclCreateTensorP(normShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight); aclCreateTensorP(normShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias); aclCreateTensorP(xShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); aclCreateTensorP(meanShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean); aclCreateTensorP(meanShape, &rstdDeviceAddr, aclDataType::ACL_FLOAT, &rstd); // 3. 调用CANN算子库API,需要修改为具体的Api名称 uint64_t workspaceSize = 0; aclOpExecutor* executor; // 调用aclnnLayerNormWithImplMode第一段接口 ret = aclnnLayerNormWithImplModeGetWorkspaceSize(x, norm, weight, bias, eps, out, mean, rstd, implMode, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLayerNormWithImplModeGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); // 根据第一段接口计算出的workspaceSize申请device内存 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); } // 调用aclnnLayerNormWithImplMode第二段接口 ret = aclnnLayerNormWithImplMode(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLayerNormWithImplMode failed. ERROR: %d\n", ret); return ret); // 4.(固定写法)同步等待任务执行结束 ret = aclrtSynchronizeStream(stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); // 5. 获取输出的值,将device侧内存上结果拷贝至host侧,根据具体API修改 auto size = GetShapeSize(xShape); 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 first result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("out result[%ld] is: %f\n", i, resultData[i]); } auto size1 = GetShapeSize(meanShape); std::vector<float> resultData1(size1, 0); ret = aclrtMemcpy(resultData1.data(), resultData1.size() * sizeof(resultData1[0]), meanDeviceAddr, size1 * sizeof(resultData1[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy second result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size1; i++) { LOG_PRINT("mean result[%ld] is: %f\n", i, resultData1[i]); } auto size2 = GetShapeSize(meanShape); std::vector<float> resultData2(size2, 0); ret = aclrtMemcpy(resultData2.data(), resultData2.size() * sizeof(resultData2[0]), rstdDeviceAddr, size2 * sizeof(resultData2[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy last result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size2; i++) { LOG_PRINT("rstd result[%ld] is: %f\n", i, resultData2[i]); } // 6. 释放aclTensor和aclIntArray,需要根据具体API的接口定义修改 aclDestroyTensor(x); aclDestroyIntArray(norm); aclDestroyTensor(weight); aclDestroyTensor(bias); aclDestroyTensor(out); aclDestroyTensor(mean); aclDestroyTensor(rstd); // 7. 释放device资源 aclrtFree(xDeviceAddr); aclrtFree(weightDeviceAddr); aclrtFree(biasDeviceAddr); aclrtFree(outDeviceAddr); aclrtFree(meanDeviceAddr); aclrtFree(rstdDeviceAddr); if (workspaceSize > 0) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }