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 : #ifndef HCCL_AIV_H
12 : #define HCCL_AIV_H
13 :
14 : #include <vector>
15 : #include "string"
16 :
17 : #include "hccl_types.h"
18 : #include "acl/acl_rt.h"
19 : #include "hccl_common.h"
20 : #include "common.h"
21 : #include "mem_device_pub.h"
22 : #include "alg_profiling.h"
23 :
24 : namespace hccl {
25 : constexpr u64 ATTR_POS_AIV_COMM_BUFFER = 0x00;
26 : constexpr u64 ATTR_POS_AIV_COMM_INFO_BUFFER = 0x01;
27 : constexpr u64 AIV_COMM_BUFFER_BITMASK = 0x01;
28 : constexpr u64 AIV_COMM_INFO_BUFFER_BITMASK = 0x02;
29 :
30 : constexpr u64 AIV_ALL_REDUCE_BIG_SIZE = 16 * 1024 * 1024;
31 : constexpr u64 AIV_ALL_REDUCE_A3_ENTRY_SIZE = 1 * 1024 * 1024; // AllReduce单张卡数据量A3
32 : constexpr u64 AIV_ALL_REDUCE_A3_GRAPH_ENTRY_SIZE = 4 * 1024 * 1024;
33 : constexpr u64 AIV_REDUCE_SCATTER_DETER_SMALL_SIZE = 1 * 1024 * 1024;
34 : constexpr u64 AIV_REDUCE_SCATTER_BIG_SIZE = 190 * 1024;
35 : constexpr u64 AIV_REDUCE_SCATTER_MID_SIZE = 2 * 1024 * 1024;
36 : constexpr u64 AIV_REDUCE_SCATTER_SMALL_SIZE = 1 * 1024 * 1024;
37 : constexpr u64 AIV_REDUCE_SCATTER_A3_ENTRY_SIZE = 1 * 1024 * 1024;
38 : constexpr u64 AIV_REDUCE_SCATTER_A3_GRAPH_ENTRY_SIZE = 4 * 1024 * 1024;
39 : constexpr u64 AIV_ALL_GATHER_BIG_SIZE = 512 * 1024;
40 : constexpr u64 AIV_ALL_GATHER_SMALL_SIZE = 700 * 1024;
41 : constexpr u64 AIV_ALL_GATHER_A3_ENTRY_SIZE = 512 * 1024;
42 : constexpr u64 AIV_ALL_GATHER_A3_GRAPH_ENTRY_SIZE = 4 * 1024 * 1024;
43 : constexpr u64 AIV_ALL_TO_ALL_BIG_SIZE = 512 * 1024;
44 : constexpr u64 AIV_ALL_TO_ALL_A3_ENTRY_SIZE = 512 * 1024;
45 : constexpr u64 AIV_BIG_SIZE = 256 * 1024 * 1024;
46 : constexpr u64 AIV_ALL_REDUCE_DETER_SIZE = 1 * 1024 * 1024; // AllReduce确定性计算
47 : constexpr u32 AIV_REDUCE_SCATTER_RANK_SIZE_8 = 8;
48 :
49 : constexpr u64 AIV_A3_ALL_REDUCE_GRAPH_GUIYI_SIZE = 190 * 1024;
50 : constexpr u64 AIV_A3_REDUCE_SCATTER_GRAPH_GUIYI_SIZE = 760 * 1024;
51 : constexpr u64 AIV_A3_ALL_GATHER_GRAPH_GUIYI_SIZE = 760 * 1024;
52 : constexpr u64 AIV_A3_ALL_TO_ALL_GRAPH_GUIYI_SIZE = 760 * 1024;
53 :
54 : constexpr u64 AIV_REDUCE_SCATTER_A3_SMALL_RANKSIZE_ENTRY_SIZE = 1 * 1024 * 1024;
55 : constexpr u64 AIV_REDUCE_SCATTER_A3_MID_RANKSIZE_ENTRY_SIZE = 512 * 1024;
56 : constexpr u64 AIV_REDUCE_SCATTER_A3_LARGE_RANKSIZE_ENTRY_SIZE = 128 * 1024;
57 :
58 : constexpr u64 AIV_ALL_GATHER_A3_SMALL_RANKSIZE_ENTRY_SIZE = 1 * 1024 * 1024;
59 : constexpr u64 AIV_ALL_GATHER_A3_MID_RANKSIZE_ENTRY_SIZE = 512 * 1024;
60 : constexpr u64 AIV_ALL_GATHER_A3_LARGE_RANKSIZE_ENTRY_SIZE = 32 * 1024;
61 :
62 : constexpr u64 AIV_A3_CROSSNODE_TINY_SIZE = 28 * 1024;
63 : constexpr u64 AIV_A3_CROSSNODE_SMALL_SIZE = 112 * 1024;
64 : constexpr u64 AIV_A3_CROSSNODE_MID_SIZE = 448 * 1024;
65 :
66 : constexpr u32 MAX_RANK_SIZE = 16; // server内最大卡数
67 : constexpr u32 MAX_RANK_SIZE_A3 = 768; // 超节点内最大卡数
68 : constexpr u32 MAX_RANK_SIZE_RDMA = 64; // 跨机支持的最大卡数
69 :
70 : constexpr u32 NUM_BLOCKS_FACTOR_TWO = 2;
71 : constexpr u32 NUM_BLOCKS_FACTOR_THREE = 3;
72 : constexpr u32 NUM_BLOCKS_FACTOR_FOUR = 4;
73 : constexpr u32 NUM_BLOCKS_FACTOR_SIX = 6;
74 : constexpr u32 NUM_BLOCKS_FACTOR_EIGHT = 8;
75 : constexpr u32 NUM_BLOCKS_THREE_PER_RANK_A3 = 3;
76 : constexpr u32 NUM_BLOCKS_FOUR_PER_RANK_A3 = 4;
77 : constexpr u32 MAX_NUM_BLOCKS = 48;
78 : constexpr u32 HALF_MAX_NUM_BLOCKS = 24;
79 : constexpr u32 ONE_THIRD_MAX_NUM_BLOCKS = 16;
80 : constexpr u32 ONE_FOURTH_MAX_NUM_BLOCKS = 12;
81 : constexpr u32 ONE_SIXTH_MAX_NUM_BLOCKS = 8;
82 : constexpr u32 ONE_EIGHTH_MAX_NUM_BLOCKS = 6;
83 :
84 : constexpr s32 TAG_INIT_VALUE = 1;
85 : constexpr s32 TAG_RESET_COUNT = 1000;
86 : constexpr s32 AIV_A2_ALL_REDUCE_RDMA_KERNEL_NUM = 2;
87 :
88 : constexpr u32 TIME_S_TO_US = 1000000;
89 : constexpr u32 AIV_TIMEOUT_DEFAULT = 1091;
90 : constexpr u32 AIV_TIMEOUT_DEFAULT_US = 1091 * TIME_S_TO_US;
91 : constexpr u32 AIV_TIMEOUT_MAX = 1091;
92 : constexpr u32 AIV_TIMEOUT_MAX_US = 1091 * TIME_S_TO_US;
93 :
94 : constexpr u32 DEV_TYPE_910_93 = 4;
95 :
96 : constexpr u32 BUFFER_DIVIDE = 2;
97 : constexpr u32 MAX_TARGET_NUM = 20;
98 :
99 : enum class KernelArgsType {
100 : ARGS_TYPE_SERVER = 0, // kernel参数为单机内
101 : ARGS_TYPE_SUPERPOD = 1, // kernel参数包含多机,当前仅A3 AlltoAllV跨机场景
102 : ARGS_TYPE_SIMPLE = 2, // kernel参数为A3跨机
103 : ARGS_TYPE_DEFAULT
104 : };
105 :
106 : // AIV直驱Roce所需的rmaInfo信息
107 : // Transport 内存类型
108 : enum class HcclAiRMAMemType : u32 {
109 : LOCAL_INPUT = 0,
110 : REMOTE_INPUT,
111 :
112 : LOCAL_OUTPUT,
113 : REMOTE_OUTPUT,
114 :
115 : // 可透传更多的内存,可在MAX_NUM之前追加,例如:
116 : // LOCAL_EXP,
117 : // REMOTE_EXP,
118 : MAX_NUM
119 : };
120 :
121 0 : constexpr u32 GetAiMemTypeVal(HcclAiRMAMemType value) { return static_cast<u32>(value); }
122 :
123 : constexpr u32 AiMemMaxNum = GetAiMemTypeVal(HcclAiRMAMemType::MAX_NUM);
124 :
125 : // Transport 内存信息
126 : struct HcclAiRMAMemInfo {
127 : uint32_t memMaxNum{0}; // 最大内存数量,等于 HcclAiRMAMemType::MAX_NUM
128 : uint32_t sizeOfMemDetails{0}; // sizeof(MemDetails),用于内存校验和偏移计算
129 : uint64_t memDetailPtr{0}; // MemDetails数组首地址, 个数: HcclAiRMAMemType::MAX_NUM
130 : // 可往后追加字段
131 : };
132 :
133 : // 全部 Transport QP/Mem 信息
134 : struct HcclRMAInfo {
135 : uint32_t curRankId{0}; // 当前rankId
136 : uint32_t rankNum{0}; // rank数量
137 : uint32_t qpNum{0}; // 单个Transport的QP数量
138 :
139 : uint32_t sizeOfRMAWQ{0}; // sizeof(HcclAiRMAWQ)
140 : uint32_t sizeOfRMACQ{0}; // sizeof(HcclAiRMACQ)
141 : uint32_t sizeOfRMAMem{0}; // sizeof(HcclAiRMAMemInfo)
142 :
143 : // HcclAiRMAWQ二维数组首地址
144 : // QP个数: rankNum * qpNum
145 : // 计算偏移获取SQ指针:sqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMAWQ
146 : // 0 <= qpIndex < qpNum
147 : uint64_t sqPtr{0};
148 :
149 : // HcclAiRMACQ二维数组首地址
150 : // QP个数: rankNum * qpNum
151 : // 计算偏移获取SCQ指针:scqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMACQ
152 : // 0 <= qpIndex < qpNum
153 : uint64_t scqPtr{0};
154 :
155 : // HcclAiRMAWQ二维数组首地址
156 : // QP个数: rankNum * qpNum
157 : // 计算偏移获取RQ指针:rqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMAWQ
158 : // 0 <= qpIndex < qpNum
159 : uint64_t rqPtr{0};
160 :
161 : // HcclAiRMACQ二维数组首地址
162 : // QP个数: rankNum * qpNum
163 : // 计算偏移获取RCQ指针: rcqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMACQ
164 : // 0 <= qpIndex < qpNum
165 : uint64_t rcqPtr{0};
166 :
167 : // HcclAivMemInfo一维数组
168 : // 内存信息个数: rankNum
169 : // 计算偏移获取内存信息指针: memPtr + rankId * sizeOfRMAMem
170 : // srcRankId 获取自身内存信息,dstRankId 获取 Transport 内存信息
171 : uint64_t memPtr{0};
172 : // 可往后追加字段
173 : };
174 :
175 : // 非均匀算子AlltoAllV/AlltoAllVC/AllGatherV/ReduceScatterV需要的额外参数信息,A2场景
176 : using ExtraArgs = struct AlltoAllExtraArgs {
177 : u64 sendCountMatrix[MAX_RANK_SIZE * MAX_RANK_SIZE] = {};
178 : u64 sendCounts[MAX_RANK_SIZE] = {};
179 : u64 sendDispls[MAX_RANK_SIZE] = {};
180 : u64 recvCounts[MAX_RANK_SIZE] = {};
181 : u64 recvDispls[MAX_RANK_SIZE] = {};
182 : u64 maxCount = 0;
183 : };
184 :
185 : // 非均匀算子AlltoAllV/AlltoAllVC/AllGatherV/ReduceScatterV需要的额外参数信息,A3场景
186 : struct ExtraArgsV2 {
187 : u64 sendCounts[MAX_RANK_SIZE_A3] = {};
188 : u64 sendDispls[MAX_RANK_SIZE_A3] = {};
189 : u64 recvCounts[MAX_RANK_SIZE_A3] = {};
190 : u64 recvDispls[MAX_RANK_SIZE_A3] = {};
191 : };
192 :
193 : // 表示算子属性的参数,相对固定
194 : struct AivOpArgs {
195 : HcclCMDType cmdType;
196 : const void* input;
197 : const void* output;
198 : u64 count;
199 : HcclDataType dataType;
200 : HcclReduceOp op;
201 : u32 root;
202 : bool isOpBase;
203 : };
204 :
205 : // 表示拓扑信息的参数
206 : struct AivTopoArgs {
207 : u32 rank;
208 : u32 rankSize;
209 : u32 devId;
210 : u32 serverId;
211 : u32 serverNum;
212 : DevType devType;
213 : std::string identify;
214 :
215 181 : AivTopoArgs(
216 : u32 rank, u32 rankSize, u32 devId = MAX_RANK_SIZE, u32 serverId = 0, u32 serverNum = 1,
217 15 : DevType devType = DevType::DEV_TYPE_910B, std::string identify = "INVALID_COMM")
218 181 : : rank(rank),
219 181 : rankSize(rankSize),
220 181 : devId(devId),
221 181 : serverId(serverId),
222 181 : serverNum(serverNum),
223 181 : devType(devType),
224 181 : identify(identify)
225 192 : {}
226 : };
227 :
228 : // 表示AIV所需要的资源参数
229 : struct AivResourceArgs {
230 : std::string commTag;
231 : rtStream_t stream;
232 : void** buffersIn; // 注册的CCLIN地址,所有卡可访问
233 : void** buffersOut; // 注册的CCLOUT地址,所有卡可访问
234 : u64 bufferSize;
235 : u32 numBlocks;
236 : s32 aivTag;
237 : };
238 :
239 : // 表示AIV算法流程控制的参数
240 : struct AivAlgArgs {
241 : s32 step;
242 : bool isSmallCount;
243 : u32 deterministic;
244 : KernelArgsType argsType;
245 : s32 execTimeOut;
246 : bool execTimeOutSet; // true表示set by commConfig
247 : bool isNpuDirectRoce;
248 : u64 rmaInfo;
249 :
250 183 : explicit AivAlgArgs(
251 : s32 step = -1, bool isSmallCount = false, u32 deterministic = 0,
252 : KernelArgsType argsType = KernelArgsType::ARGS_TYPE_SERVER,
253 : s32 execTimeOut = static_cast<s32>(AIV_TIMEOUT_DEFAULT), bool execTimeOutSet = false,
254 : bool isNpuDirectRoce = false, u64 rmaInfo = 0)
255 183 : : step(step),
256 183 : isSmallCount(isSmallCount),
257 183 : deterministic(deterministic),
258 183 : argsType(argsType),
259 183 : execTimeOut(execTimeOut),
260 183 : execTimeOutSet(execTimeOutSet),
261 183 : isNpuDirectRoce(isNpuDirectRoce),
262 183 : rmaInfo(rmaInfo)
263 183 : {}
264 : };
265 :
266 : // 表示AIVProfiling所需要的参数
267 : struct AivProfilingInfo {
268 : uint64_t beginTime = 0;
269 : OpCounterInfo counter;
270 : };
271 :
272 : struct HcclCacheInfo {
273 : bool isUseCache = false;
274 : AivOpArgs opArgs;
275 : AivTopoArgs topoArgs{0, 0};
276 : AivResourceArgs resourceArgs{"", nullptr, nullptr, nullptr, 0, 0, 0};
277 : AivAlgArgs algArgs;
278 : AivProfilingInfo profilingInfo;
279 : ExtraArgs extraArgs;
280 : void* buffersIn[MAX_RANK_SIZE] = {}; // 注册的CCLIN地址,所有卡可访问
281 : void* buffersOut[MAX_RANK_SIZE] = {}; // 注册的CCLOUT地址,所有卡可访问
282 : AlgType algType;
283 : bool selectAivAlg = false;
284 : std::string newTag;
285 : };
286 :
287 : // 表示AIVSuperKernel所需要的参数
288 : using AivSuperKernelArgs = struct AivSuperKernelArgsDef {
289 : void* buffersIn[MAX_RANK_SIZE] = {}; // 注册的CCLIN地址,所有卡可访问
290 : void* buffersOut[MAX_RANK_SIZE] = {}; // 注册的CCLOUT地址,所有卡可访问
291 : u64 rank;
292 : u64 rankSize;
293 : u64 len;
294 : u64 dataType;
295 : u64 unitSize;
296 : u64 reduceOp;
297 : u64 numBlocks;
298 : s32 tag; // 第几次调用,定时重置成1
299 : s64 clearEnable;
300 : u32 devType;
301 :
302 : AivSuperKernelArgsDef(
303 : void** buffIn, void** buffOut, u32 rank, u32 rankSize, u64 len, u32 dataType, u32 unitSize, u32 reduceOp,
304 : u32 numBlocks = 0, s32 tag = 0, bool clearEnable = true, u32 devType = DEV_TYPE_910_93)
305 : : rank(rank),
306 : rankSize(rankSize),
307 : len(len),
308 : dataType(dataType),
309 : unitSize(unitSize),
310 : reduceOp(reduceOp),
311 : numBlocks(numBlocks),
312 : tag(tag),
313 : clearEnable(clearEnable),
314 : devType(devType)
315 : {
316 : for (u32 i = 0; i < MAX_RANK_SIZE; i++) {
317 : buffersIn[i] = (u8*)buffIn[i];
318 : buffersOut[i] = (u8*)buffOut[i];
319 : }
320 : }
321 1 : AivSuperKernelArgsDef() {}
322 : };
323 :
324 : #ifdef OPEN_HCCL_TEST
325 : enum class KernelLaunchMode {
326 : LAUNCH_MODE_ARGS_BASE = 0, // Launch模式,基础参数
327 : LAUNCH_MODE_ARGS_EXTRA, // Launch模式,基础参数+ExtraArgs
328 : LAUNCH_MODE_ARGS_EXTRA_V2, // Launch模式,基础参数+ExtraArgsV2
329 : LAUNCH_MODE_ARGS_EXTRA_A3 // Launch模式,A3跨机
330 : };
331 :
332 : HcclResult ExecuteKernelLaunchImpl(
333 : const AivOpArgs& opArgs, const AivTopoArgs& topoArgs, const AivResourceArgs& resourceArgs,
334 : const AivAlgArgs& algArgs, AivProfilingInfo& aivProfilingInfo, KernelLaunchMode launchMode,
335 : void* extraArgsPtr = nullptr);
336 : #endif
337 :
338 : HcclResult RegisterKernel(DevType deviceType);
339 : HcclResult UnRegisterAivKernel();
340 :
341 : HcclResult
342 : ClearAivSyncBufForMulServer(const AivResourceArgs& resourceArgs, const AivTopoArgs& topoArgs, void* args, u32 argsSize);
343 :
344 6 : inline s32 GetNextAivTag(s32 curTag, s32 tagIncre = 1) { return (curTag + tagIncre - 1) % TAG_RESET_COUNT + 1; }
345 :
346 : HcclResult ExecuteKernelLaunchInner(
347 : const AivOpArgs& opArgs, const AivTopoArgs& topoArgs, const AivResourceArgs& resourceArgs,
348 : const AivAlgArgs& algArgs, void* args, u32 argsSize, AivProfilingInfo& aivProfilingInfo);
349 :
350 : HcclResult ExecuteKernelLaunch(
351 : const AivOpArgs& opArgs, const AivTopoArgs& topoArgs, const AivResourceArgs& resourceArgs,
352 : const AivAlgArgs& algArgs, AivProfilingInfo& aivProfilingInfo);
353 :
354 : HcclResult ExecuteKernelLaunch(
355 : const AivOpArgs& opArgs, const AivTopoArgs& topoArgs, const AivResourceArgs& resourceArgs,
356 : const AivAlgArgs& algArgs, const ExtraArgs& extraArgs, AivProfilingInfo& aivProfilingInfo);
357 :
358 : HcclResult ExecuteKernelLaunch(
359 : const AivOpArgs& opArgs, const AivTopoArgs& topoArgs, const AivResourceArgs& resourceArgs,
360 : const AivAlgArgs& algArgs, const ExtraArgsV2& extraArgs, AivProfilingInfo& aivProfilingInfo);
361 :
362 : HcclResult CacheTaskOpInfo(rtStream_t stream, const std::string& identify);
363 :
364 : HcclResult ReadBinFile(const std::string& fileName, std::string& buffer);
365 :
366 : HcclResult GetKernelFunc(aclrtFuncHandle& funcHandle, s8* stubFunc);
367 :
368 : void SetAivProfilingInfoBeginTime(AivProfilingInfo& aivProfilingInfo);
369 : void SetAivProfilingInfoBeginTime(uint64_t& beginTime);
370 : } // namespace hccl
371 :
372 : #endif // HCCL_AIV_H
|