LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/impl/coll_executor/coll_all_reduce - coll_all_reduce_small_count_aiv_rdma_executor.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 164 0
Test Date: 2026-07-28 12:11:00 Functions: 0.0 % 11 0

            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_reduce_small_count_aiv_rdma_executor.h"
      12              : #include "sender.h"
      13              : #include "reducer.h"
      14              : 
      15              : namespace hccl {
      16              : constexpr s32 INTRA_RS_STEP = 0;
      17              : constexpr s32 INTER_AR_STEP = 1;
      18              : constexpr s32 INTRA_AG_STEP = 2;
      19              : constexpr u32 A_X_AGGR_SIZE = 2;
      20              : constexpr u32 A_X_SIZE = 16;
      21              : constexpr u64 HALF_OFFSET = 16 * 1024 * 1024;
      22              : 
      23              : u64 CollAllReduceSmallCountAivRdmaExecutor::allreduceSmallDataAivRdmaCount_ = 0;
      24              :  
      25            0 : CollAllReduceSmallCountAivRdmaExecutor::CollAllReduceSmallCountAivRdmaExecutor(const HcclDispatcher dispatcher,
      26            0 :     std::unique_ptr<TopoMatcher> &topoMatcher)
      27            0 :     : CollAllReduceExecutor(dispatcher, topoMatcher)
      28              : {
      29            0 :     DMAReduceFlag_ = false;
      30            0 :     desc_.isAivMode = true;
      31            0 :     desc_.aivTagNum = AIV_A2_ALL_REDUCE_RDMA_KERNEL_NUM;
      32            0 : }
      33              : 
      34            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalcStreamNum(u32& streamNum)
      35              : {
      36            0 :     u32 totalStreamNum = topoAttr_.deviceNumPerAggregation > 1U ? topoAttr_.deviceNumPerAggregation - 1U : 1U;
      37            0 :     streamNum = totalStreamNum - 1U;
      38            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][CalcStreamNum] tag[%s] streamNum[%u]", tag_.c_str(), streamNum);
      39            0 :     return HCCL_SUCCESS;
      40              : }
      41              : 
      42            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
      43              : {
      44            0 :     TransportMemType inputType = TransportMemType::RESERVED;
      45            0 :     TransportMemType outputType = TransportMemType::RESERVED;
      46            0 :     CHK_RET(CalcTransportMemType(inputType, outputType));
      47            0 :     CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
      48            0 :     CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
      49              : 
      50              :     // aiv+rdma小数据量在server间使用HD通信域,并在多机A+X场景下当未设置使用RDMA时,默认使用PCIE
      51            0 :     bool isSingleAX = topoAttr_.serverNum == 1 && topoAttr_.moduleNum == 2;    // A+X单机跨module
      52            0 :     if (topoMatcher_->GetExternalInputIntraRoceSwitch() == 0 && isSingleAX) {
      53            0 :         std::vector<SingleSubCommTransport> &commTransportLevel1 = opTransport[COMM_LEVEL1];
      54            0 :         for (u32 ringIndex = 0; ringIndex < commTransportLevel1.size(); ringIndex++) {
      55            0 :             for (auto &transportRequest : commTransportLevel1[ringIndex].transportRequests) {
      56            0 :                 transportRequest.isUsedRdma = false;
      57              :             }
      58              :         }
      59              :     }
      60            0 :     return HCCL_SUCCESS;
      61              : }
      62              : 
      63            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalcTransportMemType(TransportMemType &inputType,
      64              :     TransportMemType &outputType)
      65              : {
      66              :     // 小数据量:使用AIVIN+AIVOUT,标记区在AIVOUT,RS前从inputPtr到AIVIN做本地拷贝
      67            0 :     inputType = TransportMemType::AIV_INPUT;
      68            0 :     outputType = TransportMemType::AIV_OUTPUT;
      69            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
      70              :         tag_.c_str(), inputType, outputType);
      71            0 :     return HCCL_SUCCESS;
      72              : }
      73              : 
      74            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalcLevel0CommInfo(TransportMemType inputType,
      75              :     TransportMemType outputType,
      76              :     std::vector<LevelNSubCommTransport>& opTransport)
      77              : {
      78            0 :     CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
      79            0 :     commParaLevel0.meshSinglePlane = true;
      80            0 :     CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
      81            0 :     return HCCL_SUCCESS;
      82            0 : }
      83              : 
      84            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalcLevel1CommInfo(TransportMemType inputType,
      85              :     TransportMemType outputType,
      86              :     std::vector<LevelNSubCommTransport>& opTransport)
      87              : {
      88            0 :     CommParaInfo commParaInfo(COMM_LEVEL1, CommType::COMM_TAG_HALVING_DOUBLING);
      89            0 :     CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL1], inputType, outputType));
      90            0 :     return HCCL_SUCCESS;
      91            0 : }
      92              : 
      93            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
      94              : {
      95            0 :     numBlocks = rankSize; // 默认情况使用rankSize个AIV
      96            0 :     u32 bestNumBlocks = numBlocks;
      97              : 
      98            0 :     CHK_PRT_RET(numBlocks_ < numBlocks,
      99              :         HCCL_WARNING("[CollAllReduceSmallCountAivRdmaExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
     100              :         numBlocks_, numBlocks), HCCL_E_PARA);
     101              : 
     102            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
     103              :         numBlocks, numBlocks_, bestNumBlocks);
     104            0 :     return HCCL_SUCCESS;
     105              : }
     106              : 
     107            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
     108              : {
     109            0 :     HcclUs startut = TIME_NOW();
     110            0 :     allreduceSmallDataAivRdmaCount_ += 1;
     111            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][Orchestrate] AllreduceSmallCountAivRdma has been called [%llu].",
     112              :         allreduceSmallDataAivRdmaCount_);
     113            0 :     tag_ = param.tag;
     114            0 :     algResResp_ = &algRes;
     115              : 
     116              :     // 小数据量:使用AIVIN+AIVOUT,标记区在AIVOUT
     117            0 :     ExecMem execMem;
     118            0 :     execMem.count = param.DataDes.count;
     119            0 :     execMem.inputPtr = param.inputPtr;
     120            0 :     execMem.outputPtr = param.outputPtr;
     121            0 :     execMem.inputMem = algRes.aivInputMem;
     122            0 :     execMem.outputMem = algRes.aivOutputMem;
     123            0 :     HcclResult ret = KernelRun(param, execMem);
     124              : 
     125            0 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     126              :         HCCL_ERROR("[CollAllReduceSmallCountAivRdmaExecutor]errNo[0x%016llx] tag[%s] executor kernel run failed",
     127              :             HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
     128              : 
     129            0 :     HCCL_INFO("tag[%s], AllReduce executor orchestrate success, take time [%lld]us.",
     130              :         param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
     131            0 :     return HCCL_SUCCESS;
     132            0 : }
     133              : 
     134            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::GetAdjInfo(AlgResourceResponse& algRes, AdjInfo& adjInfo)
     135              : {
     136            0 :     return HCCL_SUCCESS;
     137              : }
     138              : 
     139            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::InterServerHDOneshot(const OpParam &param, ExecMem &execMem,
     140              :     u32 &outputOffset, u64 sliceCount, u32 dbOffset, u32 interRankSize, u32 interRankId, bool isOpbase,
     141              :     std::vector<LINK> &interLinks)
     142              : {
     143              :     (void) isOpbase;
     144            0 :     u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.outputMem, param.DataDes.dataType, param.reduceType);
     145            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot]reduceAttr is [%llu].", reduceAttr);
     146            0 :     std::unique_ptr<Sender> senderInfo;
     147            0 :     std::unique_ptr<Reducer> reducerInfo;
     148            0 :     senderInfo.reset(new (std::nothrow) Sender(param.DataDes.dataType, param.reduceType, reduceAttr));
     149            0 :     CHK_SMART_PTR_NULL(senderInfo);
     150            0 :     reducerInfo.reset(new (std::nothrow) Reducer(param.DataDes.dataType, param.reduceType, reduceAttr));
     151            0 :     CHK_SMART_PTR_NULL(reducerInfo);
     152            0 :     u32 hdStepNum = static_cast<u32>(log2(interRankSize));
     153            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor] Find interlink type for cross-aggregation link[%d].",
     154              :         interLinks[(interRankId + 1) % A_X_AGGR_SIZE  + interRankId- interRankId % A_X_AGGR_SIZE]->GetLinkType());
     155            0 :     u32 sliceSize = sliceCount * SIZE_TABLE[param.DataDes.dataType];
     156            0 :     auto opMeta = HcclOpMetaInfo::GetOneForAllReduce(0, param.DataDes.dataType, ReduceType::INLINE_REDUCE,
     157              :                 true, 0, false, hccl::CopyPattern::BCOPY, 1, true);
     158            0 :     CHK_RET(InitTask(dispatcher_, const_cast<Stream&>(param.stream), opMeta.isEnableCache, opMeta.GetCacheKey()));
     159            0 :     for (u32 step = 1; step <= hdStepNum; step++) {
     160            0 :         u32 peerMask = 1 << (step - 1);
     161            0 :         u32 peer = interRankId ^ peerMask;
     162            0 :         HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot] Step %u, peer %u.", step, peer);
     163            0 :         u32 sliceForReadOffset = HCCL_SMALL_COUNT_2_MB + (step - 1) * sliceSize + dbOffset;
     164            0 :         u32 sliceForWriteOffset = HCCL_SMALL_COUNT_2_MB + step * sliceSize + dbOffset;
     165            0 :         DeviceMem src = execMem.inputMem.range(sliceForReadOffset, sliceSize);
     166            0 :         DeviceMem dst = execMem.inputMem.range(sliceForWriteOffset, sliceSize);
     167            0 :         CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, const_cast<Stream&>(param.stream)));
     168            0 :         interLinks[peer]->TxAck(const_cast<Stream&>(param.stream));
     169            0 :         interLinks[peer]->RxAck(const_cast<Stream&>(param.stream));
     170            0 :         if (interLinks[peer]->IsSupportTransportWithReduce() && 
     171            0 :             ((interLinks[peer]->GetLinkType() == LinkType::LINK_STANDARD_ROCE) ||
     172            0 :             static_cast<bool>((RDMA_REDUCE_BITMASK & reduceAttr)))) {
     173            0 :             HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot] inter use RDMA");
     174            0 :             CHK_RET(senderInfo->run(interLinks[peer], sliceForWriteOffset, src, const_cast<Stream&>(param.stream),
     175              :                 UserMemType::INPUT_MEM));
     176            0 :             CHK_RET(reducerInfo->run(dispatcher_, interLinks[peer], 0, src, src, src, 
     177              :                 const_cast<Stream&>(param.stream), DstMemType::RESULT_INPUT_MEM, UserMemType::INPUT_MEM));
     178            0 :         } else if (interLinks[peer]->IsSpInlineReduce() && static_cast<bool>((INLINE_REDUCE_BITMASK & reduceAttr))) {
     179            0 :             HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot] inter use SDMA");
     180            0 :             CHK_RET(senderInfo->run(interLinks[peer], sliceForWriteOffset, src, const_cast<Stream&>(param.stream),
     181              :                 UserMemType::INPUT_MEM));
     182            0 :             CHK_RET(reducerInfo->run(dispatcher_, interLinks[peer], sliceForReadOffset, dst, dst, dst, 
     183              :                 const_cast<Stream&>(param.stream), DstMemType::RESULT_INPUT_MEM, UserMemType::INPUT_MEM));
     184              :         } else {
     185            0 :             CHK_RET(interLinks[peer]->TxAsync(UserMemType::INPUT_MEM, HALF_OFFSET + sliceForWriteOffset, 
     186              :                 src.ptr(), src.size(), const_cast<Stream&>(param.stream)));
     187            0 :             DeviceMem localSrc = execMem.inputMem.range(HALF_OFFSET + sliceForWriteOffset, sliceSize);
     188            0 :             CHK_RET(reducerInfo->run(dispatcher_, interLinks[peer], 0, localSrc, dst, src, 
     189              :                 const_cast<Stream&>(param.stream), DstMemType::RESULT_INPUT_MEM, UserMemType::INPUT_MEM));
     190            0 :         }
     191            0 :     }
     192            0 :     CHK_RET(LaunchTask(dispatcher_, const_cast<Stream&>(param.stream)));
     193            0 :     outputOffset = HCCL_SMALL_COUNT_2_MB + hdStepNum * sliceSize;
     194            0 :     return HCCL_SUCCESS;
     195            0 : }
     196              : 
     197            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::KernelRun(const OpParam &param, ExecMem &execMem)
     198              : {
     199            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][KernelRun]AllReduce aiv enter");
     200            0 :     HcclWorkflowMode workflow = workflowMode_;
     201            0 :     bool isOpbase = (workflow == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
     202              : 
     203              :     // 获取通信域信息
     204            0 :     CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
     205            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
     206            0 :     u32 commIndex = level0CommInfo.localRank;
     207            0 :     CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
     208            0 :     SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
     209              : 
     210              :     // 数据准备,按照server内rankSize切片
     211            0 :     u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
     212            0 :     u64 totalSize = param.DataDes.count * perDataSize;
     213            0 :     std::vector<Slice> dataSegsSlice;   // 数据分成ranksize份,每份的起始偏移和大小
     214            0 :     u32 sliceNum = level0CommInfo.localRankSize;
     215            0 :     CHK_RET(PrepareSliceDataWithAlignSize(totalSize, sliceNum, 0, dataSegsSlice, perDataSize));
     216            0 :     CHK_PRT_RET(commIndex >= dataSegsSlice.size(),
     217              :         HCCL_ERROR("[CollAllReduceSmallCountAivRdmaExecutor][Run]commIndex[%u] >= dataSegsSlice size[%zu]", commIndex,
     218              :         dataSegsSlice.size()), HCCL_E_INTERNAL);
     219            0 :     std::vector<hccl::LINK> intraLinks = level0CommInfo.links;
     220            0 :     std::vector<hccl::LINK> interLinks = level1CommInfo.links;
     221            0 :     u32 intraRankSize = level0CommInfo.localRankSize;
     222            0 :     u32 interRankSize = level1CommInfo.localRankSize;
     223            0 :     u32 intraRankId = level0CommInfo.localRank;
     224            0 :     u32 interRankId = level1CommInfo.localRank;
     225              : 
     226              :     // reduce scatter via AIV
     227              :     void* dataBuffers[MAX_RANK_SIZE];
     228              :     void* flagBuffers[MAX_RANK_SIZE];  // 标记区的具体偏移在kernel中决定
     229            0 :     CHK_RET(PrepareAivBuffers(intraRankSize, intraRankId, 0, execMem.inputMem, execMem.inputMem, intraLinks,
     230              :         dataBuffers, flagBuffers, UserMemType::INPUT_MEM, UserMemType::INPUT_MEM, 0, HCCL_MID_COUNT_32_MB));
     231              :     // RS总数据量最大1m,rs的结果存储到2m处
     232            0 :     void* rsOutput = static_cast<u8 *>(execMem.inputMem.ptr()) + HCCL_SMALL_COUNT_2_MB;
     233              :  
     234            0 :     AivOpArgs opArgs {
     235            0 :         HcclCMDType::HCCL_CMD_ALLREDUCE, execMem.inputPtr, rsOutput, execMem.count,
     236            0 :         param.DataDes.dataType, param.reduceType, 0, isOpbase
     237            0 :     };
     238              :     AivTopoArgs topoArgs {
     239            0 :         intraRankId, intraRankSize, topoAttr_.isDiffDeviceModule ? topoAttr_.devicePhyId : A_X_SIZE
     240            0 :     };
     241            0 :     topoArgs.identify = algoAttr_.identifier;
     242              :     u32 numBlocks;
     243            0 :     CHK_PRT_RET(CalNumBlocks(numBlocks, intraRankSize) != HCCL_SUCCESS,
     244              :         HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
     245              :         HCCL_E_PARA);
     246            0 :     numBlocks_ = numBlocks;
     247              :     AivResourceArgs resourceArgs {
     248            0 :         param.tag, param.stream.ptr(), dataBuffers, flagBuffers, execMem.inputMem.size(), numBlocks_, param.aivTag
     249            0 :     };
     250            0 :     AivAlgArgs algArgs { INTRA_RS_STEP, true };
     251            0 :     algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
     252            0 :     algArgs.execTimeOutSet = true;
     253            0 :     struct AivProfilingInfo aivProfilingInfo;
     254            0 :     aivProfilingInfo.counter = opCounter_;
     255              :  
     256            0 :     CHK_RET(ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo));
     257              :  
     258              :     // use hd algo
     259            0 :     u32 arOutputOffset = 0;  // 跨机allreduce的结果的位置,相对于inputMem的偏移
     260            0 :     CHK_RET(InterServerHDOneshot(param, execMem, arOutputOffset, dataSegsSlice[commIndex].size / perDataSize,
     261              :         0, interRankSize, interRankId, isOpbase, interLinks));
     262            0 :     void *arOutput = static_cast<u8 *>(execMem.inputMem.ptr()) + arOutputOffset;
     263              : 
     264              :     // AllGather via AIV
     265            0 :     CHK_RET(PrepareAivBuffers(intraRankSize, intraRankId, 0, execMem.inputMem, execMem.inputMem, intraLinks,
     266              :         dataBuffers, flagBuffers, UserMemType::INPUT_MEM, UserMemType::INPUT_MEM, HCCL_SMALL_COUNT_8_MB,
     267              :         HCCL_MID_COUNT_32_MB));
     268              :  
     269            0 :     opArgs.input = arOutput;
     270            0 :     opArgs.output = execMem.outputPtr;
     271            0 :     resourceArgs.buffersIn = dataBuffers;
     272            0 :     resourceArgs.buffersOut = flagBuffers;
     273            0 :     resourceArgs.aivTag = GetNextAivTag(resourceArgs.aivTag);
     274            0 :     algArgs.step = INTRA_AG_STEP;
     275              : 
     276            0 :     CHK_RET(ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo));
     277              : 
     278            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][KernelRun]AllReduce aiv run success.");
     279            0 :     return HCCL_SUCCESS;
     280            0 : }
     281              : 
     282              : REGISTER_EXEC("AllReduceSmallCountAivRdmaExecutor", AllReduceSmallCountAivRdma, CollAllReduceSmallCountAivRdmaExecutor);
     283              : 
     284              : } // namespace hccl
        

Generated by: LCOV version 2.0-1