aclnnQuantMatmulAlltoAllV2
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
Function
Description: Fuses quantized Matmul computation, Permute (to ensure address contiguity after communication), and AlltoAll communication, with computation before communication. Supports K-C quantization and the mx quantization mode.
Formula: Assume the shape of x1 is (BS, H1), the shape of x2 is (H1, H2), and rankSize is the number of NPUs.
Atlas A2 products :- K-C Quantization Scenario:$$ computeOut = (x1 @ x2) * x1Scale * x2Scale + bias \\ permutedOut = computeOut.view(BS, rankSize, H2 / rankSize).permute(1, 0, 2) \\ output = AlltoAll(permutedOut).view(rankSize * BS, H2 / rankSize) $$
- K-C Quantization Scenario:
Ascend 950PR&950DT products :K-C Quantization Scenario:
$$ computeOut = (x1 @ x2 + bias) * x1Scale * x2Scale \\ permutedOut = computeOut.view(BS, rankSize, H2 / rankSize).permute(1, 0, 2) \\ output = AlltoAll(permutedOut).view(rankSize * BS, H2 / rankSize) $$mx quantization scenario:
$$ computeOut = \sum_{0}^{\left \lfloor \frac{k}{blockSize=32} \right \rfloor} (x1 @ x2 * (x1Scale * x2Scale)) + bias \\ permutedOut = computeOut.view(BS, rankSize, H2 / rankSize).permute(1, 0, 2) \\ output = AlltoAll(permutedOut).view(rankSize * BS, H2 / rankSize) $$
Compared with the aclnnQuantMatmulAlltoAll API, this API includes 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 A2 products : No new features.commModesupports onlyaiv.
Function Prototype
Each operator uses a two-phase API. Call the "aclnnQuantMatmulAlltoAllV2GetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor containing the operator computation process, and then call the "aclnnQuantMatmulAlltoAllV2" API to perform the computation.
aclnnStatus aclnnQuantMatmulAlltoAllV2GetWorkspaceSize(
const aclTensor* x1,
const aclTensor* x2,
const aclTensor* biasOptional,
const aclTensor* x1Scale,
const aclTensor* x2Scale,
const aclTensor* commScaleOptional,
const aclTensor* x1OffsetOptional,
const aclTensor* x2OffsetOptional,
const aclIntArray* alltoAllAxesOptional,
const char* group,
const char* commMode,
int64_t x1QuantMode,
int64_t x2QuantMode,
int64_t commQuantMode,
int64_t commQuantDtype,
int64_t groupSize,
bool transposeX1,
bool transposeX2,
const aclTensor* output,
uint64_t* workspaceSize,
aclOpExecutor** executor);aclnnStatus aclnnQuantMatmulAlltoAllV2(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnQuantMatmulAlltoAllV2GetWorkspaceSize
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. This input serves as the left matrix for the MatMul computation. Different data type restrictions apply based on the device model. For details, see Constraints. FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1, INT8 ND 2D, shape (BS, H1) x x2 Input Right matrix input of the fused operator, corresponding to x2 in the formula. Directly used as the right matrix input for MatMul computation. Based on device model, different restrictions apply to data types and non-contiguous tensors. See Constraints for details. FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1, INT8 ND 2D with shape (H1, H2) Support varies by device model. See Constraints. biasOptional Input Bias accumulated after matrix multiplication, corresponding to bias in the formula. Based on device model, different restrictions apply to data types. See Constraints for details. FLOAT16, BFLOAT16, FLOAT32 ND 1D, shape (H2) x x1Scale Input Quantization coefficient of the left matrix. x1Scale in the corresponding formula. FLOAT32, FLOAT8_E8M0 ND 1D/3D. In K-C quantization scenarios, the shape is (BS). In mx quantization scenarios, the shape is (BS, ceil(H1/64), 2). x x2Scale Input Quantization coefficient of the right matrix. x2Scale in the corresponding formula. FLOAT32, FLOAT8_E8M0 ND 1D/3D. For the K-C quantization scenario, the shape is (H2). For the mx quantization scenario, the shape is (H2, ceil(H1/64), 2). x commScaleOptional Input Quantization coefficient for low-bit communication. Reserved parameter. Low-bit communication is not supported yet. - - - - x1OffsetOptional Input Quantization offset of the left matrix. Reserved parameter. Not supported yet. - - - - x2OffsetOptional Input Quantization offset of the right matrix. Reserved parameter. Not supported yet. - - - - alltoAllAxesOptional Input Direction of data exchange for AlltoAll and Permute. Can be set to empty or [-1, -2]. If empty is passed, it is processed as [-1, -2] by default, indicating that the input is converted from (BS, H2) to (BS*rankSize, H2/rankSize). aclIntArray* (element type INT64) - 1 dimension, shape (2) - group Input String identifying the column group, that is, the communicator name. Use commName obtained through the HCCL API HcclGetCommName as this parameter. String length: (0, 128). - - - - commMode Input Specifies the current communication type. Supports "aiv", "ai_cpu", or "ccu" as the input. - - - - x1QuantMode Input Quantization method of the left matrix. The value is subject to different restrictions based on device model. See Also Constraints. - - - - x2QuantMode Input Quantization mode of the right matrix. The value is subject to different restrictions based on device model. See Also Constraints. - - - - commQuantMode Input Quantization method for low-bit communication. Reserved parameter. Currently, this parameter can only be configured as 0, indicating that no quantization is performed. - - - - commQuantDtype Input Quantization type of low-bit communication. Reserved parameter. Currently, only -1 is supported, indicating ACL_DT_UNDEFINED. - - - - groupSize Input Quantization group sizes along the three dimensions of the Matmul computation, formed by concatenating groupSizeM, groupSizeN, and groupSizeK of the three dimensions. Each value occupies 16 bits, and the three values together occupy the lower 48 bits of the int64_t groupSize (the upper 16 bits of groupSize are invalid). - In the mx quantization scenario, only [groupSizeM, groupSizeN, groupSizeK] = [1, 1, 32] is supported. See Also Constraints for the corresponding groupSize value. In other quantization scenarios, it is configured as 0 by default and does not take effect.
- Automatic parameter deduction is supported. When any of groupSizeM, groupSizeN, or groupSizeK decomposed from the computation formula is 0, the operator automatically derives these parameter values. See Also Constraints for details.
- - - - transposeX1 Input Identifies whether the left matrix is transposed. Configuring it as True is not supported yet. - - - - transposeX2 Input Indicates whether the right matrix is transposed. When configured as True, the shape of the right matrix is (H2, H1). In mx quantization mode, it must be configured as True. - - - - Output Output The final computation result. - FLOAT16, BFLOAT16, FLOAT32 ND 2D, shape (BS*rankSize, H2/rankSize) x workspaceSize Output Returns the size of the workspace to be requested on the Device side. - - - - - executor Output Returns the op executor, which contains the computation flow of the operator. - - - - - The enumerated values of x1QuantMode, x2QuantMode, and commQuantMode correspond to Quantization Mode as follows:
- 0: no quantization
- 1: pertensor
- 2: perchannel
- 3: pertoken
- 4: pergroup
- 5: perblock
- 6: mx quantization
- 7: pertoken dynamic quantization
Return Value
aclnnStatus: Return status code. See aclnn return codes for details.
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 tensor parameter is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of input and output are not within the supported range. The input tensor is an empty tensor. alltoAllAxesOptional is invalid. transposeX1 is true. The length of the communicator is invalid. The dimensions of input/output tensors are invalid. The format of input/output is a private format.
aclnnQuantMatmulAlltoAllV2
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device side. workspaceSize Input Workspace size requested on the Device side, obtained by calling the first-phase API aclnnQuantMatmulAlltoAllV2GetWorkspaceSize. executor Input Executor of the op, which contains the computation flow of the operator. stream Input Specifies the Stream for executing tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
aclnnQuantMatmulAlltoAllV2 defaults to deterministic computation.
Number of NPU cards (rankSize) varies by device model:
Atlas A2 products : Supports 2, 4, and 8 devices.
Ascend 950PR&950DT products : Supports 2, 4, 8, and 16 devices.
The variable H2 used in the shape in Parameters must be divisible by the number of NPU cards.
The values of BS * rankSize and H2 must not exceed 2147483647 (INT32_MAX). The value of BS must not be less than 1, and the value of H2 must not be less than 2.
Empty tensors are not supported.
Support for non-contiguous tensors varies by device model:
Atlas A2 products : Does not support any non-contiguous tensor.
Ascend 950PR&950DT products : Only x2 can be a non-contiguous tensor. Other non-contiguous tensors are not supported.
The input x1, x2, x1Scale, x2Scale, and output must not be null pointers, and
Atlas A2 products : Passing a null pointer to biasOptional is not supported.
Constraints on groupSize:
- The groupSize value is valid only when both x1Scale and x2Scale inputs are 2-dimensional or higher. In other scenarios, pass 0.
- The passed-in groupSize is internally decomposed into groupSizeM, groupSizeN, and groupSizeK according to the following formula. If one or more of them is 0, groupSizeM, groupSizeN, and groupSizeK are reset for computation based on the input shapes of x1/x2/x1Scale/x2Scale. Principle: If groupSizeM=0, the quantization group value along the m dimension is inferred by the API. The inference formula is groupSizeM = m / scaleM (m must be divisible by scaleM), where m is the same as m in the x1 shape, scaleM is the same as m in the x1Scale shape, and the same applies to the k and n dimensions.$$ groupSize = groupSizeK | groupSizeN << 16 | groupSizeM << 32 $$
- Assume that the input x and scale are divisible along all dimensions, and the automatically derived groupSizeM, groupSizeN, and groupSizeK satisfy [1,1,32]. The supported groupSize values in the mx quantization scenario are as follows:
| groupSize | Based On The Formula [gsM,gsN,gsK] | Based On Automatic Derivation [gsM,gsN,gsK] | | :------: | :------: | :------: | | 4295032864 | [1,1,32] | - | | 0 | [0,0,0] | [1,1,32] | | 32 | [0,0,32] | [1,1,32] | | 65536 | [0,1,0] | [1,1,32] | | 65568 | [0,1,32] | [1,1,32] | | 4294967296 | [1,0,0] | [1,1,32] | | 4294967328 | [1,0,32] | [1,1,32] | | 4295032832 | [1,1,0] | [1,1,32] |
The data types, data dimensions, and quantization modes of the input and output of this operator have different restrictions depending on the device model:
Atlas A2 products :- Quantization Mode:
- Currently supported: K-C quantization, left matrix perToken quantization with x1QuantMode=3, right matrix perChannel quantization with x2QuantMode=2.
- The bias is added after quantization.
- Type constraints:
- The supported input/output data type combinations are:
- K-C Quantization:
- The supported input/output data type combinations are:
- Quantization Mode:
| x1 | x2 | biasOptional | Output | | :------: | :------: | :------: | :------: | | INT8 | INT8 | FLOAT16 | FLOAT16 | | INT8 | INT8 | FLOAT32 | FLOAT16 | | INT8 | INT8 | BFLOAT16 | BFLOAT16 | | INT8 | INT8 | FLOAT32 | BFLOAT16 |
* Dimension Constraints:
* The H1 range supports only [1, 65535].
Ascend 950PR&950DT products :- Quantization Mode:
- Currently supported: K-C quantization, left matrix perToken quantization, x1QuantMode=3, right matrix perChannel quantization, x2QuantMode=2; mx quantization, left matrix mx quantization, x1QuantMode=6, right matrix mx quantization, x2QuantMode=6.
- Type constraints:
- biasOptional can be empty.
- The supported input/output data type combinations are:
- K-C Quantization:
- Quantization Mode:
| x1 | x2 | biasOptional | Output | x1QuantMode | x2QuantMode | x1ScaleOptional | x2Scale | | :------: | :------: | :------: | :------: | :------: | :------: | :------: | :------: | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | FLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | BFLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | FLOAT32 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E4M3FN | FLOAT8_E5M2 | FLOAT32 | FLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E4M3FN | FLOAT8_E5M2 | FLOAT32 | BFLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E4M3FN | FLOAT8_E5M2 | FLOAT32 | FLOAT32 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E5M2 | FLOAT8_E4M3FN | FLOAT32 | FLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E5M2 | FLOAT8_E4M3FN | FLOAT32 | BFLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E5M2 | FLOAT8_E4M3FN | FLOAT32 | FLOAT32 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | FLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | BFLOAT16 | 3 | 2 | FLOAT32 | FLOAT32 | | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | FLOAT32 | 3 | 2 | FLOAT32 | FLOAT32 |
* mx quantization:| x1 | x2 | biasOptional | Output | x1QuantMode | x2QuantMode | x1ScaleOptional | x2Scale | | :------: | :------: | :------: | :------: | :------: | :------: | :------: | :------: | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | FLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | BFLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | FLOAT32 | FLOAT32 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E4M3FN | FLOAT8_E5M2 | FLOAT32 | FLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E4M3FN | FLOAT8_E5M2 | FLOAT32 | BFLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E4M3FN | FLOAT8_E5M2 | FLOAT32 | FLOAT32 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E5M2 | FLOAT8_E4M3FN | FLOAT32 | FLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E5M2 | FLOAT8_E4M3FN | FLOAT32 | BFLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E5M2 | FLOAT8_E4M3FN | FLOAT32 | FLOAT32 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | FLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | BFLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT8_E5M2 | FLOAT8_E5M2 | FLOAT32 | FLOAT32 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT4_E2M1 | FLOAT4_E2M1 | FLOAT32 | FLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT4_E2M1 | FLOAT4_E2M1 | FLOAT32 | BFLOAT16 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 | | FLOAT4_E2M1 | FLOAT4_E2M1 | FLOAT32 | FLOAT32 | 6 | 6 | FLOAT8_E8M0 | FLOAT8_E8M0 |
* Dimension Constraints:
* The H1 range supports only [1, 65535].
* In mx quantization scenarios, x2 must be transposed, with shape (H2, H1) and transposeX2 set to True.
* In the mx quantization scenario, when the inputs x1 and x2 are FLOAT4_E2M1, H1 must be an even number, and ceil(H1/32) must be an even number.
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 : Only the inputaivis supported.
Ascend 950PR&950DT products : Inputsai_cpuandccuare supported.
Communication engine constraints:
Atlas A2 products : MTE communication is supported.
Ascend 950PR&950DT products :- CCU communication and AI_CPU communication are supported.
- CCU supports only interconnection within the UB domain of a single device, while AI_CPU supports interconnection across the UB domains of multiple devices.
- Communicator constraint: Only one communication method can be used within the same communicator.
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. 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_quant_matmul_allto_all_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 the 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 launchOneThreadQuantMatmulAlltoAllV2(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> x1ScaleShape = {32}; std::vector<int64_t> x2ScaleShape = {128}; std::vector<int64_t> outShape = {32 * ndev, 128 / ndev}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *x1ScaleDeviceAddr = nullptr; void *x2ScaleDeviceAddr = nullptr; void *outDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *x1Scale = nullptr; aclTensor *x2Scale = nullptr; aclTensor *out = nullptr; int64_t x1QuantMode = 3; int64_t x2QuantMode = 2; int64_t commQuantMode = 0; int64_t commQuantDtype = -1; int64_t groupSize = 0; int64_t a2aAxes[2] = {-1, -2}; 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 x1ScaleShapeSize = GetShapeSize(x1ScaleShape); long long x2ScaleShapeSize = GetShapeSize(x2ScaleShape); long long outShapeSize = GetShapeSize(outShape); std::vector<int8_t> x1HostData(x1ShapeSize, 1); std::vector<int8_t> x2HostData(x2ShapeSize, 1); std::vector<op::fp16_t> biasHostData(biasShapeSize, 1); std::vector<float> x1ScaleHostData(x1ScaleShapeSize, 1); std::vector<float> x2ScaleHostData(x2ScaleShapeSize, 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); ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT16, &bias); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(x1ScaleHostData, x1ScaleShape, &x1ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x1Scale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(x2ScaleHostData, x2ScaleShape, &x2ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x2Scale); 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 = aclnnQuantMatmulAlltoAllV2GetWorkspaceSize(x1, x2, bias, x1Scale, x2Scale, nullptr, nullptr, nullptr, alltoAllAxesOptional, hcom_name, "aiv", x1QuantMode, x2QuantMode, commQuantMode, commQuantDtype, groupSize, false, false, out, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAlltoAllV2GetWorkspaceSize 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 = aclnnQuantMatmulAlltoAllV2(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAlltoAllV2 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 aclnnQuantMatmulAlltoAllV2 execute success \n", args.rankId); // Release device resources, and modify them as required by the definition of the specific API if (x1 != nullptr) { aclDestroyTensor(x1); } if (x2 != nullptr) { aclDestroyTensor(x2); } if (bias != nullptr) { aclDestroyTensor(bias); } if (x1Scale != nullptr) { aclDestroyTensor(x1Scale); } if (x2Scale != nullptr) { aclDestroyTensor(x2Scale); } if (out != nullptr) { aclDestroyTensor(out); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } 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[]) { // This sample is implemented based on Atlas A2 and must run on Atlas A2 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 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(&launchOneThreadQuantMatmulAlltoAllV2, 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 "aclnn/opdev/fp16_t.h" #include "aclnnop/aclnn_quant_matmul_allto_all_v2.h" constexpr 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 data from 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 launchOneThreadQuantMatmulAlltoAllV2(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, 128}; std::vector<int64_t> biasShape = {128}; std::vector<int64_t> x1ScaleShape = {32}; std::vector<int64_t> x2ScaleShape = {128}; std::vector<int64_t> outShape = {32 * ndev, 128 / ndev}; void *x1DeviceAddr = nullptr; void *x2DeviceAddr = nullptr; void *biasDeviceAddr = nullptr; void *x1ScaleDeviceAddr = nullptr; void *x2ScaleDeviceAddr = nullptr; void *outDeviceAddr = nullptr; aclTensor *x1 = nullptr; aclTensor *x2 = nullptr; aclTensor *bias = nullptr; aclTensor *x1Scale = nullptr; aclTensor *x2Scale = nullptr; aclTensor *out = nullptr; int64_t x1QuantMode = 3; int64_t x2QuantMode = 2; int64_t commQuantMode = 0; int64_t commQuantDtype = -1; int64_t groupSize = 0; int64_t a2aAxes[2] = {-1, -2}; 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 x1ScaleShapeSize = GetShapeSize(x1ScaleShape); long long x2ScaleShapeSize = GetShapeSize(x2ScaleShape); long long outShapeSize = GetShapeSize(outShape); std::vector<int8_t> x1HostData(x1ShapeSize, 1); std::vector<int8_t> x2HostData(x2ShapeSize, 1); std::vector<float> biasHostData(biasShapeSize, 1); std::vector<float> x1ScaleHostData(x1ScaleShapeSize, 1); std::vector<float> x2ScaleHostData(x2ScaleShapeSize, 1); std::vector<op::fp16_t> outHostData(outShapeSize, 0); // Create tensors. ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &x1); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_FLOAT8_E4M3FN, &x2); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(biasHostData, biasShape, &biasDeviceAddr, aclDataType::ACL_FLOAT, &bias); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(x1ScaleHostData, x1ScaleShape, &x1ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x1Scale); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(x2ScaleHostData, x2ScaleShape, &x2ScaleDeviceAddr, aclDataType::ACL_FLOAT, &x2Scale); 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 = aclnnQuantMatmulAlltoAllV2GetWorkspaceSize(x1, x2, bias, x1Scale, x2Scale, nullptr, nullptr, nullptr, alltoAllAxesOptional, hcom_name, "ai_cpu", x1QuantMode, x2QuantMode, commQuantMode, commQuantDtype, groupSize, false, false, out, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAlltoAllV2GetWorkspaceSize 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 = aclnnQuantMatmulAlltoAllV2(workspaceAddr, workspaceSize, executor, args.stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQuantMatmulAlltoAllV2 failed. ERROR: %d\n", ret); return ret); // (boilerplate) Synchronize and wait for the task 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 aclnnQuantMatmulAlltoAllV2 execute success \n", args.rankId); // Release device resources, which needs to be modified based on the specific API definition. if (x1 != nullptr) { aclDestroyTensor(x1); } if (x2 != nullptr) { aclDestroyTensor(x2); } if (bias != nullptr) { aclDestroyTensor(bias); } if (x1Scale != nullptr) { aclDestroyTensor(x1Scale); } if (x2Scale != nullptr) { aclDestroyTensor(x2Scale); } if (out != nullptr) { aclDestroyTensor(out); } if (x1DeviceAddr != nullptr) { aclrtFree(x1DeviceAddr); } if (x2DeviceAddr != nullptr) { aclrtFree(x2DeviceAddr); } if (biasDeviceAddr != nullptr) { aclrtFree(biasDeviceAddr); } if (x1ScaleDeviceAddr != nullptr) { aclrtFree(x1ScaleDeviceAddr); } if (x2ScaleDeviceAddr != nullptr) { aclrtFree(x2ScaleDeviceAddr); } 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[]) { // This sample is implemented based on <term>Ascend 950PR&950DT products</term> and must run on <term>Ascend 950PR&950DT products</term>. 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 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(&launchOneThreadQuantMatmulAlltoAllV2, std::ref(args[rankId]))); } for (uint32_t rankId = 0; rankId < ndev; rankId++) { threads[rankId]->join(); } aclFinalize(); return 0; }