aclnnMoeDistributeCombineSetup

📄 View Source Code

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: Performs AlltoAllV communication and writes data to the peer GM.

  • Formula:

    $$ ataOut = AllToAllV(expandX)\\ $$

    The data is returned along the same path used by the MoeDistributeDispatchSetup and MoeDistributeDispatchTeardown operators to collect data. This operator only sends the communication status and communication data, and exits immediately after sending the data without waiting for the communication to complete. The communication status confirmation and data post-processing are completed by aclnnMoeDistributeCombineTeardown.

  • NOTE: This API must be used together with aclnnMoeDistributeDispatchSetup, aclnnMoeDistributeDispatchTeardown, and aclnnMoeDistributeCombineTeardown.

Function Prototype

This operator is a two-phase API. Call the "aclnnMoeDistributeCombineSetupGetWorkspaceSize" API first to obtain the input parameters, calculate the required workspace size based on the computation process, and obtain the executor containing the operator's computation process. Then call the "aclnnMoeDistributeCombineSetup" API to perform the computation. The "aclnnMoeDistributeCombineSetupTeardownCalcOutputSize" API is provided to calculate the output size of the "aclnnMoeDistributeCombineSetup" phase.

aclnnStatus aclnnMoeDistributeCombineSetupGetWorkspaceSize(
    const aclTensor* expandX,
    const aclTensor* expertIds,
    const aclTensor* assistInfoForCombine,
    const char*      groupEp,
    int64_t          epWorldSize,
    int64_t          epRankId,
    int64_t          moeExpertNum,
    int64_t          expertShardType,
    int64_t          sharedExpertNum,
    int64_t          sharedExpertRankNum,
    int64_t          globalBs,
    int64_t          commQuantMode,
    int64_t          commType,
    const char*      commAlg,
    aclTensor*       quantExpandXOut,
    aclTensor*       commCmdInfoOut,
    uint64_t*        workspaceSize,
    aclOpExecutor**  executor)
aclnnStatus aclnnMoeDistributeCombineSetup(
    void             *workspace,
    uint64_t         workspaceSize,
    aclOpExecutor    *executor,
    aclrtStream      stream)
aclnnStatus aclnnMoeDistributeCombineSetupTeardownCalcOutputSize(
    const aclTensor *expandX,
    const aclTensor *expertIds,
    const aclTensor *assistInfoForCombine,
    const char      *groupEp,
    int64_t         epWorldSize,
    int64_t         epRankId,
    int64_t         moeExpertNum,
    int64_t         expertShardType,
    int64_t         sharedExpertNum,
    int64_t         sharedExpertRankNum,
    int64_t         globalBs,
    int64_t         commQuantMode,
    int64_t         commType,
    const char      *commAlg,
    uint64_t        &tokenMsgSize,
    uint64_t        &commCmdInfoOutSize)

aclnnMoeDistributeCombineSetupGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension Non-contiguous Tensor
    expandX Input Self-refresh parameter, token features expanded based on expertIds. Empty tensors are not supported. FLOAT16, BFLOAT16 ND (A, H) √
    expertIds Input topK expert indexes of each token. Empty tensors are not supported. INT32 ND (Bs, K) √
    assistInfoForCombine Input Corresponds to the assistInfoForCombine output in aclnnMoeDistributeDispatchTeardown. Empty tensors are not supported. INT32 ND (A * 128, ) √
    groupEp Input EP communication domain name. String length range: [1, 128) - - - -
    epWorldSize Input EP communication domain size Supported value range: [2, 384] - - - -
    epRankId Input Local ID of the current device in the EP domain. Value range: [0, epWorldSize).
    The epRankId of each device in the same EP communication domain must be unique.
    - - - -
    moeExpertNum Input Number of MoE experts. Value range: (0, 512].
    moeExpertNum % (epWorldSize - sharedExpertRankNum) = 0 must be satisfied.
    - - - -
    expertShardType Input Distribution type of shared expert cards. Currently, only 0 is supported, indicating that shared expert cards are arranged before MoE expert cards. - - - -
    sharedExpertNum Input Number of shared experts Current value range: [0, 4]. 0 indicates no shared expert. - - - -
    sharedExpertRankNum Input Number of shared expert cards. Current value range [0, epWorldSize / 2]. - - - -
    globalBs Input EP-domain global batch size. In the use case where all ranks have the same Bs, globalBs = Bs * epWorldSize or globalBs = 0; in the use case where ranks have different Bs, globalBs = maxBs * epWorldSize, where maxBs indicates the maximum Bs of a single device. - - - -
    commQuantMode Input Communication quantization type. Currently, only 0 is supported, indicating that quantization is not performed. - - - -
    commType Input Communication scheme selection Currently, only 2 is supported, indicating the URMA pathway. - - - -
    commAlg Input Communication algorithm selection. Reserved field. Not supported in the current version. Pass null pointer or an empty string. - - - -
    quantExpandXOut Output Quantized tokens Empty tensors are not supported. INT8 ND (A, tokenMsgSize) √
    commCmdInfoOut Output cmd information for communication. Empty tensors are not supported. INT32 ND ((A + epWorldSize) * 16, ) √
    workspaceSize Output Returns the workspace size to be allocated on the Device. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
    • Ascend 950DT products:
      • The shared expert scenario is not supported.
      • epWorldSize: the current value only supports 2 and 8.
      • moeExpertNum indicates the MoE expert count. Currently, you can only pass in 32.
      • expertShardType: only 0 is supported currently, indicating that shared expert devices are placed before MoE expert devices.
      • sharedExpertNum indicates the shared expert count. Shared experts are currently not supported. You can only pass in 0.
      • sharedExpertRankNum indicates the number of shared expert cards. Shared experts are currently not supported. You can only pass in 0.
      • commQuantMode currently only supports 0, indicating that no quantization is performed.
      • The value range of commType is [0, 2]. Currently only 2 is supported, indicating the URMA path.
      • commAlg is not supported in the current version. Pass a null pointer.
  • 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 tensor is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is out of the supported range.
    ACLNN_ERR_INNER_TILING_ERROR 561002 The shape of the input or output is out of the supported range.
    The parameter value is out of the supported range.

aclnnMoeDistributeCombineSetup

  • Parameters:

    Parameter Input/Output Description
    workspace Input Address of the workspace memory allocated on the Device.
    workspaceSize Input Size of the workspace memory allocated on the Device, obtained by calling the first-phase API aclnnMoeDistributeCombineSetupGetWorkspaceSize.
    executor Input Executor of the operator, containing the operator computation flow.
    stream Input Stream for task execution.
  • Return value

    Returns the aclnnStatus return code. For details, see aclnn return code.

aclnnMoeDistributeCombineSetupTeardownCalcOutputSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension Non-contiguous Tensor
    expandX Input Self-refresh parameter, token features expanded based on expertIds. Empty tensors are not supported. FLOAT16, BFLOAT16 ND (A, H) √
    expertIds Input TopK expert indexes of each token. Empty tensors are not supported. INT32 ND (Bs, K) √
    assistInfoForCombine Input Corresponds to the assistInfoForCombine output in aclnnMoeDistributeDispatchTeardown. Empty tensors are not supported. INT32 ND (A * 128, ) √
    groupEp Input EP communication domain name. String length range: [1, 128) - - - -
    epWorldSize Input EP communication domain size. Value range: [2, 384] - - - -
    epRankId Input Id of the current device in the EP domain. value range [0, epWorldSize).
    The epRankId of each device in the same EP communication domain must be unique.
    - - - -
    moeExpertNum Input Number of MoE experts. Value range (0, 512].
    Satisfies moeExpertNum % (epWorldSize - sharedExpertRankNum) = 0.
    - - - -
    expertShardType Input Distribution type of shared expert devices. Currently, only 0 is supported, indicating that shared expert devices are arranged before MoE expert devices. - - - -
    sharedExpertNum Input Number of shared experts. Current value range [0, 4]. 0 indicates no shared expert. - - - -
    sharedExpertRankNum Input Number of shared expert cards. Current value range [0, epWorldSize / 2]. - - - -
    globalBs Input Global batch size across the EP domain. If the Bs values are the same across ranks, globalBs = Bs * epWorldSize or globalBs = 0. If the Bs values differ across ranks, globalBs = maxBs * epWorldSize, where maxBs indicates the maximum Bs on a single device. - - - -
    commQuantMode Input Communication quantization type. Currently only 0 is supported, indicating that quantization is not performed. - - - -
    commType Input Communication scheme selection Currently only 2 is supported, indicating the URMA pathway. - - - -
    commAlg Input Communication algorithm selection. Reserved field. Not supported in the current version. Pass null pointer or an empty string. - - - -
    tokenMsgSize Output Size of the second dimension (tokenMsgSize) of quantExpandX in the aclnnMoeDistributeCombineSetup API, indicating the dimension information of each token during data communication. - INT64 - Align512(Align32(H) + Align8(H) / 8 * sizeof(float)), where AlignN(x) = ((x + N - 1) / N) * N. -
    commCmdInfoOutSize Output Size of commCmdInfoOut of the aclnnMoeDistributeCombineSetup API. - INT64 - (A + epWorldSize) * 16 -
    workspaceSize Output Size of the workspace that needs to be allocated on the Device. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

Constraints

  • Deterministic computation:

    • aclnnMoeDistributeCombineSetup defaults to a deterministic implementation.
  • The aclnnMoeDistributeDispatchSetup API, aclnnMoeDistributeDispatchTeardown API, aclnnMoeDistributeCombineSetup API, and aclnnMoeDistributeCombineTeardown API must be used together.

  • The values of the groupEp, epWorldSize, moeExpertNum, expertShardType, sharedExpertNum, sharedExpertRankNum, globalBs, commQuantMode, commType, and commAlg parameters must be the same on all devices. The values of the groupEp, epWorldSize, expertShardType, sharedExpertNum, sharedExpertRankNum, globalBs, commQuantMode, commType, and commAlg parameters must also be the same across different layers in the network, and consistent with the corresponding parameters of the aclnnMoeDistributeDispatchSetup API, aclnnMoeDistributeDispatchTeardown API, and aclnnMoeDistributeCombineTeardown API.

  • Description of the shape format in Parameters:

    • A: the maximum number of tokens that the current card needs to distribute. The value range is as follows:
      • For shared experts, if globalBs is 0, A = BS * epWorldSize * sharedExpertNum / sharedExpertRankNum must be satisfied. If globalBs is not 0, A = globalBs* sharedExpertNum / sharedExpertRankNum must be satisfied.
      • For MoE experts, when globalBs is 0, A >= BS * epWorldSize * min(localExpertNum, K) must be satisfied; when globalBs is non-0, A >= globalBs * min(localExpertNum, K) must be satisfied.
    • H: indicates the hidden size. The value range is [1024, 8192]. Currently, only 4096 and 7168 are supported.
    • BS: indicates the batch sequence size, that is, the final number of tokens output by this device. The value range is 0 < BS ≤ 512. Currently only 8, 16, and 256 are supported.
    • K: indicates selecting topK experts. The value range is 0 < K ≤ 16 and must satisfy 0 < K ≤ moeExpertNum. Currently only 6 and 8 are supported.
    • localExpertNum: indicates the number of experts on the current card.
      • For a shared expert card, localExpertNum = 1.
      • For MoE expert cards, localExpertNum = moeExpertNum / (epWorldSize - sharedExpertRankNum). moeExpertNum currently only supports 32.
    • tokenMsgSize: indicates the dimension information of each token during data communication. The calculation formula is Align512(Align32(H)+Align8(H)/8*sizeof(float)), where AlignN(x)=((x+N-1)/N*N).
    • Shared experts are currently not supported. sharedExpertNum and sharedExpertRankNum currently only support 0.
  • HCCL_BUFFSIZE: Before calling this API, check whether the value of the HCCL_BUFFSIZE environment variable is reasonable. This environment variable indicates the memory size occupied by a single communication domain, in MB. The default value is 200 MB when not configured.

    • Ascend 950DT products:
      • Requires >= 2 and satisfies >= 4 * (localExpertNum * maxBs * epWorldSize * Align512(Align32(2 * H) + 44) + (K + sharedExpertNum) * maxBs * Align512(2 * H)), localExpertNum must use the local expert count of the MoE expert card, where Align512(x) = ((x + 512 - 1) / 512) * 512, Align32(x) = ((x + 32 - 1) / 32) * 32
  • Communication domain usage constraints:

    • The aclnnMoeDistributeDispatchSetup, aclnnMoeDistributeDispatchTeardown, aclnnMoeDistributeCombineSetup, and aclnnMoeDistributeCombineTeardown APIs in a model support only the same EP communication domain, and no other operators are allowed in the communication domain.
  • Communication mode constraints:

    • Ascend 950DT products: Only URMA communication is supported.

Example

  • File preparation:

    1. Create and modify the rank_table_m2.json file as instructed below.

    2. Copy the project to both servers, and configure the rank_table_m2.json file content based on the device IP of each machine. Ensure that the rank_table_m2.json files on both servers remain consistent.

    3. Install the CANN package, then compile and run the code by referring to Operator Invocation.

  • About rankTable:

    1. Developers can use the ranktable file to configure the NPU resource information for collective communication. For details, see the section "Communication Function Development > Cluster Information Configuration > Configuring Resource Information Using a ranktable File" in Collective Communication User Guide.

    2. Use cat /etc/hccn.conf or for i in seq 0 7; do echo "===================> dev$i, NPU$((i+1))"; hccn_tool -i $i -ip -g; done to query the device IP of the server. Then fill in the JSON file by referring to the collective communication documentation.

    Note: In a two-machine 16-card scenario, the device_id of both machines is 0 to 7, the rank_id of one machine is 0 to 7, and the rank_id of the other machine is 8 to 15. In a single-machine 16-card scenario, both device_id and rank_id are 0 to 15.

  • Environment variable configuration:

    # Set three environment variables before running.
    ## FIRST_RANK_ID description: For example, in the two-machine 16-device use case, set the value to 0 on one machine and 8 on the other.
    ## For example, export FIRST_RANK_ID=0
    export RANK_TABLE_FILE=/home/path/to/rank_table_m2.json
    export FIRST_RANK_ID=<starting rank_id of the device>
    ## ENV_DEV_NUM description: Set this variable to the number of devices on the current machine. For example, in the two-machine 16-device use case, set the value to 16 on both machines.
    export ENV_DEV_NUM=16
  • Machine quantity configuration:

    In the two-machine 16-device use case, set MACHINE_NUM to 2, that is,

    const uint32_t MACHINE_NUM = 2;

    No modification is required for the single-machine 16-device use case.

  • Ascend 950DT products:

    The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.

    #include <thread>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstring>
    #include "acl/acl.h"
    #include "hccl/hccl.h"
    #include "aclnnop/aclnn_moe_distribute_combine_setup.h"
    #include "aclnnop/aclnn_moe_distribute_combine_teardown.h"
    
    #define CHECK_RET(cond, return_expr)                                                                                   \
        do {                                                                                                               \
            if (!(cond)) {                                                                                                 \
                return_expr;                                                                                               \
            }                                                                                                              \
        } while (0)
    
    #define LOG_PRINT(message, ...)                                                                                        \
        do {                                                                                                               \
            printf(message, ##__VA_ARGS__);                                                                                \
        } while (0)
    
    constexpr int DEV_NUM = 2;
    
    template <typename Func>
    class Guard {
    public:
        explicit Guard(Func &func) : func_(func)
        {
        }
        ~Guard()
        {
            func_();
        }
    
    private:
        Func &func_;
    };
    
    int64_t AlignN(int64_t x, int64_t n)
    {
        return (x + n - 1) / n * n;
    }
    
    int64_t GetShapeSize(const std::vector<int64_t> &shape)
    {
        int64_t shape_size = 1;
        for (auto i : shape) {
            shape_size *= i;
        }
        return shape_size;
    }
    
    struct Args {
        uint32_t rankId;
        uint32_t epRankId;
        HcclComm hcclEpComm;
        aclrtStream combinesetupstream;
        aclrtStream combineteardownstream;
        aclrtContext context;
    };
    
    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;
    }
    
    void DestroyTensor(aclTensor *tensor)
    {
        if (tensor != nullptr) {
            aclDestroyTensor(tensor);
        }
    }
    
    void FreeDeviceAddr(void *deviceAddr)
    {
        if (deviceAddr != nullptr) {
            aclrtFree(deviceAddr);
        }
    }
    
    int LaunchOneProcess(Args &args)
    {
        int ret = aclrtSetCurrentContext(args.context);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSetCurrentContext failed. ret: %d\n", ret); return ret);
    
        char hcomEpName[128] = {0};
        ret = HcclGetCommName(args.hcclEpComm, hcomEpName);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclGetCommName failed. ret: %d\n", ret); return ret);
    
        auto destroyFunc = [&args]() {
            std::cout << "== begin to destroy " << std::endl;
            HcclCommDestroy(args.hcclEpComm);
            aclrtDestroyStream(args.combinesetupstream);
            aclrtDestroyStream(args.combineteardownstream);
            aclrtDestroyContext(args.context);
            aclrtResetDevice(args.rankId);
        };
        auto guard = Guard<decltype(destroyFunc)>(destroyFunc);
    
        // Use case parameters
        int64_t bs = 16;
        int64_t h = 4096;
        int64_t k = 6;
        int64_t expertSharedType = 0;
        int64_t sharedExpertNum = 0;
        int64_t sharedExpertRankNum = 0;
        int64_t moeExpertNum = 32;
        int64_t commQuantMode = 0;
        int64_t tpWorldSize = 1;
        int64_t epWorldSize = DEV_NUM;
        int64_t commType = 2;
        int64_t timeOut = 100000000;
    
        int64_t globalBS = bs * epWorldSize;
        int64_t localExpertNum;
        int64_t localToken;
    
        if (args.epRankId < sharedExpertRankNum) {
            localExpertNum = 1;
            localToken = globalBS / sharedExpertRankNum;
        } else {
            localExpertNum = moeExpertNum / (epWorldSize - sharedExpertRankNum);
            localToken = globalBS * (localExpertNum < k ? localExpertNum : k);
        }
    
        uint64_t assistInfoForCombineOutSize = localToken * 128;
    
        // Define shapes
        std::vector<int64_t> expandXShape{tpWorldSize * localToken, h};
        std::vector<int64_t> expertIdsShape{bs, k};
        std::vector<int64_t> expertScalesShape{bs, k};
        std::vector<int64_t> expandIdxShape{bs * k};
        std::vector<int64_t> assistInfoForCombineOutShape{assistInfoForCombineOutSize};
        std::vector<int64_t> quantExpandXOutShape{tpWorldSize * localToken,
                                                AlignN(AlignN(h, 32) + AlignN(h, 8) / 8 * sizeof(float), 512)};
        std::vector<int64_t> commCmdInfoOutShapeforcombine{(localToken + epWorldSize) * 16};
        std::vector<int64_t> xOutShape{bs, h};
    
        int64_t expandXShapeSize = GetShapeSize(expandXShape);
        int64_t expertIdsShapeSize = GetShapeSize(expertIdsShape);
        int64_t expertScalesShapeSize = GetShapeSize(expertScalesShape);
        int64_t expandIdxShapeSize = GetShapeSize(expandIdxShape);
        int64_t assistInfoForCombineOutShapeSize = GetShapeSize(assistInfoForCombineOutShape);
        int64_t quantExpandXOutShapeSize = GetShapeSize(quantExpandXOutShape);
        int64_t commCmdInfoOutShapeSizeforcombine = GetShapeSize(commCmdInfoOutShapeforcombine);
        int64_t xOutShapeSize = GetShapeSize(xOutShape);
    
        // Construct host data
        std::vector<int16_t> expandXHostData(expandXShapeSize, 0);
        std::vector<int32_t> expertIdsHostData;
        for (int32_t token_id = 0; token_id < expertIdsShape[0]; token_id++) {
            for (int32_t k_id = 0; k_id < expertIdsShape[1]; k_id++) {
                expertIdsHostData.push_back(k_id);
            }
        }
        std::vector<float> expertScalesHostData(expertScalesShapeSize, 0);
        std::vector<int32_t> expandIdxHostData(expandIdxShapeSize, 0);
        std::vector<int32_t> assistInfoForCombineOutHostData(assistInfoForCombineOutShapeSize, 0);
        std::vector<int8_t> quantExpandXOutHostData(quantExpandXOutShapeSize, 0);
        std::vector<int32_t> commCmdInfoOutforCombineHostData(commCmdInfoOutShapeSizeforcombine, 0);
        std::vector<float> xOutHostData(xOutShapeSize, 0);
    
        // Declare device addresses and tensors
        void *expandXDeviceAddr = nullptr;
        void *expertIdsDeviceAddr = nullptr;
        void *expertScalesDeviceAddr = nullptr;
        void *expandIdxDeviceAddr = nullptr;
        void *assistInfoForCombineOutDeviceAddr = nullptr;
        void *quantExpandXOutDeviceAddr = nullptr;
        void *commCmdInfoOutforCombineDeviceAddr = nullptr;
        void *xOutDeviceAddr = nullptr;
    
        aclTensor *expandX = nullptr;
        aclTensor *expertIds = nullptr;
        aclTensor *expertScales = nullptr;
        aclTensor *expandIdx = nullptr;
        aclTensor *assistInfoForCombineOut = nullptr;
        aclTensor *quantExpandXOut = nullptr;
        aclTensor *commCmdInfoOutforCombine = nullptr;
        aclTensor *xOut = nullptr;
    
        // Create tensors
        ret = CreateAclTensor(expandXHostData, expandXShape, &expandXDeviceAddr, aclDataType::ACL_FLOAT16, &expandX);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(expertIdsHostData, expertIdsShape, &expertIdsDeviceAddr, aclDataType::ACL_INT32, &expertIds);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(expertScalesHostData, expertScalesShape, &expertScalesDeviceAddr, aclDataType::ACL_FLOAT,
                            &expertScales);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(expandIdxHostData, expandIdxShape, &expandIdxDeviceAddr, aclDataType::ACL_INT32, &expandIdx);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(assistInfoForCombineOutHostData, assistInfoForCombineOutShape,
                            &assistInfoForCombineOutDeviceAddr, aclDataType::ACL_INT32, &assistInfoForCombineOut);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(quantExpandXOutHostData, quantExpandXOutShape, &quantExpandXOutDeviceAddr,
                            aclDataType::ACL_INT8, &quantExpandXOut);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(commCmdInfoOutforCombineHostData, commCmdInfoOutShapeforcombine,
                            &commCmdInfoOutforCombineDeviceAddr, aclDataType::ACL_INT32, &commCmdInfoOutforCombine);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
        ret = CreateAclTensor(xOutHostData, xOutShape, &xOutDeviceAddr, aclDataType::ACL_FLOAT16, &xOut);
        CHECK_RET(ret == ACL_SUCCESS, return ret);
    
        // Declare variables required for operator execution
        uint64_t workspaceSize = 0;
        aclOpExecutor *executor = nullptr;
        void *workspaceAddr = nullptr;
    
        /******************************Call combine_setup********************************************/
        ret = aclnnMoeDistributeCombineSetupGetWorkspaceSize(
            expandX, expertIds, assistInfoForCombineOut, hcomEpName, epWorldSize, args.epRankId, moeExpertNum,
            expertSharedType, sharedExpertNum, sharedExpertRankNum, globalBS, commQuantMode, commType, nullptr,
            quantExpandXOut, commCmdInfoOutforCombine, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS,
                LOG_PRINT("[ERROR] aclnnMoeDistributeCombineSetupGetWorkspaceSize 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] CombineSetup aclrtMalloc failed. ret = %d\n", ret);
                    return ret);
        }
    
        ret = aclnnMoeDistributeCombineSetup(workspaceAddr, workspaceSize, executor, args.combinesetupstream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnMoeDistributeCombineSetup failed. ret = %d \n", ret);
                return ret);
    
        ret = aclrtSynchronizeStreamWithTimeout(args.combinesetupstream, timeOut);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSynchronizeStreamWithTimeout failed. ret = %d \n", ret);
                return ret);
        LOG_PRINT("[INFO] device_%d aclnnMoeDistributeCombineSetup execute successfully.\n", args.rankId);
    
        /******************************Call combine_teardown********************************************/
        ret = aclnnMoeDistributeCombineTeardownGetWorkspaceSize(
            expandX, quantExpandXOut, expertIds, expandIdx, expertScales, commCmdInfoOutforCombine, nullptr, nullptr,
            hcomEpName, epWorldSize, args.epRankId, moeExpertNum, expertSharedType, sharedExpertNum, sharedExpertRankNum,
            globalBS, commQuantMode, commType, nullptr, xOut, &workspaceSize, &executor);
        CHECK_RET(ret == ACL_SUCCESS,
                LOG_PRINT("[ERROR] aclnnMoeDistributeCombineTeardownGetWorkspaceSize 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] CombineTeardown aclrtMalloc failed. ret = %d\n", ret);
                    return ret);
        }
    
        ret = aclnnMoeDistributeCombineTeardown(workspaceAddr, workspaceSize, executor, args.combineteardownstream);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclnnMoeDistributeCombineTeardown failed. ret = %d \n", ret);
                return ret);
    
        ret = aclrtSynchronizeStreamWithTimeout(args.combineteardownstream, timeOut);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSynchronizeStreamWithTimeout failed. ret = %d \n", ret);
                return ret);
        LOG_PRINT("[INFO] device_%d aclnnMoeDistributeCombineTeardown execute successfully.\n", args.rankId);
    
        // Release resources.
        if (workspaceSize > 0) {
            aclrtFree(workspaceAddr);
        }
        DestroyTensor(expandX);
        DestroyTensor(expertIds);
        DestroyTensor(expertScales);
        DestroyTensor(expandIdx);
        DestroyTensor(assistInfoForCombineOut);
        DestroyTensor(quantExpandXOut);
        DestroyTensor(commCmdInfoOutforCombine);
        DestroyTensor(xOut);
    
        FreeDeviceAddr(expandXDeviceAddr);
        FreeDeviceAddr(expertIdsDeviceAddr);
        FreeDeviceAddr(expertScalesDeviceAddr);
        FreeDeviceAddr(expandIdxDeviceAddr);
        FreeDeviceAddr(assistInfoForCombineOutDeviceAddr);
        FreeDeviceAddr(quantExpandXOutDeviceAddr);
        FreeDeviceAddr(commCmdInfoOutforCombineDeviceAddr);
        FreeDeviceAddr(xOutDeviceAddr);
    
        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 combineSetupStream[DEV_NUM];
        aclrtStream combineTeardownStream[DEV_NUM];
        aclrtContext context[DEV_NUM];
        for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) {
            ret = aclrtSetDevice(rankId);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtSetDevice failed. ret = %d \n", ret); return ret);
            ret = aclrtCreateContext(&context[rankId], rankId);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateContext failed. ret = %d \n", ret); return ret);
            ret = aclrtCreateStream(&combineSetupStream[rankId]);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateStream failed. ret = %d \n", ret); return ret);
            ret = aclrtCreateStream(&combineTeardownStream[rankId]);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] aclrtCreateStream failed. ret = %d \n", ret); return ret);
        }
    
        int32_t devices[DEV_NUM];
        for (int i = 0; i < DEV_NUM; i++) {
            devices[i] = i;
        }
    
        HcclComm comms[DEV_NUM];
        ret = HcclCommInitAll(DEV_NUM, devices, comms);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("[ERROR] HcclCommInitAll failed. ret = %d \n", ret); return ret);
    
        Args args[DEV_NUM];
        std::vector<std::unique_ptr<std::thread>> threads(DEV_NUM);
        for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) {
            args[rankId].rankId = rankId;
            args[rankId].epRankId = rankId;
            args[rankId].hcclEpComm = comms[rankId];
            args[rankId].combinesetupstream = combineSetupStream[rankId];
            args[rankId].combineteardownstream = combineTeardownStream[rankId];
            args[rankId].context = context[rankId];
            threads[rankId].reset(new (std::nothrow) std::thread(&LaunchOneProcess, std::ref(args[rankId])));
        }
        for (uint32_t rankId = 0; rankId < DEV_NUM; rankId++) {
            threads[rankId]->join();
        }
        aclFinalize();
        return 0;
    }
    <!-- end id10 -->