aclnnQuantGroupedMatMulAlltoAllvV2
Applicable Products
Ascend 950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description:
- Fuses quantized routed expert GroupedMatMul, Unpermute, and AlltoAllv, and performs parallel fusion with the shared expert MatMul. Computes first, then communicates. Supports the pertensor-pertensor and mx quantization mode.
Formula:
Routed experts:
$$ gmmY = (gmmX @ gmmWeight) * gmmXScale * gmmWeightScale \\ unpermuteOut = Unpermute(gmmY) \\ y = AlltoAllv(unpermuteOut) $$Shared expert:
$$ mmY = (mmX @ mmWeight) * mmXScaleOptional * mmWeightScaleOptional $$
Compared with the aclnnQuantGroupedMatMulAlltoAllv API, changes in this API are as follows:
Adds the
commModeparameter. Users can use this parameter to specify the communication engine used by the chip.Ascend 950DT products : Supportsai_cpuandccu.
Function Prototype
This operator is a two-phase API. Call the aclnnQuantGroupedMatMulAlltoAllvV2GetWorkspaceSize API first to obtain the input parameters and calculate the required workspace size and the executor that contains the operator computation flow based on the computation flow. Then call the aclnnQuantGroupedMatMulAlltoAllvV2 API to execute the computation.
aclnnStatus aclnnQuantGroupedMatMulAlltoAllvV2GetWorkspaceSize(
const aclTensor* gmmX,
const aclTensor* gmmWeight,
const aclTensor* gmmXScale,
const aclTensor* gmmWeightScale,
const aclTensor* sendCountsTensorOptional,
const aclTensor* recvCountsTensorOptional,
const aclTensor* mmXOptional,
const aclTensor* mmWeightOptional,
const aclTensor* mmXScaleOptional,
const aclTensor* mmWeightScaleOptional,
const aclTensor* commQuantScaleOptional,
int64_t gmmXQuantMode,
int64_t gmmWeightQuantMode,
int64_t mmXQuantMode,
int64_t mmWeightQuantMode,
int64_t commQuantMode,
int64_t commQuantDtypeOptional,
int64_t groupSize,
const char* group,
const char* commMode,
int64_t epWorldSize,
const aclIntArray* sendCounts,
const aclIntArray* recvCounts,
bool transGmmWeight,
bool transMmWeight,
const aclTensor* y,
const aclTensor* mmYOptional,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnQuantGroupedMatMulAlltoAllvV2(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnQuantGroupedMatMulAlltoAllvV2GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor gmmX Input Input gmmX in the formula. shape (A, H1). HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 2 x gmmWeight Input Input gmmWeight in the formula. shape (e, H1, N1). e indicates the number of experts deployed on each device, H1 indicates the hidden size, and N1 indicates the intermediate dimension of the routed expert FFN. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 3 x gmmXScale Input Quantization coefficient of gmmX. - pertensor quantization: shape (1).
- mx quantization: shape (A, ceil(H1/64), 2)
FLOAT32, FLOAT8_E8M0 ND - pertensor quantization: 1
- mx quantization: 3
x gmmWeightScale Input Quantization coefficient of gmmWeight. - pertensor quantization: shape (1).
- mx quantization: shape (e, N1, ceil(H1/64), 2); (e, ceil(H1/64), N1, 2) when weight is transposed
FLOAT32, FLOAT8_E8M0 ND - pertensor quantization: 1
- mx quantization: 3
x sendCountsTensorOptional Input Send count used by AlltoAllv. Currently, only empty is supported. Shape (e * ep, ). e is the number of experts deployed on each device, and ep is the size of the ep domain. INT64 ND 1 x recvCountsTensorOptional Input recv count used by AlltoAllv. Currently, only empty is supported. shape (e * ep, ). e indicates the number of experts deployed on each device, and ep indicates the ep domain size. INT64 ND 1 x mmXOptional Input mmX in the formula. shape (BS, H2). BS indicates batch sequence size, and H2 indicates hidden size. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 2 x mmWeightOptional Input Input mmWeight in the formula. shape (H2, N2). H2 indicates hidden size, and N2 indicates the intermediate layer dimension of the shared expert FFN. HIFLOAT8, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1 ND 2 x mmXScaleOptional Input Quantization coefficient of mmX. - pertensor quantization: shape (1).
- mx quantization: shape (BS, ceil(H2/64), 2)
FLOAT32, FLOAT8_E8M0 ND - pertensor quantization: 1
- mx quantization: 3
x mmWeightScaleOptional Input Quantization coefficient of mmWeight. - pertensor quantization: shape (1).
- mx quantization: shape (N2, ceil(H2/64), 2), or (ceil(H2/64), N2, 2) when weight is transposed
FLOAT32, FLOAT8_E8M0 ND - pertensor quantization: 1
- mx quantization: 3
x commQuantScaleOptional Input Quantization coefficient of low-bit communication. Reserved parameter. Currently, only null is supported. FLOAT32 ND 1 x gmmXQuantMode Input Quantization mode of gmmX. The quantization mode must be passed in. Currently, 1 (pertensor quantization) and 6 (mx quantization) are supported. INT64 - 1 x gmmWeightQuantMode Input Quantization mode of gmmWeight. The quantization mode must be passed in. Currently, 1 (pertensor quantization) and 6 (mx quantization) are supported. INT64 - 1 x mmXQuantMode Input Quantization mode of mmX. If mmX is not empty, the quantization mode must be specified. Currently, 1 (pertensor quantization) and 6 (mx quantization) are supported. INT64 - 1 x mmWeightQuantMode Input Quantization mode of mmWeight. If mmWeight is not empty, the quantization mode must be passed in. Currently, 1 (pertensor quantization) and 6 (mx quantization) are supported. INT64 - 1 x commQuantMode Input Low-bit communication quantization mode. Currently reserved for the low-bit feature. The value must be 0, indicating no quantization. INT64 - 1 x commQuantDtypeOptional Input Data type of low-bit communication. Currently reserved for the low-bit feature. Must be set to -1. INT64 - 1 x groupSize Input PerGroup quantization group size. Quantization group size in the three directions for the Matmul computation. Reserved parameter. Valid only when the quantization Scale input is 2D or higher; in other use cases, 0 must be passed. The groupSize input consists of three values: groupSizeM, groupSizeN, and groupSizeK for the three directions. Each value occupies 16 bits, and the three values together occupy the lower 48 bits of the int64_t groupSize (the upper 16 bits are invalid). Calculation formula: groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32. INT64 - - - group Input Communicator identifier. String length must be greater than 0 and less than 128. char* - - - commMode Input Specifies the current communication type. Enter "ai_cpu" or "ccu". char* - - - epWorldSize Input Size of the communicator. Supported values: 2/4/8/16/32/64/128/256. INT64 - - - sendCounts Input Send count used by AlltoAllv. Indicates the number of tokens sent from other ranks to each expert on the current rank. The supported dimension is e * ep. Flattened in one dimension by sendCounts[fromRank][expertId]. For example, when e=3, the order ise0,e1,e2,e0,e1,e2, ...aclIntArray* (element type INT64) ND - - recvCounts Input recv count used by AlltoAllv. Indicates the number of tokens to be received by this device after AlltoAllv. The supported dimension is e * ep. Flattened in one dimension by recvCounts[fromRank][expertId]. For example, when e=3, the order ise0,e1,e2,e0,e1,e2, ...aclIntArray* (element type INT64) ND - - transGmmWeight Input Whether the right matrix of gmm is transposed. Required. No default value. BOOL ND - - transMmWeight Input Whether the right matrix of mm is transposed. Required, no default value. BOOL ND - - y Output GroupedMatMul computation output. Empty tensors are not supported. Shape (BSK, N1). FLOAT16, BFLOAT16 ND 2 x mmYOptional Output matmul computation output. shape (bs, N2). FLOAT16, BFLOAT16 ND 2 x workspaceSize Output Returns the workspace size to be allocated on the Device side. - UINT64 ND - - executor Output Returns the op executor containing the operator computation flow. - aclOpExecutor* ND - - The enumeration values of gmmXQuantMode, gmmWeightQuantMode, mmXQuantMode, mmWeightQuantMode, and commQuantMode correspond to the quantization modes as follows:
- 0: non-quantized
- 1: pertensor
- 2: perchannel
- 3: pertoken
- 4: pergroup
- 5: perblock
- 6: mx quantization
- 7: pertoken dynamic quantization
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 A required input or output parameter tensor is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The input or output data type is not within the supported range.
aclnnQuantGroupedMatMulAlltoAllvV2
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 aclnnQuantGroupedMatMulAlltoAllvV2GetWorkspaceSize.executor Input Op executor that contains the operator computation flow. stream Input Stream for executing the task. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnQuantGroupedMatMulAlltoAllvV2 defaults to a deterministic implementation.
Communication engine constraints:
- Ascend 950DT products: CCU communication and AI_CPU communication are supported. CCU supports intra-machine UB domain interconnection only, and AI_CPU supports inter-machine UB domain interconnection.
The maximum product of e * epWorldSize is 256, where e (the number of experts per card) supports a maximum of 32, and epWorldSize supports 2/4/8/16/32/64/128/256;
The shape of gmmX is (A, H1), where A is the sum of sendCounts, and the value range of H1 is (0, 65536);
The shape of gmmWeight is (e, H1, N1), and the value range of N1 is (0, 65536);
The shape of y is (BSK, N1), where K in the first dimension has a range of [2, 8], and BSK is the sum of recvCounts;
mmX is the left matrix of the shared expert, with the shape (BS, H2). The value range of H2 is (0, 12288];
mmWeight is the right matrix of the shared expert, with the shape (H2, N2). The value range of N2 is (0, 65536);
sendCounts indicates the number of tokens sent to other devices, and the array size is e * epWorldSize;
recvCounts indicates the number of tokens received from other devices, and the array size is e * epWorldSize;
The quantization Scale, Mode, and other parameters of routed experts and shared experts are all mandatory;
low-bit communication Mode is a mandatory parameter; DType and Scale are optional and must be provided when Mode is non-zero;
Variables used in shape in the parameter description:
- BSK: The number of tokens received by the current card, which is the cumulative sum of the recvCounts parameter. Value range: (0, 52428800].
- H1: Indicates the hidden size of the routed expert. Value range: (0, 65536).
- H2: Indicates the hidden size of the shared expert. Value range: (0, 12288].
- e: indicates the number of experts on a single device. 0<e<=32, and e * epWorldSize supports a maximum of 256.
- N1: indicates the intermediate layer dimension of the routing expert FFN. Value range: (0, 65536).
- N2: indicates the intermediate layer dimension of the shared expert FFN. Value range: (0, 65536).
- BS: batch sequence size.
- K: indicates the number of TopK experts selected. K ranges from [2, 8].
- A: the number of tokens sent by the current card, which is the cumulative sum of the sendCounts parameter.
- The sum of the A parameters of all devices in the ep communicator equals the sum of the BSK parameters on all devices.
- For mx quantization, when gmmX and gmmWeight are FLOAT4_E2M1, H1 and H2 must be even numbers and cannot be 2. In addition, when transGmmWeight and transMmWeight are false, N1 and N2 must be even numbers.
- The transpose states of gmmWeight and gmmWeightScale must be consistent: both transposed or both not transposed. The transpose states of mmWeight and mmWeightScale must also be consistent.
- groupSize:
- The value of groupSize is valid only when the gmmXScale/gmmWeightScale/mmXScale/mmWeightScale inputs are all 2 dimensions and above. In other use cases, 0 must be passed in.
- The groupSize value supports formula deduction: the input groupSize is internally decomposed into groupSizeM, groupSizeN, and groupSizeK according to the following formula. If one or more of them is 0, the groupSizeM, groupSizeN, and groupSizeK used for computation are reset based on the shapes of the gmmX/gmmWeight/mmX/mmWeight/gmmXScale/gmmWeightScale/mmXScale/mmWeightScale inputs. Setting principle: If groupSizeM=0, the quantization group value in the m direction is determined by interface deduction, and the deduction formula is groupSizeM = m / scaleM (m must be divisible by scaleM), where m is consistent with m in the gmmX/mmX shape and scaleM is consistent with m in the gmmXScale/mmXScale shape; if groupSizeK=0, the quantization group value in the k direction is determined by interface deduction, and the deduction formula is groupSizeK = k / scaleK (k must be divisible by scaleK), where k is consistent with k in the gmmX/mmX shape and scaleK is consistent with k in the gmmXScale/mmXScale shape; if groupSizeN=0, the quantization group value in the n direction is determined by interface deduction, and the deduction formula is groupSizeN = n / scaleN (n must be divisible by scaleN), where n is consistent with n in the gmmWeight/mmWeight shape and scaleN is consistent with n in the gmmWeightScale/mmWeightScale shape.$$ groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32 $$
- If the reset condition is met, when the gmmXScale/gmmWeightScale/mmXScale/mmWeightScale inputs are 2 dimensions and above and their data types are all FLOAT8_E8M0, the [groupSizeM, groupSizeN, groupSizeK] value combination is derived as [1, 1, 32], and the corresponding groupSize value is 4295032864.
Quantization parameter constraints:
- The current version supports pertensor quantization and mx quantization.
Type constraints
Pertensor quantization
gmmX gmmWeight gmmXScale gmmWeightScale mmXScale mmWeightScale y HIFLOAT8 HIFLOAT8 FLOAT32 FLOAT32 FLOAT32 FLOAT32 FLOAT16/BFLOAT16 mx quantization
gmmX gmmWeight gmmXScale gmmWeightScale mmXScale mmWeightScale y FLOAT8_E4M3FN FLOAT8_E4M3FN FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16/BFLOAT16 FLOAT8_E4M3FN FLOAT8_E5M2 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16/BFLOAT16 FLOAT8_E5M2 FLOAT8_E4M3FN FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16/BFLOAT16 FLOAT8_E5M2 FLOAT8_E5M2 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16/BFLOAT16 FLOAT4_E2M1 FLOAT4_E2M1 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT8_E8M0 FLOAT16/BFLOAT16 The mmX type and gmmX type must be consistent, the mmWeight type and gmmWeight type must be consistent, and the mmY type and y type must be consistent.
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. For details, see <<HCCL API (C)>>.
Ascend 950DT products :#include <thread> #include <iostream> #include <string> #include <vector> #include "acl/acl.h" #include "hccl/hccl.h" #include "aclnnop/aclnn_quant_grouped_mat_mul_allto_allv_v2.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 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; }; // Basic shape information constexpr int64_t EP_WORLD_SIZE = 8; constexpr int64_t BS = 4096; constexpr int64_t K = 2; constexpr int64_t H = 7168; constexpr int64_t e = 4; constexpr int64_t N1 = 4096; constexpr int64_t N2 = 4096; constexpr int64_t A = BS * K; std::vector<int16_t> pGmmYData(BS *K *N1, 0); std::vector<int16_t> pmmYData(BS *N2, 0); int LaunchOneThreadAlltoAllvGmm(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); std::vector<int64_t> gmmXShape = {A, H}; std::vector<int64_t> gmmWShape = {e, H, N1}; std::vector<int64_t> gmmXScaleShape = {1}; std::vector<int64_t> gmmWScaleShape = {1}; std::vector<int64_t> yShape = {BS * K, N1}; std::vector<int64_t> mmXShape = {BS, H}; std::vector<int64_t> mmWShape = {H, N2}; std::vector<int64_t> mmXScaleShape = {1}; std::vector<int64_t> mmWScaleShape = {1}; std::vector<int64_t> mmYShape = {BS, N2}; std::vector<int64_t> sendCountsShape = {EP_WORLD_SIZE * e}; std::vector<int64_t> recvCountsShape = {EP_WORLD_SIZE * e}; std::vector<int64_t> sendCountsList(EP_WORLD_SIZE * e, A / (EP_WORLD_SIZE * e)); std::vector<int64_t> recvCountsList(EP_WORLD_SIZE * e, A / (EP_WORLD_SIZE * e)); void *gmmXDeviceAddr = nullptr; void *gmmWDeviceAddr = nullptr; void *gmmXScaleDeviceAddr = nullptr; void *gmmWScaleDeviceAddr = nullptr; void *yDeviceAddr = nullptr; void *mmXDeviceAddr = nullptr; void *mmWDeviceAddr = nullptr; void *mmXScaleDeviceAddr = nullptr; void *mmWScaleDeviceAddr = nullptr; void *mmYDeviceAddr = nullptr; aclTensor *gmmX = nullptr; aclTensor *gmmW = nullptr; aclTensor *gmmXScale = nullptr; aclTensor *gmmWScale = nullptr; aclTensor *y = nullptr; aclTensor *mmX = nullptr; aclTensor *mmW = nullptr; aclTensor *mmXScale = nullptr; aclTensor *mmWScale = nullptr; aclTensor *mmY = nullptr; aclTensor *sendCountsTensor = nullptr; aclTensor *recvCountsTensor = nullptr; aclTensor *commQuantScaleOptional = nullptr; int64_t gmmXQuantMode = 1; int64_t gmmWQuantMode = 1; int64_t mmXQuantMode = 1; int64_t mmWQuantMode = 1; int64_t commQuantMode = 0; int64_t commQuantDtypeOptional = -1; int64_t groupSize = 0; uint64_t workspaceSize = 0; aclOpExecutor *executor = nullptr; void *workspaceAddr = nullptr; long long gmmXShapeSize = GetShapeSize(gmmXShape); long long gmmWShapeSize = GetShapeSize(gmmWShape); long long gmmXScaleShapeSize = GetShapeSize(gmmXScaleShape); long long gmmWScaleShapeSize = GetShapeSize(gmmWScaleShape); long long yShapeSize = GetShapeSize(yShape); long long mmXShapeSize = GetShapeSize(mmXShape); long long mmWShapeSize = GetShapeSize(mmWShape); long long mmXScaleShapeSize = GetShapeSize(mmXScaleShape); long long mmWScaleShapeSize = GetShapeSize(mmWScaleShape); long long mmYShapeSize = GetShapeSize(mmYShape); std::vector<uint8_t> gmmXHostData(gmmXShapeSize, (args.rankId + 1) * 1024); // HIFLOAT8 std::vector<uint8_t> gmmWHostData(gmmWShapeSize, (args.rankId + 1) * 512); std::vector<float> gmmXScaleHostData(gmmXScaleShapeSize, 1); std::vector<float> gmmWScaleHostData(gmmWScaleShapeSize, 1); std::vector<int16_t> yHostData(yShapeSize, 65535); std::vector<uint8_t> mmXHostData(mmXShapeSize, (args.rankId + 1) * 1024); // HIFLOAT8 std::vector<uint8_t> mmWHostData(mmWShapeSize, (args.rankId + 1) * 512); std::vector<float> mmXScaleHostData(mmXScaleShapeSize, 1); std::vector<float> mmWScaleHostData(mmWScaleShapeSize, 1); std::vector<int16_t> mmYHostData(mmYShapeSize, 0); ret = CreateAclTensor(gmmXHostData, gmmXShape, &gmmXDeviceAddr, aclDataType::ACL_HIFLOAT8, &gmmX); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gmmWHostData, gmmWShape, &gmmWDeviceAddr, aclDataType::ACL_HIFLOAT8, &gmmW); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gmmXScaleHostData, gmmXScaleShape, &gmmXScaleDeviceAddr, aclDataType::ACL_FLOAT, &gmmXScale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gmmWScaleHostData, gmmWScaleShape, &gmmWScaleDeviceAddr, aclDataType::ACL_FLOAT, &gmmWScale); 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(mmXHostData, mmXShape, &mmXDeviceAddr, aclDataType::ACL_HIFLOAT8, &mmX); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(mmWHostData, mmWShape, &mmWDeviceAddr, aclDataType::ACL_HIFLOAT8, &mmW); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(mmXScaleHostData, mmXScaleShape, &mmXScaleDeviceAddr, aclDataType::ACL_FLOAT, &mmXScale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(mmWScaleHostData, mmWScaleShape, &mmWScaleDeviceAddr, aclDataType::ACL_FLOAT, &mmWScale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(mmYHostData, mmYShape, &mmYDeviceAddr, aclDataType::ACL_FLOAT16, &mmY); CHECK_RET(ret == ACL_SUCCESS, return ret); aclIntArray *sendCounts = aclCreateIntArray(sendCountsList.data(), sendCountsList.size()); aclIntArray *recvCounts = aclCreateIntArray(recvCountsList.data(), recvCountsList.size()); // Call the first-phase API ret = aclnnQuantGroupedMatMulAlltoAllvV2GetWorkspaceSize( gmmX, gmmW, gmmXScale, gmmWScale, sendCountsTensor, recvCountsTensor, mmX, mmW, mmXScale, mmWScale, commQuantScaleOptional, gmmXQuantMode, gmmWQuantMode, mmXQuantMode, mmWQuantMode, commQuantMode, commQuantDtypeOptional, groupSize, hcomName, "ai_cpu", EP_WORLD_SIZE, sendCounts, recvCounts, false, false, y, mmY, &workspaceSize, &executor); CHECK_RET( ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnQuantGroupedMatMulAlltoAllvV2GetWorkspaceSize failed. ret = %d \n", ret); return ret); 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 = aclnnQuantGroupedMatMulAlltoAllvV2(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnQuantGroupedMatMulAlltoAllvV2 failed. ret = %d \n", ret); return ret); // (boilerplate) Synchronously wait for the task to complete ret = aclrtSynchronizeStreamWithTimeout(args.stream, 10000000); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSynchronizeStreamWithTimeout failed. ret = %d \n", ret); return ret); LOG_PRINT("[INFO] device_%d aclnnQuantGroupedMatMulAlltoAllvV2 execute successfully.\n", args.rankId); // Release device resources. Modify according to the specific API definition. if (args.rankId == 0) { size_t size = A * N1 * sizeof(int16_t); aclrtMemcpy(pGmmYData.data(), size, yDeviceAddr, size, ACL_MEMCPY_DEVICE_TO_HOST); } if (gmmX != nullptr) { aclDestroyTensor(gmmX); } if (gmmW != nullptr) { aclDestroyTensor(gmmW); } if (gmmXScale != nullptr) { aclDestroyTensor(gmmXScale); } if (gmmWScale != nullptr) { aclDestroyTensor(gmmWScale); } if (y != nullptr) { aclDestroyTensor(y); } if (mmX != nullptr) { aclDestroyTensor(mmX); } if (mmW != nullptr) { aclDestroyTensor(mmW); } if (mmXScale != nullptr) { aclDestroyTensor(mmXScale); } if (mmWScale != nullptr) { aclDestroyTensor(mmWScale); } if (mmY != nullptr) { aclDestroyTensor(mmY); } if (gmmXDeviceAddr != nullptr) { aclrtFree(gmmXDeviceAddr); } if (gmmWDeviceAddr != nullptr) { aclrtFree(gmmWDeviceAddr); } if (gmmXScaleDeviceAddr != nullptr) { aclrtFree(gmmXScaleDeviceAddr); } if (gmmWScaleDeviceAddr != nullptr) { aclrtFree(gmmWScaleDeviceAddr); } if (yDeviceAddr != nullptr) { aclrtFree(yDeviceAddr); } if (mmXDeviceAddr != nullptr) { aclrtFree(mmXDeviceAddr); } if (mmWDeviceAddr != nullptr) { aclrtFree(mmWDeviceAddr); } if (mmXScaleDeviceAddr != nullptr) { aclrtFree(mmXScaleDeviceAddr); } if (mmWScaleDeviceAddr != nullptr) { aclrtFree(mmWScaleDeviceAddr); } if (mmYDeviceAddr != nullptr) { aclrtFree(mmYDeviceAddr); } if (workspaceSize > 0) { aclrtFree(workspaceAddr); } HcclCommDestroy(args.hcclComm); aclrtDestroyStream(args.stream); aclrtDestroyContext(args.context); aclrtResetDevice(args.rankId); 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[EP_WORLD_SIZE]; aclrtContext context[EP_WORLD_SIZE]; for (uint32_t rankId = 0; rankId < EP_WORLD_SIZE; 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[EP_WORLD_SIZE]; for (int i = 0; i < EP_WORLD_SIZE; i++) { devices[i] = i; } //Initialize the collective communicator. HcclComm comms[EP_WORLD_SIZE]; ret = HcclCommInitAll(EP_WORLD_SIZE, devices, comms); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclCommInitAll failed. ret = %d \n", ret); return ret); Args args[EP_WORLD_SIZE]; // Start multiple threads. std::vector<std::unique_ptr<std::thread>> threads(EP_WORLD_SIZE); for (uint32_t rankId = 0; rankId < EP_WORLD_SIZE; 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::thread(&LaunchOneThreadAlltoAllvGmm, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < EP_WORLD_SIZE; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }