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 "coll_reduce_scatter_mesh_opbase_pipeline_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollReduceScatterMeshOpbasePipelineExecutor::CollReduceScatterMeshOpbasePipelineExecutor(
16 : const HcclDispatcher dispatcher,
17 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
18 0 : : CollReduceScatterExecutor(dispatcher, topoMatcher)
19 : {
20 0 : DMAReduceFlag_ = true;
21 0 : }
22 :
23 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::RunLoop(OpParam ¶m, AlgResourceResponse &algRes)
24 : {
25 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceScatterMeshOpbasePipelineExecutor][RunLoop] begins.");
26 :
27 0 : u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
28 0 : ReduceType reduceType = ((param.reduceType != HCCL_REDUCE_PROD) &&
29 0 : (param.DataDes.dataType != HCCL_DATA_TYPE_INT64)) ?
30 : ReduceType::INLINE_REDUCE : ReduceType::TBE_REDUCE;
31 :
32 0 : u8 *curInputPtr = static_cast<u8 *>(param.inputPtr);
33 0 : u8 *curOutputPtr = static_cast<u8 *>(param.outputPtr);
34 0 : CHK_PTR_NULL(curInputPtr);
35 0 : CHK_PTR_NULL(curOutputPtr);
36 :
37 0 : u64 maxCountPerLoop = CalcLoopMaxCount(unitSize);
38 0 : HCCL_DEBUG("[CollReduceScatterMeshOpbasePipelineExecutor][RunLoop]tag[%s], userRankSize is [%u], maxCountPerLoop "
39 : "is [%llu].", param.tag.c_str(), topoAttr_.userRankSize, maxCountPerLoop);
40 :
41 : // 先获取 comm level0 \ comm level1 的value
42 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
43 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
44 0 : u32 commIndex = level0CommInfo.localRank;
45 :
46 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
47 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
48 :
49 0 : DeviceMem userInMem = DeviceMem::create(param.inputPtr, param.DataDes.count * unitSize);
50 0 : u64 reduceAttr = GetReduceAttr(userInMem, const_cast<DeviceMem&>(algRes.cclInputMem), param.DataDes.dataType,
51 : param.reduceType); // scratchMem用的cclin
52 0 : u64 bufferSize = algRes.cclInputMem.size();
53 :
54 0 : auto originalAlgTypeLevel1 = static_cast<u32>(algType_.algoLevel1);
55 :
56 0 : for (u64 countLeft = param.DataDes.count, curCount = 0, curOffset = 0, curSize = 0; countLeft > 0;
57 0 : countLeft -= curCount) {
58 0 : curInputPtr += curSize;
59 0 : curOutputPtr += curSize;
60 :
61 0 : curCount = (countLeft > maxCountPerLoop) ? maxCountPerLoop : countLeft;
62 0 : curSize = curCount * unitSize;
63 :
64 0 : HCCL_CONFIG_DEBUG(HCCL_ALG, "[CollReduceScatterMeshOpbasePipelineExecutor][RunLoop]tag[%s], curOffset[%llu]," \
65 : "curInputPtr[%p], curOutputPtr[%p], curCount[%llu], dataType[%d].",
66 : param.tag.c_str(), curOffset, curInputPtr, curOutputPtr, curCount, param.DataDes.dataType);
67 :
68 0 : bool hugeData = IsHugeData(curSize);
69 0 : u8 deterministic = topoMatcher_->GetExternalInputHcclDeterministic();
70 0 : auto meta = HcclOpMetaInfo::GetOneForReduceScatter(originalAlgTypeLevel1, param.DataDes.dataType, reduceType,
71 : hugeData, false, CopyPattern::BCOPY, false, deterministic, false);
72 0 : CHK_RET(InitTask(dispatcher_, const_cast<Stream&>(param.stream), meta.isEnableCache, meta.GetCacheKey()));
73 :
74 0 : ExecMem execMem;
75 0 : execMem.count = curCount;
76 0 : execMem.inputMem = algRes.cclInputMem;
77 0 : execMem.outputMem = algRes.cclOutputMem;
78 0 : execMem.scratchMem = algRes.scratchMem;
79 : // 使用当前Loop偏移到的地址作为当前的inputPtr和outputPtr
80 0 : execMem.inputPtr = curInputPtr;
81 0 : execMem.outputPtr = curOutputPtr;
82 :
83 0 : std::unique_ptr<AlgTemplateBase> tempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
84 0 : TemplateType::TEMPLATE_REDUCESCATTER_PIPELINE, dispatcher_);
85 0 : CHK_SMART_PTR_NULL(tempAlg);
86 :
87 0 : HcomCollOpInfo opInfo = {"", execMem.inputPtr, execMem.outputPtr, param.DataDes.count, param.DataDes.dataType,
88 0 : param.root, param.reduceType};
89 :
90 0 : CHK_RET(tempAlg->Prepare(&opInfo, execMem.inputMem, curCount, bufferSize, curOffset, level0CommInfo,
91 : level1CommInfo, const_cast<Stream&>(param.stream), algResResp_->slaveStreams, algResResp_->notifiesMain,
92 : algResResp_->notifiesAux, reduceAttr));
93 0 : CHK_RET(tempAlg->RunAsync());
94 :
95 0 : CHK_RET(LaunchTask(dispatcher_, const_cast<Stream&>(param.stream)));
96 :
97 0 : curOffset += curSize;
98 0 : }
99 :
100 0 : return HCCL_SUCCESS;
101 0 : }
102 :
103 0 : void CollReduceScatterMeshOpbasePipelineExecutor::ParseParam(const OpParam& param)
104 : {
105 0 : tag_ = param.tag;
106 0 : aicpuUnfoldMode_ = param.aicpuUnfoldMode;
107 0 : }
108 :
109 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::CalcStreamNum(u32& streamNum)
110 : {
111 0 : u32 totalStreamNum = topoAttr_.deviceNumPerAggregation + 1U;
112 0 : streamNum = totalStreamNum - 1U;
113 0 : HCCL_INFO("[CollReduceScatterMeshOpbasePipelineExecutor][CalcStreamNum] tag[%s] streamNum[%u]",
114 : tag_.c_str(), streamNum);
115 0 : return HCCL_SUCCESS;
116 : }
117 :
118 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
119 : {
120 0 : TransportMemType inputType = TransportMemType::RESERVED;
121 0 : TransportMemType outputType = TransportMemType::RESERVED;
122 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
123 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
124 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
125 0 : return HCCL_SUCCESS;
126 : }
127 :
128 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::CalcLevel0CommInfo(TransportMemType inputType,
129 : TransportMemType outputType,
130 : std::vector<LevelNSubCommTransport>& opTransport)
131 : {
132 0 : CommParaInfo commParaInfo(COMM_LEVEL0, CommType::COMM_TAG_MESH);
133 0 : commParaInfo.meshSinglePlane = true;
134 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL0], inputType, outputType));
135 0 : return HCCL_SUCCESS;
136 0 : }
137 :
138 : // PipeLine模式下使用Ring算法
139 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::CalcLevel1CommInfo(TransportMemType inputType,
140 : TransportMemType outputType,
141 : std::vector<LevelNSubCommTransport>& opTransport)
142 : {
143 0 : CommParaInfo commParaInfo(COMM_LEVEL1, CommType::COMM_TAG_RING_INNER);
144 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL1], inputType, outputType));
145 0 : return HCCL_SUCCESS;
146 0 : }
147 :
148 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::CalcTransportMemType(TransportMemType &inputType,
149 : TransportMemType &outputType)
150 : {
151 0 : inputType = TransportMemType::CCL_INPUT;
152 0 : outputType = TransportMemType::CCL_OUTPUT;
153 0 : HCCL_INFO("[CollReduceScatterMeshOpbasePipelineExecutor][CalcTransportMemType] tag[%s] inputType[%d],"
154 : " outputType[%d]", tag_.c_str(), inputType, outputType);
155 0 : return HCCL_SUCCESS;
156 : }
157 :
158 0 : u64 CollReduceScatterMeshOpbasePipelineExecutor::CalcLoopMaxCount(const u32 unitSize)
159 : {
160 : // 中转内存单次最多能够接受的output count,放开ranksize限制
161 0 : u64 maxCountPerLoop = ((inCCLbufferSize_ / (HCCL_MIN_SLICE_ALIGN_910B * PIPELINE_DEPTH)) \
162 0 : * HCCL_MIN_SLICE_ALIGN_910B - HCCL_MIN_SLICE_ALIGN_910B) / unitSize;
163 0 : HCCL_INFO("[CollReduceScatterMeshOpbasePipelineExecutor][CalcLoopMaxCount] maxCountPerLoop[%llu]", maxCountPerLoop);
164 0 : return maxCountPerLoop;
165 : }
166 :
167 0 : bool CollReduceScatterMeshOpbasePipelineExecutor::IsHugeData(const u64 curSize, OpParam *param)
168 : {
169 0 : bool hugeData = curSize > RDMA_SEND_MAX_SIZE || curSize > SDMA_SEND_MAX_SIZE;
170 0 : return hugeData;
171 : }
172 :
173 :
174 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::Getlevel1CommRank(SubCommInfo& level1CommInfo)
175 : {
176 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
177 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
178 :
179 0 : u32 commIndex = level0CommInfo.localRank; // 找到rank所在的节点间平面
180 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
181 :
182 0 : level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
183 0 : return HCCL_SUCCESS;
184 0 : }
185 :
186 0 : HcclResult CollReduceScatterMeshOpbasePipelineExecutor::SelectTempAlg(std::unique_ptr<AlgTemplateBase> &level1TempAlg, u32 level1RankSize)
187 : {
188 0 : if (level1RankSize > 1) {
189 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
190 0 : TemplateType::TEMPLATE_REDUCESCATTER_PIPELINE, dispatcher_);
191 0 : CHK_SMART_PTR_NULL(level1TempAlg);
192 : }
193 0 : return HCCL_SUCCESS;
194 : }
195 :
196 : REGISTER_EXEC("ReduceScatterMeshOpbasePipelineExecutor", ReduceScatterMeshOpbasePipeline,
197 : CollReduceScatterMeshOpbasePipelineExecutor);
198 :
199 : }
200 :
|