LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/impl/coll_executor/coll_all_reduce - coll_all_reduce_mesh_opbase_mid_count_deterministic_executor.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 171 0
Test Date: 2026-07-28 12:11:00 Functions: 0.0 % 13 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_mesh_opbase_mid_count_deterministic_executor.h"
      12              : 
      13              : namespace hccl {
      14            0 : CollAllReduceMeshOpbaseMidCountDeterministicExecutor::CollAllReduceMeshOpbaseMidCountDeterministicExecutor(
      15            0 :     const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher> &topoMatcher)
      16            0 :     : CollAllReduceExecutor(dispatcher, topoMatcher)
      17              : {
      18            0 :     DMAReduceFlag_ = true;
      19            0 : }
      20              : 
      21            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::CalcStreamNum(u32& streamNum)
      22              : {
      23            0 :     const u32 level0AlltoallStreamNum = topoAttr_.deviceNumPerAggregation - 1;
      24            0 :     const u32 level0LocalReduceStreamNum = 1 << static_cast<int>(std::floor(log2(topoAttr_.deviceNumPerAggregation)));
      25            0 :     streamNum = level0AlltoallStreamNum + level0LocalReduceStreamNum;
      26              :     
      27            0 :     HCCL_INFO("[%s]tag[%s] level0AlltoallStreamNum[%u], level0LocalReduceStreamNum[%u], streamNum[%u]", __func__,
      28              :         tag_.c_str(), level0AlltoallStreamNum, level0LocalReduceStreamNum, streamNum);
      29            0 :     return HCCL_SUCCESS;
      30              : }
      31              : 
      32            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
      33              : {
      34            0 :     TransportMemType inputType = TransportMemType::RESERVED;
      35            0 :     TransportMemType outputType = TransportMemType::RESERVED;
      36            0 :     CHK_RET(CalcTransportMemType(inputType, outputType));
      37            0 :     CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
      38            0 :     CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
      39            0 :     return HCCL_SUCCESS;
      40              : }
      41              : 
      42              : 
      43            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::CalcTransportMemType(TransportMemType &inputType,
      44              :     TransportMemType &outputType)
      45              : {
      46            0 :     inputType = TransportMemType::CCL_INPUT;
      47            0 :     outputType = TransportMemType::CCL_OUTPUT;
      48            0 :     HCCL_INFO("[CollAllReduceMeshOpbaseMidCountDeterministicExecutor][CalcTransportMemType]" \
      49              :         "tag[%s] inputType[%d], outputType[%d]",
      50              :         tag_.c_str(), inputType, outputType);
      51            0 :     return HCCL_SUCCESS;
      52              : }
      53              : 
      54            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::CalcLevel0CommInfo(TransportMemType inputType,
      55              :     TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
      56              : {   
      57            0 :     CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
      58            0 :     commParaLevel0.meshSinglePlane = true;
      59            0 :     CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
      60            0 :     return HCCL_SUCCESS;
      61            0 : }
      62              : 
      63            0 : bool CollAllReduceMeshOpbaseMidCountDeterministicExecutor::IsHugeData(const u64 curSize)
      64              : {
      65            0 :     bool hugeData = curSize / topoAttr_.deviceNumPerAggregation / HCCL_INTERNODE_MAX_DATA_RATE > RDMA_SEND_MAX_SIZE ||
      66              :         curSize > SDMA_SEND_MAX_SIZE;
      67            0 :     HCCL_DEBUG("[%s]isHugeData[%d], curSize[%llu], topoAttr_.deviceNumPerAggregation[%u]",
      68              :         __func__, hugeData, curSize, topoAttr_.deviceNumPerAggregation);
      69            0 :     return hugeData;
      70              : }
      71              : 
      72            0 : bool CollAllReduceMeshOpbaseMidCountDeterministicExecutor::IsSmallData(const u64 totalSize, const u64 curSize)
      73              : {
      74            0 :     bool smallData = IsAllReduceSmallData(curSize);
      75            0 :     return smallData;
      76              : }
      77              : 
      78            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::PrepareSlicesInfo(const OpParam &param,
      79              :     const ExecMem &execMem, std::vector<Slice>& dataSegsSlice, GroupSlicesInfo& groupSlicesInfo, const u32 sliceSize)
      80              : {
      81            0 :     const u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
      82            0 :     MemBlockInfo memInfo;
      83            0 :     u64 sizePerBlock = (execMem.count + sliceSize - 1) / sliceSize * perDataSize;
      84            0 :     sizePerBlock = AlgTemplateBase::RoundUpWithDivisor(sizePerBlock, HCCL_MIN_SLICE_ALIGN);
      85            0 :     const u64 totalSize = execMem.count * perDataSize;
      86            0 :     u64 sizeRemain = totalSize;
      87            0 :     for (u32 dataId = 0; dataId < sliceSize; dataId ++) {
      88            0 :         u64 size = (sizeRemain > sizePerBlock) ? sizePerBlock : sizeRemain;
      89            0 :         u64 offset = totalSize - sizeRemain;
      90            0 :         memInfo.size.push_back(size);
      91            0 :         memInfo.userInputOffsets.push_back(offset);
      92            0 :         memInfo.inputOffsets.push_back(offset);
      93            0 :         memInfo.outputOffsets.push_back(offset);
      94            0 :         Slice slice{offset, size};
      95            0 :         dataSegsSlice.emplace_back(std::move(slice));
      96            0 :         sizeRemain -= size;
      97              :     }
      98            0 :     groupSlicesInfo.push_back(memInfo);
      99            0 :     return HCCL_SUCCESS;
     100            0 : }
     101              : 
     102            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::RunReduceScatterLevel0(const OpParam &param,
     103              :     ExecMem &execMem, GroupSlicesInfo& groupSlicesInfo)
     104              : {
     105            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
     106            0 :     std::unique_ptr<AlgTemplateBase> level0TempAlg;
     107            0 :     const u32 all2allOffset = 0;
     108            0 :     level0TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
     109            0 :         TemplateType::TEMPLATE_REDUCESCATTER_PLANT_LOCAL_REDUCE, dispatcher_);
     110            0 :     CHK_SMART_PTR_NULL(level0TempAlg);
     111              :     
     112            0 :     CHK_RET(level0TempAlg->Prepare(execMem.inputPtr, execMem.inputMem, execMem.outputMem, param.stream,
     113              :         algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux, groupSlicesInfo,
     114              :         param.reduceType, all2allOffset, param.DataDes.dataType, true, true));
     115              : 
     116            0 :     CHK_RET(level0TempAlg->RegisterProfiler(
     117              :         (level0CommInfo.localRankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level0CommInfo.localRank,
     118              :         PROF_STAGE_2, HCCL_EXEC_STEP_NOT_SET, param.stream));
     119            0 :     CHK_RET(RunTemplate(level0TempAlg, level0CommInfo));
     120            0 :     return HCCL_SUCCESS;
     121            0 : }
     122              : 
     123            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::RunAllReduceLevel1(const OpParam &param,
     124              :     ExecMem &execMem, const std::vector<Slice>& dataSegsSlice)
     125              : {
     126            0 :     std::unique_ptr<AlgTemplateBase> level1TempAlg;
     127            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
     128            0 :     const u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
     129            0 :     const u32 commIndex = level0CommInfo.localRank;
     130              : 
     131            0 :     CHK_PRT_RET(commIndex >= dataSegsSlice.size(),
     132              :         HCCL_ERROR("[%s]commIndex[%u] >= dataSegsSlice size[%zu]", __func__, commIndex,
     133              :         dataSegsSlice.size()), HCCL_E_INTERNAL);
     134              : 
     135            0 :     CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
     136            0 :     SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
     137              : 
     138            0 :     DeviceMem allreduceInput = execMem.inputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
     139            0 :     CHK_SMART_PTR_NULL(allreduceInput);
     140            0 :     DeviceMem allreduceOutput = execMem.outputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
     141            0 :     CHK_SMART_PTR_NULL(allreduceOutput);
     142              : 
     143            0 :     const u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.outputMem, param.DataDes.dataType, param.reduceType);
     144              : 
     145            0 :     if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
     146            0 :         level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_RING,
     147            0 :             dispatcher_);
     148            0 :         HCCL_INFO("[%s]: using ring algo inter-server.", __func__);
     149            0 :         CHK_SMART_PTR_NULL(level1TempAlg);
     150            0 :         CHK_RET(level1TempAlg->Prepare(reduceAttr));
     151            0 :     } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
     152            0 :         level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
     153            0 :             TemplateType::TEMPLATE_ALL_REDUCE_NHR, dispatcher_);
     154            0 :         HCCL_INFO("[%s]: using nhr algo inter-server.", __func__);
     155            0 :         CHK_SMART_PTR_NULL(level1TempAlg);
     156            0 :         CHK_RET(level1TempAlg->Prepare(reduceAttr));
     157            0 :         level1TempAlg->CloseBarrier();
     158            0 :     } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR_V1) {
     159            0 :         level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
     160            0 :             TemplateType::TEMPLATE_ALL_REDUCE_NHR_V1, dispatcher_);
     161            0 :         HCCL_INFO("[%s]: using nhr_v1 algo inter-server.", __func__);
     162            0 :         CHK_SMART_PTR_NULL(level1TempAlg);
     163            0 :         CHK_RET(level1TempAlg->Prepare(reduceAttr));
     164            0 :     } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NB) {
     165            0 :         level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
     166            0 :             TemplateType::TEMPLATE_ALL_REDUCE_NB, dispatcher_);
     167            0 :         HCCL_INFO("[%s]: using nb algo inter-server.", __func__);
     168            0 :         CHK_SMART_PTR_NULL(level1TempAlg);
     169            0 :         CHK_RET(level1TempAlg->Prepare(reduceAttr));
     170              :     } else {
     171            0 :         level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
     172            0 :             TemplateType::TEMPLATE_ALL_REDUCE_RECURSIVE_HALVING_DOUBLING, dispatcher_);
     173            0 :         HCCL_INFO("[%s]: using Recursive halving-doubling algo inter-server.", __func__);
     174            0 :         CHK_SMART_PTR_NULL(level1TempAlg);
     175            0 :         CHK_RET(level1TempAlg->Prepare(reduceAttr));
     176              :     }
     177            0 :     CHK_SMART_PTR_NULL(level1TempAlg);
     178              : 
     179            0 :     const u32 rankSize = level1CommInfo.localRankSize;
     180              : 
     181            0 :     const u64 hdCount = dataSegsSlice[commIndex].size / perDataSize;
     182            0 :     CHK_RET(level1TempAlg->Prepare(allreduceInput, allreduceOutput, allreduceOutput, hdCount,
     183              :         param.DataDes.dataType, param.stream, param.reduceType,
     184              :         LEVEL0_BRIDGE_RANK_ID, std::vector<Slice>(0), dataSegsSlice[commIndex].offset));
     185              : 
     186            0 :     CHK_RET(level1TempAlg->RegisterProfiler((rankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) +
     187              :         level1CommInfo.localRank, PROF_STAGE_1, HCCL_EXEC_STEP_NOT_SET, param.stream));
     188              : 
     189            0 :     CHK_RET(RunTemplate(level1TempAlg, level1CommInfo));
     190            0 :     return HCCL_SUCCESS;
     191            0 : }
     192              : 
     193            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::RunAllGatherLevel0(const OpParam &param,
     194              :     ExecMem &execMem, const std::vector<Slice>& dataSegsSlice)
     195              : {
     196            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
     197            0 :     std::unique_ptr<AlgTemplateBase> level0TempAlg;
     198            0 :     level0TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_GATHER_MESH_ATOMIC, 
     199            0 :         dispatcher_);
     200              : 
     201            0 :     CHK_SMART_PTR_NULL(level0TempAlg);
     202            0 :     CHK_RET(level0TempAlg->Prepare(algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux, 
     203              :         topoAttr_.userRank, nullptr, level0CommInfo.localRank, level0CommInfo.localRankSize));
     204              : 
     205            0 :     u32 rankSize = level0CommInfo.localRankSize;
     206            0 :     CHK_RET(level0TempAlg->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count,
     207              :         param.DataDes.dataType, param.stream, param.reduceType,
     208              :         LEVEL0_BRIDGE_RANK_ID, dataSegsSlice, 0));
     209              : 
     210            0 :     CHK_RET(level0TempAlg->RegisterProfiler(
     211              :         (rankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level0CommInfo.localRank,
     212              :         PROF_STAGE_2, HCCL_EXEC_STEP_NOT_SET, param.stream));
     213              : 
     214            0 :     CHK_RET(RunTemplate(level0TempAlg, level0CommInfo));
     215            0 :     return HCCL_SUCCESS;
     216            0 : }
     217              : 
     218            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::KernelRun(const OpParam &param, ExecMem &execMem)
     219              : {
     220            0 :     HCCL_CONFIG_INFO(HCCL_ALG, "[%s]userRank[%u] starts.", __func__, topoAttr_.userRank);
     221              : 
     222            0 :     CHK_RET(CheckCommSize(COMM_LEVEL0, 1));
     223            0 :     SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
     224            0 :     const u32 sliceNum = level0CommInfo.localRankSize;
     225              : 
     226            0 :     std::vector<Slice> dataSegsSlice;
     227            0 :     GroupSlicesInfo groupSlicesInfoLevel0;
     228            0 :     CHK_RET(PrepareSlicesInfo(param, execMem, dataSegsSlice, groupSlicesInfoLevel0, sliceNum));
     229            0 :     CHK_RET(ActiveSlaveStreams(param.stream));
     230              : 
     231              :     /* STAGE 0: level 0 reduce scatter - plant local reduce */
     232            0 :     CHK_RET(RunReduceScatterLevel0(param, execMem, groupSlicesInfoLevel0));
     233            0 :     HCCL_INFO("[%s]AllReduce stage0 run success.", __func__);
     234              : 
     235              :     /* STAGE 1: level1 all_reduce - auto selected */
     236            0 :     CHK_RET(RunAllReduceLevel1(param, execMem, dataSegsSlice));
     237            0 :     HCCL_INFO("[%s]AllReduce stage1 run success.", __func__);
     238              : 
     239              :     /* STAGE 2: level0 all_gather - mesh atomic */
     240            0 :     CHK_RET(RunAllGatherLevel0(param, execMem, dataSegsSlice));
     241            0 :     HCCL_INFO("[%s]AllReduce stage2 run success", __func__);
     242              : 
     243            0 :     const u64 curSize = execMem.count * SIZE_TABLE[param.DataDes.dataType];
     244            0 :     DeviceMem outCommMem = execMem.outputMem.range(0, curSize);
     245            0 :     DeviceMem outMem(execMem.outputPtr, curSize);
     246            0 :     CHK_RET(HcclD2DMemcpyAsync(dispatcher_, outMem, outCommMem, const_cast<Stream &>(param.stream)));
     247              : 
     248            0 :     return HCCL_SUCCESS;
     249            0 : }
     250              : 
     251            0 : HcclResult CollAllReduceMeshOpbaseMidCountDeterministicExecutor::RunLoopInner(OpParam &param,
     252              :     const ReduceType &reduceType, ExecMem &execMem)
     253              : {
     254            0 :     const u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
     255            0 :     const u64 curSize = execMem.count * unitSize;
     256            0 :     HCCL_DEBUG("[%s]inputMem[%p][%llu], outputMem[%p][%llu], " \
     257              :         "intputPtr[%p], outputPtr[%p], curCount[%llu], curSize[%llu]",
     258              :         __func__, execMem.inputMem.ptr(), execMem.inputMem.size(), execMem.outputMem.ptr(), execMem.outputMem.size(),
     259              :         execMem.inputPtr, execMem.outputPtr, execMem.count, curSize);
     260            0 :     CHK_PRT_RET((execMem.count == 0),
     261              :         HCCL_ERROR("[%s]In OP_BASE curCount is zero.", __func__), HCCL_E_PARA);
     262              : 
     263              :     /* init task */
     264            0 :     const auto autoSelectedAlgTypeLevel1 = static_cast<u32>(algType_.algoLevel1);
     265            0 :     const bool hugeData = IsHugeData(curSize);
     266            0 :     const bool smallData = IsSmallData(param.DataDes.count * unitSize, curSize);
     267            0 :     u64 sliceNum = 0;
     268            0 :     CHK_RET(GetSliceNum(execMem.count * unitSize, smallData, sliceNum, unitSize));
     269            0 :     const bool dataSplit = true;
     270            0 :     const u8 deterministic = topoMatcher_->GetExternalInputHcclDeterministic();
     271            0 :     const CopyPattern copy =  CopyPattern::ZCOPY;
     272            0 :     const auto opMeta = HcclOpMetaInfo::GetOneForAllReduce(autoSelectedAlgTypeLevel1,
     273              :         param.DataDes.dataType, reduceType, smallData, 1, hugeData, copy, sliceNum,
     274              :         false, true, dataSplit, deterministic);
     275            0 :     CHK_RET(InitTask(dispatcher_, param.stream, opMeta.isEnableCache, opMeta.GetCacheKey()));
     276              : 
     277              :     /* kernel run */
     278            0 :     CHK_RET(KernelRun(param, execMem));
     279            0 :     CHK_RET(LaunchTaskExtend(dispatcher_,
     280              :         const_cast<Stream &>(param.stream),
     281              :         const_cast<std::vector<Stream> &>(algResResp_->slaveStreams)));
     282              : 
     283            0 :     return HCCL_SUCCESS;
     284              : }
     285              : 
     286              : REGISTER_EXEC("AllReduceMeshOpbaseMidCountDeterministicExecutor",
     287              :     AllReduceMeshOpbaseMidCountDeterministic, CollAllReduceMeshOpbaseMidCountDeterministicExecutor);
     288              : 
     289              : } // namespace hccl
        

Generated by: LCOV version 2.0-1