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) {
122 0 : return static_cast<u32>(value);
123 : }
124 :
125 : constexpr u32 AiMemMaxNum = GetAiMemTypeVal(HcclAiRMAMemType::MAX_NUM);
126 :
127 : // Transport 内存信息
128 : struct HcclAiRMAMemInfo {
129 : uint32_t memMaxNum{0}; // 最大内存数量,等于 HcclAiRMAMemType::MAX_NUM
130 : uint32_t sizeOfMemDetails{0}; // sizeof(MemDetails),用于内存校验和偏移计算
131 : uint64_t memDetailPtr{0}; // MemDetails数组首地址, 个数: HcclAiRMAMemType::MAX_NUM
132 : // 可往后追加字段
133 : };
134 :
135 : // 全部 Transport QP/Mem 信息
136 : struct HcclRMAInfo {
137 : uint32_t curRankId{0}; // 当前rankId
138 : uint32_t rankNum{0}; // rank数量
139 : uint32_t qpNum{0}; // 单个Transport的QP数量
140 :
141 : uint32_t sizeOfRMAWQ{0}; // sizeof(HcclAiRMAWQ)
142 : uint32_t sizeOfRMACQ{0}; // sizeof(HcclAiRMACQ)
143 : uint32_t sizeOfRMAMem{0}; // sizeof(HcclAiRMAMemInfo)
144 :
145 : // HcclAiRMAWQ二维数组首地址
146 : // QP个数: rankNum * qpNum
147 : // 计算偏移获取SQ指针:sqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMAWQ
148 : // 0 <= qpIndex < qpNum
149 : uint64_t sqPtr{0};
150 :
151 : // HcclAiRMACQ二维数组首地址
152 : // QP个数: rankNum * qpNum
153 : // 计算偏移获取SCQ指针:scqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMACQ
154 : // 0 <= qpIndex < qpNum
155 : uint64_t scqPtr{0};
156 :
157 : // HcclAiRMAWQ二维数组首地址
158 : // QP个数: rankNum * qpNum
159 : // 计算偏移获取RQ指针:rqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMAWQ
160 : // 0 <= qpIndex < qpNum
161 : uint64_t rqPtr{0};
162 :
163 : // HcclAiRMACQ二维数组首地址
164 : // QP个数: rankNum * qpNum
165 : // 计算偏移获取RCQ指针: rcqPtr + (dstRankId * qpNum + qpIndex) * sizeOfRMACQ
166 : // 0 <= qpIndex < qpNum
167 : uint64_t rcqPtr{0};
168 :
169 : // HcclAivMemInfo一维数组
170 : // 内存信息个数: rankNum
171 : // 计算偏移获取内存信息指针: memPtr + rankId * sizeOfRMAMem
172 : // srcRankId 获取自身内存信息,dstRankId 获取 Transport 内存信息
173 : uint64_t memPtr{0};
174 : // 可往后追加字段
175 : };
176 :
177 : // 非均匀算子AlltoAllV/AlltoAllVC/AllGatherV/ReduceScatterV需要的额外参数信息,A2场景
178 : using ExtraArgs = struct AlltoAllExtraArgs {
179 : u64 sendCountMatrix[MAX_RANK_SIZE * MAX_RANK_SIZE] = {};
180 : u64 sendCounts[MAX_RANK_SIZE] = {};
181 : u64 sendDispls[MAX_RANK_SIZE] = {};
182 : u64 recvCounts[MAX_RANK_SIZE] = {};
183 : u64 recvDispls[MAX_RANK_SIZE] = {};
184 : u64 maxCount = 0;
185 : };
186 :
187 : // 非均匀算子AlltoAllV/AlltoAllVC/AllGatherV/ReduceScatterV需要的额外参数信息,A3场景
188 : struct ExtraArgsV2 {
189 : u64 sendCounts[MAX_RANK_SIZE_A3] = {};
190 : u64 sendDispls[MAX_RANK_SIZE_A3] = {};
191 : u64 recvCounts[MAX_RANK_SIZE_A3] = {};
192 : u64 recvDispls[MAX_RANK_SIZE_A3] = {};
193 : };
194 :
195 : // 表示算子属性的参数,相对固定
196 : struct AivOpArgs {
197 : HcclCMDType cmdType;
198 : const void* input;
199 : const void* output;
200 : u64 count;
201 : HcclDataType dataType;
202 : HcclReduceOp op;
203 : u32 root;
204 : bool isOpBase;
205 : };
206 :
207 : // 表示拓扑信息的参数
208 : struct AivTopoArgs {
209 : u32 rank;
210 : u32 rankSize;
211 : u32 devId;
212 : u32 serverId;
213 : u32 serverNum;
214 : DevType devType;
215 : std::string identify;
216 :
217 179 : AivTopoArgs(u32 rank, u32 rankSize, u32 devId = MAX_RANK_SIZE, u32 serverId = 0, u32 serverNum = 1,
218 15 : DevType devType = DevType::DEV_TYPE_910B, std::string identify= "INVALID_COMM")
219 179 : : rank(rank), rankSize(rankSize), devId(devId), serverId(serverId), serverNum(serverNum), devType(devType), identify(identify)
220 : {
221 189 : }
222 : };
223 :
224 : // 表示AIV所需要的资源参数
225 : struct AivResourceArgs {
226 : std::string commTag;
227 : rtStream_t stream;
228 : void** buffersIn; // 注册的CCLIN地址,所有卡可访问
229 : void** buffersOut; // 注册的CCLOUT地址,所有卡可访问
230 : u64 bufferSize;
231 : u32 numBlocks;
232 : s32 aivTag;
233 : };
234 :
235 : // 表示AIV算法流程控制的参数
236 : struct AivAlgArgs {
237 : s32 step;
238 : bool isSmallCount;
239 : u32 deterministic;
240 : KernelArgsType argsType;
241 : s32 execTimeOut;
242 : bool execTimeOutSet; // true表示set by commConfig
243 : bool isNpuDirectRoce;
244 : u64 rmaInfo;
245 :
246 179 : explicit AivAlgArgs(s32 step = -1, bool isSmallCount = false, u32 deterministic = 0,
247 : KernelArgsType argsType = KernelArgsType::ARGS_TYPE_SERVER,
248 : s32 execTimeOut = static_cast<s32>(AIV_TIMEOUT_DEFAULT), bool execTimeOutSet = false,
249 : bool isNpuDirectRoce = false, u64 rmaInfo = 0)
250 179 : : step(step), isSmallCount(isSmallCount), deterministic(deterministic), argsType(argsType),
251 179 : execTimeOut(execTimeOut), execTimeOutSet(execTimeOutSet), isNpuDirectRoce(isNpuDirectRoce),
252 179 : rmaInfo(rmaInfo)
253 : {
254 179 : }
255 : };
256 :
257 : // 表示AIVProfiling所需要的参数
258 : struct AivProfilingInfo{
259 : uint64_t beginTime = 0;
260 : OpCounterInfo counter;
261 : };
262 :
263 : struct HcclCacheInfo {
264 : bool isUseCache = false;
265 : AivOpArgs opArgs;
266 : AivTopoArgs topoArgs{0, 0};
267 : AivResourceArgs resourceArgs{"", nullptr, nullptr, nullptr, 0, 0, 0};
268 : AivAlgArgs algArgs;
269 : AivProfilingInfo profilingInfo;
270 : ExtraArgs extraArgs;
271 : void* buffersIn[MAX_RANK_SIZE] = {}; // 注册的CCLIN地址,所有卡可访问
272 : void* buffersOut[MAX_RANK_SIZE] = {}; // 注册的CCLOUT地址,所有卡可访问
273 : AlgType algType;
274 : bool selectAivAlg = false;
275 : std::string newTag;
276 : };
277 :
278 : // 表示AIVSuperKernel所需要的参数
279 : using AivSuperKernelArgs = struct AivSuperKernelArgsDef {
280 : void* buffersIn[MAX_RANK_SIZE] = {}; // 注册的CCLIN地址,所有卡可访问
281 : void* buffersOut[MAX_RANK_SIZE] = {}; // 注册的CCLOUT地址,所有卡可访问
282 : u64 rank;
283 : u64 rankSize;
284 : u64 len;
285 : u64 dataType;
286 : u64 unitSize;
287 : u64 reduceOp;
288 : u64 numBlocks;
289 : s32 tag; // 第几次调用,定时重置成1
290 : s64 clearEnable;
291 : u32 devType;
292 :
293 : AivSuperKernelArgsDef(void** buffIn, void** buffOut, u32 rank,
294 : u32 rankSize, u64 len, u32 dataType, u32 unitSize, u32 reduceOp,u32 numBlocks = 0, s32 tag = 0, bool clearEnable = true, u32 devType = DEV_TYPE_910_93)
295 : : rank(rank), rankSize(rankSize), len(len), dataType(dataType), unitSize(unitSize), reduceOp(reduceOp), numBlocks(numBlocks),tag(tag), clearEnable(clearEnable), devType(devType)
296 : {
297 : for (u32 i = 0; i < MAX_RANK_SIZE; i++) {
298 : buffersIn[i] = (u8 *) buffIn[i];
299 : buffersOut[i] = (u8 *) buffOut[i];
300 : }
301 : }
302 1 : AivSuperKernelArgsDef() {}
303 : };
304 :
305 : #ifdef OPEN_HCCL_TEST
306 : enum class KernelLaunchMode {
307 : LAUNCH_MODE_ARGS_BASE = 0, // Launch模式,基础参数
308 : LAUNCH_MODE_ARGS_EXTRA, // Launch模式,基础参数+ExtraArgs
309 : LAUNCH_MODE_ARGS_EXTRA_V2, // Launch模式,基础参数+ExtraArgsV2
310 : LAUNCH_MODE_ARGS_EXTRA_A3 // Launch模式,A3跨机
311 : };
312 :
313 : HcclResult ExecuteKernelLaunchImpl(const AivOpArgs &opArgs, const AivTopoArgs &topoArgs,
314 : const AivResourceArgs &resourceArgs, const AivAlgArgs &algArgs,
315 : AivProfilingInfo& aivProfilingInfo, KernelLaunchMode launchMode, void* extraArgsPtr = nullptr);
316 : #endif
317 :
318 : HcclResult RegisterKernel(DevType deviceType);
319 : HcclResult UnRegisterAivKernel();
320 :
321 : HcclResult ClearAivSyncBufForMulServer(const AivResourceArgs &resourceArgs, const AivTopoArgs &topoArgs, void* args,
322 : u32 argsSize);
323 :
324 6 : inline s32 GetNextAivTag(s32 curTag, s32 tagIncre = 1) { return (curTag + tagIncre - 1) % TAG_RESET_COUNT + 1; }
325 :
326 : HcclResult ExecuteKernelLaunchInner(const AivOpArgs &opArgs, const AivTopoArgs &topoArgs,
327 : const AivResourceArgs &resourceArgs, const AivAlgArgs &algArgs, void* args, u32 argsSize,
328 : AivProfilingInfo& aivProfilingInfo);
329 :
330 : HcclResult ExecuteKernelLaunch(const AivOpArgs &opArgs, const AivTopoArgs &topoArgs,
331 : const AivResourceArgs &resourceArgs, const AivAlgArgs &algArgs,
332 : AivProfilingInfo& aivProfilingInfo);
333 :
334 : HcclResult ExecuteKernelLaunch(const AivOpArgs &opArgs, const AivTopoArgs &topoArgs,
335 : const AivResourceArgs &resourceArgs, const AivAlgArgs &algArgs, const ExtraArgs &extraArgs,
336 : AivProfilingInfo& aivProfilingInfo);
337 :
338 : HcclResult ExecuteKernelLaunch(const AivOpArgs &opArgs, const AivTopoArgs &topoArgs,
339 : const AivResourceArgs &resourceArgs, const AivAlgArgs &algArgs, const ExtraArgsV2 &extraArgs,
340 : AivProfilingInfo& aivProfilingInfo);
341 :
342 : HcclResult CacheTaskOpInfo(rtStream_t stream, const std::string &identify);
343 :
344 : HcclResult ReadBinFile(const std::string& fileName, std::string& buffer);
345 :
346 : HcclResult GetKernelFunc(aclrtFuncHandle& funcHandle, s8* stubFunc);
347 :
348 : void SetAivProfilingInfoBeginTime(AivProfilingInfo& aivProfilingInfo);
349 : void SetAivProfilingInfoBeginTime(uint64_t& beginTime);
350 : }
351 :
352 : #endif // HCCL_AIV_H
|