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