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_broadcast_mesh2d.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_ins_group.h"
20 : #include "ccu_context_broadcast_mesh2d.h"
21 : #include "ccu_temp_broadcast_mesh_2D.h"
22 :
23 : namespace Hccl {
24 :
25 : static CcuInstRegister<CcuContextBroadcastMesh2D> g_registrarBroadcastMesh2D(
26 : CcuInstType::CCU_BROADCAST_MESH_2D_DIRECT);
27 :
28 0 : CcuTempBroadcastMesh2D::CcuTempBroadcastMesh2D(const RankId virtualRank, const u32 tempRankSize,
29 : 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() != 2 || tempVTopo_[0].size() <= 1 || tempVTopo_[1].size() <= 1) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
34 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempBroadcastMesh2D] 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 : CcuTempBroadcastMesh2D::~CcuTempBroadcastMesh2D()
44 : {
45 0 : }
46 :
47 :
48 0 : HcclResult CcuTempBroadcastMesh2D::CalcRes(AlgTempResReq &tempResReq)
49 : {
50 : // 按照IODienum来确定stream数量,支持2D和2D的template
51 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
52 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
53 0 : uint32_t dieNum = tempVTopo_.size();
54 0 : if (dieNum != 2) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
55 0 : HCCL_ERROR("[CcuTempBroadcastMesh2D] Rank[%d], Invalid IODieNum[%zu].", myRank_, tempVTopo_.size());
56 0 : return HcclResult::HCCL_E_PARA;
57 : }
58 0 : HCCL_INFO("[CcuTempBroadcastMesh2D] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u] VtopoSize1[%u].",
59 : myRank_, tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size());
60 :
61 : uint32_t myAlgRank;
62 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
63 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
64 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
65 : // find neighbors -> virtualRank
66 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
67 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
68 0 : HCCL_INFO("[CollAlgFactory] [CcuTempBroadcastMesh2D] Rank[%d], Dim[%u], NeighborRank[%d].", myRank_,
69 : dim, neighborRank);
70 :
71 : // LinkNum
72 0 : tempResReq.links[neighborRank] = 1;
73 : }
74 : }
75 0 : return HcclResult::HCCL_SUCCESS;
76 : }
77 :
78 0 : HcclResult CcuTempBroadcastMesh2D::CalcSliceInfo(const AllignInfo &allignInfo, const u64 dataSize,
79 : RankSliceInfo &sliceInfoVec)
80 : {
81 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
82 0 : sliceInfoVec.resize(tempRankSize_, tmp);
83 :
84 0 : CHK_RET(CalcRsAgSliceInfoMesh(myRank_, tempRankSize_, allignInfo, dataSize, sliceInfoVec));
85 :
86 0 : return HcclResult::HCCL_SUCCESS;
87 0 : }
88 :
89 0 : HcclResult CcuTempBroadcastMesh2D::Run(const TempFuncs &tempFuncs, const RankSliceInfo &sliceInfoVec,
90 : const BuffInfo &buffInfo, const ResLinks &tempLinks,
91 : std::vector<InsQuePtr> &tempInsQues)
92 : {
93 : (void)sliceInfoVec;
94 0 : opMode_ = tempFuncs.opMode;
95 0 : buffInfo_ = buffInfo;
96 :
97 : // 分别记录两个Die上的link,构造rankGroup
98 0 : for (auto pair : tempLinks) {
99 0 : if (pair.second.size() == 0 || pair.second[0].GetHop() != 1) { // ESL环境上暂只有直连链路
100 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempBroadcastMesh2D] Rank[%d]--Peer[%d], InvalidHop[%u].",
101 0 : myRank_, pair.first, pair.second[0].GetHop()));
102 : }
103 0 : if ((pair.first / dimSize_[0] == myRank_ / dimSize_[0]) && pair.second[0].GetHop() == 1) {
104 0 : HCCL_INFO("[CcuTempBroadcastMesh2D][Run] Rank[%d] insert link to Rank[%d] in linksX", myRank_, pair.first);
105 0 : linksX_.emplace_back(pair.second[0]);
106 0 : } else if ((pair.first % dimSize_[0] == myRank_ % dimSize_[0]) && pair.second[0].GetHop() == 1) {
107 0 : HCCL_INFO("[CcuTempBroadcastMesh2D][Run] Rank[%d] insert link to Rank[%d] in linksY", myRank_, pair.first);
108 0 : linksY_.emplace_back(pair.second[0]);
109 : } else {
110 0 : THROW<InvalidParamsException>(StringFormat(
111 0 : "[CcuTempBroadcastMesh2D] Rank[%d], Unexpected peerRank[%d] in tempLinks.", myRank_, pair.first));
112 : }
113 0 : }
114 :
115 0 : RankGroup rankGroupX;
116 0 : RankGroup rankGroupY;
117 0 : AddRanksToGroup(tempVTopo_,rankGroupX,rankGroupY);
118 :
119 : uint64_t inputAddr;
120 0 : if (opMode_ == OpMode::OPBASE) {
121 0 : if (tempFuncs.isForepart) {
122 : // 从 UserIn 获取数据
123 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
124 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
125 : } else {
126 : // 从 inBuff 获取数据
127 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
128 : }
129 : } else {
130 : // 图模式没有 tempFuncs.usrData,直接通过 buffInfo_ 获取输入输出地址
131 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
132 : }
133 0 : uint64_t sliceSize = tempFuncs.usrData.usrInSlices[0].GetSize(); // 获取本rank需要处理的数据量
134 :
135 : uint64_t token;
136 0 : CHK_RET(GetToken(op_, token));
137 :
138 0 : std::vector<uint32_t> dimId;
139 0 : dimId.emplace_back(myRank_ % dimSize_[0]);
140 0 : dimId.emplace_back(myRank_ / dimSize_[0]);
141 :
142 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
143 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2D算法,需要执行两次
144 : // 计算每次编译的偏移量和数据量
145 0 : uint64_t xAxisSize = sliceSize * dimSize_[0] / (dimSize_[axisId] + dimSize_[1 - axisId]);
146 0 : uint64_t yAxisSize = sliceSize - xAxisSize;
147 :
148 0 : CcuInstructionBroadcastMesh2D ccuInsBroadcastMesh2D;
149 0 : ccuInsBroadcastMesh2D.Init(dimSize_, static_cast<uint32_t>(myRank_), inputAddr, axisId,
150 0 : sliceSize, xAxisSize, yAxisSize, token, op_, tempVTopo_);
151 :
152 0 : HCCL_INFO("[CcuTempBroadcastMesh2D] Run Init: dimSize0[%llu], dimSize1[%llu], myRank_[%d],"\
153 : "inputAddr[%llu], sliceSize[%llu], xAxisSize[%llu], yAxisSize[%llu]",
154 : dimSize_[0], dimSize_[1], myRank_, inputAddr, sliceSize, xAxisSize, yAxisSize);
155 :
156 0 : ccuInsBroadcastMesh2D.SetLinks(axisId == 0 ? linksX_ : linksY_);
157 0 : ccuInsBroadcastMesh2D.SetRankGroup(axisId == 0 ? rankGroupX : rankGroupY);
158 0 : u32 ckeNum = 5;
159 0 : ccuInsBroadcastMesh2D.SetCntCkeNum(ckeNum); // 每个transport用4个CKE
160 0 : ccuInsBroadcastMesh2D.Describe();
161 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionBroadcastMesh2D>(ccuInsBroadcastMesh2D)));
162 0 : }
163 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
164 :
165 0 : return HcclResult::HCCL_SUCCESS;
166 0 : }
167 : } // namespace Hccl
|