aclnnBatchNormBackward
支持的产品型号
- Atlas 训练系列产品。
- Atlas A2训练系列产品/Atlas 800I A2推理产品。
接口原型
每个算子分为两段式接口,必须先调用“aclnnBatchNormBackwardGetWorkspaceSize”接口获取入参并根据流程计算所需workspace大小,再调用“aclnnBatchNormBackward”接口执行计算。
aclnnStatus aclnnBatchNormBackwardGetWorkspaceSize(const aclTensor *gradOut, const aclTensor *input, const aclTensor *weight, const aclTensor *runningMean, const aclTensor *runningVar, const aclTensor *saveMean, const aclTensor *saveInvstd, bool training, double eps, const aclBoolArray *outputMask, aclTensor *gradInput, aclTensor *gradWeight, aclTensor *gradBias, uint64_t *workspaceSize, aclOpExecutor **executor)
aclnnStatus aclnnBatchNormBackward(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)
功能描述
- 算子功能: 正则化反向计算。
- 计算公式:
aclnnBatchNormBackwardGetWorkspaceSize
参数说明:
- gradOut(const aclTensor *, 计算输入): 梯度Tensor,Device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持),支持非连续的Tensor,支持的shape和格式有:2维(对应的格式为NC),3维(对应的格式为NCL),4维(对应的格式为NCHW),5维(对应的格式为NCDHW),6-8维(对应的格式为ND,其中第2维固定为channel轴)。
- input(const aclTensor *, 计算输入): 正向的输入Tensor,Device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持),支持非连续的Tensor,支持的shape和格式有:2维(对应的格式为NC),3维(对应的格式为NCL),4维(对应的格式为NCHW),5维(对应的格式为NCDHW),6-8维(对应的格式为ND,其中第2维固定为channel轴)。
- weight(const aclTensor *, 计算输入): 权重Tensor,Device侧的aclTensor,数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- runningMean(const aclTensor *, 计算输入): 训练期间计算的平均值,Device侧的aclTensor数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- runningVar(const aclTensor *, 计算输入): 训练期间计算的方差,Device侧的aclTensor,数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- saveMean(const aclTensor *, 计算输入): 保存的均值,Device侧的aclTensor,数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- saveInvstd(const aclTensor *, 计算输入): 保存的标准差的倒数,Device侧的aclTensor,数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- training(bool, 计算输入): Host侧的bool值,标记是否训练场景,true表示训练场景,false表示推理场景。
- eps(double *, 计算输入): Host侧的double值。添加到方差中的值,以避免出现除以零的情况。
- outputMask(const aclBoolArray *, 计算输入): aclBoolArray类型,输出的掩码。
- gradInput(aclTensor *, 出参): 可选输出,若outputMask[0]为True,则需要输出,否则不输出;输入Tensor的梯度,Device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持),支持非连续的Tensor,支持的shape和格式有:2维(对应的格式为NC),3维(对应的格式为NCL),4维(对应的格式为NCHW),5维(对应的格式为NCDHW),6-8维(对应的格式为ND,其中第2维固定为channel轴)。
- gradWeight(aclTensor *, 出参): 可选输出,若outputMask[1]为True,则需要输出,否则不输出;缩放参数的梯度,Device侧的aclTensor,数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- gradBias(aclTensor *, 出参): 可选输出,若outputMask[2]为True,则需要输出,否则不输出;偏置参数的梯度,Device侧的aclTensor,数据类型仅支持FLOAT,支持非连续的Tensor, 数据格式为ND。shape为1维,长度与input入参中channel轴的长度相等。
- workspaceSize(uint64_t *, 出参): 返回需要在Device侧申请的workspace大小。
- executor(aclOpExecutor **, 出参): 返回op执行器,包含了算子计算流程。
返回值:
aclnnStatus: 返回状态码,具体参见aclnn返回码。
第一段接口完成入参校验,出现以下场景时报错:
ACLNN_ERR_PARAM_NULLPTR:1. 传入的指针类型入参是空指针。
ACLNN_ERR_PARAM_INVALID:1. input,gradOut,gradInput数据类型和数据格式不在支持的范围之内。
2. input,gradOut,gradInput数据的shape不在支持的范围内。
aclnnBatchNormBackward
参数说明:
- workspace(void *, 入参): 在Device侧申请的workspace内存地址。
- workspaceSize(uint64_t, 入参): 在Device侧申请的workspace大小,由第一段接口aclnnBatchNormBackwardGetWorkspaceSize获取。
- executor(aclOpExecutor *, 入参): op执行器,包含了算子计算流程。
- stream(aclrtStream, 入参): 指定执行任务的AscendCL Stream流。
返回值:
aclnnStatus: 返回状态码,具体参见aclnn返回码。
约束与限制
无
调用示例
示例代码如下,仅供参考,具体编译和执行过程请参考编译与运行样例。
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_batch_norm_backward.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 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() {
// 1. (固定写法)device/stream初始化,参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// check根据自己的需要处理
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. 构造输入与输出,需要根据API的接口自定义构造
std::vector<int64_t> gradOutShape = {1, 2, 4};
std::vector<int64_t> selfShape = {1, 2, 4};
std::vector<int64_t> weightShape = {2};
std::vector<int64_t> rMeanShape = {2};
std::vector<int64_t> rVarShape = {2};
std::vector<int64_t> sMeanShape = {2};
std::vector<int64_t> sVarShape = {2};
std::vector<int64_t> gradInShape = {1, 2, 4};
std::vector<int64_t> gradWeightShape = {2};
std::vector<int64_t> gradBiasShape = {2};
void* gradOutDeviceAddr = nullptr;
void* selfDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* rMeanDeviceAddr = nullptr;
void* rVarDeviceAddr = nullptr;
void* sMeanDeviceAddr = nullptr;
void* sVarDeviceAddr = nullptr;
void* outMaskDeviceAddr = nullptr;
void* gradInDeviceAddr = nullptr;
void* gradWeightDeviceAddr = nullptr;
void* gradBiasDeviceAddr = nullptr;
aclTensor* gradOut = nullptr;
aclTensor* self = nullptr;
aclTensor* weight = nullptr;
aclTensor* rMean = nullptr;
aclTensor* rVar = nullptr;
aclTensor* sMean = nullptr;
aclTensor* sVar = nullptr;
aclBoolArray* outMask = nullptr;
aclTensor* gradIn = nullptr;
aclTensor* gradWeight = nullptr;
aclTensor* gradBias = nullptr;
std::vector<float> gradOutHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> weightHostData = {1, 1};
std::vector<float> rMeanHostData = {0, 0};
std::vector<float> rVarHostData = {1, 1};
std::vector<float> sMeanHostData = {0, 0};
std::vector<float> sVarHostData = {1, 1};
std::vector<float> gradInHostData(8, 0);
std::vector<float> gradWeightHostData(2, 0);
std::vector<float> gradBiasHostData(2, 0);;
bool training = true;
double eps = 1e-5;
// 创建gradOut aclTensor
ret = CreateAclTensor(gradOutHostData, gradOutShape, &gradOutDeviceAddr, aclDataType::ACL_FLOAT, &gradOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建self aclTensor
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建weight aclTensor
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建rMean aclTensor
ret = CreateAclTensor(rMeanHostData, rMeanShape, &rMeanDeviceAddr, aclDataType::ACL_FLOAT, &rMean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建rVar aclTensor
ret = CreateAclTensor(rVarHostData, rVarShape, &rVarDeviceAddr, aclDataType::ACL_FLOAT, &rVar);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建sMean aclTensor
ret = CreateAclTensor(sMeanHostData, sMeanShape, &sMeanDeviceAddr, aclDataType::ACL_FLOAT, &sMean);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建sVar aclTensor
ret = CreateAclTensor(sVarHostData, sVarShape, &sVarDeviceAddr, aclDataType::ACL_FLOAT, &sVar);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建outMask aclBoolArray
bool maskData[2] = {true, true};
outMask = aclCreateBoolArray(&(maskData[0]), 2);
// 创建gradIn aclTensor
ret = CreateAclTensor(gradInHostData, gradInShape, &gradInDeviceAddr, aclDataType::ACL_FLOAT, &gradIn);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建gradWeight aclTensor
ret = CreateAclTensor(gradWeightHostData, gradWeightShape, &gradWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradWeight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建gradBias aclTensor
ret = CreateAclTensor(gradBiasHostData, gradBiasShape, &gradBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradBias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// aclnnBatchNormBackward接口调用示例
// 3. 调用CANN算子库API,需要修改为具体的API名称
// 调用aclnnBatchNormBackward第一段接口
ret = aclnnBatchNormBackwardGetWorkspaceSize(gradOut, self, weight, rMean, rVar, sMean, sVar, training, eps, outMask, gradIn, gradWeight, gradBias, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBatchNormBackwardGetWorkspaceSize 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);
}
// 调用aclnnBatchNormBackward第二段接口
ret = aclnnBatchNormBackward(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBatchNormBackward 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(gradInShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), gradInDeviceAddr,
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. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(gradOut);
aclDestroyTensor(self);
aclDestroyTensor(weight);
aclDestroyTensor(rMean);
aclDestroyTensor(rVar);
aclDestroyTensor(sMean);
aclDestroyTensor(sVar);
aclDestroyBoolArray(outMask);
aclDestroyTensor(gradIn);
aclDestroyTensor(gradWeight);
aclDestroyTensor(gradBias);
// 7. 释放device资源,需要根据具体API的接口定义修改
aclrtFree(gradOutDeviceAddr);
aclrtFree(selfDeviceAddr);
aclrtFree(weightDeviceAddr);
aclrtFree(rMeanDeviceAddr);
aclrtFree(rVarDeviceAddr);
aclrtFree(sMeanDeviceAddr);
aclrtFree(sVarDeviceAddr);
aclrtFree(outMaskDeviceAddr);
aclrtFree(gradInDeviceAddr);
aclrtFree(gradWeightDeviceAddr);
aclrtFree(gradBiasDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}