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