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_all_reduce_reduce_plus_bcast_executor.h"
12 :
13 : namespace hccl {
14 :
15 1 : CollAllReduceReducePlusBcastExecutor::CollAllReduceReducePlusBcastExecutor(const HcclDispatcher dispatcher,
16 1 : std::unique_ptr<TopoMatcher> &topoMatcher)
17 1 : : CollAllReduceExecutor(dispatcher, topoMatcher)
18 : {
19 1 : }
20 :
21 1 : HcclResult CollAllReduceReducePlusBcastExecutor::CalcStreamNum(u32& streamNum)
22 : {
23 1 : streamNum = 0;
24 1 : HCCL_INFO("[CollAllReduceReducePlusBcastExecutor][CalcStreamNum] tag[%s] streamNum[%u]",
25 : tag_.c_str(), streamNum);
26 1 : return HCCL_SUCCESS;
27 : }
28 :
29 1 : HcclResult CollAllReduceReducePlusBcastExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
30 : {
31 1 : TransportMemType inputType = TransportMemType::RESERVED;
32 1 : TransportMemType outputType = TransportMemType::RESERVED;
33 1 : CHK_RET(CalcTransportMemType(inputType, outputType));
34 1 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
35 1 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
36 1 : return HCCL_SUCCESS;
37 : }
38 :
39 1 : HcclResult CollAllReduceReducePlusBcastExecutor::CalcTransportMemType(TransportMemType &inputType,
40 : TransportMemType &outputType)
41 : {
42 1 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
43 0 : inputType = TransportMemType::CCL_INPUT;
44 0 : outputType = TransportMemType::CCL_OUTPUT;
45 : } else {
46 1 : inputType = TransportMemType::PARAM_INPUT;
47 1 : outputType = TransportMemType::PARAM_OUTPUT;
48 : }
49 1 : HCCL_INFO("[CollAllReduceReducePlusBcastExecutor][CalcTransportMemType]" \
50 : "tag[%s] inputType[%d], outputType[%d]",
51 : tag_.c_str(), inputType, outputType);
52 1 : return HCCL_SUCCESS;
53 : }
54 :
55 1 : HcclResult CollAllReduceReducePlusBcastExecutor::CalcLevel0CommInfo(TransportMemType inputType,
56 : TransportMemType outputType,
57 : std::vector<LevelNSubCommTransport>& opTransport)
58 : {
59 1 : HCCL_INFO("[CollAllReduceReducePlusBcastExecutor][CalcLevel0CommInfo]tag[%s] start", tag_.c_str());
60 1 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
61 1 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
62 1 : HCCL_INFO("[CollAllReduceReducePlusBcastExecutor][CalcLevel0CommInfo]tag[%s] Calc RingComm finish",
63 : tag_.c_str());
64 1 : return HCCL_SUCCESS;
65 1 : }
66 :
67 0 : bool CollAllReduceReducePlusBcastExecutor::IsHugeData(const u64 curSize)
68 : {
69 0 : bool hugeData = curSize / topoAttr_.deviceNumPerAggregation / HCCL_INTERNODE_MAX_DATA_RATE > RDMA_SEND_MAX_SIZE ||
70 : curSize > SDMA_SEND_MAX_SIZE;
71 0 : return hugeData;
72 : }
73 :
74 0 : bool CollAllReduceReducePlusBcastExecutor::IsSmallData(const u64 totalSize, const u64 curSize)
75 : {
76 0 : bool smallData = IsAllReduceSmallData(curSize);
77 0 : return smallData;
78 : }
79 :
80 1 : HcclResult CollAllReduceReducePlusBcastExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
81 : {
82 1 : HCCL_CONFIG_INFO(HCCL_ALG,
83 : "[CollAllReduceReducePlusBcastExecutor][KernelRun] userRank[%u] starts.", topoAttr_.userRank);
84 1 : u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.outputMem, param.DataDes.dataType, param.reduceType);
85 :
86 1 : std::unique_ptr<AlgTemplateBase> reduceTempAlg;
87 1 : reduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_REDUCE_RECURSIVE_HALVING_DOUBLING, dispatcher_);
88 1 : CHK_SMART_PTR_NULL(reduceTempAlg);
89 1 : CHK_RET(reduceTempAlg->Prepare(reduceAttr));
90 :
91 2 : std::vector<u32> nicRankList{0, 1};
92 3 : CHK_RET(reduceTempAlg->Prepare(execMem.inputMem, execMem.outputMem, execMem.inputMem, execMem.count,
93 : param.DataDes.dataType, param.stream, param.reduceType, 0,
94 : std::vector<Slice>(0), 0, nicRankList));
95 :
96 1 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
97 1 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
98 1 : CHK_RET(RunTemplate(reduceTempAlg, level0CommInfo));
99 :
100 : // AllReduce算子实现为input->output, 所以此处将reduce算子的结果从output拷贝到input
101 2 : HcclResult ret = HcclD2DMemcpyAsync(dispatcher_,
102 1 : execMem.inputMem, execMem.outputMem, const_cast<Stream&>(param.stream));
103 1 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("MemcpyAsync failed"), ret);
104 :
105 : // 执行server间allreduce
106 1 : if (topoAttr_.devicePhyId == 0) {
107 1 : std::unique_ptr<AlgTemplateBase> allreduceTempAlg = nullptr;
108 1 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
109 0 : allreduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
110 0 : TemplateType::TEMPLATE_ALL_REDUCE_RING, dispatcher_);
111 0 : HCCL_INFO("AllReduce ring: using ring algo inter-server.");
112 0 : CHK_SMART_PTR_NULL(allreduceTempAlg);
113 0 : CHK_RET(allreduceTempAlg->Prepare(reduceAttr));
114 1 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
115 0 : u64 curSize = execMem.count * SIZE_TABLE[param.DataDes.dataType]; // 单位 byte
116 0 : HCCL_DEBUG("AllReduce recursive hd: curSize[%llu] deviceNumPerAggregation[%u] commLevel0Size[%u]",
117 : curSize, topoAttr_.deviceNumPerAggregation, level0CommInfo.localRankSize);
118 0 : if (curSize / topoAttr_.deviceNumPerAggregation <= NHR_ALLREDUCE_SMALL_SIZE) {
119 0 : allreduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR_ONESHOT, dispatcher_);
120 : } else {
121 0 : allreduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR, dispatcher_);
122 : }
123 0 : HCCL_INFO("AllReduce recursive hd: using nhr algo inter-server.");
124 0 : CHK_SMART_PTR_NULL(allreduceTempAlg);
125 0 : CHK_RET(allreduceTempAlg->Prepare(reduceAttr));
126 0 : allreduceTempAlg->CloseBarrier();
127 1 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR_V1) {
128 0 : allreduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR_V1, dispatcher_);
129 0 : HCCL_INFO("AllReduce recursive hd: using nhr_v1 algo inter-server.");
130 0 : CHK_SMART_PTR_NULL(allreduceTempAlg);
131 0 : CHK_RET(allreduceTempAlg->Prepare(reduceAttr));
132 1 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NB) {
133 0 : allreduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
134 0 : TemplateType::TEMPLATE_ALL_REDUCE_NB, dispatcher_);
135 0 : HCCL_INFO("AllReduce recursive hd: using nb algo inter-server.");
136 0 : CHK_SMART_PTR_NULL(allreduceTempAlg);
137 0 : CHK_RET(allreduceTempAlg->Prepare(reduceAttr));
138 : } else {
139 1 : allreduceTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_RECURSIVE_HALVING_DOUBLING, dispatcher_);
140 1 : HCCL_INFO("AllReduce recursive hd: using halving-doubling algo inter-server.");
141 1 : CHK_SMART_PTR_NULL(allreduceTempAlg);
142 1 : CHK_RET(allreduceTempAlg->Prepare(reduceAttr));
143 : }
144 :
145 1 : CHK_SMART_PTR_NULL(allreduceTempAlg);
146 3 : CHK_RET(allreduceTempAlg->Prepare(execMem.inputMem, execMem.outputMem, execMem.outputMem, execMem.count,
147 : param.DataDes.dataType, param.stream, param.reduceType, 0,
148 : std::vector<Slice>(0), 0, nicRankList));
149 :
150 1 : CHK_RET(CheckCommSize(COMM_LEVEL1, COMM_INDEX_0 + 1));
151 1 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, COMM_INDEX_0);
152 1 : CHK_RET(RunTemplate(allreduceTempAlg, level1CommInfo));
153 1 : }
154 :
155 : // 执行server内broadcast
156 1 : std::unique_ptr<AlgTemplateBase> bcastTempAlg;
157 1 : bcastTempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_BROADCAST_RING, dispatcher_);
158 1 : CHK_SMART_PTR_NULL(bcastTempAlg);
159 5 : CHK_RET(bcastTempAlg->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count,
160 : param.DataDes.dataType, param.stream, param.reduceType, 0));
161 1 : CHK_RET(RunTemplate(bcastTempAlg, level0CommInfo));
162 :
163 1 : return HCCL_SUCCESS;
164 1 : }
165 :
166 : REGISTER_EXEC("AllReduceReducePlusBcast", AllReduceReducePlusBcast, CollAllReduceReducePlusBcastExecutor);
167 :
168 : } // namespace hccl
|