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 % 162 0
Test Date: 2026-08-17 10:19:35 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(
      26            0 :     const HcclDispatcher dispatcher, 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              : HcclResult
      64            0 : CollAllReduceSmallCountAivRdmaExecutor::CalcTransportMemType(TransportMemType& inputType, 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(
      70              :         "[CollAllReduceSmallCountAivRdmaExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
      71              :         tag_.c_str(), inputType, outputType);
      72            0 :     return HCCL_SUCCESS;
      73              : }
      74              : 
      75            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::CalcLevel0CommInfo(
      76              :     TransportMemType inputType, TransportMemType outputType, 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(
      85              :     TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
      86              : {
      87            0 :     CommParaInfo commParaInfo(COMM_LEVEL1, CommType::COMM_TAG_HALVING_DOUBLING);
      88            0 :     CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL1], inputType, outputType));
      89            0 :     return HCCL_SUCCESS;
      90            0 : }
      91              : 
      92              : HcclResult
      93            0 : 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(
      99              :         numBlocks_ < numBlocks,
     100              :         HCCL_WARNING(
     101              :             "[CollAllReduceSmallCountAivRdmaExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
     102              :             numBlocks_, numBlocks),
     103              :         HCCL_E_PARA);
     104              : 
     105            0 :     HCCL_INFO(
     106              :         "[CollAllReduceSmallCountAivRdmaExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
     107              :         numBlocks, numBlocks_, bestNumBlocks);
     108            0 :     return HCCL_SUCCESS;
     109              : }
     110              : 
     111            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
     112              : {
     113            0 :     HcclUs startut = TIME_NOW();
     114            0 :     allreduceSmallDataAivRdmaCount_ += 1;
     115            0 :     HCCL_INFO(
     116              :         "[CollAllReduceSmallCountAivRdmaExecutor][Orchestrate] AllreduceSmallCountAivRdma has been called [%llu].",
     117              :         allreduceSmallDataAivRdmaCount_);
     118            0 :     tag_ = param.tag;
     119            0 :     algResResp_ = &algRes;
     120              : 
     121              :     // 小数据量:使用AIVIN+AIVOUT,标记区在AIVOUT
     122            0 :     ExecMem execMem;
     123            0 :     execMem.count = param.DataDes.count;
     124            0 :     execMem.inputPtr = param.inputPtr;
     125            0 :     execMem.outputPtr = param.outputPtr;
     126            0 :     execMem.inputMem = algRes.aivInputMem;
     127            0 :     execMem.outputMem = algRes.aivOutputMem;
     128            0 :     HcclResult ret = KernelRun(param, execMem);
     129              : 
     130            0 :     CHK_PRT_RET(
     131              :         ret != HCCL_SUCCESS,
     132              :         HCCL_ERROR(
     133              :             "[CollAllReduceSmallCountAivRdmaExecutor]errNo[0x%016llx] tag[%s] executor kernel run failed",
     134              :             HCCL_ERROR_CODE(ret), param.tag.c_str()),
     135              :         ret);
     136              : 
     137            0 :     HCCL_INFO(
     138              :         "tag[%s], AllReduce executor orchestrate success, take time [%lld]us.", param.tag.c_str(),
     139              :         DURATION_US(TIME_NOW() - startut));
     140            0 :     return HCCL_SUCCESS;
     141            0 : }
     142              : 
     143            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::GetAdjInfo(AlgResourceResponse& algRes, AdjInfo& adjInfo)
     144              : {
     145            0 :     return HCCL_SUCCESS;
     146              : }
     147              : 
     148            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::InterServerHDOneshot(
     149              :     const OpParam& param, ExecMem& execMem, u32& outputOffset, u64 sliceCount, u32 dbOffset, u32 interRankSize,
     150              :     u32 interRankId, bool isOpbase, std::vector<LINK>& interLinks)
     151              : {
     152              :     (void)isOpbase;
     153            0 :     u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.outputMem, param.DataDes.dataType, param.reduceType);
     154            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot]reduceAttr is [%llu].", reduceAttr);
     155            0 :     std::unique_ptr<Sender> senderInfo;
     156            0 :     std::unique_ptr<Reducer> reducerInfo;
     157            0 :     senderInfo.reset(new (std::nothrow) Sender(param.DataDes.dataType, param.reduceType, reduceAttr));
     158            0 :     CHK_SMART_PTR_NULL(senderInfo);
     159            0 :     reducerInfo.reset(new (std::nothrow) Reducer(param.DataDes.dataType, param.reduceType, reduceAttr));
     160            0 :     CHK_SMART_PTR_NULL(reducerInfo);
     161            0 :     u32 hdStepNum = static_cast<u32>(log2(interRankSize));
     162            0 :     HCCL_INFO(
     163              :         "[CollAllReduceSmallCountAivRdmaExecutor] Find interlink type for cross-aggregation link[%d].",
     164              :         interLinks[(interRankId + 1) % A_X_AGGR_SIZE + interRankId - interRankId % A_X_AGGR_SIZE]->GetLinkType());
     165            0 :     u32 sliceSize = sliceCount * SIZE_TABLE[param.DataDes.dataType];
     166            0 :     auto opMeta = HcclOpMetaInfo::GetOneForAllReduce(
     167            0 :         0, param.DataDes.dataType, ReduceType::INLINE_REDUCE, true, 0, false, hccl::CopyPattern::BCOPY, 1, true);
     168            0 :     CHK_RET(InitTask(dispatcher_, const_cast<Stream&>(param.stream), opMeta.isEnableCache, opMeta.GetCacheKey()));
     169            0 :     for (u32 step = 1; step <= hdStepNum; step++) {
     170            0 :         u32 peerMask = 1 << (step - 1);
     171            0 :         u32 peer = interRankId ^ peerMask;
     172            0 :         HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot] Step %u, peer %u.", step, peer);
     173            0 :         u32 sliceForReadOffset = HCCL_SMALL_COUNT_2_MB + (step - 1) * sliceSize + dbOffset;
     174            0 :         u32 sliceForWriteOffset = HCCL_SMALL_COUNT_2_MB + step * sliceSize + dbOffset;
     175            0 :         DeviceMem src = execMem.inputMem.range(sliceForReadOffset, sliceSize);
     176            0 :         DeviceMem dst = execMem.inputMem.range(sliceForWriteOffset, sliceSize);
     177            0 :         CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, const_cast<Stream&>(param.stream)));
     178            0 :         interLinks[peer]->TxAck(const_cast<Stream&>(param.stream));
     179            0 :         interLinks[peer]->RxAck(const_cast<Stream&>(param.stream));
     180            0 :         if (interLinks[peer]->IsSupportTransportWithReduce()
     181            0 :             && ((interLinks[peer]->GetLinkType() == LinkType::LINK_STANDARD_ROCE)
     182            0 :                 || static_cast<bool>((RDMA_REDUCE_BITMASK & reduceAttr)))) {
     183            0 :             HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot] inter use RDMA");
     184            0 :             CHK_RET(senderInfo->run(
     185              :                 interLinks[peer], sliceForWriteOffset, src, const_cast<Stream&>(param.stream), UserMemType::INPUT_MEM));
     186            0 :             CHK_RET(reducerInfo->run(
     187              :                 dispatcher_, interLinks[peer], 0, src, src, src, const_cast<Stream&>(param.stream),
     188              :                 DstMemType::RESULT_INPUT_MEM, UserMemType::INPUT_MEM));
     189            0 :         } else if (interLinks[peer]->IsSpInlineReduce() && static_cast<bool>((INLINE_REDUCE_BITMASK & reduceAttr))) {
     190            0 :             HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][InterServerHDOneshot] inter use SDMA");
     191            0 :             CHK_RET(senderInfo->run(
     192              :                 interLinks[peer], sliceForWriteOffset, src, const_cast<Stream&>(param.stream), UserMemType::INPUT_MEM));
     193            0 :             CHK_RET(reducerInfo->run(
     194              :                 dispatcher_, interLinks[peer], sliceForReadOffset, dst, dst, dst, const_cast<Stream&>(param.stream),
     195              :                 DstMemType::RESULT_INPUT_MEM, UserMemType::INPUT_MEM));
     196              :         } else {
     197            0 :             CHK_RET(interLinks[peer]->TxAsync(
     198              :                 UserMemType::INPUT_MEM, HALF_OFFSET + sliceForWriteOffset, src.ptr(), src.size(),
     199              :                 const_cast<Stream&>(param.stream)));
     200            0 :             DeviceMem localSrc = execMem.inputMem.range(HALF_OFFSET + sliceForWriteOffset, sliceSize);
     201            0 :             CHK_RET(reducerInfo->run(
     202              :                 dispatcher_, interLinks[peer], 0, localSrc, dst, src, const_cast<Stream&>(param.stream),
     203              :                 DstMemType::RESULT_INPUT_MEM, UserMemType::INPUT_MEM));
     204            0 :         }
     205            0 :     }
     206            0 :     CHK_RET(LaunchTask(dispatcher_, const_cast<Stream&>(param.stream)));
     207            0 :     outputOffset = HCCL_SMALL_COUNT_2_MB + hdStepNum * sliceSize;
     208            0 :     return HCCL_SUCCESS;
     209            0 : }
     210              : 
     211            0 : HcclResult CollAllReduceSmallCountAivRdmaExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
     212              : {
     213            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][KernelRun]AllReduce aiv enter");
     214            0 :     HcclWorkflowMode workflow = workflowMode_;
     215            0 :     bool isOpbase = (workflow == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
     216              : 
     217              :     // 获取通信域信息
     218            0 :     CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
     219            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
     220            0 :     u32 commIndex = level0CommInfo.localRank;
     221            0 :     CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
     222            0 :     SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
     223              : 
     224              :     // 数据准备,按照server内rankSize切片
     225            0 :     u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
     226            0 :     u64 totalSize = param.DataDes.count * perDataSize;
     227            0 :     std::vector<Slice> dataSegsSlice; // 数据分成ranksize份,每份的起始偏移和大小
     228            0 :     u32 sliceNum = level0CommInfo.localRankSize;
     229            0 :     CHK_RET(PrepareSliceDataWithAlignSize(totalSize, sliceNum, 0, dataSegsSlice, perDataSize));
     230            0 :     CHK_PRT_RET(
     231              :         commIndex >= dataSegsSlice.size(),
     232              :         HCCL_ERROR(
     233              :             "[CollAllReduceSmallCountAivRdmaExecutor][Run]commIndex[%u] >= dataSegsSlice size[%zu]", commIndex,
     234              :             dataSegsSlice.size()),
     235              :         HCCL_E_INTERNAL);
     236            0 :     std::vector<hccl::LINK> intraLinks = level0CommInfo.links;
     237            0 :     std::vector<hccl::LINK> interLinks = level1CommInfo.links;
     238            0 :     u32 intraRankSize = level0CommInfo.localRankSize;
     239            0 :     u32 interRankSize = level1CommInfo.localRankSize;
     240            0 :     u32 intraRankId = level0CommInfo.localRank;
     241            0 :     u32 interRankId = level1CommInfo.localRank;
     242              : 
     243              :     // reduce scatter via AIV
     244              :     void* dataBuffers[MAX_RANK_SIZE];
     245              :     void* flagBuffers[MAX_RANK_SIZE]; // 标记区的具体偏移在kernel中决定
     246            0 :     CHK_RET(PrepareAivBuffers(
     247              :         intraRankSize, intraRankId, 0, execMem.inputMem, execMem.inputMem, intraLinks, dataBuffers, flagBuffers,
     248              :         UserMemType::INPUT_MEM, UserMemType::INPUT_MEM, 0, HCCL_MID_COUNT_32_MB));
     249              :     // RS总数据量最大1m,rs的结果存储到2m处
     250            0 :     void* rsOutput = static_cast<u8*>(execMem.inputMem.ptr()) + HCCL_SMALL_COUNT_2_MB;
     251              : 
     252            0 :     AivOpArgs opArgs{HcclCMDType::HCCL_CMD_ALLREDUCE, execMem.inputPtr, rsOutput, execMem.count,
     253            0 :                      param.DataDes.dataType,          param.reduceType, 0,        isOpbase};
     254            0 :     AivTopoArgs topoArgs{intraRankId, intraRankSize, topoAttr_.isDiffDeviceModule ? topoAttr_.devicePhyId : A_X_SIZE};
     255            0 :     topoArgs.identify = algoAttr_.identifier;
     256              :     u32 numBlocks;
     257            0 :     CHK_PRT_RET(
     258              :         CalNumBlocks(numBlocks, intraRankSize) != HCCL_SUCCESS, HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
     259              :         HCCL_E_PARA);
     260            0 :     numBlocks_ = numBlocks;
     261            0 :     AivResourceArgs resourceArgs{param.tag,  param.stream.ptr(), dataBuffers, flagBuffers, execMem.inputMem.size(),
     262            0 :                                  numBlocks_, param.aivTag};
     263            0 :     AivAlgArgs algArgs{INTRA_RS_STEP, true};
     264            0 :     algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
     265            0 :     algArgs.execTimeOutSet = true;
     266            0 :     struct AivProfilingInfo aivProfilingInfo;
     267            0 :     aivProfilingInfo.counter = opCounter_;
     268              : 
     269            0 :     CHK_RET(ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo));
     270              : 
     271              :     // use hd algo
     272            0 :     u32 arOutputOffset = 0; // 跨机allreduce的结果的位置,相对于inputMem的偏移
     273            0 :     CHK_RET(InterServerHDOneshot(
     274              :         param, execMem, arOutputOffset, dataSegsSlice[commIndex].size / perDataSize, 0, interRankSize, interRankId,
     275              :         isOpbase, interLinks));
     276            0 :     void* arOutput = static_cast<u8*>(execMem.inputMem.ptr()) + arOutputOffset;
     277              : 
     278              :     // AllGather via AIV
     279            0 :     CHK_RET(PrepareAivBuffers(
     280              :         intraRankSize, intraRankId, 0, execMem.inputMem, execMem.inputMem, intraLinks, dataBuffers, flagBuffers,
     281              :         UserMemType::INPUT_MEM, UserMemType::INPUT_MEM, HCCL_SMALL_COUNT_8_MB, HCCL_MID_COUNT_32_MB));
     282              : 
     283            0 :     opArgs.input = arOutput;
     284            0 :     opArgs.output = execMem.outputPtr;
     285            0 :     resourceArgs.buffersIn = dataBuffers;
     286            0 :     resourceArgs.buffersOut = flagBuffers;
     287            0 :     resourceArgs.aivTag = GetNextAivTag(resourceArgs.aivTag);
     288            0 :     algArgs.step = INTRA_AG_STEP;
     289              : 
     290            0 :     CHK_RET(ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo));
     291              : 
     292            0 :     HCCL_INFO("[CollAllReduceSmallCountAivRdmaExecutor][KernelRun]AllReduce aiv run success.");
     293            0 :     return HCCL_SUCCESS;
     294            0 : }
     295              : 
     296              : REGISTER_EXEC("AllReduceSmallCountAivRdmaExecutor", AllReduceSmallCountAivRdma, CollAllReduceSmallCountAivRdmaExecutor);
     297              : 
     298              : } // namespace hccl
        

Generated by: LCOV version 2.0-1