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_mem2mem.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_reduce_mesh2d_mem2mem.h"
20 : #include "ccu_temp_reduce_mesh_2D_mem2mem.h"
21 : #include "ccu_ins_group.h"
22 :
23 : namespace Hccl {
24 :
25 : static CcuInstRegister<CcuContextReduceMeshMem2Mem2D> registrarReduce2D(CcuInstType::CCU_REDUCE_MESH_2D_MEM2MEM);
26 :
27 0 : CcuTempReduceMeshMem2Mem2D::CcuTempReduceMeshMem2Mem2D(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 0 : uint32_t topoNum = 2;
33 0 : if (tempVTopo_.size() != topoNum || tempVTopo_[0].size() <= 1
34 0 : || tempVTopo_[1].size() <= 1) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
35 0 : THROW<InvalidParamsException>(
36 0 : StringFormat("[CcuTempReduceMeshMem2Mem2D] 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 : CcuTempReduceMeshMem2Mem2D::~CcuTempReduceMeshMem2Mem2D()
45 : {
46 0 : }
47 :
48 0 : void CcuTempReduceMeshMem2Mem2D::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType)
49 : {
50 0 : reduceOp_ = reduceOp;
51 0 : dataType_ = dataType;
52 0 : }
53 :
54 0 : HcclResult CcuTempReduceMeshMem2Mem2D::CalcRes(AlgTempResReq &tempResReq)
55 : {
56 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
57 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
58 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
59 0 : uint32_t dieNum = tempVTopo_.size();
60 0 : if (dieNum != 2) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
61 0 : HCCL_ERROR("[CcuTempReduceMeshMem2Mem2D] Rank[%d], Invalid IODieNum[%zu].", myRank_, tempVTopo_.size());
62 0 : return HcclResult::HCCL_E_PARA;
63 : }
64 0 : HCCL_INFO("[CcuTempReduceMeshMem2Mem2D] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u] VtopoSize1[%u].",
65 : myRank_, tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size());
66 :
67 : uint32_t myAlgRank;
68 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
69 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
70 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
71 : // find neighbors -> virtualRank
72 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
73 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
74 0 : HCCL_INFO("[CollAlgFactory] [CcuTempReduceMeshMem2Mem2D] Rank[%d], Dim[%u], NeighborRank[%d].", myRank_,
75 : dim, neighborRank);
76 : // LinkNum
77 0 : tempResReq.links[neighborRank] = 1;
78 : }
79 : }
80 0 : return HcclResult::HCCL_SUCCESS;
81 : }
82 :
83 0 : HcclResult CcuTempReduceMeshMem2Mem2D::GenExtIns(const TempFuncs &tempFuncs,
84 : const TemplateDataParams &templateDataParams,
85 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
86 : {
87 0 : buffInfo_ = templateDataParams.buffInfo;
88 0 : rootId_ = op_.root;
89 0 : opMode_ = tempFuncs.opMode;
90 : // 分别记录两个Die上的link,构造rankGroup
91 0 : for (auto pair : tempLinks) {
92 0 : if (pair.second.size() == 0 || pair.second[0].GetHop() != 1) { // ESL环境上暂只有直连链路
93 0 : THROW<InvalidParamsException>(
94 0 : StringFormat("[CcuTempReduceMeshMem2Mem2D] Rank[%d]--Peer[%d], InvalidHop[%u].", myRank_, pair.first,
95 0 : pair.second[0].GetHop()));
96 : }
97 0 : if ((pair.first / dimSize_[0] == myRank_ / dimSize_[0]) && pair.second[0].GetHop() == 1) {
98 0 : HCCL_INFO("[CcuTempReduceMeshMem2Mem2D][Run] Rank[%d] insert link to Rank[%d] in linksX", myRank_,
99 : pair.first);
100 0 : linksX_.emplace_back(pair.second[0]);
101 0 : } else if ((pair.first % dimSize_[0] == myRank_ % dimSize_[0]) && pair.second[0].GetHop() == 1) {
102 0 : HCCL_INFO("[CcuTempReduceMeshMem2Mem2D][Run] Rank[%d] insert link to Rank[%d] in linksY", myRank_,
103 : pair.first);
104 0 : linksY_.emplace_back(pair.second[0]);
105 : } else {
106 0 : THROW<InvalidParamsException>(StringFormat(
107 0 : "[CcuTempReduceMeshMem2Mem2D] Rank[%d], Unexpected peerRank[%d] in tempLinks.", myRank_, pair.first));
108 : }
109 0 : }
110 :
111 0 : RankGroup rankGroupX;
112 0 : RankGroup rankGroupY;
113 0 : AddRanksToGroup(tempVTopo_,rankGroupX,rankGroupY);
114 :
115 0 : uint64_t sliceSize = templateDataParams.sliceSize; // 获取本rank需要处理的数据量
116 0 : uint64_t inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
117 0 : uint64_t outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
118 :
119 : uint64_t token;
120 0 : CHK_RET(GetToken(op_, token));
121 0 : std::vector<uint32_t> dimId;
122 0 : dimId.emplace_back(myRank_ % dimSize_[0]);
123 0 : dimId.emplace_back(myRank_ / dimSize_[0]); // dimId里面放本rank在拓扑图上的位置
124 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
125 :
126 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2D算法,需要执行两次
127 : // 计算每次编译的偏移量和数据量
128 0 : uint64_t sliceCount = sliceSize / DataTypeSizeGet(dataType_);
129 : uint64_t xAxisSize
130 0 : = (sliceCount / (dimSize_[axisId] + dimSize_[1 - axisId])) * dimSize_[0] * DataTypeSizeGet(dataType_);
131 0 : uint64_t yAxisSize = sliceSize - xAxisSize;
132 0 : CcuInstructionReduceMeshMem2Mem2D ccuInsReduceMeshMem2Mem2D;
133 0 : ccuInsReduceMeshMem2Mem2D.Init(dimSize_, static_cast<uint32_t>(myRank_), rootId_, axisId, inputAddr, outputAddr,
134 0 : sliceSize, xAxisSize, yAxisSize, token, op_, tempVTopo_);
135 :
136 0 : HCCL_INFO("[CcuTempReduceMeshMem2Mem2D] Run Init: myRank_[%d], inputAddr[%llu],"
137 : "outputAddr[%llu], sliceSize[%llu], xAxisSize[%llu], yAxisSize[%llu], axisId_[%u]",
138 : myRank_, inputAddr, outputAddr, sliceSize, xAxisSize, yAxisSize, axisId);
139 :
140 0 : ccuInsReduceMeshMem2Mem2D.SetLinks(axisId == 0 ? linksX_ : linksY_);
141 0 : ccuInsReduceMeshMem2Mem2D.SetRankGroup(axisId == 0 ? rankGroupX : rankGroupY);
142 0 : u32 ckeNum = 5;
143 0 : ccuInsReduceMeshMem2Mem2D.SetCntCkeNum(ckeNum); // 每个transport用5个CKE
144 0 : ccuInsReduceMeshMem2Mem2D.Describe();
145 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionReduceMeshMem2Mem2D>(ccuInsReduceMeshMem2Mem2D)));
146 0 : }
147 :
148 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
149 :
150 0 : return HcclResult::HCCL_SUCCESS;
151 0 : }
152 :
153 : } // namespace Hccl
|