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