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_instruction_reduce_mesh2d.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_reduce_mesh2d.h"
20 : #include "ccu_temp_reduce_mesh_2D.h"
21 : #include "ccu_ins_group.h"
22 :
23 : namespace Hccl {
24 : constexpr u32 MESH_2D_DIMENSION_NUM = 2;
25 :
26 : static CcuInstRegister<CcuContextReduceMesh2D> registrarReduce2D(CcuInstType::CCU_REDUCE_MESH_2D_DIRECT);
27 :
28 0 : CcuTempReduceMesh2D::CcuTempReduceMesh2D(
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 0 : if (tempVTopo_.size() != MESH_2D_DIMENSION_NUM || tempVTopo_[0].size() <= 1
34 0 : || tempVTopo_[1].size() <= 1) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
35 0 : THROW<InvalidParamsException>(StringFormat(
36 : "[CcuTempReduceMesh2D] Rank[%d], Invalid tempVTopo "
37 : "Size[%u] or Invalid tempVTopo[0] size [%u] or tempVTopo[1] size [%u].",
38 0 : myRank_, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size()));
39 : }
40 0 : dimSize_.emplace_back(tempVTopo[0].size());
41 0 : dimSize_.emplace_back(tempVTopo[1].size());
42 0 : }
43 :
44 0 : CcuTempReduceMesh2D::~CcuTempReduceMesh2D() {}
45 :
46 0 : void CcuTempReduceMesh2D::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
47 : {
48 0 : reduceOp_ = reduceOp;
49 0 : dataType_ = dataType;
50 0 : }
51 :
52 0 : HcclResult CcuTempReduceMesh2D::CalcRes(AlgTempResReq& tempResReq)
53 : {
54 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
55 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
56 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
57 0 : uint32_t dieNum = tempVTopo_.size();
58 0 : if (dieNum != 2) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
59 0 : HCCL_ERROR("[CcuTempReduceMesh2D] Rank[%d], Invalid IODieNum[%zu].", myRank_, tempVTopo_.size());
60 0 : return HcclResult::HCCL_E_PARA;
61 : }
62 0 : HCCL_INFO(
63 : "[CcuTempReduceMesh2D] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u] VtopoSize1[%u].", myRank_,
64 : tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size());
65 :
66 : uint32_t myAlgRank;
67 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
68 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
69 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
70 : // find neighbors -> virtualRank
71 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
72 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
73 0 : HCCL_INFO(
74 : "[CollAlgFactory] [CcuTempReduceMesh2D] Rank[%d], Dim[%u], NeighborRank[%d].", myRank_, dim,
75 : neighborRank);
76 : // LinkNum
77 0 : tempResReq.links[neighborRank] = 1;
78 : }
79 : }
80 0 : return HcclResult::HCCL_SUCCESS;
81 : }
82 :
83 : HcclResult
84 0 : CcuTempReduceMesh2D::CalcSliceInfo(const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
85 : {
86 : (void)allignInfo;
87 : SliceInfo basicSlice;
88 0 : basicSlice.offset = 0;
89 0 : basicSlice.size = dataSize;
90 0 : HCCL_INFO("[CcuTempReduceMesh2D] [CalcSliceInfo]basicSlice.size[%u]", basicSlice.size);
91 0 : std::vector<SliceInfo> singleRankSliceInfoVector{basicSlice};
92 0 : sliceInfoVec.resize(tempRankSize_, singleRankSliceInfoVector);
93 :
94 0 : return HcclResult::HCCL_SUCCESS;
95 0 : }
96 :
97 0 : HcclResult CcuTempReduceMesh2D::Run(
98 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
99 : std::vector<InsQuePtr>& tempInsQues)
100 : {
101 0 : opMode_ = tempFuncs.opMode;
102 0 : buffInfo_ = buffInfo;
103 0 : rootId_ = op_.root;
104 : // 分别记录两个Die上的link,构造rankGroup
105 0 : for (auto pair : tempLinks) {
106 0 : if (pair.second.size() == 0 || pair.second[0].GetHop() != 1) { // ESL环境上暂只有直连链路
107 0 : THROW<InvalidParamsException>(StringFormat(
108 0 : "[CcuTempReduceMesh2D] Rank[%d]--Peer[%d], InvalidHop[%u].", myRank_, pair.first,
109 0 : pair.second[0].GetHop()));
110 : }
111 0 : if ((pair.first / dimSize_[0] == myRank_ / dimSize_[0]) && pair.second[0].GetHop() == 1) {
112 0 : HCCL_INFO("[CcuTempReduceMesh2D][Run] Rank[%d] insert link to Rank[%d] in linksX", myRank_, pair.first);
113 0 : linksX_.emplace_back(pair.second[0]);
114 0 : } else if ((pair.first % dimSize_[0] == myRank_ % dimSize_[0]) && pair.second[0].GetHop() == 1) {
115 0 : HCCL_INFO("[CcuTempReduceMesh2D][Run] Rank[%d] insert link to Rank[%d] in linksY", myRank_, pair.first);
116 0 : linksY_.emplace_back(pair.second[0]);
117 : } else {
118 0 : THROW<InvalidParamsException>(StringFormat(
119 0 : "[CcuTempReduceMesh2D] Rank[%d], Unexpected peerRank[%d] in tempLinks.", myRank_, pair.first));
120 : }
121 0 : }
122 :
123 0 : RankGroup rankGroupX;
124 0 : RankGroup rankGroupY;
125 0 : AddRanksToGroup(tempVTopo_, rankGroupX, rankGroupY);
126 :
127 0 : std::vector<uint64_t> dimSize;
128 0 : dimSize.push_back(tempRankSize_); // tempRankSize_ 就是rank数量
129 :
130 : // 只传userIn的起始位置,不带偏移,偏移已在offset中包含
131 : uint64_t inputAddr;
132 : // userOut 的位置,需要带上偏移
133 : uint64_t outputAddr;
134 0 : if (opMode_ == OpMode::OPBASE) {
135 0 : if (tempFuncs.isForepart) {
136 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
137 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
138 : } else {
139 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
140 : }
141 0 : if (tempFuncs.isBottom) {
142 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
143 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
144 : } else {
145 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
146 : }
147 : } else {
148 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
149 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
150 : }
151 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size;
152 : // 自己需要 reduce 的数据基于userIn的起始位置的偏移
153 0 : uint64_t offset = 0;
154 : uint64_t token;
155 0 : CHK_RET(GetToken(op_, token));
156 0 : std::vector<uint32_t> dimId;
157 0 : dimId.emplace_back(myRank_ % dimSize_[0]);
158 0 : dimId.emplace_back(myRank_ / dimSize_[0]); // dimId里面放本rank在拓扑图上的位置
159 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
160 :
161 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2D算法,需要执行两次
162 : // 计算每次编译的偏移量和数据量
163 0 : uint64_t xAxisSize = sliceSize;
164 0 : uint64_t yAxisSize = sliceSize;
165 0 : CcuInstructionReduceMesh2D ccuInsReduceMesh2D;
166 0 : ccuInsReduceMesh2D.Init(
167 0 : dimSize_, static_cast<uint32_t>(myRank_), rootId_, axisId, inputAddr, outputAddr, sliceSize, xAxisSize,
168 0 : yAxisSize, offset, token, op_, tempVTopo_);
169 :
170 0 : HCCL_INFO(
171 : "[CcuTempReduceMesh2D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu],"
172 : "outputAddr[%llu], sliceSize[%llu], xAxisSize[%llu], yAxisSize[%llu], offset[%llu], axisId_[%u]",
173 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, xAxisSize, yAxisSize, offset, axisId);
174 :
175 0 : ccuInsReduceMesh2D.SetLinks(axisId == 0 ? linksX_ : linksY_);
176 0 : ccuInsReduceMesh2D.SetRankGroup(axisId == 0 ? rankGroupX : rankGroupY);
177 0 : u32 ckeNum = 5;
178 0 : ccuInsReduceMesh2D.SetCntCkeNum(ckeNum); // 每个transport用5个CKE
179 0 : ccuInsReduceMesh2D.Describe();
180 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionReduceMesh2D>(ccuInsReduceMesh2D)));
181 0 : }
182 :
183 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
184 :
185 0 : return HcclResult::HCCL_SUCCESS;
186 0 : }
187 : } // namespace Hccl
|