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_UTILS_H
12 : #define HCCL_AIV_UTILS_H
13 :
14 : #include "string"
15 :
16 : #include "hccl_types.h"
17 : #include "orion_adapter_rts.h"
18 : #include "template_utils.h"
19 : #include "acl/acl_rt.h"
20 :
21 : namespace Hccl {
22 : constexpr u32 MAX_RANK_SIZE_ = 64; // 注意要和device侧的一致
23 : constexpr u32 MAX_NUM_BLOCKS = 56; // 56-72
24 :
25 : constexpr s32 TAG_INIT_VALUE = 1;
26 : constexpr s32 TAG_RESET_COUNT = 1000;
27 : constexpr s32 TOPO_LEN = 32;
28 :
29 : constexpr u32 AIV_TAG_MOVE_LEFT_BITS = 16;
30 : constexpr u32 AIV_TAG_ADDR_OFFSET = 16 * 1024;
31 : constexpr u32 AIV_FLAG_ADDR_OFFSET = 40 * 1024;
32 : constexpr u32 AIV_FLAG_AREA_SIZE = 1000 * 1024;
33 : constexpr u32 AIV_FLAG_CLEAR_OFFSET = 1040 * 1024;
34 : constexpr u32 AIV_LOW_16_BITS = 0xFFFF;
35 :
36 : constexpr u32 CACHEMAP_MAXSIZE = 65536;
37 : constexpr float CACHEMAP_CLEARPERCENT = 0.1;
38 :
39 : constexpr u32 BATCH_SEND_RECV_ITEM_SIZE = 16; // 注意要和device侧的BATCH_SEND_RECV_ITEM_SIZE保持一致
40 :
41 : enum class KernelArgsType {
42 : ARGS_TYPE_SERVER = 0, // kernel参数为单机内
43 : ARGS_TYPE_TWO_SHOT = 1,
44 : ARGS_TYPE_DEFAULT
45 : };
46 :
47 : struct all2AllDataDes {
48 : DataType sendType;
49 : DataType recvType;
50 : u64 sendCount;
51 : u64 recvCount;
52 : // 重载 != 操作符
53 0 : bool operator!=(const all2AllDataDes& other) const
54 : {
55 0 : return sendType != other.sendType || recvType != other.recvType || sendCount != other.sendCount
56 0 : || recvCount != other.recvCount;
57 : }
58 :
59 : // 重载 < 操作符
60 0 : bool operator<(const all2AllDataDes& other) const
61 : {
62 0 : if (sendType != other.sendType)
63 0 : return sendType < other.sendType;
64 0 : if (recvType != other.recvType)
65 0 : return recvType < other.recvType;
66 0 : if (sendCount != other.sendCount)
67 0 : return sendCount < other.sendCount;
68 0 : return recvCount < other.recvCount;
69 : }
70 : };
71 :
72 : struct all2AllVDataDes {
73 : DataType sendType;
74 : DataType recvType;
75 : void* sendCounts;
76 : void* recvCounts;
77 : void* sdispls;
78 : void* rdispls;
79 : // 重载 != 操作符
80 0 : bool operator!=(const all2AllVDataDes& other) const
81 : {
82 0 : return sendType != other.sendType || recvType != other.recvType || sendCounts != other.sendCounts
83 0 : || recvCounts != other.recvCounts || sdispls != other.sdispls || rdispls != other.rdispls;
84 : }
85 :
86 : // 重载 < 操作符
87 0 : bool operator<(const all2AllVDataDes& other) const
88 : {
89 0 : if (sendType != other.sendType)
90 0 : return sendType < other.sendType;
91 0 : if (recvType != other.recvType)
92 0 : return recvType < other.recvType;
93 0 : if (sendCounts != other.sendCounts)
94 0 : return sendCounts < other.sendCounts;
95 0 : if (recvCounts != other.recvCounts)
96 0 : return recvCounts < other.recvCounts;
97 0 : if (sdispls != other.sdispls)
98 0 : return sdispls < other.sdispls;
99 0 : return rdispls < other.rdispls;
100 : }
101 : };
102 :
103 : // 表示算子属性的参数,相对固定
104 : struct AivOpCacheArgs {
105 : std::string algName = "default";
106 : u64 count = 0;
107 : DataType dataType = DataType::INT32;
108 : uint16_t opType = 0;
109 : ReduceOp reduceOp = ReduceOp::INVALID;
110 : u32 root = 0;
111 : u32 numBlocksLimit = 0;
112 : DataType outputDataType = DataType::INT32;
113 : all2AllDataDes all2allDataDes{};
114 : all2AllVDataDes all2allVDataDes{};
115 :
116 0 : AivOpCacheArgs(
117 : const std::string& algName_, u64 count_, DataType dataType_, uint16_t opType_, ReduceOp reduceOp_, u32 root_,
118 : u32 numBlocksLimit_, DataType outputDataType_, const all2AllDataDes& all2allDataDes_ = {},
119 : const all2AllVDataDes& all2allVDataDes_ = {})
120 0 : : algName(algName_),
121 0 : count(count_),
122 0 : dataType(dataType_),
123 0 : opType(opType_),
124 0 : reduceOp(reduceOp_),
125 0 : root(root_),
126 0 : numBlocksLimit(numBlocksLimit_),
127 0 : outputDataType(outputDataType_),
128 0 : all2allDataDes(all2allDataDes_),
129 0 : all2allVDataDes(all2allVDataDes_)
130 0 : {}
131 : // 自定义 operator<
132 0 : bool operator<(const AivOpCacheArgs& other) const
133 : {
134 0 : if (algName != other.algName)
135 0 : return algName < other.algName;
136 0 : if (count != other.count)
137 0 : return count < other.count;
138 0 : if (dataType != other.dataType)
139 0 : return dataType < other.dataType;
140 0 : if (opType != other.opType)
141 0 : return opType < other.opType;
142 0 : if (reduceOp != other.reduceOp)
143 0 : return reduceOp < other.reduceOp;
144 0 : if (root != other.root)
145 0 : return root < other.root;
146 0 : if (numBlocksLimit != other.numBlocksLimit)
147 0 : return numBlocksLimit < other.numBlocksLimit;
148 0 : if (all2allDataDes != other.all2allDataDes)
149 0 : return all2allDataDes < other.all2allDataDes;
150 0 : if (all2allVDataDes != other.all2allVDataDes)
151 0 : return all2allVDataDes < other.all2allVDataDes;
152 0 : return outputDataType < other.outputDataType;
153 : }
154 : };
155 :
156 : struct HcclSendRecvItemHost {
157 : uint32_t sendRecvType;
158 : uint64_t bufAddr;
159 : uint64_t count;
160 : uint32_t dataTypeSize;
161 : uint32_t remoteRank;
162 : };
163 :
164 : // 非均匀算子AlltoAllV/AlltoAllVC/AllGatherV/ReduceScatterV需要的额外参数信息,A3场景
165 : struct ExtraArgsA2A { // 后面考虑把这个参数名换一下,或者直接独立弄个参数出来
166 : u64 sendCounts[MAX_RANK_SIZE_] = {};
167 : u64 sendDispls[MAX_RANK_SIZE_] = {};
168 : u64 recvCounts[MAX_RANK_SIZE_] = {};
169 : u64 recvDispls[MAX_RANK_SIZE_] = {};
170 : uint64_t itemNum = 0;
171 : HcclSendRecvItemHost sendRecvInfo[BATCH_SEND_RECV_ITEM_SIZE] = {};
172 : };
173 :
174 : // 算子计数信息
175 : struct OpCounterInfo {
176 : u64 headCountMem = 0;
177 : u64 tailCountMem = 0;
178 : u64 addOneMem = 0;
179 : u32 memSize = 0;
180 : bool isEnableCounter = false;
181 : };
182 :
183 : // 表示算子属性的参数,相对固定
184 : struct AivOpArgs {
185 : HcclCMDType cmdType = HcclCMDType::HCCL_CMD_MAX;
186 : std::string comm = {};
187 : u32 numBlocks = MAX_NUM_BLOCKS;
188 : rtStream_t stream = nullptr;
189 : uint64_t beginTime = 0;
190 : OpCounterInfo counter = {};
191 : const void* buffersIn = nullptr;
192 : u64 input = 0;
193 : u64 output = 0;
194 : u32 rank = 0;
195 : u32 sendRecvRemoteRank = 0;
196 : u32 rankSize = 0;
197 : u64 xRankSize = 0;
198 : u64 yRankSize = 0;
199 : u64 zRankSize = 0;
200 : u64 count = 0;
201 : DataType dataType = DataType::INT32;
202 : ReduceOp op = ReduceOp::SUM;
203 : u32 root = 0;
204 : u32 aivTag = 0;
205 : u64 inputSliceStride = 0;
206 : u64 outputSliceStride = 0;
207 : u64 repeatNum = 0;
208 : u64 inputRepeatStride = 0;
209 : u64 outputRepeatStride = 0;
210 : bool isOpBase = false;
211 : ExtraArgsA2A extraArgs = {};
212 : uint64_t topo_[TOPO_LEN] = {0};
213 7 : AivOpArgs() {};
214 : KernelArgsType argsType = KernelArgsType::ARGS_TYPE_SERVER;
215 : };
216 :
217 : using AivSuperKernelArgs = struct AivSuperKernelArgsDef {
218 : const void* buffersIn = nullptr; // 注册的CCLIN地址,所有卡可访问
219 : u64 rank{};
220 : u64 rankSize{};
221 : u64 len{};
222 : u64 dataType{};
223 : u64 unitSize{};
224 : u64 reduceOp{};
225 : u64 numBlocks{};
226 : s64 tag{}; // 第几次调用,定时重置成1
227 : s64 clearEnable{};
228 : uint64_t inputSliceStride{};
229 : uint64_t outputSliceStride{};
230 : uint64_t repeatNum{};
231 : uint64_t inputRepeatStride{};
232 : uint64_t outputRepeatStride{};
233 : u64 input{};
234 : u64 output{};
235 : u64 cclBufferSize{};
236 : AivSuperKernelArgsDef(
237 : u64 input, u64 output, u32 rank, u32 rankSize, u64 len, u32 dataType, u64 unitSize, u32 reduceOp,
238 : u32 numBlocks = 0, s32 tag = 0, bool clearEnable = true, uint64_t inputSliceStride = 0,
239 : uint64_t outputSliceStride = 0, uint64_t repeatNum = 0, uint64_t inputRepeatStride = 0,
240 : uint64_t outputRepeatStride = 0, u64 cclBufferSize = 0)
241 : : rank(rank),
242 : rankSize(rankSize),
243 : len(len),
244 : dataType(dataType),
245 : unitSize(unitSize),
246 : reduceOp(reduceOp),
247 : numBlocks(numBlocks),
248 : tag(tag),
249 : clearEnable(clearEnable),
250 : inputSliceStride(inputSliceStride),
251 : outputSliceStride(outputSliceStride),
252 : repeatNum(repeatNum),
253 : inputRepeatStride(inputRepeatStride),
254 : outputRepeatStride(outputRepeatStride),
255 : input(input),
256 : output(output),
257 : cclBufferSize(cclBufferSize)
258 : {}
259 1 : AivSuperKernelArgsDef() {}
260 : };
261 :
262 : HcclResult RegisterKernel();
263 :
264 : HcclResult ExecuteKernelLaunchInner(const AivOpArgs& opArgs, void* args, u32 argsSize);
265 :
266 : HcclResult ExecuteKernelLaunch(const AivOpArgs& opArgs);
267 : } // namespace Hccl
268 :
269 : #endif // HCCL_AIV_UTILS_H
|