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