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 0 : return sendType != other.sendType ||
55 0 : recvType != other.recvType ||
56 0 : sendCount != other.sendCount ||
57 0 : recvCount != other.recvCount;
58 : }
59 :
60 : // 重载 < 操作符
61 0 : bool operator<(const all2AllDataDes& other) const {
62 0 : if (sendType != other.sendType) return sendType < other.sendType;
63 0 : if (recvType != other.recvType) return recvType < other.recvType;
64 0 : if (sendCount != other.sendCount) return sendCount < other.sendCount;
65 0 : return recvCount < other.recvCount;
66 : }
67 : };
68 :
69 : struct all2AllVDataDes{
70 : DataType sendType;
71 : DataType recvType;
72 : void* sendCounts;
73 : void* recvCounts;
74 : void* sdispls;
75 : void* rdispls;
76 : // 重载 != 操作符
77 0 : bool operator!=(const all2AllVDataDes& other) const {
78 0 : return sendType != other.sendType ||
79 0 : recvType != other.recvType ||
80 0 : sendCounts != other.sendCounts ||
81 0 : recvCounts != other.recvCounts ||
82 0 : sdispls != other.sdispls ||
83 0 : rdispls != other.rdispls;
84 : }
85 :
86 : // 重载 < 操作符
87 0 : bool operator<(const all2AllVDataDes& other) const {
88 0 : if (sendType != other.sendType) return sendType < other.sendType;
89 0 : if (recvType != other.recvType) return recvType < other.recvType;
90 0 : if (sendCounts != other.sendCounts) return sendCounts < other.sendCounts;
91 0 : if (recvCounts != other.recvCounts) return recvCounts < other.recvCounts;
92 0 : if (sdispls != other.sdispls) return sdispls < other.sdispls;
93 0 : return rdispls < other.rdispls;
94 : }
95 : };
96 :
97 : // 表示算子属性的参数,相对固定
98 : struct AivOpCacheArgs {
99 : std::string algName = "default";
100 : u64 count = 0;
101 : DataType dataType = DataType::INT32;
102 : uint16_t opType = 0;
103 : ReduceOp reduceOp = ReduceOp::INVALID;
104 : u32 root = 0;
105 : u32 numBlocksLimit = 0;
106 : DataType outputDataType = DataType::INT32;
107 : all2AllDataDes all2allDataDes{};
108 : all2AllVDataDes all2allVDataDes{};
109 :
110 0 : AivOpCacheArgs(
111 : const std::string& algName_,
112 : u64 count_,
113 : DataType dataType_,
114 : uint16_t opType_,
115 : ReduceOp reduceOp_,
116 : u32 root_,
117 : u32 numBlocksLimit_,
118 : DataType outputDataType_,
119 : const all2AllDataDes& all2allDataDes_ = {},
120 : const all2AllVDataDes& all2allVDataDes_ = {}
121 0 : ) : algName(algName_),
122 0 : count(count_),
123 0 : dataType(dataType_),
124 0 : opType(opType_),
125 0 : reduceOp(reduceOp_),
126 0 : root(root_),
127 0 : numBlocksLimit(numBlocksLimit_),
128 0 : outputDataType(outputDataType_),
129 0 : all2allDataDes(all2allDataDes_),
130 0 : all2allVDataDes(all2allVDataDes_)
131 0 : {}
132 : // 自定义 operator<
133 0 : bool operator<(const AivOpCacheArgs& other) const {
134 0 : if (algName != other.algName) return algName < other.algName;
135 0 : if (count != other.count) return count < other.count;
136 0 : if (dataType != other.dataType) return dataType < other.dataType;
137 0 : if (opType != other.opType) return opType < other.opType;
138 0 : if (reduceOp != other.reduceOp) return reduceOp < other.reduceOp;
139 0 : if (root != other.root) return root < other.root;
140 0 : if (numBlocksLimit != other.numBlocksLimit) return numBlocksLimit < other.numBlocksLimit;
141 0 : if (all2allDataDes != other.all2allDataDes) return all2allDataDes < other.all2allDataDes;
142 0 : if (all2allVDataDes != other.all2allVDataDes) return all2allVDataDes < other.all2allVDataDes;
143 0 : return outputDataType < other.outputDataType;
144 : }
145 : };
146 :
147 : struct HcclSendRecvItemHost {
148 : uint32_t sendRecvType;
149 : uint64_t bufAddr;
150 : uint64_t count;
151 : uint32_t dataTypeSize;
152 : uint32_t remoteRank;
153 : };
154 :
155 : // 非均匀算子AlltoAllV/AlltoAllVC/AllGatherV/ReduceScatterV需要的额外参数信息,A3场景
156 : struct ExtraArgsA2A { // 后面考虑把这个参数名换一下,或者直接独立弄个参数出来
157 : u64 sendCounts[MAX_RANK_SIZE_] = {};
158 : u64 sendDispls[MAX_RANK_SIZE_] = {};
159 : u64 recvCounts[MAX_RANK_SIZE_] = {};
160 : u64 recvDispls[MAX_RANK_SIZE_] = {};
161 : uint64_t itemNum = 0;
162 : HcclSendRecvItemHost sendRecvInfo[BATCH_SEND_RECV_ITEM_SIZE] = {};
163 : };
164 :
165 : // 算子计数信息
166 : struct OpCounterInfo {
167 : u64 headCountMem = 0;
168 : u64 tailCountMem = 0;
169 : u64 addOneMem = 0;
170 : u32 memSize = 0;
171 : bool isEnableCounter = false;
172 : };
173 :
174 : // 表示算子属性的参数,相对固定
175 : struct AivOpArgs {
176 : HcclCMDType cmdType = HcclCMDType::HCCL_CMD_MAX;
177 : std::string comm = {};
178 : u32 numBlocks = MAX_NUM_BLOCKS;
179 : rtStream_t stream = nullptr;
180 : uint64_t beginTime = 0;
181 : OpCounterInfo counter = {};
182 : const void* buffersIn = nullptr;
183 : u64 input = 0;
184 : u64 output = 0;
185 : u32 rank = 0;
186 : u32 sendRecvRemoteRank = 0;
187 : u32 rankSize = 0;
188 : u64 xRankSize = 0;
189 : u64 yRankSize = 0;
190 : u64 zRankSize = 0;
191 : u64 count = 0;
192 : DataType dataType = DataType::INT32;
193 : ReduceOp op = ReduceOp::SUM;
194 : u32 root = 0;
195 : u32 aivTag = 0;
196 : u64 inputSliceStride = 0;
197 : u64 outputSliceStride = 0;
198 : u64 repeatNum = 0;
199 : u64 inputRepeatStride = 0;
200 : u64 outputRepeatStride = 0;
201 : bool isOpBase = false;
202 : ExtraArgsA2A extraArgs = {};
203 : uint64_t topo_[TOPO_LEN] = {0};
204 7 : AivOpArgs() {};
205 : KernelArgsType argsType = KernelArgsType::ARGS_TYPE_SERVER;
206 : };
207 :
208 : using AivSuperKernelArgs = struct AivSuperKernelArgsDef {
209 : const void* buffersIn = nullptr; // 注册的CCLIN地址,所有卡可访问
210 : u64 rank{};
211 : u64 rankSize{};
212 : u64 len{};
213 : u64 dataType{};
214 : u64 unitSize{};
215 : u64 reduceOp{};
216 : u64 numBlocks{};
217 : s64 tag{}; // 第几次调用,定时重置成1
218 : s64 clearEnable{};
219 : uint64_t inputSliceStride{};
220 : uint64_t outputSliceStride{};
221 : uint64_t repeatNum{};
222 : uint64_t inputRepeatStride{};
223 : uint64_t outputRepeatStride{};
224 : u64 input{};
225 : u64 output{};
226 : u64 cclBufferSize{};
227 : AivSuperKernelArgsDef(u64 input, u64 output, u32 rank,
228 : u32 rankSize, u64 len, u32 dataType, u64 unitSize, u32 reduceOp,u32 numBlocks = 0, s32 tag = 0, bool clearEnable = true,
229 : uint64_t inputSliceStride = 0, uint64_t outputSliceStride = 0, uint64_t repeatNum = 0,
230 : uint64_t inputRepeatStride = 0, uint64_t outputRepeatStride = 0, u64 cclBufferSize = 0)
231 : : rank(rank), rankSize(rankSize), len(len), dataType(dataType), unitSize(unitSize),
232 : reduceOp(reduceOp), numBlocks(numBlocks),tag(tag),
233 : clearEnable(clearEnable), inputSliceStride(inputSliceStride), outputSliceStride(outputSliceStride),
234 : repeatNum(repeatNum), inputRepeatStride(inputRepeatStride), outputRepeatStride(outputRepeatStride),
235 : input(input), output(output), cclBufferSize(cclBufferSize)
236 : {
237 : }
238 1 : AivSuperKernelArgsDef() {}
239 : };
240 :
241 : HcclResult RegisterKernel();
242 :
243 : HcclResult ExecuteKernelLaunchInner(const AivOpArgs &opArgs, void* args, u32 argsSize);
244 :
245 : HcclResult ExecuteKernelLaunch(const AivOpArgs &opArgs);
246 : }
247 :
248 : #endif // HCCL_AIV_UTILS_H
|