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