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_aiv_deter_small_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollAllReduceAivDeterSmallExecutor::CollAllReduceAivDeterSmallExecutor(
16 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
17 0 : : CollAllReduceExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = false;
20 0 : desc_.isAivMode = true;
21 0 : }
22 :
23 0 : HcclResult CollAllReduceAivDeterSmallExecutor::CalcStreamNum(u32& streamNum)
24 : {
25 0 : streamNum = 0; // AIV通信不需要申请从流
26 0 : HCCL_INFO("[CollAllReduceAivDeterSmallExecutor][CalcStreamNum] tag[%s] streamNum[%u].", tag_.c_str(), streamNum);
27 0 : return HCCL_SUCCESS;
28 : }
29 :
30 0 : HcclResult CollAllReduceAivDeterSmallExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
31 : {
32 0 : TransportMemType inputType = TransportMemType::RESERVED;
33 0 : TransportMemType outputType = TransportMemType::RESERVED;
34 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
35 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 : HcclResult
40 0 : CollAllReduceAivDeterSmallExecutor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
41 : {
42 0 : inputType = TransportMemType::AIV_INPUT;
43 0 : outputType = TransportMemType::AIV_OUTPUT;
44 0 : HCCL_INFO(
45 : "[CollAllReduceAivDeterSmallExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].",
46 : tag_.c_str(), inputType, outputType);
47 0 : return HCCL_SUCCESS;
48 : }
49 :
50 0 : HcclResult CollAllReduceAivDeterSmallExecutor::CalcLevel0CommInfo(
51 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
52 : {
53 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
54 0 : commParaLevel0.meshSinglePlane = true;
55 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
56 0 : return HCCL_SUCCESS;
57 0 : }
58 :
59 0 : HcclResult CollAllReduceAivDeterSmallExecutor::CalNumBlocks(
60 : u32& numBlocks, u32 rankSize, u64 dataSize, [[maybe_unused]] HcclCMDType cmdType)
61 : {
62 0 : numBlocks = rankSize; // 默认情况使用rankSize个AIV
63 :
64 0 : numBlocks = NUM_BLOCKS_FACTOR_TWO * rankSize; // 小数据量使用2倍rankSize的AIV core
65 0 : if (dataSize >= AIV_ALL_REDUCE_DETER_SIZE) {
66 0 : numBlocks = NUM_BLOCKS_FACTOR_THREE * rankSize;
67 : }
68 :
69 0 : u32 bestNumBlocks = numBlocks;
70 0 : CHK_PRT_RET(
71 : numBlocks_ < rankSize,
72 : HCCL_WARNING(
73 : "[CollAllReduceAivDeterSmallExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].", numBlocks_,
74 : rankSize),
75 : HCCL_E_PARA);
76 0 : if (numBlocks_ < numBlocks) {
77 0 : numBlocks = numBlocks_ / rankSize * rankSize;
78 : }
79 :
80 0 : HCCL_INFO(
81 : "[CollAllReduceAivDeterSmallExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
82 : numBlocks, numBlocks_, bestNumBlocks);
83 0 : return HCCL_SUCCESS;
84 : }
85 :
86 0 : HcclResult CollAllReduceAivDeterSmallExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
87 : {
88 0 : HcclUs startut = TIME_NOW();
89 0 : tag_ = param.tag;
90 0 : algResResp_ = &algRes;
91 :
92 0 : HcclResult ret = HCCL_SUCCESS;
93 0 : ExecMem execMem;
94 0 : execMem.count = param.DataDes.count;
95 0 : execMem.inputPtr = param.inputPtr;
96 0 : execMem.outputPtr = param.outputPtr;
97 :
98 0 : execMem.inputMem = algRes.aivInputMem;
99 0 : execMem.outputMem = algRes.aivOutputMem; // 使用AIVOUT
100 0 : ret = KernelRun(param, execMem);
101 :
102 0 : CHK_PRT_RET(
103 : ret != HCCL_SUCCESS,
104 : HCCL_ERROR(
105 : "[CollAllReduceAivDeterSmallExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel run failed",
106 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
107 : ret);
108 :
109 0 : HCCL_INFO(
110 : "tag[%s], AllReduce executor orchestrate success, take time [%lld]us", param.tag.c_str(),
111 : DURATION_US(TIME_NOW() - startut));
112 0 : return HCCL_SUCCESS;
113 0 : }
114 :
115 0 : HcclResult CollAllReduceAivDeterSmallExecutor::GetAivExecParam(
116 : [[maybe_unused]] const OpParam& param, [[maybe_unused]] AlgResourceResponse& algRes,
117 : [[maybe_unused]] AivSuperKernelArgs& args)
118 : {
119 0 : return HCCL_SUCCESS;
120 : }
121 :
122 0 : HcclResult CollAllReduceAivDeterSmallExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
123 : {
124 0 : HCCL_INFO("[CollAllReduceAivDeterSmallExecutor][KernelRun]AllReduce aiv enter.");
125 :
126 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
127 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
128 :
129 : void* buffersIn[MAX_RANK_SIZE];
130 : void* buffersOut[MAX_RANK_SIZE];
131 :
132 0 : u32 localRank = level0CommInfo.localRank;
133 0 : u32 localRankSize = level0CommInfo.localRankSize;
134 0 : HCCL_DEBUG(
135 : "[CollAllReduceAivDeterSmallExecutor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank, localRank);
136 :
137 0 : for (u32 i = 0; i < localRankSize; i++) {
138 0 : if (i != localRank) {
139 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersIn[i])));
140 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersOut[i])));
141 : } else {
142 0 : buffersIn[i] = execMem.inputMem.ptr();
143 0 : buffersOut[i] = execMem.outputMem.ptr();
144 : }
145 : }
146 :
147 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
148 0 : AivOpArgs opArgs{
149 : HcclCMDType::HCCL_CMD_ALLREDUCE,
150 0 : execMem.inputPtr,
151 0 : execMem.outputPtr,
152 0 : execMem.count,
153 0 : param.DataDes.dataType,
154 0 : param.reduceType,
155 : 0,
156 0 : isOpbase};
157 0 : AivTopoArgs topoArgs{localRank, localRankSize, MAX_RANK_SIZE, 0, 1, topoAttr_.deviceType};
158 0 : topoArgs.identify = algoAttr_.identifier;
159 :
160 0 : u64 dataSize = SIZE_TABLE[param.DataDes.dataType] * execMem.count;
161 : u32 numBlocks;
162 0 : CHK_PRT_RET(
163 : CalNumBlocks(numBlocks, localRankSize, dataSize) != HCCL_SUCCESS,
164 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__), HCCL_E_PARA);
165 0 : numBlocks_ = numBlocks;
166 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
167 0 : numBlocks_, param.aivTag};
168 0 : AivAlgArgs algArgs{};
169 0 : algArgs.deterministic = 1;
170 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
171 0 : algArgs.execTimeOutSet = true;
172 0 : struct AivProfilingInfo aivProfilingInfo;
173 0 : aivProfilingInfo.counter = opCounter_;
174 :
175 0 : HCCL_INFO(
176 : "[CollAllReduceAivDeterSmallExecutor][KernelRun]AllReduce bufferin[%d] bufferout[%d]", execMem.inputMem.size(),
177 : execMem.outputMem.size());
178 :
179 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
180 :
181 0 : ExtraArgs extraArgs;
182 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, false));
183 :
184 0 : CHK_PRT_RET(
185 : ret != HCCL_SUCCESS,
186 : HCCL_ERROR("[CollAllReduceAivDeterSmallExecutor][KernelRun]AllReduce aiv failed, return[%d]", ret), ret);
187 :
188 0 : HCCL_INFO("[CollAllReduceAivDeterSmallExecutor][KernelRun]AllReduce aiv run success.");
189 0 : return HCCL_SUCCESS;
190 0 : }
191 :
192 : REGISTER_EXEC("AllReduceAivDeterSmallExecutor", AllReduceAivSmallDeter, CollAllReduceAivDeterSmallExecutor);
193 :
194 : } // namespace hccl
|