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_mesh_executor.h"
12 :
13 : namespace hccl {
14 :
15 1 : CollReduceMeshExecutor::CollReduceMeshExecutor(
16 1 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
17 1 : : CollReduceExecutor(dispatcher, topoMatcher)
18 4 : {}
19 :
20 4 : void CollReduceMeshExecutor::ParseParam(const OpParam& param)
21 : {
22 4 : tag_ = param.tag;
23 4 : root_ = param.root;
24 : bool isInlineReduce
25 4 : = IsSupportSDMAReduce(param.inputPtr, param.outputPtr, param.DataDes.dataType, param.reduceType);
26 8 : meshSinglePlane_ = (topoAttr_.deviceType == DevType::DEV_TYPE_910B)
27 4 : && topoMatcher_->GetDeterministicConfig() == DETERMINISTIC_DISABLE && isInlineReduce
28 8 : && (GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
29 4 : }
30 :
31 4 : HcclResult CollReduceMeshExecutor::CalcStreamNum(u32& streamNum)
32 : {
33 4 : u32 totalStreamNum = topoAttr_.deviceNumPerAggregation > 1U ? topoAttr_.deviceNumPerAggregation - 1U : 1U;
34 4 : streamNum = totalStreamNum - 1U;
35 4 : HCCL_INFO("[CollReduceMeshExecutor][CalcStreamNum] tag[%s] streamNum[%u]", tag_.c_str(), streamNum);
36 4 : return HCCL_SUCCESS;
37 : }
38 :
39 4 : HcclResult CollReduceMeshExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
40 : {
41 4 : TransportMemType inputType = TransportMemType::RESERVED;
42 4 : TransportMemType outputType = TransportMemType::RESERVED;
43 4 : CHK_RET(CalcTransportMemType(inputType, outputType));
44 4 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
45 4 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
46 4 : return HCCL_SUCCESS;
47 : }
48 :
49 4 : HcclResult CollReduceMeshExecutor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
50 : {
51 4 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
52 4 : inputType = TransportMemType::CCL_INPUT;
53 4 : outputType = TransportMemType::CCL_OUTPUT;
54 : } else {
55 0 : inputType = TransportMemType::PARAM_INPUT;
56 0 : outputType = TransportMemType::PARAM_OUTPUT;
57 : }
58 4 : HCCL_INFO(
59 : "[CollReduceMeshExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]", tag_.c_str(), inputType,
60 : outputType);
61 4 : return HCCL_SUCCESS;
62 : }
63 :
64 4 : HcclResult CollReduceMeshExecutor::CalcLevel0CommInfo(
65 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
66 : {
67 4 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
68 4 : commParaLevel0.meshSinglePlane = meshSinglePlane_;
69 4 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
70 4 : return HCCL_SUCCESS;
71 4 : }
72 :
73 0 : HcclResult CollReduceMeshExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
74 : {
75 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceMeshExecutor][KernelRun] userRank[%u] starts.", topoAttr_.userRank);
76 0 : u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
77 :
78 0 : std::vector<Slice> dataSegsSlice; // 数据分成ranksize份,每份的起始偏移和大小
79 0 : std::vector<std::vector<Slice>> multiStreamSlice; // 每个stream使用的数据基于用户buffer的偏移
80 : // std::unique_ptr<AlgTemplateBase> level1TempAlg;
81 : // std::unique_ptr<AlgTemplateBase> level0TempAlg;
82 :
83 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
84 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
85 :
86 0 : u32 sliceNum = level0CommInfo.localRankSize;
87 : // 根据数据量算每个环上数据的偏移和大小
88 0 : CHK_RET(AlgTemplateBase::PrepareSliceData(execMem.count, perDataSize, sliceNum, 0, dataSegsSlice));
89 : // mesh算法stream数量为server内rank数减1
90 :
91 0 : CHK_RET(ActiveSlaveStreams(param.stream));
92 :
93 0 : if (topoMatcher_->GetExternalInputHcclDeterministic() == DETERMINISTIC_DISABLE
94 0 : && (param.DataDes.dataType != HCCL_DATA_TYPE_INT64)
95 0 : && (topoAttr_.deviceType == DevType::DEV_TYPE_910B && param.reduceType != HCCL_REDUCE_PROD)) {
96 0 : CHK_RET(MultiStreamReduceScatterMeshAtomic(
97 : tag_, execMem.inputMem, execMem.outputMem, execMem.count, param.DataDes.dataType, param.reduceType,
98 : dataSegsSlice, const_cast<Stream&>(param.stream), COMM_LEVEL0));
99 : } else {
100 0 : std::vector<std::vector<Slice>> multiStreamSlice; // 每个stream使用的数据基于用户buffer的偏移
101 : // mesh算法stream数量为rank数减1
102 0 : CHK_RET(AlgTemplateBase::PrepareSliceMeshStreams(dataSegsSlice, sliceNum - 1, multiStreamSlice));
103 0 : CHK_RET(MultiStreamReduceScatterMesh(
104 : tag_, execMem.inputMem, execMem.outputMem, execMem.count, param.DataDes.dataType, param.reduceType,
105 : multiStreamSlice, const_cast<Stream&>(param.stream), COMM_LEVEL0));
106 0 : }
107 0 : HCCL_INFO("[CollReduceMeshExecutor]reduce mesh stage0 run success");
108 :
109 : // step2: 节点间的reduce
110 0 : u32 commIndex = level0CommInfo.localRank;
111 0 : CHK_PRT_RET(
112 : commIndex >= dataSegsSlice.size(),
113 : HCCL_ERROR(
114 : "[CollReduceMeshExecutor][Run]commIndex[%u] >= "
115 : "dataSegsSlice size[%zu]",
116 : commIndex, dataSegsSlice.size()),
117 : HCCL_E_INTERNAL);
118 :
119 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
120 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
121 :
122 0 : HCCL_DEBUG(
123 : "commIdx:%u TagCommInfo[%s].commLevel1.size():%zu", commIndex, tag_.c_str(), level1CommInfo.links.size());
124 :
125 0 : DeviceMem reduceInput = execMem.inputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
126 0 : CHK_SMART_PTR_NULL(reduceInput);
127 0 : DeviceMem reduceOutput = execMem.outputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
128 0 : CHK_SMART_PTR_NULL(reduceOutput);
129 :
130 0 : u32 rankSize = level1CommInfo.localRankSize;
131 0 : if (rankSize > 1) {
132 0 : u64 reduceAttr = GetReduceAttr(reduceInput, reduceOutput, param.DataDes.dataType, param.reduceType);
133 :
134 0 : u32 subUserrankRoot = topoMatcher_->GetSubRootUserRank(topoAttr_.userRank, param.root);
135 :
136 0 : CHK_PRT_RET(
137 : subUserrankRoot == INVALID_VALUE_RANKID,
138 : HCCL_ERROR(
139 : "[ReduceOperator][ReduceMeshExecutor]subUserrankRoot[%u] is invalid,userRank[%u],root[%u]",
140 : subUserrankRoot, topoAttr_.userRank, param.root),
141 : HCCL_E_INTERNAL);
142 :
143 0 : u32 planeRoot = 0;
144 0 : CHK_RET(GetRankByUserRank(COMM_LEVEL1, commIndex, subUserrankRoot, planeRoot));
145 :
146 0 : std::unique_ptr<AlgTemplateBase> level1TempAlg;
147 0 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
148 : level1TempAlg
149 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_REDUCE_RING, dispatcher_);
150 : } else {
151 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
152 0 : TemplateType::TEMPLATE_REDUCE_RECURSIVE_HALVING_DOUBLING, dispatcher_);
153 : }
154 0 : CHK_SMART_PTR_NULL(level1TempAlg);
155 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
156 : // 节点间的hd 使用环0来记录
157 0 : u64 hdCount = dataSegsSlice[commIndex].size / perDataSize;
158 :
159 0 : CHK_RET(level1TempAlg->Prepare(
160 : reduceInput, reduceOutput, reduceOutput, hdCount, param.DataDes.dataType, param.stream, param.reduceType,
161 : planeRoot, std::vector<Slice>(0), dataSegsSlice[commIndex].offset));
162 :
163 0 : CHK_RET(level1TempAlg->RegisterProfiler(
164 : (level1CommInfo.localRankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level1CommInfo.localRank, PROF_STAGE_1,
165 : HCCL_EXEC_STEP_NOT_SET, param.stream));
166 :
167 0 : CHK_RET(RunTemplate(level1TempAlg, level1CommInfo));
168 0 : } else {
169 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, reduceOutput, reduceInput, const_cast<Stream&>(param.stream)));
170 : }
171 :
172 0 : HCCL_INFO("[CollReduceMeshExecutor]reduce mesh stage1 run success");
173 :
174 : SingleSubCommTransport& level0TransportInfo
175 0 : = const_cast<SingleSubCommTransport&>(algResResp_->opTransportResponse[COMM_LEVEL0][COMM_INDEX_0]);
176 :
177 0 : if (level0TransportInfo.userRank2subCommRank.find(param.root) != level0TransportInfo.userRank2subCommRank.end()) {
178 0 : const u32 rootRank = level0TransportInfo.userRank2subCommRank[param.root];
179 :
180 : std::unique_ptr<AlgTemplateBase> level0TempAlg
181 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_GATHER_MESH, dispatcher_);
182 0 : CHK_SMART_PTR_NULL(level0TempAlg);
183 0 : CHK_RET(level0TempAlg->Prepare(
184 : algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux, topoAttr_.userRank));
185 0 : CHK_RET(level0TempAlg->Prepare(
186 : execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count, param.DataDes.dataType,
187 : const_cast<Stream&>(param.stream), param.reduceType, rootRank, dataSegsSlice));
188 :
189 0 : u32 rankSize = level0CommInfo.localRankSize;
190 0 : CHK_RET(level0TempAlg->RegisterProfiler(
191 : (0 << PROF_RINGINDEX_OFFSET_OF_PLANEID) + (rankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID)
192 : + level0CommInfo.localRank,
193 : PROF_STAGE_0, HCCL_EXEC_STEP_NOT_SET, param.stream));
194 0 : CHK_RET(RunTemplate(level0TempAlg, level0CommInfo));
195 0 : }
196 0 : HCCL_INFO("[CollReduceMeshExecutor]reduce mesh stage2 run success");
197 :
198 0 : return HCCL_SUCCESS;
199 0 : }
200 :
201 : REGISTER_EXEC("ReduceMeshExecutor", ReduceMesh, CollReduceMeshExecutor);
202 :
203 : } // namespace hccl
|