aclnnQuantMatmulAllReduceV3
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
NOTE: When using this API, ensure that the driver firmware package and the CANN package are both of the matching 8.0.RC2 version or a matching later version. Otherwise, errors such as BUS ERROR will occur.
Function
Description: The
aclnnQuantMatmulAllReduceV3API is a functional extension of theaclnnQuantMatmulAllReduceV2API and adds support for low-bit communication: the MatMul computation result sequentially undergoes AllToAll communication, ReduceSum computation, AllGather communication, and dequantization, replacing the original flow of first performing dequantization and pertoken computation and then AllReduce communication. Supports pertensor, perchannel, and pertoken quantization modes.Formula:
into the following three scenarios:
Scenario 1: After performing MatMul on the quantized inputs x1 and x2, dequantization is performed, followed by an Add operation with x3, and finally an AllReduce computation.
$$ output= AllReduce(dequantScale*(x1_{int8}@x2_{int8} + bias_{int32}) + x3) $$Scenario 2: After performing MatMul on the quantized inputs x1 and x2, dequantization and per-token computation are performed, followed by an Add operation with x3, and finally an AllReduce computation.
$$ output= AllReduce(dequantScale * pertokenScaleOptional * (x1_{int8}@x2_{int8} + biasOptional_{int32}) + x3Optional) $$Scenario 3: Perform MatMul, dequant, and pertoken computation on the quantized inputs x1 and x2, then perform an Add operation with x3, apply perchannel quantization to the output, then perform AllToAll communication, perform ReduceSum computation on the first communication result, then perform AllGather communication, and finally dequant the second communication result to obtain the final output.
$$ matmulAddOutPut = (dequantScale * pertokenScaleOptional * (x1_{int8}@x2_{int8} + biasOptional_{int32}) + x3Optional); $$$$ alltoallOutPut_{int8} = AllToAll(matmulAddOutPut / commQuantScale1Optional); $$$$ reduceSumOutPut_{int8} = (add(alltoallOutPut_{int8}) * (commQuantScale1Optional / commQuantScale2Optional)); $$$$ outPut = (AllGather(reduceSumOutPut_{int8}) * commQuantScale2Optional); $$
Function Prototype
Each operator uses a two-phase API. Call aclnnQuantMatmulAllReduceV3GetWorkspaceSize first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call aclnnQuantMatmulAllReduceV3 to perform the computation.
aclnnStatus aclnnQuantMatmulAllReduceV3GetWorkspaceSize(
const aclTensor *x1,
const aclTensor *x2,
const aclTensor *biasOptional,
const aclTensor *x3Optional,
const aclTensor *dequantScale,
const aclTensor *pertokenScaleOptional,
const aclTensor *commQuantScale1Optional,
const aclTensor *commQuantScale2Optional,
const char *group,
const char *reduceOp,
int64_t commTurn,
int64_t streamMode,
const aclTensor *output,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnQuantMatmulAllReduceV3(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnQuantMatmulAllReduceV3GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor x1 Input Left matrix of the MatMul computation, that is, x1 in the formula. - The current version supports only 2D or 3D inputs.
- Non-transpose use cases are supported.
INT8 ND 2-3 × x2 Input Right matrix of the MatMul computation, that is, x2 in the formula. - Supports transpose/non-transpose use cases.
- In ND format, supports non-contiguous tensors with the last two axes transposed. Other non-contiguous tensors are not supported.
INT8 ND, FRACTAL_NZ 2 × biasOptional Input Bias offset, i.e., biasOptional in the formula. The current version supports only one-dimensional input. INT32 ND 1 √ x3Optional Input Add computation after MatMul computation, that is, x3Optional in the formula. Requires the data type and dimensions to be consistent with output. FLOAT16, BFLOAT16 ND 2 √ dequantScale Input Dequantization scale after MatMul computation, that is, dequantScale in the computation formula. - shape is (1) for the pertensor use case and (n)/(1, n) for the perchannel use case
- If the output is BFLOAT16, directly pass the dequantScale of BFLOAT16 type to this API.
- If the output is FLOAT16 and pertokenScale is not empty, directly pass the dequantScale of FLOAT32 type to this API. If pertokenScale is empty, call the aclnn API of the TransQuantParamV2 operator in advance to convert dequantScale to the INT64/UINT64 data type.
INT64, UINT64, FLOAT32, BFLOAT16 ND 2 √ pertokenScaleOptional Input Pertoken dequantization scale after MatMul computation, that is, pertokenScaleOptional in the formula. If x1 is (b, s, k), the shape is (b*s); if x1 is (m, k), the shape is (m). FLOAT32 ND 2 √ commQuantScale1Optional Input Perchannel quantization scale after matmulAdd computation, that is, commQuantScale1Optional in the formula. If x2 is (k, n), the shape can be (n) or (1,n). BFLOAT16, FLOAT16 ND 2 √ commQuantScale2Optional Input Perchannel quantization scale after allGather computation, that is, commQuantScale2Optional in the formula. If x2 is (k, n), the shape can be (n) or (1,n). BFLOAT16, FLOAT16 ND 2 √ group Input Communicator name. Obtained through the Hccl API "extern HcclResult HcclGetCommName(HcclComm comm, char* commName);", where commName is the group. String - - - reduceOp Input reduce operation type. Currently supports only "sum". String - - - commTurn Input Number of communication data splits, that is, total data volume/single communication volume. The current version supports only input 0. INT64 - - - streamMode Input Enumeration of stream modes. Currently supports only enumeration value 1. INT64 - - - Output Output Result of the MatMul computation and AllReduce communication, that is, output in the formula. The dimensions of output are consistent with those of x1. FLOAT16, BFLOAT16 ND 2-3 √ workspaceSize Output Returns the workspace size to be requested on the Device. - - - - - executor Output Returns the op executor containing the computation flow of the operator. - - - - - Atlas A2 products :- The data format of input x2 supports ND and
FRACTAL_NZformats. The shape rules for the input are as follows:- When the data format of x2 is
FRACTAL_NZ, the current version only supports four-dimensional input. UseaclnnCalculateMatmulWeightSizeV2andaclnnTransMatmulWeightto complete the conversion from ND to NZ. Non-contiguous tensors only support the transpose scenario. - When the data format of x2 is ND, the current version only supports two-dimensional input.
- When the data format of x2 is
- The data format of input x2 supports ND and
Ascend 950PR&950DT products :- The data format of input x2 only supports the ND format (the current version only supports two-dimensional input).
Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 x1, x2, dequantScale, or output is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of x1, x2, biasOptional, dequantScale, pertokenScaleOptional, x3Optional, commQuantScale1Optional, commQuantScale2Optional, or output is not in the supported range. streamMode is not within the valid range. The shape of x1, x2, biasOptional, dequantScale, pertokenScaleOptional, x3Optional, commQuantScale1Optional, commQuantScale2Optional, or output does not comply with the constraints.
aclnnQuantMatmulAllReduceV3
Parameters
Parameter Input/Output Description workspace Input Workspace memory address requested on the device. workspaceSize Input Workspace size requested on the device, obtained by calling the first-phase API aclnnQuantMatmulAllReduceV3GetWorkspaceSize.executor Input op executor, which contains the computation flow of the operator. stream Input Stream for executing the task. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
Atlas A2 products :aclnnQuantMatmulAllReduceV3defaults to a non-deterministic implementation. To enable deterministic computation, set theHCCL_DETERMINISTICenvironment variable to true.
- Ascend 950PR&950DT products:
aclnnQuantMatmulAllReduceV3defaults to a deterministic implementation.
MC2 is disabled in incremental scenarios and enabled in full scenarios.
The input x1 can be 2D or 3D and must not be an empty tensor, with shape (b, s, k) or (m, k). x2 must be 2D and must not be an empty tensor, with shape (k, n). The k axes must satisfy the input requirements of the mm operator, meaning the k axes must be equal.
The size of m must not exceed 2147483647. The last dimension size of x1 and x2 must not exceed 65535. The last dimension of x1 refers to k, and the last dimension of x2 refers to k when transposed or n when not transposed.
The passed x1, x2, dequantScale, or output must not be null pointers.
The data types and data formats of x1, x2, dequantScale, output, biasOptional (when not null), and x3 (when not null) must fall within the supported ranges.
If the output type is FLOAT16: when pertokenScale is null, the type of dequantScale is INT64 or UINT64; when pertokenScale is not null, the type of dequantScale is FLOAT32. If the output type is BFLOAT16, the type of dequantScale is BFLOAT16 and the type of x3 is BFLOAT16.
The passed commQuantScale1 and commQuantScale2 must be either both null pointers or both non-null pointers. If both commQuantScale1 and commQuantScale2 are non-null pointers, the shapes of the two quantization parameters must be consistent, their types must be consistent with the operator output type, and the inputs on each card must be consistent.
When the shape of x1 is (b, s, k), the shape of pertokenScaleOptional is (b*s); when the shape of x1 is (m, k), the shape of pertokenScaleOptional is (m).
Only HCCS all-mesh networking is supported.
Atlas A2 products : supports 1, 2, 4, and 8 devices.
Ascend 950PR&950DT products : supports 1, 2, 4, 8, 16, 32, and 64 devices.
Atlas A2 products : MC2 operators (communication-computation fusion) in a model support only the same communicator.
- int8 low-bit communication provides performance gains only in communication-bound cases. It is not recommended in compute-bound cases, that is, do not pass in commQuantScale1 and commQuantScale2.
- Null tensor support:
- Empty tensors are not supported.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
NOTE: This sample code calls some HCCL collective communication library APIs: HcclGetCommName, HcclCommInitAll, and HcclCommDestroy. For details, see <<HCCL API (C)>>.
Atlas A2 products andAscend 950PR&950DT products :#include <iostream> #include <vector> #include <thread> #include "hccl/hccl.h" #include "aclnn/opdev/fp16_t.h" #include "aclnnop/aclnn_trans_matmul_weight.h" #include "aclnnop/aclnn_quant_matmul_all_reduce_v3.h" int ndev = 2; #define ACL_CHECK(ret) \ do { \ auto retcode = ret; \ if (retcode != ACL_SUCCESS) { \ printf("[ERROR] acl interface return err %s:%d, retcode: %d \n", __FILE__, __LINE__, retcode); \ return retcode; \ } \ } while (0) #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; } struct Args { uint32_t rankId; HcclComm hcclComm; aclrtStream stream; aclrtContext context; std::string format; }; template<typename T> int CreateWeightNzAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr, aclDataType dataType, aclTensor **tensor, Args &args) { auto size = GetShapeSize(shape) * sizeof(T); const aclIntArray *mat2Size = aclCreateIntArray(shape.data(), shape.size()); auto ret = aclnnCalculateMatmulWeightSizeV2(mat2Size, ACL_INT8, &size); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCalculateMatmulWeightSizeV2 failed. ERROR: %d\n", ret); return ret); // Call aclrtMalloc to allocate device memory. 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 data from the host to device memory. 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 aclTensor. *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); uint64_t transWorkspaceSize; aclOpExecutor *executor; void *transWorkspaceAddr = nullptr; ret = aclnnTransMatmulWeightGetWorkspaceSize(*tensor, &transWorkspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS && transWorkspaceSize > 0, printf("[ERROR] aclnnTransMatmulWeightGetWorkspaceSize failed. ret = %d \n", ret); return ret); ACL_CHECK(aclrtMalloc(&transWorkspaceAddr, transWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); ret = aclnnTransMatmulWeight(transWorkspaceAddr, transWorkspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, printf("[ERROR] aclnnTransMatmulWeight failed. ret = %d \n", ret);return ret); ACL_CHECK(aclrtSynchronizeStreamWithTimeout(args.stream, 20000)); 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 device memory. 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 host-side data to the device-side memory. 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 aclTensor. *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); return 0; } int launchOneThreadQuantMatmulAllReduce(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> dequantScaleShape = {128}; std::vector<int64_t> pertokenScaleShape = {32}; std::vector<int64_t> commQuantScale1Shape = {128}; std::vector<int64_t> commQuantScale2Shape = {128}; std::vector<int64_t> x3Shape = {32, 128}; std::vector<int64_t> outShape = {32, 128}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *dequantScaleDeviceAddr = nullptr; void *pertokenScaleDeviceAddr = nullptr; void *commQuantScale1DeviceAddr = nullptr; void *commQuantScale2DeviceAddr = nullptr; void *x3DeviceAddr = nullptr; void *outDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *dequantScale = nullptr; aclTensor *pertokenScale = nullptr; aclTensor *commQuantScale1 = nullptr; aclTensor *commQuantScale2 = nullptr; aclTensor *x3 = nullptr; aclTensor *out = nullptr; int64_t commTurn = 0; int64_t streamMode = 1; 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 dequantScaleShapeSize = GetShapeSize(dequantScaleShape); long long pertokenScaleShapeSize = GetShapeSize(pertokenScaleShape); long long commQuantScale1ShapeSize = GetShapeSize(commQuantScale1Shape); long long commQuantScale2ShapeSize = GetShapeSize(commQuantScale2Shape); long long x3ShapeSize = GetShapeSize(x3Shape); long long outShapeSize = GetShapeSize(outShape); std::vector<int8_t> x1HostData(x1ShapeSize, 1); std::vector<int8_t> x2HostData(x2ShapeSize, 1); std::vector<int32_t> biasHostData(biasShapeSize, 1); std::vector<float> dequantScaleHostData(dequantScaleShapeSize, 1); std::vector<float> pertokenScaleHostData(pertokenScaleShapeSize, 1); std::vector<op::fp16_t> commQuantScale1HostData(commQuantScale1ShapeSize, 1); std::vector<op::fp16_t> commQuantScale2HostData(commQuantScale2ShapeSize, 1); std::vector<op::fp16_t> x3HostData(x3ShapeSize, 1); std::vector<op::fp16_t> outHostData(outShapeSize, 0); // Create a tensor. ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_INT8, &x1); CHECK_RET(ret == ACL_SUCCESS, return ret); if (args.format == "NZ") { ret = CreateWeightNzAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2, args); } else { ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2); } CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_INT32, &bias); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(dequantScaleHostData, dequantScaleShape, &dequantScaleDeviceAddr, aclDataType::ACL_FLOAT, &dequantScale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(pertokenScaleHostData, pertokenScaleShape, &pertokenScaleDeviceAddr, aclDataType::ACL_FLOAT, &pertokenScale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(commQuantScale1HostData, commQuantScale1Shape, &commQuantScale1DeviceAddr, aclDataType::ACL_FLOAT16, &commQuantScale1); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(commQuantScale2HostData, commQuantScale2Shape, &commQuantScale2DeviceAddr, aclDataType::ACL_FLOAT16, &commQuantScale2); 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(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out); CHECK_RET(ret == ACL_SUCCESS, return ret); // Call the first-phase API. ret = aclnnQuantMatmulAllReduceV3GetWorkspaceSize(x1, x2, bias, x3, dequantScale, pertokenScale, commQuantScale1, commQuantScale2, hcom_name, "sum", commTurn, streamMode, out, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAllReduceV3GetWorkspaceSize failed. ERROR: %d\n", ret); return ret); // Allocate device memory based on the workspaceSize computed by the first-phase API. 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); } // Call the second-phase API. ret = aclnnQuantMatmulAllReduceV3(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAllReduceV3 failed. ERROR: %d\n", ret); return ret); // (boilerplate) Synchronously wait for the task execution to complete. ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); LOG_PRINT("device%d aclnnQuantMatmulAllReduceV3 execute success \n", args.rankId); // Release device resources. Modify according to the API definition of the specific API. if (x1 != nullptr) { aclDestroyTensor(x1); } if (x2 != nullptr) { aclDestroyTensor(x2); } if (bias != nullptr) { aclDestroyTensor(bias); } if (dequantScale != nullptr) { aclDestroyTensor(dequantScale); } if (pertokenScale != nullptr) { aclDestroyTensor(pertokenScale); } if (commQuantScale1 != nullptr) { aclDestroyTensor(commQuantScale1); } if (commQuantScale2 != nullptr) { aclDestroyTensor(commQuantScale2); } if (x3 != nullptr) { aclDestroyTensor(x3); } if (out != nullptr) { aclDestroyTensor(out); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } if (dequantScaleDeviceAddr != nullptr) { aclrtFree(dequantScaleDeviceAddr); } if (pertokenScaleDeviceAddr != nullptr) { aclrtFree(pertokenScaleDeviceAddr); } if (commQuantScale1DeviceAddr != nullptr) { aclrtFree(commQuantScale1DeviceAddr); } if (commQuantScale2DeviceAddr != nullptr) { aclrtFree(commQuantScale2DeviceAddr); } if (x3DeviceAddr != nullptr) { aclrtFree(x3DeviceAddr); } if (outDeviceAddr != nullptr) { aclrtFree(outDeviceAddr); } 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); // Initialize the communicator. 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); } // Start multithreading. 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(&launchOneThreadQuantMatmulAllReduce, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < ndev; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }