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_MC2_TYPE_H
12 : #define HCCL_MC2_TYPE_H
13 :
14 : #include <string>
15 : #include <unordered_map>
16 : #include <vector>
17 : #include "exception_util.h"
18 : #include "ccu_api_exception.h"
19 : #include "op_type.h"
20 : #include "data_type.h"
21 : #include "reduce_op.h"
22 :
23 : namespace Hccl {
24 :
25 : constexpr uint32_t MC2_RES_CTX_MAX = 3;
26 : constexpr uint32_t CCU_PARAM_NUM_PER_DIE = 32;
27 : constexpr uint32_t CCU_PARAM_NUM_MAX = CCU_PARAM_NUM_PER_DIE * 2;
28 : constexpr uint32_t CCU_ONE_PARAM_SIZE = 8;
29 : constexpr uint32_t CCU_TASK_NUM_MAX = 64;
30 : constexpr uint32_t MAX_RANK_NUM = 64; // 最大卡数
31 : constexpr uint32_t MAX_OP_NUM = 8; // MC2最大通信算子数
32 :
33 : constexpr uint32_t UNKNOWN_TILING_V1 = 3; // 旧版本 MC2 Tiling version = 3
34 : constexpr uint32_t UNKNOWN_TILING_V2 = 100; // 新版本 MC2 Tiling version = 100
35 : constexpr uint64_t MC2_WORKSPACE_SIZE = 16 * 1024 * 1024; // aic与ccu的交互空间workspace大小为16*1024*1024B
36 :
37 : struct HcclCommParamDesc {
38 : uint64_t version : 4; // 版本号,当前是1
39 : uint64_t groupNum : 4; // groupMatmul的输入数量,每个group对应一个输入和一个输出地址
40 : uint64_t hasFfts : 1; // 910下是否是ffts融合算子(多一个ffts_addr参数
41 : uint64_t
42 : tilingDataPtrOff : 7; // tilingdata指针所在的参数索引, 此处修改为tilingDataPtr的Offset,需要二次索引到tilingData
43 : uint64_t
44 : isDyn : 48; // 输入参数是否是动态输入,从IR输入开始计算,不包含前面的参数,is_dyn是一个bitmap,每个bit对应一个IR输入,如果是动态输入则为1,否则是0
45 : };
46 :
47 : struct KFCTilingData {
48 : uint32_t preparePosition; // 新增结构体,用来区分是否是高阶api
49 : uint32_t sendOff; // 发送数据地址偏移,count * dataTypeSize
50 : uint32_t recvOff; // 接收数据地址偏移, count * dataTypeSize
51 : uint32_t tailSendOff; // 尾块发送数据地址偏移,count * dataTypeSize
52 : uint32_t tailRecvOff; // 尾块发送数据地址偏移,count * dataTypeSize
53 : uint64_t sendCnt; // 整块发送数据个数
54 : uint32_t recvCnt; // 整块接收数据个数
55 : uint32_t tailSendCnt; // 尾块发送数据个数
56 : uint32_t tailRecvCnt; // 尾块接收数据个数
57 : uint32_t totalCnt; // 总数据个数
58 : uint32_t turnNum; // 总轮次
59 : uint32_t tailNum; // 尾块的轮次
60 : uint32_t stride; // 跳写间隔
61 : uint32_t workspaceOff; // 使用workspace作为recvbuf时的workspace偏移
62 : uint32_t notifyOff; // device notify write/read value偏移
63 : uint16_t notifyBeginCnt; // notify write value的使用个数
64 : uint16_t notifyEndCnt; // notify read value的使用个数
65 : uint8_t useBufferType; // 是否使用workspace作为recvbuf
66 : uint8_t funID; // function ID
67 : uint8_t dataType; // hccl 数据类型
68 : uint8_t groupNum; // groupNum
69 : uint8_t reuseMode; // tiling调试,填msgCnt,内存优化选择复用的内存块个数
70 : uint8_t commType; // 通信类型
71 : uint8_t reduceOp; // reduce op type
72 : uint8_t commOrder; // 通信顺序,0表示通信在前,1表示通信在后
73 : uint8_t waitPolicy; // 等待任务启动的阻塞策略
74 : // 2、首轮等待,1、每轮等待。KFC根据此标记在主流任务前面加wait,AIC需要按策略发对应record才能触发执行
75 : uint8_t rspPolicy; // 任务执行结束时的响应策略, 2、最后通知一次,
76 : // 1、每轮通知一次。KFC根据此标记在主流任务后面加record
77 : uint8_t exitPolicy; // 退出策略,0,一次通信任务下发完成直接退出;1. 通信任务执行完成退出;2.
78 : // 等待AIC通知退出(可以多次执行任务)。
79 : uint8_t commAlg; // 用于指定具体通信算法。
80 : uint8_t taskType; // 用于识别不同任务。参考KfcTaskType定义
81 : uint8_t debugMode; // 调测模式
82 : // 1:单独执行CUBE
83 : // 2:单独执行Vector
84 : // 4:单独执行AICPU KFC算子
85 : // 8:KFC等待通信结束
86 : // 16:KFC统计各阶段耗时
87 : // 32:调试多ccu任务模式,通过ccuNum告诉ccu是几个ccu任务
88 : uint8_t stepSize; // 用于指定通算频率步长
89 : uint8_t sendArgIndex; // 发送数据参数索引,对应算子原型的参数顺序
90 : uint8_t recvArgIndex; // 接收数据参数索引,对应算子原型的参数顺序
91 : uint8_t commOutArgIndex; // 通信输出参数索引,对应算子原型的参数顺序
92 : uint8_t hasCommOut; // 是否有通信输出
93 :
94 : uint8_t reduceOutputDataType; // 输入datatype类型为hif8、fp8、int8 才生效, 输出只有fp16/fp32/bf16类型
95 : uint32_t workspaceSendOffset;
96 : uint32_t workspaceRecvOffset;
97 : uint32_t ccuNum;
98 : uint32_t ccuParamNum[CCU_TASK_NUM_MAX];
99 : uint64_t paramAddr[CCU_PARAM_NUM_MAX * CCU_TASK_NUM_MAX];
100 : uint64_t paramValue[CCU_PARAM_NUM_MAX * CCU_TASK_NUM_MAX];
101 :
102 : std::string ToString() const
103 : {
104 : return StringFormat(
105 : "sendArgIndex = %u\nrecvArgIndex = %u\nsendOff = %u\nrecvOff = %u\ntailSendOff = %u\ntailRecvOff = "
106 : "%u\nsendCnt = %u\nrecvCnt = %u\ntailSendCnt = %u\ntailRecvCnt = %u\ntotalCnt = %u\nturnNum = %u\ntailNum "
107 : "= %u\ndataType = %u\ncommType = %u\ncommOutArgIndex = %u\nnotifyBeginCnt = %u\nnotifyEndCnt = "
108 : "%u\nuseBufferType = %u\nreduceOp = %u\nreduceOutputDataType = %u\nworkspaceSendOffset = "
109 : "%lu\nworkspaceRecvOffset = %lu",
110 : sendArgIndex, recvArgIndex, sendOff, recvOff, tailSendOff, tailRecvOff, sendCnt, recvCnt, tailSendCnt,
111 : tailRecvCnt, totalCnt, turnNum, tailNum, dataType, commType, commOutArgIndex, notifyBeginCnt, notifyEndCnt,
112 : useBufferType, reduceOp, reduceOutputDataType, workspaceSendOffset, workspaceRecvOffset);
113 : }
114 : };
115 :
116 : enum HcclBufferType {
117 : HCCL_BUFFER_TYPE_DEFAULT = 0,
118 : HCCL_BUFFER_TYPE_OUTPUT,
119 : HCCL_BUFFER_TYPE_WINDOW_IN,
120 : HCCL_BUFFER_TYPE_WINDOW_OUT,
121 : HCCL_BUFFER_TYPE_WORKSPACE,
122 : HCCL_BUFFER_TYPE_INPUT,
123 : HCCL_BUFFER_TYPE_COMMOUT,
124 : HCCL_BUFFER_TYPE_SEND_WORKSPACE,
125 : HCCL_BUFFER_TYPE_RECV_WORKSPACE,
126 : HCCL_BUFFER_TYPE_SEND_RECV_WORKSPACE,
127 : HCCL_BUFFER_TYPE_END,
128 : };
129 :
130 : enum AicpuComType {
131 : HCCL_CMD_INVALID = 0,
132 : HCCL_CMD_BROADCAST = 1,
133 : HCCL_CMD_ALLREDUCE,
134 : HCCL_CMD_REDUCE,
135 : HCCL_CMD_SEND,
136 : HCCL_CMD_RECEIVE,
137 : HCCL_CMD_ALLGATHER,
138 : HCCL_CMD_REDUCE_SCATTER,
139 : HCCL_CMD_ALLTOALLV,
140 : HCCL_CMD_ALLTOALLVC,
141 : HCCL_CMD_ALLTOALL,
142 : HCCL_CMD_GATHER,
143 : HCCL_CMD_SCATTER,
144 : HCCL_CMD_BATCH_SEND_RECV,
145 : HCCL_CMD_BATCH_PUT,
146 : HCCL_CMD_BATCH_GET,
147 : HCCL_CMD_ALLGATHER_V,
148 : HCCL_CMD_REDUCE_SCATTER_V,
149 : HCCL_CMD_BATCH_WRITE,
150 : HCCL_CMD_HALF_ALLTOALLV = 20,
151 : HCCL_CMD_ALL,
152 : HCCL_CMD_RESERVED
153 : };
154 :
155 : constexpr OpType MC2_OP_TYPE[]
156 : = {OpType::INVALID, OpType::BROADCAST, OpType::ALLREDUCE, OpType::REDUCE, OpType::SEND,
157 : OpType::RECV, OpType::ALLGATHER, OpType::REDUCESCATTER, OpType::ALLTOALLV, OpType::ALLTOALLVC,
158 : OpType::ALLTOALL, OpType::GATHER, OpType::INVALID, OpType::INVALID, OpType::INVALID,
159 : OpType::INVALID, OpType::INVALID, OpType::INVALID, OpType::INVALID, OpType::INVALID,
160 : OpType::HALFALLTOALLV, OpType::INVALID};
161 :
162 125 : inline OpType MC2OpType(AicpuComType comType)
163 : {
164 125 : if (comType >= HCCL_CMD_RESERVED || comType <= HCCL_CMD_INVALID) {
165 0 : THROW<Hccl::CcuApiException>(StringFormat("Invalid OpType [%u].", comType));
166 : }
167 125 : return MC2_OP_TYPE[comType];
168 : }
169 :
170 : constexpr ReduceOp MC2_REDUCE_TYPE[] = {ReduceOp::SUM, ReduceOp::PROD, ReduceOp::MAX, ReduceOp::MIN, ReduceOp::INVALID};
171 :
172 6 : inline ReduceOp MC2ReduceType(HcclReduceOp reduceOp)
173 : {
174 6 : if (reduceOp >= (sizeof(MC2_REDUCE_TYPE) / sizeof(MC2_REDUCE_TYPE[0])) || reduceOp < HCCL_REDUCE_SUM) {
175 0 : THROW<Hccl::CcuApiException>(StringFormat("Invalid ReduceOp [%u].", reduceOp));
176 : }
177 6 : return MC2_REDUCE_TYPE[reduceOp];
178 : }
179 :
180 : constexpr DataType MC2_DATA_TYPE[]
181 : = {DataType::INT8, DataType::INT16, DataType::INT32, DataType::FP16, DataType::FP32,
182 : DataType::INT64, DataType::UINT64, DataType::UINT8, DataType::UINT16, DataType::UINT32,
183 : DataType::FP64, DataType::BFP16, DataType::INT128, DataType::BF16_SAT, DataType::HIF8,
184 : DataType::FP8E4M3, DataType::FP8E5M2, DataType::FP8E8M0, DataType::INVALID};
185 :
186 250 : inline DataType MC2DataType(HcclDataType dataType)
187 : {
188 250 : if (dataType >= (sizeof(MC2_DATA_TYPE) / sizeof(MC2_DATA_TYPE[0])) || dataType < HCCL_DATA_TYPE_INT8) {
189 0 : THROW<Hccl::CcuApiException>(StringFormat("Invalid DataType [%u].", dataType));
190 : }
191 250 : return MC2_DATA_TYPE[dataType];
192 : }
193 :
194 : struct AivAicpuOpParam {
195 : AicpuComType commType; // 32b
196 : HcclReduceOp opType; // 32b
197 : uint64_t sendBuffer;
198 : uint64_t recvBuffer;
199 : uint64_t count;
200 : uint64_t strideLen;
201 :
202 : // offset 32B
203 : HcclDataType hcclDataType;
204 : uint32_t valid; // 检查消息有效性
205 :
206 : // 存地址
207 : uint64_t sendCnt; // send CKE地址
208 : uint64_t rcvCnt; // rcv CKE地址
209 :
210 : uint8_t isLast; // 是否最后一个下
211 : uint8_t funID; // 功能ID,1地址消息; 2开始工作
212 : uint8_t everyTurnRsp; // 每轮都需要等待执行结束发送响应,再执行下一轮
213 : uint8_t everyTurnWait; // 每轮都需要等待work消息再执行
214 : uint8_t totalTurnCnt; // 总轮次
215 : uint8_t res[59]; // 整体消息128字节
216 : };
217 :
218 : struct KFCTaskV2 {
219 : uint64_t inputA; // A矩阵地址,通信在前时为sendbuffer
220 : uint64_t outputC; // 输出C矩阵地址
221 : uint64_t commOut; // 双输出时,通信输出地址
222 : uint64_t ctxNum;
223 : uint64_t context[MC2_RES_CTX_MAX]; // HCCL通信context
224 : uint64_t workSpace; // 通信结果不直接输出时,放到workspace中
225 : uint64_t tilingData; // 通信
226 : };
227 :
228 : struct HcclAiRMAWQ {
229 : u32 jettyId;
230 : u64 sqVA;
231 : u32 wqeSize;
232 : u32 sqDepth;
233 : u64 headAddr; // AIV无依赖
234 : u64 tailAddr; // AIV无依赖
235 : u64 dbAddr;
236 : u32 tp_id;
237 : uint8_t rmtEid[16];
238 : uint32_t rmtObjId; // rmtTokenID
239 : uint32_t rmtTokenValue;
240 : uint32_t localTokenId;
241 : };
242 :
243 : struct HcclAiRMACQ {
244 : u32 jfcId;
245 : u64 cqVA;
246 : u32 cqeSize;
247 : u32 cqDepth;
248 : u64 headAddr;
249 : u64 tailAddr;
250 : u64 dbAddr;
251 : };
252 :
253 : struct HcclCombinOpParam {
254 : uint64_t workSpace; // client和server之间通信的地址
255 : uint64_t workSpaceSize; // client和server之间通信的空间大小
256 : uint32_t rankId; // 当前卡rankId
257 : uint32_t rankDim; // 总卡数
258 : uint64_t winSize; // ccu不使用
259 : uint64_t windowsIn[MAX_RANK_NUM]; // ccu不使用
260 : uint64_t windowsOut[MAX_RANK_NUM]; // ccu不使用
261 :
262 : // for ccu
263 : uint64_t xnAddr; // Xn寄存器其实地址
264 : uint64_t ckeAddr; // CKE寄存器其实地址
265 : uint64_t msAddr; // MS地址,预留
266 : uint64_t msSize; // 可写的MS个数,预留
267 :
268 : uint32_t opType[MAX_OP_NUM];
269 : uint8_t algorithmType[MAX_OP_NUM];
270 :
271 : HcclAiRMAWQ wq[MAX_RANK_NUM];
272 : HcclAiRMACQ cq[MAX_RANK_NUM];
273 : };
274 :
275 : struct Mc2ServerCfg {
276 : uint32_t version;
277 : uint8_t debugMode;
278 : uint8_t sendArgIndex;
279 : uint8_t recvArgIndex;
280 : uint8_t commOutArgIndex;
281 : uint8_t reserved[8];
282 :
283 2 : std::string ToString() const
284 : {
285 : return StringFormat(
286 2 : "debugMode = %u\nsendArgIndex = %u\nrecvArgIndex = %u\ncommOutArgIndex = %u\n", debugMode, sendArgIndex,
287 2 : recvArgIndex, commOutArgIndex);
288 : }
289 : };
290 :
291 : struct Mc2CommConfig {
292 : uint8_t skipLocalRankCopy;
293 : uint8_t skipBufferWindowCopy;
294 : uint8_t stepSize;
295 : uint8_t communicationEngine; // 用于标记使用AIV、CCU、AICPU做通信加速器,定义同通信域加速器配置
296 : char reserved[12];
297 : char groupName[128]; // 指定通信域
298 : char algConfig[128]; // 指定算法
299 : uint32_t opType; // 算子类型
300 : uint32_t reduceType; // reduce类型,sum,max等
301 : uint32_t dataType; // 输入数据类型
302 : uint32_t outputDataType; // 输出数据类型
303 :
304 2 : std::string ToString() const
305 : {
306 : return StringFormat(
307 2 : "opType = %u\nreduceType = %u\ndataType = %u\noutputDataType = %u\n", opType, reduceType, dataType,
308 2 : outputDataType);
309 : }
310 : };
311 : struct Mc2Tiling {
312 : uint32_t version; // 版本
313 : uint32_t commConfigNum; // commComfig的个数,每个通信切片一个hcclConfig
314 : struct Mc2ServerCfg serverCfg; // 计算部分tiling
315 : struct Mc2CommConfig commConfig; // 通信部分tiling,共有Mc2CommConfig个,每个通信切片一个hcclConfig
316 :
317 2 : std::string ToString()
318 : {
319 2 : auto selfDesc = StringFormat("version = %u\ncommConfigNum = %u\n", version, commConfigNum);
320 4 : return selfDesc + serverCfg.ToString() + commConfig.ToString();
321 2 : }
322 : };
323 :
324 : struct Mc2InitTilingInner { // 这个必须放到mc2tiling的最前面
325 : uint32_t version; // tiling结构体版本号,外部不可配置, 100开始
326 : uint32_t mc2HcommCnt; // 通信的次数
327 : uint32_t offset[MAX_OP_NUM]; // 每个通信的偏移
328 : uint8_t debugMode; // 调测模式, 0表示关闭,1表示开启,外部可配置
329 : uint8_t preparePosition; // prepare消息发送的位置,0表示device,1表示host,外部可配置
330 : char reserved[22];
331 : };
332 :
333 : struct Mc2CcTilingInner {
334 : uint8_t skipLocalRankCopy; // 跳过本卡拷贝,在通信结果只需要给MC2内部计算使用或者本卡拷贝由aicore完成时,
335 : uint8_t skipBufferWindowCopy; // 跳过hbm到window间搬运 0不跳过,1跳过snd-window, 2跳过window-rcv
336 : uint8_t stepSize; // 通信步长,粗粒度融合时填0,
337 : uint8_t version; // 版本号
338 : char reserved[8]; // 保留字段
339 : uint8_t protocol; // 协议类型 0:ubmemory 1:urma
340 : uint8_t
341 : communicationEngine; // 用于标记使用AIV、CCU、AICPU做通信加速器,定义同通信域加速器配置 0:默认ccu 1:ccu 2:aiv
342 : uint8_t srcDataType; // 输入数据类型
343 : uint8_t dstDataType; // 输出数据类型
344 : char groupName[128]; // groupName
345 : char algConfig[128]; // 算法配置
346 : uint32_t opType; // tiling结构体版本号
347 : uint32_t reduceType; // reduce类型
348 : };
349 :
350 : constexpr uint32_t ALG_CONFIG_SIZE = 128;
351 : constexpr uint32_t MAX_OP_NAME_SIZE = 256;
352 : constexpr uint32_t MAX_MEM_TAG_SIZE = 256;
353 : struct HcclOpArgs {
354 : DataType srcDataType;
355 : DataType dstDataType;
356 : ReduceOp reduceType;
357 : uint64_t count{0};
358 : char algConfig[ALG_CONFIG_SIZE];
359 : HcclAccelerator commEngine;
360 : uint64_t reverse;
361 :
362 3 : void Init()
363 : { // if not set value, give a default
364 3 : srcDataType = DataType::FP16;
365 3 : dstDataType = DataType::FP16;
366 3 : reduceType = ReduceOp::SUM;
367 3 : count = 0;
368 3 : }
369 : };
370 :
371 : } // namespace Hccl
372 :
373 : #endif // HCCL_MC2_TYPE_H
|