LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/impl/coll_executor/coll_all_to_all - coll_all_to_all_mesh_aiv_executor.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 65.6 % 180 118
Test Date: 2026-08-04 10:52:23 Functions: 80.0 % 10 8

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "coll_all_to_all_mesh_aiv_executor.h"
      12              : 
      13              : namespace hccl {
      14              : 
      15            5 : CollAlltoAllMeshAivExecutor::CollAlltoAllMeshAivExecutor(const HcclDispatcher dispatcher,
      16            5 :                                                          std::unique_ptr<TopoMatcher> &topoMatcher)
      17            5 :     : CollAlltoAllExecutor(dispatcher, topoMatcher)
      18              : {
      19            5 :     desc_.isAivMode = true;
      20            5 : }
      21              : 
      22            5 : HcclResult CollAlltoAllMeshAivExecutor::CalcStreamNum(u32& streamNum)
      23              : {
      24            5 :     streamNum = 0; // AIV通信不需要申请从流
      25            5 :     HCCL_INFO("[CollAlltoAllMeshAivExecutor][CalcStreamNum] tag[%s] streamNum[%u]", tag_.c_str(), streamNum);
      26            5 :     return HCCL_SUCCESS;
      27              : }
      28              : 
      29            5 : HcclResult CollAlltoAllMeshAivExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
      30              : {
      31            5 :     TransportMemType inputType = TransportMemType::RESERVED;
      32            5 :     TransportMemType outputType = TransportMemType::RESERVED;
      33            5 :     CHK_RET(CalcTransportMemType(inputType, outputType));
      34            5 :     CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
      35            5 :     return HCCL_SUCCESS;
      36              : }
      37              : 
      38            5 : HcclResult CollAlltoAllMeshAivExecutor::CalcTransportMemType(TransportMemType &inputType, TransportMemType &outputType)
      39              : {
      40            5 :     if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
      41            5 :         inputType = TransportMemType::CCL_INPUT;
      42            5 :         outputType = TransportMemType::AIV_OUTPUT;
      43              :     } else {
      44            0 :         inputType = TransportMemType::PARAM_INPUT;
      45            0 :         outputType = TransportMemType::AIV_OUTPUT;
      46              :     }
      47            5 :     HCCL_INFO("[CollAlltoAllMeshAivExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
      48              :         tag_.c_str(), inputType, outputType);
      49            5 :     return HCCL_SUCCESS;
      50              : }
      51              : 
      52            5 : HcclResult CollAlltoAllMeshAivExecutor::CalcLevel0CommInfo(TransportMemType inputType,
      53              :     TransportMemType outputType,
      54              :     std::vector<LevelNSubCommTransport>& opTransport)
      55              : {
      56            5 :     CommParaInfo commParaLevel0(COMM_MESH_L0, CommType::COMM_TAG_MESH);
      57            5 :     commParaLevel0.meshSinglePlane = true;
      58            5 :     CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_MESH_L0], inputType, outputType));
      59            5 :     return HCCL_SUCCESS;
      60            5 : }
      61              : 
      62            5 : HcclResult CollAlltoAllMeshAivExecutor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
      63              : {
      64            5 :     numBlocks = rankSize; // 默认情况使用rankSize个AIV
      65              : 
      66            5 :     bool isOpBase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
      67            5 :     if (cmdType == HcclCMDType::HCCL_CMD_ALLTOALL) {
      68            0 :         if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93 && !isOpBase) {
      69            0 :             numBlocks = rankSize * NUM_BLOCKS_FOUR_PER_RANK_A3 > MAX_NUM_BLOCKS ?
      70              :                 rankSize * NUM_BLOCKS_THREE_PER_RANK_A3 : rankSize * NUM_BLOCKS_FOUR_PER_RANK_A3;
      71            0 :         } else if (isOpBase && dataSize >= AIV_ALL_TO_ALL_BIG_SIZE) {
      72            0 :             numBlocks = NUM_BLOCKS_FACTOR_TWO * rankSize; // 单机场景,单算子AlltoAll使用2倍 rankSize个aiv
      73              :         }
      74            5 :     } else if (cmdType == HcclCMDType::HCCL_CMD_ALLTOALLVC || cmdType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
      75            5 :         if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93 &&
      76            0 :             ((isOpBase && cmdType == HcclCMDType::HCCL_CMD_ALLTOALLV) ||
      77            0 :             (!isOpBase && cmdType == HcclCMDType::HCCL_CMD_ALLTOALLVC))) {
      78              :             // A3单机单算子场景,numBlocks_为3倍或者4倍的ranksize
      79            0 :             numBlocks = rankSize * NUM_BLOCKS_FOUR_PER_RANK_A3 > MAX_NUM_BLOCKS ?
      80              :                 rankSize * NUM_BLOCKS_THREE_PER_RANK_A3 : rankSize * NUM_BLOCKS_FOUR_PER_RANK_A3;
      81            5 :         } else if (isOpBase) {
      82            5 :             numBlocks = NUM_BLOCKS_FACTOR_TWO * rankSize; // 单机场景,单算子AlltoAll使用2倍 rankSize个aiv
      83              :         }
      84              :     }
      85              : 
      86            5 :     u32 bestNumBlocks = numBlocks;
      87            5 :     if (topoAttr_.deviceType == DevType::DEV_TYPE_910B || 
      88            0 :         (isOpBase && topoAttr_.deviceType == DevType::DEV_TYPE_910_93 && cmdType != HcclCMDType::HCCL_CMD_ALLTOALLV)
      89            0 :         || (!isOpBase && topoAttr_.deviceType == DevType::DEV_TYPE_910_93 && cmdType == HcclCMDType::HCCL_CMD_ALLTOALLV)) {
      90            5 :         CHK_PRT_RET(numBlocks_ < numBlocks,
      91              :             HCCL_WARNING("[CollAlltoAllMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
      92              :             numBlocks_, numBlocks), HCCL_E_PARA);
      93            0 :     } else if ((isOpBase && topoAttr_.deviceType == DevType::DEV_TYPE_910_93 && cmdType == HcclCMDType::HCCL_CMD_ALLTOALLV)
      94            0 :         || (!isOpBase && topoAttr_.deviceType == DevType::DEV_TYPE_910_93 && cmdType != HcclCMDType::HCCL_CMD_ALLTOALLV)) {
      95            0 :         CHK_PRT_RET(numBlocks_ < rankSize,
      96              :             HCCL_WARNING("[CollAlltoAllMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
      97              :             numBlocks_, rankSize), HCCL_E_PARA);
      98            0 :         if (numBlocks_ < numBlocks) {
      99            0 :             numBlocks = numBlocks_ / rankSize * rankSize;
     100              :         }
     101              :     }
     102              : 
     103            5 :     HCCL_INFO("[CollAlltoAllMeshAivExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
     104              :         numBlocks, numBlocks_, bestNumBlocks);
     105            5 :     return HCCL_SUCCESS;
     106              : }
     107              : 
     108            5 : HcclResult CollAlltoAllMeshAivExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
     109              : {
     110            5 :     HcclUs startut = TIME_NOW();
     111            5 :     tag_ = param.tag;
     112            5 :     algResResp_ = &algRes;
     113              : 
     114            5 :     HcclResult ret = HCCL_SUCCESS;
     115            5 :     ExecMem execMem;
     116            5 :     execMem.count = param.DataDes.count;
     117            5 :     execMem.inputPtr = param.inputPtr;
     118            5 :     execMem.outputPtr = param.outputPtr;
     119              : 
     120            5 :     if (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
     121            0 :         execMem.inputMem = algRes.paramInputMem;
     122            0 :         execMem.outputMem = algRes.aivOutputMem; // 存放flag
     123            0 :         ret = KernelRun(param, execMem);
     124              :     } else {
     125            5 :         execMem.inputMem = algRes.cclInputMem;
     126            5 :         execMem.outputMem = algRes.aivOutputMem;
     127            5 :         ret = KernelRun(param, execMem);
     128              :     }
     129              : 
     130            5 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     131              :         HCCL_ERROR("[CollAlltoAllMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel run failed",
     132              :             HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
     133              : 
     134            5 :     HCCL_INFO("tag[%s], AlltoAll executor orchestrate success, take time [%lld]us.",
     135              :         param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
     136            5 :     return HCCL_SUCCESS;
     137            5 : }
     138              : 
     139            0 : HcclResult CollAlltoAllMeshAivExecutor::GetAdjInfo(AlgResourceResponse& algRes, AdjInfo& adjInfo)
     140              : {
     141            0 :     return HCCL_SUCCESS;
     142              : }
     143              : 
     144            0 : HcclResult CollAlltoAllMeshAivExecutor::GetAivExecParam(const OpParam& param, AlgResourceResponse& algRes, AivSuperKernelArgs &args)
     145              : {
     146            0 :     HcclUs startut = TIME_NOW();
     147            0 :     tag_ = param.tag;
     148            0 :     algResResp_ = &algRes;
     149              :  
     150            0 :     HcclResult ret = HCCL_SUCCESS;
     151            0 :     ExecMem execMem;
     152            0 :     execMem.count = param.All2AllDataDes.sendCount;
     153            0 :     execMem.inputPtr = param.inputPtr;
     154            0 :     execMem.outputPtr = param.outputPtr;
     155              :  
     156              :     // 单算子大数据量
     157            0 :     execMem.inputMem = algRes.paramInputMem;
     158            0 :     execMem.outputMem = algRes.aivOutputMem;
     159              : 
     160            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_MESH_L0, COMM_INDEX_0);
     161              :  
     162            0 :     u32 localRank = level0CommInfo.localRank;
     163            0 :     u32 localRankSize = level0CommInfo.localRankSize;
     164            0 :     HCCL_DEBUG("[CollAlltoAllMeshAivExecutor][GetAivExecParam] userRank [%d] localRank [%d]",
     165              :         topoAttr_.userRank, localRank);
     166              :  
     167            0 :     for (u32 i = 0; i < localRankSize; i++) {
     168            0 :         if (i != localRank) {
     169            0 :             CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(args.buffersIn[i])));
     170            0 :             CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(args.buffersOut[i])));
     171              :         } else {
     172            0 :             args.buffersIn[i] = execMem.inputMem.ptr();
     173            0 :             args.buffersOut[i] = execMem.outputMem.ptr();
     174              :         }
     175              :     }
     176            0 :     args.rank = localRank;
     177            0 :     args.rankSize = localRankSize;
     178            0 :     args.len = execMem.count;
     179            0 :     args.dataType = param.All2AllDataDes.sendType;
     180            0 :     args.unitSize = SIZE_TABLE[param.All2AllDataDes.sendType];
     181            0 :     args.devType = static_cast<u32>(topoAttr_.deviceType);
     182            0 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     183              :         HCCL_ERROR("[CollAlltoAllMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
     184              :             "run failed", HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
     185              :  
     186            0 :     HCCL_INFO("tag[%s], AlltoAll executor getalgexecparam success, take time [%lld]us.",
     187              :         param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
     188            0 :     return HCCL_SUCCESS;
     189            0 : }
     190              : 
     191            5 : HcclResult CollAlltoAllMeshAivExecutor::KernelRun(const OpParam &param, ExecMem &execMem)
     192              : {
     193            5 :     HCCL_CONFIG_INFO(HCCL_ALG, "[CollAlltoAllMeshAivExecutor][KernelRun]AllToAll aiv enter");
     194              : 
     195            5 :     CHK_RET(CheckCommSize(COMM_MESH_L0, COMM_INDEX_0 + 1));
     196            5 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_MESH_L0, COMM_INDEX_0);
     197              : 
     198              :     void *buffersIn[MAX_RANK_SIZE];
     199              :     void *buffersOut[MAX_RANK_SIZE];
     200              : 
     201            5 :     u32 localRank = level0CommInfo.localRank;
     202            5 :     u32 localRankSize = level0CommInfo.localRankSize;
     203            5 :     HCCL_DEBUG("[CollAlltoAllMeshAivExecutor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank, localRank);
     204              : 
     205           10 :     for (u32 i = 0; i < localRankSize; i++) {
     206            5 :         if (i != localRank) {
     207            0 :             CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersIn[i])));
     208            0 :             CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersOut[i])));
     209              :         } else {
     210            5 :             buffersIn[i] = execMem.inputMem.ptr();
     211            5 :             buffersOut[i] = execMem.outputMem.ptr();
     212              :         }
     213              :     }
     214              : 
     215            5 :     ExtraArgs extraArgs;
     216            5 :     bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
     217              :     HcclResult ret;
     218            5 :     u64 dataSize = (param.opType == HcclCMDType::HCCL_CMD_ALLTOALL ?
     219            0 :         param.All2AllDataDes.sendCount * SIZE_TABLE[param.All2AllDataDes.sendType] : 0);
     220              : 
     221            5 :     AivOpArgs opArgs {
     222            5 :             HcclCMDType::HCCL_CMD_ALLTOALL, execMem.inputPtr, execMem.outputPtr, 0,
     223            5 :             param.All2AllDataDes.sendType, HCCL_REDUCE_RESERVED, 0, isOpbase
     224            5 :     };
     225           10 :     AivTopoArgs topoArgs { localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType };
     226            5 :     topoArgs.identify = algoAttr_.identifier;
     227            5 :     AivResourceArgs resourceArgs {
     228           20 :         param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(), 0, param.aivTag
     229            5 :     };
     230            5 :     AivAlgArgs algArgs {};
     231            5 :     algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
     232            5 :     algArgs.execTimeOutSet = true;
     233            5 :     struct AivProfilingInfo aivProfilingInfo;
     234            5 :     aivProfilingInfo.counter = opCounter_;
     235              : 
     236            5 :     if (param.opType == HcclCMDType::HCCL_CMD_ALLTOALL && ((isOpbase && dataSize < AIV_ALL_TO_ALL_BIG_SIZE) ||
     237            0 :         (!isOpbase && topoAttr_.deviceType == DevType::DEV_TYPE_910_93))) {
     238            0 :         opArgs.count = param.All2AllDataDes.sendCount;
     239              :         u32 numBlocks;
     240            0 :         CHK_PRT_RET(CalNumBlocks(numBlocks, localRankSize, dataSize, opArgs.cmdType) != HCCL_SUCCESS,
     241              :             HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
     242              :             HCCL_E_PARA);
     243            0 :         numBlocks_ = numBlocks;
     244            0 :         resourceArgs.numBlocks = numBlocks_;
     245            0 :         ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
     246            5 :     } else if (param.opType == HcclCMDType::HCCL_CMD_ALLTOALLVC || param.opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
     247            2 :         for (u32 i = 0; i < localRankSize; i++) {
     248            1 :             u64 rankCount = 0;
     249            2 :             for (u32 j = 0; j < localRankSize; j++) {
     250            1 :                 u64 curSendCount =
     251            1 :                     *(static_cast<const u64 *>(param.All2AllDataDes.sendCountMatrix) + i * localRankSize + j);
     252            1 :                 extraArgs.sendCountMatrix[i * localRankSize + j] = curSendCount;
     253            1 :                 rankCount += curSendCount;
     254              :             }
     255            1 :             if (rankCount > extraArgs.maxCount) {
     256            1 :                 extraArgs.maxCount = rankCount;
     257              :             }
     258              :         }
     259            1 :         opArgs.count = extraArgs.maxCount;
     260            1 :         opArgs.cmdType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
     261              :         u32 numBlocks;
     262            1 :         CHK_PRT_RET(CalNumBlocks(numBlocks, localRankSize, dataSize, opArgs.cmdType) != HCCL_SUCCESS,
     263              :             HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
     264              :             HCCL_E_PARA);
     265            1 :         numBlocks_ = numBlocks;
     266            1 :         resourceArgs.numBlocks = numBlocks_;
     267            1 :         ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo);
     268            1 :     } else {
     269            8 :         for (u32 i = 0; i < localRankSize; i++) {
     270            4 :             extraArgs.sendCounts[i] = *(static_cast<const u64 *>(param.All2AllDataDes.sendCounts) + i);
     271            4 :             extraArgs.sendDispls[i] = *(static_cast<const u64 *>(param.All2AllDataDes.sdispls) + i);
     272            4 :             extraArgs.recvCounts[i] = *(static_cast<const u64 *>(param.All2AllDataDes.recvCounts) + i);
     273            4 :             extraArgs.recvDispls[i] = *(static_cast<const u64 *>(param.All2AllDataDes.rdispls) + i);
     274              :         }
     275              : 
     276            4 :         opArgs.cmdType = HcclCMDType::HCCL_CMD_ALLTOALLV;
     277              :         u32 numBlocks;
     278            4 :         CHK_PRT_RET(CalNumBlocks(numBlocks, localRankSize, dataSize, opArgs.cmdType) != HCCL_SUCCESS,
     279              :             HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
     280              :             HCCL_E_PARA);
     281            4 :         numBlocks_ = numBlocks;
     282            4 :         resourceArgs.numBlocks = numBlocks_;
     283            4 :         ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo);
     284              :     }
     285            5 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     286              :         HCCL_ERROR("[CollAlltoAllMeshAivExecutor][KernelRun]AllToAll aiv failed, return[%d]", ret), ret);
     287              : 
     288            5 :     if (param.opType == HcclCMDType::HCCL_CMD_ALLTOALL && isOpbase) {
     289            0 :         CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, false));
     290              :     }
     291              : 
     292            5 :     HCCL_INFO("[CollAlltoAllMeshAivExecutor][KernelRun]AllToAll aiv run success");
     293            5 :     return HCCL_SUCCESS;
     294            5 : }
     295              : 
     296              : REGISTER_EXEC("AlltoAllMeshAivExecutor", AlltoAllMeshAiv, CollAlltoAllMeshAivExecutor);
     297              : 
     298              : } // namespace hccl
        

Generated by: LCOV version 2.0-1