aclnnAllGatherMatmul
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : 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: Fuses all-gather communication with matrix multiplication computation.
- Formula:$$ output=AllGather(x1)@x2+bias $$$$ gatherOut=AllGather(x1) $$
Function Prototype
Each operator uses a two-phase API. Call the "aclnnAllGatherMatmulGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation flow, and then call the "aclnnAllGatherMatmul" API to perform the computation.
aclnnStatus aclnnAllGatherMatmulGetWorkspaceSize(
const aclTensor *x1,
const aclTensor *x2,
const aclTensor *bias,
const char *group,
int64_t gatherIndex,
int64_t commTurn,
int64_t streamMode,
const aclTensor *output,
const aclTensor *gatherOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAllGatherMatmul(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAllGatherMatmulGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-continuous Tensor x1 Input x1 in the Formula. - Supports empty tensors.
- Same data type as x2.
- The current version supports only 2D shape input and only the non-transpose use case.
FLOAT16, BFLOAT16 ND 2 × x2 Input x2 in the Formula. FLOAT16, BFLOAT16 ND 2 √ (transpose use case only) bias Input The bias in the formula. - Supports passing a null pointer.
- The current version supports only 1D inputs.
FLOAT16, BFLOAT16 ND 1 × group Input Communicator name. Obtained through the Hccl API "extern HcclResult HcclGetCommName(HcclComm comm, char* commName);", where commName is the group. - - - - gatherIndex Input Identifies the Gather target. - 0 indicates that the target is x1, and 1 indicates that the target is x2.
- In the current version, only 0 is supported.
- - - - commTurn Input Number of splits of the data to communicate, that is, total data volume/volume of a single communication. The current version supports only input 0. - - - - streamMode Input Enum of the stream mode. Currently, only enumeration value 1 is supported. - - - - Output Output Result of the AllGather communication and MatMul computation, that is, output in the formula. - Supports empty tensors.
- The data type must be the same as that of x1.
FLOAT16, BFLOAT16 ND 2 × gatherOut Output Outputs only the result of AllGather communication, that is, gatherOut in the formula. - Supports empty tensors.
- Data type must be the same as that of x1.
FLOAT16, BFLOAT16 ND 2 × workspaceSize Output Returns the size of the workspace to be requested on the Device. - - - - - executor Output Returns an op executor, which contains the computation flow of the operator. - - - - - Atlas A2 products ,Atlas A3 products :- bias: Currently does not support non-zero input scenarios.
Ascend 950PR&950DT products :- bias: Supports non-zero input scenarios.
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, or output is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of x1, x2, bias, or output is not supported. streamMode is out of the valid range.
aclnnAllGatherMatmul
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Size of the workspace allocated on the Device side, obtained by the first-phase API aclnnAllGatherMatmulGetWorkspaceSize.executor Input Op executor that contains the computation flow of the operator. stream Input Stream that executes the task. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnAllGatherMatmul defaults to a deterministic implementation.
The input x1 is 2-dimensional with shape (m, k). x2 must be 2-dimensional with shape (k, n), where the axes meet the input requirements of the MatMul operator, the k axes are equal, and the k axis value range is [256, 65535).
For the empty tensor scenario supported by x1/x2, m and n can be empty, k cannot be empty, and the following conditions must be met:
- m is null, k is not null, and n is not null.
- m is not null, k is not null, and n is null.
- m is null, k is not null, and n is null.
The output is a 2D tensor with shape (m*rank_size, n), where rank_size is the number of devices.
Atlas A2 products : support 2, 4, 8 devices, and only supports HCCS all-mesh networking.
Atlas A3 products : support 2, 4, 8, 16, 32 devices and only support HCCS double-ring networking.
Ascend 950PR&950DT products :- Supports 2, 4, 8, 16, 32, and 64 cards, and only supports HCCS link all mesh networking.
- The total data volume of the AllGather(x1) collective communication cannot exceed 63 * 256MB. The total communication data volume is calculated as: m * k * sizeof(x1_dtype) * number of devices. Due to different shapes, the internal implementation of the operator may vary, and the actually supported total communication data volume may be slightly smaller than this value.
Atlas A2 products : The communication-computation fusion MC2 operators in a model support only the same communicator.
Example
The following is sample code 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. See HCCL API (C).
Atlas A2 products ,Atlas A3 products , andAscend 950PR&950DT products :#include <thread> #include <iostream> #include <vector> #include <memory> #include <cstdio> #include "hccl/hccl.h" #include "aclnn/opdev/fp16_t.h" #include "aclnnop/aclnn_all_gather_matmul.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) constexpr int DEV_NUM = 2; int64_t GetShapeSize(const std::vector<int64_t> &shape) { int64_t shape_size = 1; for (auto i : shape) { shape_size *= i; } return shape_size; } 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); auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtMalloc failed. ret: %d\n", ret); return ret); ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtMemcpy failed. ret: %d\n", ret); return ret); 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); return 0; } struct Args { int rankId; HcclComm hcclComm; aclrtStream stream; aclrtContext context; }; int launchOneThread_AllGatherMm(Args &args) { int ret = aclrtSetCurrentContext(args.context); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSetCurrentContext failed. ret = %d \n", ret); return ret); char hcomName[128] = {0}; ret = HcclGetCommName(args.hcclComm, hcomName); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclGetCommName failed. ret: %d\n", ret); return -1); LOG_PRINT("[INFO] rank = %d, hcomName = %s, stream = %p\n", args.rankId, hcomName, args.stream); std::vector<int64_t> x1Shape = {128, 256}; std::vector<int64_t> x2Shape = {256, 512}; std::vector<int64_t> biasShape = {512}; std::vector<int64_t> outShape = {128 * DEV_NUM, 512}; std::vector<int64_t> gatherOutShape = {128 * DEV_NUM, 256}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *outDeviceAddr = nullptr; void *gatherOutDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *out = nullptr; aclTensor *gatherOut = nullptr; int64_t gatherIndex = 0; int64_t commTurn = 0; int64_t streamMode = 1; uint64_t workspaceSize = 0; aclOpExecutor *executor = nullptr; void *workspaceAddr = nullptr; long long x1ShapeSize = GetShapeSize(x1Shape); long long x2ShapeSize = GetShapeSize(x2Shape); long long biasShapeSize = GetShapeSize(biasShape); long long outShapeSize = GetShapeSize(outShape); long long gatherOutShapeSize = GetShapeSize(gatherOutShape); std::vector<op::fp16_t> x1HostData(x1ShapeSize, 0); std::vector<op::fp16_t> x2HostData(x2ShapeSize, 0); std::vector<op::fp16_t> biasHostData(biasShapeSize, 0); std::vector<op::fp16_t> outHostData(outShapeSize, 0); std::vector<op::fp16_t> gatherOutHostData(gatherOutShapeSize, 0); ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT16, &x1); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_FLOAT16, &x2); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gatherOutHostData, gatherOutShape, &gatherOutDeviceAddr, aclDataType::ACL_FLOAT16, &gatherOut); CHECK_RET(ret == ACL_SUCCESS, return ret); // Call the first-phase API. ret = aclnnAllGatherMatmulGetWorkspaceSize( x1, x2, bias, hcomName, gatherIndex, commTurn, streamMode, out, gatherOut, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnAllGatherMatmulGetWorkspaceSize failed. ret = %d \n", ret); return ret); // Allocate device memory based on the workspaceSize calculated by the first-phase API. if (workspaceSize > 0) { ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtMalloc workspace failed. ret = %d \n", ret); return ret); } // Call the second-phase API. ret = aclnnAllGatherMatmul(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnAllGatherMatmul failed. ret = %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("[ERROR] aclrtSynchronizeStreamWithTimeout failed. ret = %d \n", ret); return ret); LOG_PRINT("[INFO] device_%d aclnnAllGatherMatmul execute successfully.\n", args.rankId); // Release device resources. Modify this part according to the definition of the specific API. if (x1 != nullptr) { aclDestroyTensor(x1); } if (x2 != nullptr) { aclDestroyTensor(x2); } if (bias != nullptr) { aclDestroyTensor(bias); } if (out != nullptr) { aclDestroyTensor(out); } if (gatherOut != nullptr) { aclDestroyTensor(gatherOut); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } if (outDeviceAddr != nullptr) { aclrtFree(outDeviceAddr); } if (gatherOutDeviceAddr != nullptr) { aclrtFree(gatherOutDeviceAddr); } if (workspaceSize > 0) { aclrtFree(workspaceAddr); } ret = HcclCommDestroy(args.hcclComm); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclCommDestroy failed. ret = %d \n", ret); return ret); ret = aclrtDestroyStream(args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtDestroyStream failed. ret = %d \n", ret); return ret); ret = aclrtResetDevice(args.rankId); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtResetDevice failed. ret = %d \n", ret); return ret); ret = aclrtDestroyContext(args.context); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtDestroyContext failed. ret = %d \n", ret); return ret); return 0; } int main(int argc, char *argv[]) { int ret = aclInit(nullptr); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclInit failed. ret = %d \n", ret); return ret); aclrtStream stream[DEV_NUM]; aclrtContext context[DEV_NUM]; for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) { ret = aclrtSetDevice(rankId); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSetDevice failed. ret = %d \n", ret); return ret); ret = aclrtCreateContext(&context[rankId], rankId); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateContext failed. ret = %d \n", ret); return ret); ret = aclrtCreateStream(&stream[rankId]); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateStream failed. ret = %d \n", ret); return ret); } int32_t devices[DEV_NUM]; for (int i = 0; i < DEV_NUM; i++) { devices[i] = i; } // Initialize the collective communication communicator. HcclComm comms[DEV_NUM]; ret = HcclCommInitAll(DEV_NUM, devices, comms); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclCommInitAll failed. ret = %d \n", ret); return ret); Args args[DEV_NUM]; // Start multiple threads. std::vector<std::unique_ptr<std::thread>> threads(DEV_NUM); for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) { args[rankId].rankId = rankId; args[rankId].hcclComm = comms[rankId]; args[rankId].context = context[rankId]; args[rankId].stream = stream[rankId]; threads[rankId].reset(new(std::nothrow) std::thread(&launchOneThread_AllGatherMm, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }