aclnnWeightQuantMatmulAllReduceAddRmsNorm&aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm
支持的产品型号
- Atlas A2训练系列产品/Atlas 800I A2推理产品。
说明: 使用该接口时,请确保驱动固件包和CANN包都为配套的8.0.RC2版本或者配套的更高版本,否则将会引发报错,比如BUS ERROR等。
接口原型
aclnnWeightQuantMatmulAllReduceAddRmsNorm和aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm实现相同的功能,使用区别如下,请根据自身实际场景选择合适的算子。
- aclnnWeightQuantMatmulAllReduceAddRmsNorm:需新建一个输出张量对象存储计算结果。
- aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm:无需新建输出张量对象,直接在输入张量residual的内存中存储计算结果。
每个算子分为两段式接口,必须先调用“aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize”或者“aclnnInplaceWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnWeightQuantMatmulAllReduceAddRmsNorm”或者“aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm”接口执行计算。
aclnnStatus aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize(const aclTensor *x1, const aclTensor *x2, const aclTensor *bias, const aclTensor *antiquantScale, const aclTensor *antiquantOffset, const aclTensor *residual, const aclTensor *gamma, double epsilon, const char* group, const char *reduceOp, int64_t commTurn, int64_t streamMode, int64_t antiquantGroupSize, const aclTensor *y, const aclTensor *normOut, uint64_t *workspaceSize, aclOpExecutor **executor)
aclnnStatus aclnnWeightQuantMatmulAllReduceAddRmsNorm(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, const aclrtStream stream)
aclnnStatus aclnnInplaceWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize(const aclTensor *x1, const aclTensor *x2, const aclTensor *bias, const aclTensor *antiquantScale, const aclTensor *antiquantOffset, const aclTensor *residual, const aclTensor *gamma, double epsilon, const char* group, const char *reduceOp, int64_t commTurn, int64_t streamMode, int64_t antiquantGroupSize, const aclTensor *normOut, uint64_t *workspaceSize, aclOpExecutor **executor)
aclnnStatus aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, const aclrtStream stream)
功能描述
- 算子功能:完成mm + all_reduce + add + rms_norm计算。
- 计算公式:
aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize
参数说明:
- x1(const aclTensor *, 计算输入):Device侧的aclTensor,mm左矩阵,维度可为2维或者3维。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND。
- x2(const aclTensor *, 计算输入):Device侧的2维aclTensor,mm右矩阵。数据类型支持:INT8、INT4。数据格式支持:ND。
- bias(const aclTensor *, 计算输入):bias。维度为1维。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND,可选,可为空。非空时shape和x2最后一维相等
- antiquantScale(aclTensor*, 计算输入):antiquantScale,对x2进行伪量化计算的scale参数。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND。per_tensor场景shape为[1],per_channel场景shape为[n] / [1,n],n为x2最后一维的大小。per_group场景shape为(ceil(k,antiquantGroupSize),n)。
- antiquantOffset(aclTensor*, 计算输入):antiquantOffset,对x2进行伪量化计算的offset参数。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND,可选,可为空。非空时shape,与antiquantScale一致。
- residual(aclTensor*,计算输入):AddRmsNorm融合算子的残差输入,Device侧的三维aclTensor,数据类型支持:FLOAT16、BFLOAT16。数据格式支持:ND。不支持非连续tensor。inplace接口将该接口作为y的输出地址。
- gamma(aclTensor*,计算输入):AddRmsNorm融合算子的RmsNorm计算输入,Device侧的一维aclTensor,数据类型支持:FLOAT16、BFLOAT16。数据格式支持:ND。不支持非连续tensor。
- epsilon(double,计算输入):Host侧双精度,用于防止除0错误。默认值为1e-06
- group(char*,计算输入):通信域名称。数据类型支持:string。通过Hccl提供的接口获取:extern HcclResult HcclGetCommName(HcclComm comm, char* commName); commName即为group。
- reduceOp(char*,计算输入):reduce操作类型。数据类型支持:String。当前版本仅支持输入"sum"。
- commTurn(int64_t,计算输入):Host侧的整型,通信数据切分数,即总数据量/单次通信量。数据类型支持:int64_t。当前版本仅支持输入0。
- streamMode(int64_t,计算输入):Host侧的整型,acl流模式的枚举,当前只支持枚举值1,类型支持:int64_t。
- antiquantGroupSize(int64_t,计算输入):伪量化per_group模式下,对x2进行反量化计算的groupSize输入。当不支持per_group时,传入0,支持时,传入值的范围为[32,min(k-1,INT_MAX)],且为32的倍数。k取值范围与matmul接口保持一致
- y(aclTensor*,计算输出):Device侧的aclTensor,mm + all_reduce + add的结果。数据类型支持FLOAT16、BFLOAT16, 且数据类型同residual输入。
- normOut(aclTensor*,计算输出):Device侧的aclTensor,mm + all_reduce + add + rms_norm的结果。数据类型支持FLOAT16、BFLOAT16, 且数据类型同residual输入。
- workspaceSize(uint64_t*,出参):返回需要在Device侧申请的workspace大小。
- executor(aclOpExecutor **,出参):返回op执行器,包含了算子计算流程。
返回值:
返回aclnnStatus状态码,具体参见aclnn返回码。
aclnnWeightQuantMatmulAllReduceAddRmsNorm
参数说明:
- workspace(void*,入参):在Device侧申请的workspace内存地址。
- workspaceSize(uint64_t,入参):在Device侧申请的workspace大小,由第一段接口aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize。
- executor(aclOpExecutor*,入参):op执行器,包含了算子计算流程。
- stream(aclrtStream,入参):指定执行任务的AscendCL stream流。
返回值:
返回aclnnStatus状态码,具体参见aclnn返回码。
aclnnInplaceWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize
参数说明:
- x1(const aclTensor *, 计算输入):Device侧的aclTensor,mm左矩阵,维度可为2维或者3维。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND。
- x2(const aclTensor *, 计算输入):Device侧的2维aclTensor,mm右矩阵。数据类型支持:INT8、INT4。数据格式支持:ND。
- bias(const aclTensor *, 计算输入):bias。维度为1维。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND,可选,可为空。非空时shape和x2最后一维相等
- antiquantScale(aclTensor*, 计算输入):antiquantScale,对x2进行伪量化计算的scale参数。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND。per_tensor场景shape为[1],per_channel场景shape为[n] / [1,n],n为x2最后一维的大小。per_group场景shape为(ceil(k,antiquantGroupSize),n)。
- antiquantOffset(aclTensor*, 计算输入):antiquantOffset,对x2进行伪量化计算的offset参数。数据类型支持:BFLOAT16,FLOAT16。数据格式支持:ND,可选,可为空。非空时shape,与antiquantScale一致。
- residual(aclTensor*,计算输入):Device侧的三维aclTensor,数据类型支持:FLOAT16、BFLOAT16。数据格式支持:ND。不支持非连续tensor。
- gamma(aclTensor*,计算输入):Device侧的一维aclTensor,数据类型支持:FLOAT16、BFLOAT16。数据格式支持:ND。不支持非连续tensor。
- epsilon(double,计算输入):Host侧双精度,用于防止除0错误。默认值为1e-06
- group(char*,计算输入):Host侧标识列组的字符串。通信域名称。数据类型支持:string。通过Hccl提供的接口获取:extern HcclResult HcclGetCommName(HcclComm comm, char* commName); commName即为group。
- reduceOp(char*,计算输入):reduce操作类型。数据类型支持:String。当前版本仅支持输入"sum"。
- commTurn(int64_t,计算输入):Host侧的整型,通信数据切分数,即总数据量/单次通信量。数据类型支持:int64_t。当前版本仅支持输入0。
- streamMode(int64_t,计算输入):Host侧的整型,acl流模式的枚举,当前只支持枚举值1,类型支持:int64_t。
- antiquantGroupSize(int64_t,计算输入):伪量化per_group模式下,对x2进行反量化计算的groupSize输入。当不支持per_group时,传入0,支持时,传入值的范围为[32,min(k-1,INT_MAX)],且为32的倍数。k取值范围与matmul接口保持一致
- normOut(aclTensor*,计算输出):Device侧的aclTensor,mm + all_reduce + add + rms_norm的结果。数据类型支持FLOAT16、BFLOAT16, 且数据类型同residual输入。
- workspaceSize(uint64_t*,出参):返回需要在Device侧申请的workspace大小。
- executor(aclOpExecutor **,出参):返回op执行器,包含了算子计算流程。
返回值:
返回aclnnStatus状态码,具体参见aclnn返回码。
aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm
参数说明:
- workspace(void*,入参):在Device侧申请的workspace内存地址。
- workspaceSize(uint64_t,入参):在Device侧申请的workspace大小,由第一段接口aclnnInplaceWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize。
- executor(aclOpExecutor*,入参):op执行器,包含了算子计算流程。
- stream(aclrtStream,入参):指定执行任务的AscendCL stream流。
返回值:
返回aclnnStatus状态码,具体参见aclnn返回码。
约束与限制
- 使用场景同融合算子aclnnWeightQuantMatmulAllReduce一致:增量场景不使能MC2,全量场景使能MC2
- 输入x1可为2维或者3维,其维度为(b, s, k)或者(s, k)。x2必须是2维,其维度为(k, n),轴满足mm算子入参要求,k轴相等,k、n的范围为[1, 65535]。bias若非空,bias为1维,其维度为(n)。
- 输入residual必须是3维,其维度为(b, s, n),当x1为两维时,residual的(b*s)等于x1的s。输入gamma必须是一维,其维度为(n)。
- antiquantScale满足per-tensor场景shape为[1],per-channel场景shape为[1,n][n],per-group场景shape为[ceil(k,antiquantGroupSize),n]。antiquantOffset若非空,shape与antiquantScale一致。
- 输出y和normOut的维度和数据类型同residual。bias若非空,shape大小与最后一维相等。
- x2的数据类型需为int8或者int4,x1、bias(若支持)、residual、gamma、y、normOut计算输入的数据类型要一致。
- 只支持x2矩阵转置/不转置,x1矩阵支持不转置场景。
- antiquantGroupSize取值满足取值范围[32, min(k-1, INT_MAX)]且为32倍数。
- epsilon取值满足取值范围(0,1)。
- Atlas A2训练系列产品/Atlas 800I A2推理产品支持1、2、4、8卡,并且仅支持hccs链路all mesh组网。
- Atlas A2训练系列产品/Atlas 800I A2推理产品支持(b*s)、n为0的空tensor,不支持k为0的空tensor。
调用示例
#include <iostream>
#include <vector>
#include <thread>
#include "aclnnop/aclnn_weight_quant_matmul_all_reduce_add_rms_norm.h"
#include "aclnnop/aclnn_inplace_weight_quant_matmul_all_reduce_add_rms_norm.h"
int ndev = 8;
#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;
}
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;
}
struct Args {
uint32_t rankId;
HcclComm hcclComm;
aclrtStream stream;
aclrtContext context;
};
int launchOneThreadweightQuantmatmulAllReduceAddRmsNorm(Args &args) {
int ret;
ret = aclrtSetCurrentContext(args.context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext failed. ERROR: %d\n", ret); return ret);
char hcom_name[128];
ret = HcclGetCommName(args.hcclComm, hcom_name);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclGetCommName failed. ret = %d \n", ret); return -1);
LOG_PRINT("[INFO] rank %d hcom: %s stream: %p, context : %p\n", args.rankId, hcom_name, args.stream,
args.context);
std::vector<int64_t> x1Shape = {32, 64};
std::vector<int64_t> x2Shape = {64, 128};
std::vector<int64_t> biasShape = {128};
std::vector<int64_t> antiquantScaleShape = {128};
std::vector<int64_t> antiquantOffsetShape = {128};
std::vector<int64_t> x3Shape = {32, 128};
std::vector<int64_t> residualShape = {1, 32, 128};
std::vector<int64_t> gammaShape = {128};
std::vector<int64_t> yShape = {1, 32, 128};
std::vector<int64_t> normOutShape = {1, 32, 128};
void *x1DeviceAddr = nullptr;
void *x2DeviceAddr = nullptr;
void *biasDeviceAddr = nullptr;
void *antiquantScaleDeviceAddr = nullptr;
void *antiquantOffsetDeviceAddr = nullptr;
void *x3DeviceAddr = nullptr;
void *residualDeviceAddr = nullptr;
void *gammaDeviceAddr = nullptr;
void *yDeviceAddr = nullptr;
void *normOutDeviceAddr = nullptr;
aclTensor *x1 = nullptr;
aclTensor *x2 = nullptr;
aclTensor *bias = nullptr;
aclTensor *antiquantScale = nullptr;
aclTensor *antiquantOffset = nullptr;
aclTensor *x3 = nullptr;
aclTensor *residual = nullptr;
aclTensor *gamma = nullptr;
aclTensor *y = nullptr;
aclTensor *normOut = nullptr;
int64_t commTurn = 0;
int64_t streamMode = 1;
double epsilon = 0.000001;
int64_t antiquantGroupSize = 0;
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
void *workspaceAddr = nullptr;
long long x1ShapeSize = GetShapeSize(x1Shape);
long long x2ShapeSize = GetShapeSize(x2Shape);
long long biasShapeSize = GetShapeSize(biasShape);
long long antiquantScaleShapeSize = GetShapeSize(antiquantScaleShape);
long long antiquantOffsetShapeSize = GetShapeSize(antiquantOffsetShape);
long long x3ShapeSize = GetShapeSize(x3Shape);
long long residualShapeSize = GetShapeSize(residualShape);
long long gammaShapeSize = GetShapeSize(gammaShape);
long long yShapeSize = GetShapeSize(yShape);
long long normOutShapeSize = GetShapeSize(normOutShape);
std::vector<int16_t> x1HostData(x1ShapeSize, 0);
std::vector<int8_t> x2HostData(x2ShapeSize, 0);
std::vector<int16_t> biasHostData(biasShapeSize, 0);
std::vector<int16_t> antiquantScaleHostData(antiquantScaleShapeSize, 0);
std::vector<int16_t> antiquantOffsetHostData(antiquantOffsetShapeSize, 0);
std::vector<int16_t> x3HostData(x3ShapeSize, 0);
std::vector<int16_t> reisudalHostData(residualShapeSize, 0);
std::vector<int16_t> gammaHostData(gammaShapeSize, 0);
std::vector<int16_t> yHostData(yShapeSize, 0);
std::vector<int16_t> normOutHostData(normOutShapeSize, 0);
// 创建 tensor
ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT16, &x1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT16, &bias);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(antiquantScaleHostData, antiquantScaleShape, &antiquantScaleDeviceAddr,
aclDataType::ACL_FLOAT16, &antiquantScale);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(antiquantOffsetHostData, antiquantOffsetShape, &antiquantOffsetDeviceAddr,
aclDataType::ACL_FLOAT16, &antiquantOffset);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(x3HostData, x3Shape, &x3DeviceAddr, aclDataType::ACL_FLOAT16, &x3);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(reisudalHostData, residualShape, &residualDeviceAddr, aclDataType::ACL_FLOAT16, &residual);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(gammaHostData, gammaShape, &gammaDeviceAddr, aclDataType::ACL_FLOAT16, &gamma);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(yHostData, yShape, &yDeviceAddr, aclDataType::ACL_FLOAT16, &y);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(normOutHostData, normOutShape, &normOutDeviceAddr, aclDataType::ACL_FLOAT16, &normOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 调用aclnnWeightQuantMatmulAllReduceAddRmsNorm示例
// 调用第一段接口
ret = aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize(x1, x2, bias, antiquantScale, antiquantOffset,
residual, gamma, epsilon, hcom_name,
"sum", commTurn, streamMode, antiquantGroupSize, y, normOut,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
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);
}
// 调用第二段接口
ret = aclnnWeightQuantMatmulAllReduceAddRmsNorm(workspaceAddr, workspaceSize, executor, args.stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnWeightQuantMatmulAllReduceAddRmsNorm failed. ERROR: %d\n", ret); return ret);
//(固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
LOG_PRINT("device%d aclnnWeightQuantMatmulAllReduceAddRmsNorm execute success \n", args.rankId);
// 释放device资源,需要根据具体API的接口定义修改
// 调用aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm示例
// 调用第一段接口
ret = aclnnInplaceWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize(x1, x2, bias, antiquantScale, antiquantOffset,
residual, gamma, epsilon, hcom_name,
"sum", commTurn, streamMode, antiquantGroupSize, normOut,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnWeightQuantMatmulAllReduceAddRmsNormGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
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);
}
// 调用第二段接口
ret = aclnnInplaceWeightQuantMatmulAllReduceAddRmsNorm(workspaceAddr, workspaceSize, executor, args.stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnWeightQuantMatmulAllReduceAddRmsNorm failed. ERROR: %d\n", ret); return ret);
//(固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
LOG_PRINT("device%d aclnnWeightQuantMatmulAllReduceAddRmsNorm execute success \n", args.rankId);
// 释放device资源,需要根据具体API的接口定义修改
if (x1 != nullptr) {
aclDestroyTensor(x1);
}
if (x2 != nullptr) {
aclDestroyTensor(x2);
}
if (bias != nullptr) {
aclDestroyTensor(bias);
}
if (antiquantScale != nullptr) {
aclDestroyTensor(antiquantScale);
}
if (antiquantOffset != nullptr) {
aclDestroyTensor(antiquantOffset);
}
if (x3 != nullptr) {
aclDestroyTensor(x3);
}
if (residual != nullptr) {
aclDestroyTensor(residual);
}
if (gamma != nullptr) {
aclDestroyTensor(gamma);
}
if (y != nullptr) {
aclDestroyTensor(y);
}
if (normOut != nullptr) {
aclDestroyTensor(normOut);
}
if (x1DeviceAddr != nullptr) {
aclrtFree(x1DeviceAddr);
}
if (x2DeviceAddr != nullptr) {
aclrtFree(x2DeviceAddr);
}
if (biasDeviceAddr != nullptr) {
aclrtFree(biasDeviceAddr);
}
if (antiquantScaleDeviceAddr != nullptr) {
aclrtFree(antiquantScaleDeviceAddr);
}
if (antiquantOffsetDeviceAddr != nullptr) {
aclrtFree(antiquantOffsetDeviceAddr);
}
if (x3DeviceAddr != nullptr) {
aclrtFree(x3DeviceAddr);
}
if (residualDeviceAddr != nullptr) {
aclrtFree(residualDeviceAddr);
}
if (gammaDeviceAddr != nullptr) {
aclrtFree(gammaDeviceAddr);
}
if (yDeviceAddr != nullptr) {
aclrtFree(yDeviceAddr);
}
if (normOutDeviceAddr != nullptr) {
aclrtFree(normOutDeviceAddr);
}
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(args.stream);
HcclCommDestroy(args.hcclComm);
aclrtDestroyContext(args.context);
aclrtResetDevice(args.rankId);
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int32_t devices[ndev];
for (int i = 0; i < ndev; i++) {
devices[i] = i;
}
HcclComm comms[128];
ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
// 初始化集合通信域
for (int i = 0; i < ndev; i++) {
ret = aclrtSetDevice(devices[i]);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
}
ret = HcclCommInitAll(ndev, devices, comms);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("HcclCommInitAll failed. ERROR: %d\n", ret); return ret);
Args args[ndev];
aclrtStream stream[ndev];
aclrtContext context[ndev];
for (uint32_t rankId = 0; rankId < ndev; rankId++) {
ret = aclrtSetDevice(rankId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateContext(&context[rankId], rankId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(&stream[rankId]);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
}
// 启动多线程
std::vector<std::unique_ptr<std::thread>> threads(ndev);
for (uint32_t rankId = 0; rankId < ndev; rankId++) {
args[rankId].rankId = rankId;
args[rankId].hcclComm = comms[rankId];
args[rankId].stream = stream[rankId];
args[rankId].context = context[rankId];
threads[rankId].reset(
new(std::nothrow) std::thread(&launchOneThreadweightQuantmatmulAllReduceAddRmsNorm, std::ref(args[rankId])));
}
for (uint32_t rankId = 0; rankId < ndev; rankId++) {
threads[rankId]->join();
}
aclFinalize();
return 0;
}