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 : #include "log.h"
14 : #include "ccu_rank_group.h"
15 : #include "ccu_ctx_creator_registry.h"
16 : #include "ccu_context_scatter_nhr1d_mem2mem.h"
17 : #include "ccu_temp_scatter_nhr_1D_mem2mem.h"
18 : #include "ccu_ins_group.h"
19 :
20 : namespace Hccl {
21 :
22 : static CcuInstRegister<CcuContextScatterNHR1DMem2Mem> g_registrarScatter(CcuInstType::CCU_SCATTER_NHR_1D_MEM2MEM);
23 :
24 0 : CcuTempScatterNHRMem2Mem1D::CcuTempScatterNHRMem2Mem1D(
25 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
26 0 : const std::map<RankId, u32>& tempVirtRankMap)
27 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
28 0 : {}
29 :
30 0 : CcuTempScatterNHRMem2Mem1D::~CcuTempScatterNHRMem2Mem1D() {}
31 :
32 0 : HcclResult CcuTempScatterNHRMem2Mem1D::CalcRes(AlgTempResReq& tempResReq)
33 : {
34 0 : tempResReq.queNum = 1;
35 0 : tempResReq.streamNum = tempResReq.queNum;
36 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
37 0 : u32 linkNum = 1;
38 0 : linkNumBtwPeers_ = linkNum;
39 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
40 0 : return HcclResult::HCCL_SUCCESS;
41 : }
42 :
43 0 : uint64_t CcuTempScatterNHRMem2Mem1D::GetMaxSliceSize() const { return UB_MAX_DATA_SIZE; }
44 :
45 0 : uint32_t CcuTempScatterNHRMem2Mem1D::virtRankId2RankId(const uint32_t virtRankId)
46 : {
47 0 : for (auto iter = tempVirtRankMap_.begin(); iter != tempVirtRankMap_.end(); iter++) {
48 0 : if (iter->second == virtRankId) {
49 0 : return iter->first;
50 : }
51 : }
52 0 : return 0;
53 : }
54 :
55 0 : u32 CcuTempScatterNHRMem2Mem1D::CalcScratchMultiple(BufferType input, BufferType output)
56 : {
57 : (void)input;
58 : (void)output;
59 0 : return tempRankSize_;
60 : }
61 :
62 0 : HcclResult CcuTempScatterNHRMem2Mem1D::GenExtIns(
63 : const TempFuncs& tempFuncs, TemplateDataParams& tempAlgParams, const ResLinks& tempLinks,
64 : std::vector<InsQuePtr>& tempInsQues)
65 : {
66 0 : CHK_PRT_RET(
67 : tempInsQues.empty(), HCCL_ERROR("[CcuInstructionScatterNHR1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
68 0 : CHK_PTR_NULL(tempInsQues[0]);
69 0 : opMode_ = tempFuncs.opMode;
70 0 : uint64_t rootId = tempVirtRankMap_[rootId_];
71 0 : CcuInstructionScatterNHR1D ccuInsScatterNHR1D;
72 0 : std::vector<uint64_t> dimSize;
73 0 : dimSize.push_back(tempRankSize_);
74 0 : DataType dataType_ = op_.dataType;
75 0 : uint32_t axisSize = tempLinks.begin()->second.size();
76 0 : uint32_t myVirtRankId = tempVirtRankMap_[myRank_];
77 0 : uint64_t sliceSize = tempAlgParams.sliceSize;
78 0 : uint64_t DataCount = (tempAlgParams.sliceSize / DataTypeSizeGet(dataType_));
79 0 : uint64_t die0Size = DataCount / axisSize * DataTypeSizeGet(dataType_);
80 0 : uint64_t die1Size = tempAlgParams.sliceSize - die0Size;
81 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
82 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
83 0 : uint64_t isOutputScratch = (tempAlgParams.buffInfo.outBuffType == BufferType::SCRATCH) ? 1 : 0;
84 : uint64_t scratchAddr
85 0 : = BufferTypeToAddr(tempAlgParams.buffInfo.scratBuffType) + tempAlgParams.buffInfo.scratchBuffBaseOff;
86 : uint64_t token;
87 0 : CHK_RET(GetToken(op_, token));
88 0 : uint64_t inputSliceStride = tempAlgParams.inputSliceStride;
89 0 : uint64_t outputSliceStride = tempAlgParams.outputSliceStride;
90 0 : uint64_t inputRepeatStride = tempAlgParams.inputRepeatStride;
91 0 : uint64_t outputRepeatStride = tempAlgParams.outputRepeatStride;
92 0 : uint64_t repeatNum = tempAlgParams.repeatNum;
93 0 : uint64_t repeatNumVar = UINT64_MAX - repeatNum;
94 :
95 0 : HCCL_INFO(
96 : "[CcuInstructionScatterNHR1D] dimSize[%llu], die0Size[%llu], die1Size[%llu], inputAddr[%llu], outputAddr[%llu],"
97 : "scratchAddr[%llu], repeatNum[%llu]",
98 : dimSize[0], die0Size, die1Size, inputAddr, outputAddr, scratchAddr, repeatNum);
99 :
100 0 : if (DataCount == 0) {
101 0 : HCCL_INFO("[CcuTempScatterNHRMem2Mem1D] DataCount == 0, Template Run Ends.");
102 0 : return HCCL_SUCCESS;
103 : }
104 :
105 0 : if (axisSize > 1 || die1Size == 0) {
106 0 : axisSize = 1;
107 : }
108 :
109 0 : std::vector<LinkData> linksDie0;
110 0 : std::vector<LinkData> linksDie1;
111 0 : RankGroup scatterRankGroup;
112 0 : std::map<u32, u32> indexMap;
113 0 : std::vector<NHRStepInfo> stepInfoVector;
114 0 : u32 nSteps = GetNHRStepNum(tempRankSize_);
115 0 : for (u32 step = 0; step < nSteps; step++) {
116 0 : NHRStepInfo stepInfo;
117 0 : CHK_RET(GetScatterStepInfo(step, nSteps, stepInfo));
118 0 : stepInfoVector.push_back(stepInfo);
119 0 : if (indexMap.count(stepInfo.fromRank) == 0 && stepInfo.rxSliceIdxs.size() > 0) {
120 0 : u32 fromRankIdx = virtRankId2RankId(stepInfo.fromRank);
121 0 : indexMap[stepInfo.fromRank] = linksDie0.size();
122 0 : linksDie0.push_back(tempLinks.at(fromRankIdx)[0]);
123 0 : if (axisSize > 1) {
124 0 : linksDie1.push_back(tempLinks.at(fromRankIdx)[1]);
125 : }
126 0 : scatterRankGroup.AddRank(fromRankIdx);
127 : }
128 0 : if (indexMap.count(stepInfo.toRank) == 0 && stepInfo.txSliceIdxs.size() > 0) {
129 0 : u32 toRankIdx = virtRankId2RankId(stepInfo.toRank);
130 0 : indexMap[stepInfo.toRank] = linksDie0.size();
131 0 : linksDie0.push_back(tempLinks.at(toRankIdx)[0]);
132 0 : if (axisSize > 1) {
133 0 : linksDie1.push_back(tempLinks.at(toRankIdx)[1]);
134 : }
135 0 : scatterRankGroup.AddRank(toRankIdx);
136 : }
137 0 : }
138 0 : scatterRankGroup.AddRank(myRank_);
139 :
140 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
141 0 : for (uint32_t axisId = 0; axisId < axisSize; axisId++) { // 2个die上各一个mission
142 0 : CcuInstructionScatterNHR1D ccuInstruction;
143 0 : ccuInstruction.Init(
144 : myVirtRankId, rootId, inputAddr, outputAddr, scratchAddr, axisId, axisSize, sliceSize, die0Size, die1Size,
145 : inputSliceStride, outputSliceStride, inputRepeatStride, outputRepeatStride, repeatNum, repeatNumVar,
146 0 : stepInfoVector, indexMap, token, op_, tempVTopo_, isOutputScratch);
147 0 : HCCL_INFO(
148 : "[CcuTempScatterNHRMem2Mem1D] Run Init: myVirtRankId[%u], myRank_[%d], repeatNum[%u], inputAddr[%llu], "
149 : "outputAddr[%llu], inputSliceStride[%llu], outputSliceStride[%llu], "
150 : "inputRepeatStride[%llu], outputRepeatStride[%llu], die0Size[%llu], die1Size[%llu], "
151 : "repeatNumVar[%llu]",
152 : myVirtRankId, myRank_, repeatNum, inputAddr, outputAddr, inputSliceStride, outputSliceStride,
153 : inputRepeatStride, outputRepeatStride, die0Size, die1Size, repeatNumVar);
154 0 : ccuInstruction.SetLinks(axisId == 0 ? linksDie0 : linksDie1);
155 0 : ccuInstruction.SetRankGroup(scatterRankGroup);
156 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
157 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionScatterNHR1D>(ccuInstruction)));
158 0 : }
159 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
160 0 : HCCL_INFO("[CcuTempScatterNHRMem2Mem1D] Template Run for all steps Ends.");
161 0 : return HcclResult::HCCL_SUCCESS;
162 0 : }
163 :
164 0 : HcclResult CcuTempScatterNHRMem2Mem1D::GetScatterStepInfo(u32 step, u32 nSteps, NHRStepInfo& stepInfo)
165 : {
166 0 : u32 virtRankIdx = tempVirtRankMap_[myRank_];
167 0 : stepInfo.txSliceIdxs.clear();
168 0 : stepInfo.rxSliceIdxs.clear();
169 0 : stepInfo.nSlices = 0;
170 0 : stepInfo.toRank = tempRankSize_;
171 0 : stepInfo.fromRank = tempRankSize_;
172 0 : stepInfo.step = step;
173 0 : stepInfo.myRank = virtRankIdx;
174 0 : uint32_t rootId = tempVirtRankMap_[rootId_];
175 0 : u32 deltaRoot = (rootId + tempRankSize_ - virtRankIdx) % tempRankSize_;
176 0 : u32 deltaRankPair = 1 << step;
177 : // 数据份数和数据编号增量
178 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << step)) / (1 << (step + 1)); // 向上取整设计了下的
179 0 : u32 deltaSliceIndex = 1 << (step + 1);
180 : // 是否为2的幂
181 0 : u32 nRanks = 0;
182 0 : bool isPowerOfTwo = (tempRankSize_ & (tempRankSize_ - 1)) == 0;
183 0 : if (!isPowerOfTwo && step == nSteps - 1) {
184 0 : nRanks = tempRankSize_ - deltaRankPair;
185 : } else {
186 0 : nRanks = deltaRankPair;
187 : }
188 :
189 0 : if (deltaRoot < nRanks) { // 需要发
190 0 : u32 sendTo = (virtRankIdx + tempRankSize_ - deltaRankPair) % tempRankSize_;
191 0 : u32 txSliceIdx = sendTo;
192 0 : for (u32 i = 0; i < nSlices; i++) {
193 0 : u32 targetTxSliceIdx = txSliceIdx;
194 0 : stepInfo.txSliceIdxs.push_back(targetTxSliceIdx);
195 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
196 : }
197 0 : stepInfo.toRank = sendTo;
198 0 : stepInfo.nSlices = nSlices;
199 0 : } else if (deltaRoot >= deltaRankPair && deltaRoot < nRanks + deltaRankPair) { // 需要收
200 0 : u32 recvFrom = (virtRankIdx + deltaRankPair) % tempRankSize_;
201 0 : u32 rxSliceIdx = virtRankIdx;
202 0 : for (u32 i = 0; i < nSlices; i++) {
203 0 : u32 targetRxSliceIdx = rxSliceIdx;
204 0 : stepInfo.rxSliceIdxs.push_back(targetRxSliceIdx);
205 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
206 : }
207 0 : stepInfo.fromRank = recvFrom;
208 0 : stepInfo.nSlices = nSlices;
209 : }
210 0 : return HcclResult::HCCL_SUCCESS;
211 : }
212 : } // namespace Hccl
|