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 <ios>
12 : #include <iostream>
13 :
14 : #include "log.h"
15 :
16 : #include "ccu_temp_all_gather_mesh_2D_mem2mem.h"
17 : #include "alg_data_trans_wrapper.h"
18 : #include "ccu_instruction_all_gather_mesh2d_mem2mem.h"
19 : #include "ccu_rank_group.h"
20 : #include "ccu_ctx_creator_registry.h"
21 : #include "ccu_context_all_gather_mesh2d_mem2mem.h"
22 : #include "ccu_ins_group.h"
23 :
24 : namespace Hccl {
25 :
26 : static CcuInstRegister<CcuContextAllGatherMeshMem2Mem2D> registrarAllGather(CcuInstType::CCU_ALLGATHER_MESH_2D_MEM2MEM);
27 :
28 0 : CcuTempAllGatherMeshMem2Mem2D::CcuTempAllGatherMeshMem2Mem2D(const RankId virtualRank, const u32 tempRankSize,
29 0 : const std::vector<std::vector<RankId>> &tempVTopo, const std::map<RankId, u32> &tempVirtRankMap)
30 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
31 : {
32 : // 填充框内的维度大小, concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
33 0 : if (tempVTopo_.size() != DIM_SIZE || tempVTopo_[0].size() <= 1 || tempVTopo_[1].size() <= 1) {
34 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempAllGatherMeshMem2Mem2D] Rank[%d], Invalid tempVTopo "
35 : "Size[%u] or Invalid tempVTopo[0] size [%u] or tempVTopo[1] size [%u].",
36 0 : myRank_, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size()));
37 : }
38 0 : dimSize_.emplace_back(tempVTopo[0].size());
39 0 : dimSize_.emplace_back(tempVTopo[1].size());
40 0 : }
41 :
42 0 : CcuTempAllGatherMeshMem2Mem2D::~CcuTempAllGatherMeshMem2Mem2D()
43 : {
44 0 : }
45 :
46 0 : HcclResult CcuTempAllGatherMeshMem2Mem2D::CalcRes(AlgTempResReq &tempResReq)
47 : {
48 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
49 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
50 0 : uint32_t dieNum = tempVTopo_.size();
51 0 : if (dieNum != 2) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
52 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempAllGatherMeshMem2Mem2D] Rank[%d], Invalid IODieNum[%u].",
53 : myRank_, dieNum));
54 : }
55 0 : HCCL_INFO("[CcuTempAllGatherMeshMem2Mem2D] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u] "
56 : "VtopoSize1[%u].", myRank_, tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size());
57 :
58 : uint32_t myAlgRank;
59 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
60 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
61 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
62 : // find neighbors -> virtualRank
63 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
64 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
65 0 : HCCL_INFO("[CollAlgFactory] [CcuTempAllGatherMeshMem2Mem2D] Rank[%d], Dim[%u], NeighborRank[%d].", myRank_,
66 : dim, neighborRank);
67 :
68 : // LinkNum
69 0 : tempResReq.links[neighborRank] = 1;
70 : }
71 : }
72 :
73 0 : return HcclResult::HCCL_SUCCESS;
74 : }
75 :
76 0 : HcclResult CcuTempAllGatherMeshMem2Mem2D::GenExtIns(const TempFuncs &tempFuncs,
77 : const TemplateDataParams &templateDataParams, const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
78 : {
79 0 : opMode_ = tempFuncs.opMode;
80 0 : buffInfo_ = templateDataParams.buffInfo;
81 :
82 : // 分别记录两个Die上的link,构造rankGroup
83 0 : for (auto pair : tempLinks) {
84 0 : if (pair.second.size() == 0 || pair.second[0].GetHop() != 1) { // ESL环境上暂只有直连链路
85 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempAllGatherMeshMem2Mem2D] Rank[%d]--Peer[%d], "
86 0 : "InvalidHop[%u].", myRank_, pair.first, pair.second[0].GetHop()));
87 : }
88 0 : if ((pair.first / dimSize_[0] == myRank_ / dimSize_[0]) && pair.second[0].GetHop() == 1) {
89 0 : linksX_.emplace_back(pair.second[0]);
90 0 : } else if ((pair.first % dimSize_[0] == myRank_ % dimSize_[0]) && pair.second[0].GetHop() == 1) {
91 0 : linksY_.emplace_back(pair.second[0]);
92 : } else {
93 0 : THROW<InvalidParamsException>(StringFormat(
94 : "[CcuTempAllGatherMeshMem2Mem2D] Rank[%d], Unexpected peerRank[%d] in tempLinks.", myRank_,
95 0 : pair.first));
96 : }
97 0 : }
98 :
99 0 : RankGroup rankGroupX;
100 0 : RankGroup rankGroupY;
101 0 : AddRanksToGroup(tempVTopo_,rankGroupX,rankGroupY);
102 :
103 0 : uint64_t inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
104 0 : uint64_t outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
105 0 : uint64_t offset = static_cast<uint64_t>(op_.inputMem->GetSize());
106 0 : uint64_t sliceSize = templateDataParams.sliceSize;
107 : uint64_t token;
108 0 : CHK_RET(GetToken(op_, token));
109 :
110 0 : uint64_t aSize = sliceSize * dimSize_[0] / (dimSize_[0] + dimSize_[1]);
111 0 : uint64_t bSize = sliceSize - aSize;
112 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
113 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2D算法,需要执行两次
114 0 : HCCL_INFO("[CcuTempAllGatherMeshMem2Mem2D][GenExtIns] Rank[%d], axisId[%u], aSize[%llu], bSize[%llu].",
115 : myRank_, axisId, aSize, bSize);
116 :
117 0 : CcuInstructionAllGatherMeshMem2Mem2D ins = CcuInstructionAllGatherMeshMem2Mem2D();
118 0 : ins.Init(myRank_, axisId, inputAddr, outputAddr, aSize, bSize, sliceSize, offset, token, op_, tempVTopo_);
119 0 : ins.SetLinks(axisId == 0 ? linksX_ : linksY_);
120 0 : ins.SetRankGroup(axisId == 0 ? rankGroupX : rankGroupY);
121 0 : u32 cntCkeNum = 5; // 每个transport用5个CKE
122 0 : ins.SetCntCkeNum(cntCkeNum);
123 0 : ins.Describe();
124 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllGatherMeshMem2Mem2D>(ins)));
125 0 : }
126 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
127 :
128 0 : return HcclResult::HCCL_SUCCESS;
129 0 : }
130 : } // namespace Hccl
|