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_ring_plus_hd_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollReduceRingPlusHdExecutor::CollReduceRingPlusHdExecutor(const HcclDispatcher dispatcher,
16 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
17 0 : : CollReduceExecutor(dispatcher, topoMatcher)
18 : {
19 0 : }
20 :
21 0 : HcclResult CollReduceRingPlusHdExecutor::CalcStreamNum(u32& streamNum)
22 : {
23 0 : u32 totalStreamNum = 1U;
24 :
25 0 : if (algType_.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_8P_RING) {
26 0 : totalStreamNum = LEVEL0_PLANE_NUM_IN_8PRING;
27 0 : } else if (algType_.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_SINGLE_RING) {
28 0 : totalStreamNum = LEVEL0_PLANE_NUM_IN_NPRING_SINGLE;
29 : }
30 0 : streamNum = totalStreamNum - 1;
31 0 : HCCL_INFO("[CollReduceRingPlusHdExecutor][CalcStreamNum] tag[%s] streamNum[%u]",
32 : tag_.c_str(), streamNum);
33 0 : return HCCL_SUCCESS;
34 : }
35 :
36 0 : HcclResult CollReduceRingPlusHdExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
37 : {
38 0 : TransportMemType inputType = TransportMemType::RESERVED;
39 0 : TransportMemType outputType = TransportMemType::RESERVED;
40 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
41 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
42 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
43 0 : return HCCL_SUCCESS;
44 : }
45 :
46 0 : HcclResult CollReduceRingPlusHdExecutor::CalcTransportMemType(TransportMemType &inputType, TransportMemType &outputType)
47 : {
48 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
49 0 : inputType = TransportMemType::CCL_INPUT;
50 0 : outputType = TransportMemType::CCL_OUTPUT;
51 : } else {
52 0 : inputType = TransportMemType::PARAM_INPUT;
53 0 : outputType = TransportMemType::PARAM_OUTPUT;
54 : }
55 0 : HCCL_INFO("[CollReduceRingPlusHdExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
56 : tag_.c_str(), inputType, outputType);
57 0 : return HCCL_SUCCESS;
58 : }
59 :
60 0 : HcclResult CollReduceRingPlusHdExecutor::CalcLevel0CommInfo(TransportMemType inputType,
61 : TransportMemType outputType,
62 : std::vector<LevelNSubCommTransport>& opTransport)
63 : {
64 0 : HCCL_INFO("[CollReduceRingPlusHdExecutor][CalcLevel0CommInfo]tag[%s] start", tag_.c_str());
65 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_RING_INNER);
66 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
67 0 : HCCL_INFO("[CollReduceRingPlusHdExecutor][CalcLevel0CommInfo]tag[%s] Calc RingComm finish", tag_.c_str());
68 0 : return HCCL_SUCCESS;
69 0 : }
70 :
71 0 : HcclResult CollReduceRingPlusHdExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
72 : {
73 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceRingPlusHdExecutor][KernelRun] userRank[%u] starts.", topoAttr_.userRank);
74 0 : u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
75 :
76 0 : std::vector<Slice> dataSegsSlice; // 数据分成ranksize份,每份的起始偏移和大小
77 0 : std::vector<std::vector<Slice> > mulRingSlice; // 数据基于该rank上环0的偏移
78 :
79 : // step1: 节点内的reducescatter
80 0 : u32 ringNum = (topoType_ == TopoType::TOPO_TYPE_8P_RING) ? LEVEL0_PLANE_NUM_IN_8PRING :
81 : LEVEL0_PLANE_NUM_IN_NPRING_SINGLE;
82 :
83 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, ringNum));
84 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
85 :
86 : // 按ranksize得到内存切分slice数为8
87 0 : u32 sliceNum = level0CommInfo.localRankSize;
88 0 : CHK_RET(AlgTemplateBase::PrepareSliceData(execMem.count, perDataSize, sliceNum, 0, dataSegsSlice));
89 :
90 : /* 外层:reducescatter */
91 : // 将每slice再切分成4份,按各ring的dev顺序排列
92 0 : if (ringNum == LEVEL0_PLANE_NUM_IN_8PRING) {
93 : // 构造ring algorithm对应的reduce-scatter实例
94 0 : mulRingSlice = PrepareMultiRingSlice(dataSegsSlice, tag_, false, topoAttr_.nicList);
95 0 : CHK_PRT_RET(mulRingSlice.size() != ringNum, HCCL_ERROR("[CollReduceRingPlusHdExecutor]ringNum[%u] "\
96 : "!=mulRingSlice size[%zu]", ringNum, mulRingSlice.size()), HCCL_E_INTERNAL);
97 : } else {
98 0 : mulRingSlice.push_back(dataSegsSlice); // 应该offset全为0,而大小和dataSegsSlice中一样,里面的offset不使用
99 : }
100 0 : CHK_RET(MultiRingReduceScatter(tag_, execMem.inputMem, execMem.outputMem, execMem.count,
101 : param.DataDes.dataType, param.reduceType, mulRingSlice, param.stream,
102 : PROF_STAGE_0, 0, nullptr));
103 :
104 0 : HCCL_INFO("reduce 8PringHD stage0 run success");
105 :
106 : // step2: 节点间的reduce
107 : u64 hdSize;
108 : u32 segmentIdx;
109 : u32 commIndex;
110 0 : CHK_RET(PrepareLevel1CommInfo(segmentIdx, commIndex, hdSize, level0CommInfo, mulRingSlice, tag_));
111 :
112 0 : u64 hdCount = hdSize / perDataSize;
113 :
114 0 : HCCL_DEBUG("commIdx:%u TagCommInfo[%s].commLevel1.size():%u", commIndex, tag_.c_str(),
115 : level0CommInfo.localRankSize);
116 :
117 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
118 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
119 :
120 0 : DeviceMem reduceInput = execMem.inputMem.range(dataSegsSlice[segmentIdx].offset, hdSize);
121 0 : CHK_SMART_PTR_NULL(reduceInput);
122 0 : DeviceMem reduceOutput = execMem.outputMem.range(dataSegsSlice[segmentIdx].offset, hdSize);
123 0 : CHK_SMART_PTR_NULL(reduceOutput);
124 :
125 0 : u64 reduceAttr = GetReduceAttr(reduceInput, reduceOutput, param.DataDes.dataType, param.reduceType);
126 :
127 0 : std::unique_ptr<AlgTemplateBase> level1TempAlg;
128 0 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
129 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_REDUCE_RING, dispatcher_);
130 : } else {
131 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_REDUCE_RECURSIVE_HALVING_DOUBLING,
132 0 : dispatcher_);
133 : }
134 0 : CHK_SMART_PTR_NULL(level1TempAlg);
135 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
136 :
137 0 : u32 subUserrankRoot = topoMatcher_->GetSubRootUserRank(topoAttr_.userRank, param.root);
138 0 : CHK_PRT_RET(subUserrankRoot == INVALID_VALUE_RANKID,
139 : HCCL_ERROR("[ReduceOperator][ReduceRingPlusHd]subUserrankRoot[%u] is invalid,userRank[%u],root[%u]",
140 : subUserrankRoot, topoAttr_.userRank, param.root), HCCL_E_INTERNAL);
141 :
142 0 : u32 planeRoot = 0;
143 0 : CHK_RET(GetRankByUserRank(COMM_LEVEL1, commIndex, subUserrankRoot, planeRoot));
144 :
145 0 : u32 ranksize = level1CommInfo.localRankSize;
146 : // 节点间的hd 使用环0来记录
147 0 : CHK_RET(level1TempAlg->Prepare(reduceInput, reduceOutput, reduceOutput, hdCount, param.DataDes.dataType,
148 : param.stream, param.reduceType, planeRoot, std::vector<Slice>(0), dataSegsSlice[segmentIdx].offset));
149 :
150 0 : CHK_RET(level1TempAlg->RegisterProfiler((ranksize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level1CommInfo.localRank, \
151 : PROF_STAGE_1, HCCL_EXEC_STEP_NOT_SET, param.stream));
152 :
153 0 : CHK_RET(RunTemplate(level1TempAlg, level1CommInfo));
154 :
155 0 : HCCL_INFO("reduce 8PringHD stage1 run success");
156 :
157 : // step3: 节点内的gatherring,只有在root所在server内进行gather操作
158 : SingleSubCommTransport &level0TransportInfo =
159 0 : const_cast<SingleSubCommTransport&>(algResResp_->opTransportResponse[COMM_LEVEL0][COMM_INDEX_0]);
160 :
161 0 : if (level0TransportInfo.userRank2subCommRank.find(param.root) !=
162 0 : level0TransportInfo.userRank2subCommRank.end()) {
163 0 : CHK_RET(MultiRingGather(tag_, execMem.outputMem, execMem.outputMem, hdCount, param.DataDes.dataType,
164 : mulRingSlice, param.reduceType, param.root, const_cast<Stream &>(param.stream), PROF_STAGE_2));
165 : }
166 0 : HCCL_INFO("reduce 8PringHD stage2 run success");
167 :
168 0 : return HCCL_SUCCESS;
169 0 : }
170 :
171 : REGISTER_EXEC("ReduceRingPlusHd", ReduceRingPlusHd, CollReduceRingPlusHdExecutor);
172 :
173 : } // namespace hccl
|