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.h"
17 : #include "alg_data_trans_wrapper.h"
18 : #include "ccu_rank_group.h"
19 : #include "ccu_ctx_creator_registry.h"
20 : #include "ccu_context_all_gather_mesh2d.h"
21 : #include "ccu_ins_group.h"
22 :
23 : namespace Hccl {
24 : constexpr u32 MESH_2D_DIMENSION_NUM = 2;
25 :
26 : static CcuInstRegister<CcuContextAllGatherMesh2D> registrarAllGather(CcuInstType::CCU_ALLGATHER_MESH_2D_DIRECT);
27 :
28 0 : CcuTempAllGatherMesh2D::CcuTempAllGatherMesh2D(
29 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
30 0 : const std::map<RankId, u32>& tempVirtRankMap)
31 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
32 : {
33 : // 填充框内的维度大小
34 0 : if (tempVTopo_.size() != MESH_2D_DIMENSION_NUM || tempVTopo_[0].size() <= 1
35 0 : || tempVTopo_[1].size() <= 1) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
36 0 : THROW<InvalidParamsException>(StringFormat(
37 : "[CcuTempAllGatherMesh2D] Rank[%d], Invalid tempVTopo "
38 : "Size[%u] or Invalid tempVTopo[0] size [%u] or tempVTopo[1] size [%u].",
39 0 : myRank_, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size()));
40 : }
41 0 : dimSize_.emplace_back(tempVTopo[0].size());
42 0 : dimSize_.emplace_back(tempVTopo[1].size());
43 0 : }
44 :
45 0 : CcuTempAllGatherMesh2D::~CcuTempAllGatherMesh2D() {}
46 :
47 0 : HcclResult CcuTempAllGatherMesh2D::CalcRes(AlgTempResReq& tempResReq)
48 : {
49 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
50 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
51 0 : uint32_t dieNum = tempVTopo_.size();
52 0 : if (dieNum != 2) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
53 0 : THROW<InvalidParamsException>(
54 0 : StringFormat("[CcuTempAllGatherMesh2D] Rank[%d], Invalid IODieNum[%u].", myRank_, dieNum));
55 : }
56 0 : HCCL_INFO(
57 : "[CcuTempAllGatherMesh2D] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u] VtopoSize1[%u].", myRank_,
58 : tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size());
59 :
60 : uint32_t myAlgRank;
61 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
62 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
63 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
64 : // find neighbors -> virtualRank
65 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
66 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
67 0 : HCCL_INFO(
68 : "[CollAlgFactory] [CcuTempAllGatherMesh2D] Rank[%d], Dim[%u], NeighborRank[%d].", myRank_, dim,
69 : neighborRank);
70 :
71 : // LinkNum
72 0 : tempResReq.links[neighborRank] = 1;
73 : }
74 : }
75 :
76 0 : return HcclResult::HCCL_SUCCESS;
77 : }
78 : /*
79 : dataSize / (rankSize) --> chunkSize
80 : dataSize / (rankSize * queNum) --> sliceSize
81 :
82 : SliceInfoVecforNHR: [1st chunk: [1st Slice, 2nd Slice, ...], 2nd chunk: [1st Slice, 2nd Slice, ...], ...]
83 : */
84 : HcclResult
85 0 : CcuTempAllGatherMesh2D::CalcSliceInfo(const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
86 : {
87 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
88 0 : sliceInfoVec.resize(tempRankSize_, tmp);
89 :
90 0 : CHK_RET(CalcRsAgSliceInfoMesh(myRank_, tempRankSize_, allignInfo, dataSize, sliceInfoVec));
91 :
92 0 : return HcclResult::HCCL_SUCCESS;
93 0 : }
94 :
95 0 : HcclResult CcuTempAllGatherMesh2D::Run(
96 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
97 : std::vector<InsQuePtr>& tempInsQues)
98 : {
99 0 : opMode_ = tempFuncs.opMode;
100 0 : buffInfo_ = buffInfo;
101 :
102 : uint64_t inputAddr;
103 : uint64_t outputAddr;
104 : uint64_t offset;
105 :
106 : // 分别记录两个Die上的link,构造rankGroup
107 0 : for (auto pair : tempLinks) {
108 0 : if (pair.second.size() == 0 || pair.second[0].GetHop() != 1) { // ESL环境上暂只有直连链路
109 0 : THROW<InvalidParamsException>(StringFormat(
110 0 : "[CcuTempAllGatherMesh2D] Rank[%d]--Peer[%d], InvalidHop[%u].", myRank_, pair.first,
111 0 : pair.second[0].GetHop()));
112 : }
113 0 : if ((pair.first / dimSize_[0] == myRank_ / dimSize_[0]) && pair.second[0].GetHop() == 1) {
114 0 : HCCL_INFO("[CcuTempAllGatherMesh2D][Run] Rank[%d] insert link to Rank[%d] in linksX", myRank_, pair.first);
115 0 : linksX_.emplace_back(pair.second[0]);
116 0 : } else if ((pair.first % dimSize_[0] == myRank_ % dimSize_[0]) && pair.second[0].GetHop() == 1) {
117 0 : HCCL_INFO("[CcuTempAllGatherMesh2D][Run] Rank[%d] insert link to Rank[%d] in linksY", myRank_, pair.first);
118 0 : linksY_.emplace_back(pair.second[0]);
119 : } else {
120 0 : THROW<InvalidParamsException>(StringFormat(
121 0 : "[CcuTempAllGatherMesh2D] Rank[%d], Unexpected peerRank[%d] in tempLinks.", myRank_, pair.first));
122 : }
123 0 : }
124 :
125 0 : RankGroup rankGroupX;
126 0 : RankGroup rankGroupY;
127 0 : AddRanksToGroup(tempVTopo_, rankGroupX, rankGroupY);
128 :
129 0 : if (opMode_ == OpMode::OPBASE) {
130 0 : if (tempFuncs.isForepart) {
131 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
132 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
133 : } else {
134 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
135 : }
136 0 : if (tempFuncs.isBottom) {
137 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
138 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
139 : } else {
140 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
141 : }
142 : } else {
143 : // 图模式没有 tempFuncs.usrData,直接通过 buffInfo_ 获取输入输出地址
144 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff
145 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
146 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff
147 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
148 : }
149 :
150 0 : offset = static_cast<uint64_t>(op_.inputMem->GetSize());
151 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size;
152 : uint64_t token;
153 0 : CHK_RET(GetToken(op_, token));
154 0 : HCCL_INFO("[CcuTempAllGatherMesh2D] Rank[%d], input[%llu], output[%llu].", myRank_, inputAddr, outputAddr);
155 :
156 0 : uint64_t aSize = sliceSize * dimSize_[0] / (dimSize_[0] + dimSize_[1]);
157 0 : uint64_t bSize = sliceSize - aSize;
158 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
159 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2D算法,需要执行两次
160 0 : HCCL_INFO(
161 : "[CcuTempAllGatherMesh2D][Run] Rank[%d], axisId[%u], aSize[%llu], bSize[%llu].", myRank_, axisId, aSize,
162 : bSize);
163 :
164 0 : CcuInstructionAllGatherMesh2D ins = CcuInstructionAllGatherMesh2D();
165 0 : ins.Init(myRank_, axisId, inputAddr, outputAddr, aSize, bSize, sliceSize, offset, token, op_, tempVTopo_);
166 0 : ins.SetLinks(axisId == 0 ? linksX_ : linksY_);
167 0 : ins.SetRankGroup(axisId == 0 ? rankGroupX : rankGroupY);
168 0 : u32 cntCkeNum = 5; // 每个transport用5个CKE
169 0 : ins.SetCntCkeNum(cntCkeNum);
170 0 : ins.Describe();
171 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllGatherMesh2D>(ins)));
172 0 : }
173 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
174 :
175 0 : return HcclResult::HCCL_SUCCESS;
176 0 : }
177 :
178 : } // namespace Hccl
|