aclnnAlltoAllMatmulV2
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
Function
Description: Fuses AlltoAll communication, Permute (ensuring contiguous addresses after communication), and Matmul computation, with communication first, computation second.
Formula: Assume the input shape of x1 is (BS, H), and rankSize is the number of NPU cards.
$$ commOut = AlltoAll(x1.view(rankSize, BS/rankSize, H)) \\ permutedOut = commOut.permute(1, 0, 2).view(BS/rankSize, rankSize*H) \\ output = permutedOut @ x2 + bias \\ $$
Compared with the aclnnAlltoAllMatmul API, this API has the following changes:
Adds the
commModeparameter. Users can use this parameter to specify the communication engine used by the chip.Ascend 950PR&950DT products :commModesupportsai_cpuandccu.
Atlas A3 products : No new features.commModesupports onlyai_cpu.
Atlas A2 products : No new features.commModesupports onlyaiv.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnAlltoAllMatmulV2GetWorkspaceSize" API to obtain the workspace size required for computation and the executor containing the operator computation process, and then call the "aclnnAlltoAllMatmulV2" API to perform the computation.
aclnnStatus aclnnAlltoAllMatmulV2GetWorkspaceSize(
const aclTensor* x1,
const aclTensor* x2,
const aclTensor* biasOptional,
const aclIntArray* alltoAllAxesOptional,
const char* group,
const char* commMode,
bool transposeX1,
bool transposeX2,
const aclTensor* output,
const aclTensor* alltoAllOutOptional,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnAlltoAllMatmulV2(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnAlltoAllMatmulV2GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x1 Input Left matrix input of the fused operator, corresponding to x1 in the formula. After AlltoAll communication and the Permute operation, the result of this input is used as the left matrix input of the MatMul computation. FLOAT16, BFLOAT16 ND 2 dimensions with shape (BS, H) x x2 Input Right matrix input of the fused operator, and also the right matrix of the MatMul computation. Directly used as the right matrix input of the MatMul computation. FLOAT16, BFLOAT16 ND 2-D, shape (H*rankSize, N) Support varies by device model. See Constraints. biasOptional Input Bias accumulated after the matrix multiplication, corresponding to bias in the formula. Passing a null pointer is supported. Data type restrictions vary by device model. For details, see Constraints. FLOAT16, BFLOAT16, FLOAT32 ND 1D, shape (N) x alltoAllAxesOptional Input Directions of data exchange for AlltoAll and Permute. Can be set to empty or [-2,-1]. An empty value is processed as [-2,-1] by default, indicating that the input is converted from (BS, H) to (BS/rankSize, rankSize*H). aclIntArray* (element type INT64) - 1D, shape (2) - group Input String on the Host that identifies the column group, that is, the communicator name. Obtain commName by calling the Hccl API HcclGetCommName and pass it as this parameter. String length range: (0, 128). - - - - commMode Input Specifies the current communication type. Supports "aiv", "ai_cpu", or "ccu". - - - - transposeX1 Input Indicates whether the left matrix has been transposed. Setting it to True is not supported yet. - - - - transposeX2 Input Indicates whether the right matrix is transposed. When set to True, the shape of the right matrix is (N, rankSize*H). - - - - Output Output Final computation result. Same data type as input x1. FLOAT16, BFLOAT16 ND 2D, shape (BS/rankSize, N) x alltoAllOutOptional Output Receives the content after AlltoAll and Permute. Passing nullptr indicates that the communication output is not returned. FLOAT16, BFLOAT16 ND 2D, shape (BS/rankSize, H*rankSize) x workspaceSize Output Returns the workspace size to be requested on the Device. - - - - - executor Output Returns the op executor containing the operator computation flow. - - - - - 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 Required input and output tensors are null pointers. ACLNN_ERR_PARAM_INVALID 161002 Data types of inputs and outputs are out of the supported range. Input tensor is an empty tensor. alltoAllAxesOptional is invalid. transposeX1 is true. Communicator length is invalid. Input and output tensor dimensions are invalid. The input or output format is a private format.
aclnnAlltoAllMatmulV2
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Workspace size allocated on the Device, obtained by the first-phase API aclnnAlltoAllMatmulV2GetWorkspaceSize. executor Input op executor, which contains the operator computation flow. stream Input Specifies the Stream for executing tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
aclnnAlltoAllMatmulV2 supports deterministic computation by default.
Number of NPU cards (rankSize), with different restrictions based on the device model:
Atlas A2 products : Supports 2, 4, and 8 devices.
Atlas A3 products : Supports 2, 4, 8, and 16 devices.
Ascend 950PR&950DT products : Supports 2, 4, 8, and 16 devices.
The variable BS used in the shape description must be divisible by the number of NPU cards.
The values of BS and N must not exceed 2147483647 (INT32_MAX). BS must not be less than 0, and N must not be less than 1.
The range of H*rankSize varies by device model:
Atlas A2 products : Supports [1, 35000].
Atlas A3 products ,Ascend 950PR&950DT products : Supports [2, 65535].
The support for empty tensors varies by device model:
Atlas A2 products : No empty tensor is supported.
Atlas A3 products ,Ascend 950PR&950DT products : Only empty tensors with the first dimension (BS) of input x1 equal to 0 are supported. Other empty tensors are not supported.
Support for non-contiguous tensors varies by device model:
Atlas A2 products ,Atlas A3 products : No non-contiguous tensor is supported.
Ascend 950PR&950DT products : Only x2 can be a non-contiguous tensor. Other non-contiguous tensors are not supported.
The data types of the x1 and x2 computation inputs must be consistent with the data types of the output and alltoAllOutOptional computation outputs. The x1, x2, and output passed in must not be null pointers.
The data type of biasOptional has different restrictions depending on the device model:
Atlas A2 products ,Atlas A3 products : When the data type of computation inputs x1/x2 is FLOAT16, computation input biasOptional supports FLOAT16. When the data type of computation inputs x1/x2 is BFLOAT16, computation input biasOptional supports FLOAT32.
Ascend 950PR&950DT products : When the data type of computation inputs x1/x2 is FLOAT16, computation input biasOptional supports FLOAT16 and FLOAT32. When the data type of computation inputs x1/x2 is BFLOAT16, computation input biasOptional supports BFLOAT16 and FLOAT32.
The communication-computation fusion operator does not support concurrent calls, and different communication-computation fusion operators also do not support concurrent calls.
Cross-supernode communication is not supported. Only intra-supernode communication is supported.
The communication engine parameter commMode has different restrictions depending on the device model:
Atlas A2 products : Onlyaivis supported.
Atlas A3 products : Onlyai_cpuis supported.
Ascend 950PR&950DT products : Supportsai_cpuandccuinputs.
Communication engine constraints:
Atlas A2 products : Supports MTE communication.
Atlas A3 products : Supports AI_CPU communication.
Ascend 950PR&950DT products :- Supports CCU communication and AI_CPU communication.
- CCU communication supports only interconnection within a single-server UB domain, while AI_CPU supports interconnection within a UB domain across servers.
- Communicator constraint: Only one communication method can be used within 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 APIs of the HCCL collective communication library: HcclGetCommName, HcclCommInitAll, and HcclCommDestroy. For details, see HCCL API (C).
Atlas A2 products :#include <thread> #include <iostream> #include <string> #include <cstring> #include <vector> #include <acl/acl.h> #include <hccl/hccl.h> #include "aclnn/opdev/fp16_t.h" #include "aclnnop/aclnn_allto_all_matmul_v2.h" int ndev = 2; #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); // 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 data 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); return 0; } struct Args { uint32_t rankId; HcclComm hcclComm; aclrtStream stream; aclrtContext context; }; int launchOneThreadAlltoAllMatmul(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] = {0}; 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 * ndev, 128}; std::vector<int64_t> biasShape = {128}; std::vector<int64_t> outShape = {32 / ndev, 128}; std::vector<int64_t> alltoalloutShape = {32 / ndev, 64 * ndev}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *outDeviceAddr = nullptr; void *alltoalloutDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *out = nullptr; aclTensor *alltoallout = nullptr; int64_t a2aAxes[2] = {-2, -1}; aclIntArray* alltoAllAxesOptional = aclCreateIntArray(a2aAxes, static_cast<uint64_t>(2)); 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 outShapeSize = GetShapeSize(outShape); long long alltoalloutShapeSize = GetShapeSize(alltoalloutShape); std::vector<op::fp16_t> x1HostData(x1ShapeSize, 1); std::vector<op::fp16_t> x2HostData(x2ShapeSize, 1); std::vector<op::fp16_t> biasHostData(biasShapeSize, 1); std::vector<op::fp16_t> outHostData(outShapeSize, 0); std::vector<op::fp16_t> alltoalloutHostData(alltoalloutShapeSize, 0); // Create a tensor. 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(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT16, &bias); 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(alltoalloutHostData, alltoalloutShape, &alltoalloutDeviceAddr, aclDataType::ACL_FLOAT16, &alltoallout); CHECK_RET(ret == ACL_SUCCESS, return ret); // Call the first-phase API. ret = aclnnAlltoAllMatmulV2GetWorkspaceSize(x1, x2, bias, alltoAllAxesOptional, hcom_name, "aiv", false, false, out, alltoallout, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAlltoAllMatmulV2GetWorkspaceSize failed. ERROR: %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("allocate workspace failed. ERROR: %d\n", ret); return ret); } // Call the second-phase API. ret = aclnnAlltoAllMatmulV2(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAlltoAllMatmulV2 failed. ERROR: %d\n", ret); return ret); // (Boilerplate) Synchronously wait for task execution to finish. ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); LOG_PRINT("device%d aclnnAlltoAllMatmulV2 execute success \n", args.rankId); // Release device resources. Modify the code based on 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 (alltoallout != nullptr) { aclDestroyTensor(alltoallout); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } if (outDeviceAddr != nullptr) { aclrtFree(outDeviceAddr); } if (alltoalloutDeviceAddr != nullptr) { aclrtFree(alltoalloutDeviceAddr); } 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[]) { // This sample is implemented based on Atlas A2 and must run on Atlas A2. int ret = aclInit(nullptr); int32_t devices[ndev]; for (int i = 0; i < ndev; i++) { devices[i] = i; } HcclComm comms[128]; 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 multiple threads. 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(&launchOneThreadAlltoAllMatmul, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < ndev; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }
Atlas A3 products :#include <thread> #include <iostream> #include <string> #include <cstring> #include <vector> #include <acl/acl.h> #include <hccl/hccl.h> #include "aclnn/opdev/fp16_t.h" #include "aclnnop/aclnn_allto_all_matmul_v2.h" int ndev = 2; #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); // Call aclrtMalloc to allocate memory on the 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); // 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); // Calculate 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; } struct Args { uint32_t rankId; HcclComm hcclComm; aclrtStream stream; aclrtContext context; }; int launchOneThreadAlltoAllMatmul(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] = {0}; 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 * ndev, 128}; std::vector<int64_t> biasShape = {128}; std::vector<int64_t> outShape = {32 / ndev, 128}; std::vector<int64_t> alltoalloutShape = {32 / ndev, 64 * ndev}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *outDeviceAddr = nullptr; void *alltoalloutDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *out = nullptr; aclTensor *alltoallout = nullptr; int64_t a2aAxes[2] = {-2, -1}; aclIntArray* alltoAllAxesOptional = aclCreateIntArray(a2aAxes, static_cast<uint64_t>(2)); 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 outShapeSize = GetShapeSize(outShape); long long alltoalloutShapeSize = GetShapeSize(alltoalloutShape); std::vector<op::fp16_t> x1HostData(x1ShapeSize, 1); std::vector<op::fp16_t> x2HostData(x2ShapeSize, 1); std::vector<op::fp16_t> biasHostData(biasShapeSize, 1); std::vector<op::fp16_t> outHostData(outShapeSize, 0); std::vector<op::fp16_t> alltoalloutHostData(alltoalloutShapeSize, 0); // Create a tensor. 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(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT16, &bias); 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(alltoalloutHostData, alltoalloutShape, &alltoalloutDeviceAddr, aclDataType::ACL_FLOAT16, &alltoallout); CHECK_RET(ret == ACL_SUCCESS, return ret); // Call the first-phase API. ret = aclnnAlltoAllMatmulV2GetWorkspaceSize(x1, x2, bias, alltoAllAxesOptional, hcom_name, "ai_cpu", false, false, out, alltoallout, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAlltoAllMatmulV2GetWorkspaceSize failed. ERROR: %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("allocate workspace failed. ERROR: %d\n", ret); return ret); } // Call the second-phase API. ret = aclnnAlltoAllMatmulV2(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAlltoAllMatmulV2 failed. ERROR: %d\n", ret); return ret); // (Boilerplate) Wait synchronously 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 aclnnAlltoAllMatmulV2 execute success \n", args.rankId); // Release device resources. Modify them based on the specific API definition. if (x1 != nullptr) { aclDestroyTensor(x1); } if (x2 != nullptr) { aclDestroyTensor(x2); } if (bias != nullptr) { aclDestroyTensor(bias); } if (out != nullptr) { aclDestroyTensor(out); } if (alltoallout != nullptr) { aclDestroyTensor(alltoallout); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } if (outDeviceAddr != nullptr) { aclrtFree(outDeviceAddr); } if (alltoalloutDeviceAddr != nullptr) { aclrtFree(alltoalloutDeviceAddr); } 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[]) { // This sample is implemented based on Atlas A2 and must run on Atlas A2. int ret = aclInit(nullptr); int32_t devices[ndev]; for (int i = 0; i < ndev; i++) { devices[i] = i; } HcclComm comms[128]; 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 multiple threads. 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(&launchOneThreadAlltoAllMatmul, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < ndev; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }
Ascend 950PR&950DT products :#include <thread> #include <iostream> #include <string> #include <cstring> #include <vector> #include <acl/acl.h> #include <hccl/hccl.h> #include "aclnnop/aclnn_allto_all_matmul_v2.h" int ndev = 2; #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); // 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 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); // Calculate 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; } struct Args { uint32_t rankId; HcclComm hcclComm; aclrtStream stream; aclrtContext context; }; int launchOneThreadAlltoAllMatmul(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] = {0}; 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 * ndev, 128}; std::vector<int64_t> biasShape = {128}; std::vector<int64_t> outShape = {32 / ndev, 128}; std::vector<int64_t> alltoalloutShape = {32 / ndev, 64 * ndev}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *outDeviceAddr = nullptr; void *alltoalloutDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *out = nullptr; aclTensor *alltoallout = nullptr; int64_t a2aAxes[2] = {-2, -1}; aclIntArray* alltoAllAxesOptional = aclCreateIntArray(a2aAxes, static_cast<uint64_t>(2)); 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 outShapeSize = GetShapeSize(outShape); long long alltoalloutShapeSize = GetShapeSize(alltoalloutShape); std::vector<int16_t> x1HostData(x1ShapeSize, 1); std::vector<int16_t> x2HostData(x2ShapeSize, 1); std::vector<int16_t> biasHostData(biasShapeSize, 1); std::vector<int16_t> outHostData(outShapeSize, 0); std::vector<int16_t> alltoalloutHostData(alltoalloutShapeSize, 0); // Create a tensor. 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(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT16, &bias); 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(alltoalloutHostData, alltoalloutShape, &alltoalloutDeviceAddr, aclDataType::ACL_FLOAT16, &alltoallout); CHECK_RET(ret == ACL_SUCCESS, return ret); // Call the first-phase API. ret = aclnnAlltoAllMatmulV2GetWorkspaceSize(x1, x2, bias, alltoAllAxesOptional, hcom_name, "ai_cpu", false, false, out, alltoallout, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAlltoAllMatmulV2GetWorkspaceSize failed. ERROR: %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("allocate workspace failed. ERROR: %d\n", ret); return ret); } // Call the second-phase API. ret = aclnnAlltoAllMatmulV2(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAlltoAllMatmulV2 failed. ERROR: %d\n", ret); return ret); // (Boilerplate) Synchronously wait for 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 aclnnAlltoAllMatmulV2 execute success \n", args.rankId); // Release device resources. Modify according to the specific API definition. if (x1 != nullptr) { aclDestroyTensor(x1); } if (x2 != nullptr) { aclDestroyTensor(x2); } if (bias != nullptr) { aclDestroyTensor(bias); } if (out != nullptr) { aclDestroyTensor(out); } if (alltoallout != nullptr) { aclDestroyTensor(alltoallout); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } if (outDeviceAddr != nullptr) { aclrtFree(outDeviceAddr); } if (alltoalloutDeviceAddr != nullptr) { aclrtFree(alltoalloutDeviceAddr); } 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[]) { // This sample is implemented based on <term>Ascend 950PR&950DT products</term> and must run on <term>Ascend 950PR&950DT products</term>. int ret = aclInit(nullptr); int32_t devices[ndev]; for (int i = 0; i < ndev; i++) { devices[i] = i; } HcclComm comms[128]; 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 multiple threads. 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(&launchOneThreadAlltoAllMatmul, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < ndev; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }