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 : /*!
12 : * \file kernel_check_data_copy_util.h
13 : * \brief
14 : */
15 :
16 : #ifndef ASCENDC_CHECK_DATA_COPY_UTIL_H
17 : #define ASCENDC_CHECK_DATA_COPY_UTIL_H
18 : #if ASCENDC_CPU_DEBUG
19 : #include <string>
20 : #include "kernel_utils.h"
21 : namespace AscendC {
22 : namespace check {
23 : struct CopyApiParams {
24 : CopyApiParams() {}
25 16 : CopyApiParams(
26 : uint64_t dstAddrIn, uint64_t srcAddrIn, uint8_t repeatIn, uint16_t dstStrideIn, uint16_t srcStrideIn,
27 : uint16_t dstRepeatSizeIn, uint16_t srcRepeatSizeIn, uint32_t dstDtypeBytesIn, uint32_t srcDtypeBytesIn,
28 : uint64_t dstSizeIn, uint64_t srcSizeIn, uint8_t dstPosIn, uint8_t srcPosIn)
29 16 : {
30 16 : dstAddr = dstAddrIn;
31 16 : srcAddr = srcAddrIn;
32 16 : repeatTimes = repeatIn;
33 16 : dstStride = dstStrideIn;
34 16 : srcStride = srcStrideIn;
35 16 : dstRepeatSize = dstRepeatSizeIn;
36 16 : srcRepeatSize = srcRepeatSizeIn;
37 16 : dstDtypeBytes = dstDtypeBytesIn;
38 16 : srcDtypeBytes = srcDtypeBytesIn;
39 16 : dstSize = dstSizeIn;
40 16 : srcSize = srcSizeIn;
41 16 : dstLogicPos = dstPosIn;
42 16 : srcLogicPos = srcPosIn;
43 16 : dstPos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(dstPosIn)));
44 16 : srcPos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(srcPosIn)));
45 16 : }
46 : CopyApiParams(
47 : uint64_t dstAddrIn, uint64_t srcAddrIn, uint32_t dstDtypeBytesIn, uint32_t srcDtypeBytesIn, uint64_t dstSizeIn,
48 : uint64_t srcSizeIn, uint8_t dstPosIn, uint8_t srcPosIn, uint32_t calCountIn)
49 : {
50 : dstAddr = dstAddrIn;
51 : srcAddr = srcAddrIn;
52 : dstDtypeBytes = dstDtypeBytesIn;
53 : srcDtypeBytes = srcDtypeBytesIn;
54 : dstSize = dstSizeIn;
55 : srcSize = srcSizeIn;
56 : dstLogicPos = dstPosIn;
57 : srcLogicPos = srcPosIn;
58 : dstPos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(dstPosIn)));
59 : srcPos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(srcPosIn)));
60 : calCount = calCountIn;
61 : }
62 :
63 : uint64_t dstAddr = 0;
64 : uint64_t srcAddr = 0;
65 : uint8_t repeatTimes = 0;
66 : uint16_t dstStride = 0;
67 : uint16_t srcStride = 0;
68 : uint16_t dstRepeatSize = 0;
69 : uint16_t srcRepeatSize = 0;
70 : uint32_t dstDtypeBytes = 0;
71 : uint32_t srcDtypeBytes = 0;
72 : uint64_t dstSize = 0;
73 : uint64_t srcSize = 0;
74 : uint8_t dstLogicPos = 0;
75 : uint8_t srcLogicPos = 0;
76 : uint8_t dstPos = 0;
77 : uint8_t srcPos = 0;
78 : uint32_t calCount = 0;
79 : };
80 :
81 : struct DataCopyBaseParams {
82 : DataCopyBaseParams() {}
83 4 : DataCopyBaseParams(
84 : uint64_t dstAddrIn, uint64_t srcAddrIn, uint32_t dstDtypeBytesIn, uint32_t srcDtypeBytesIn, uint8_t dstPosIn,
85 : uint8_t srcPosIn, uint16_t blockCountIn, uint16_t blockLenIn, uint16_t srcStrideIn, uint16_t dstStrideIn)
86 4 : {
87 4 : dstAddr = dstAddrIn;
88 4 : srcAddr = srcAddrIn;
89 4 : dstDtypeBytes = dstDtypeBytesIn;
90 4 : srcDtypeBytes = srcDtypeBytesIn;
91 4 : dstLogicPos = dstPosIn;
92 4 : srcLogicPos = srcPosIn;
93 4 : dstPos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(dstPosIn)));
94 4 : srcPos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(srcPosIn)));
95 4 : blockCount = blockCountIn;
96 4 : blockLen = blockLenIn;
97 4 : srcStride = srcStrideIn;
98 4 : dstStride = dstStrideIn;
99 4 : }
100 :
101 : uint64_t dstAddr = 0;
102 : uint64_t srcAddr = 0;
103 : uint32_t dstDtypeBytes = 0;
104 : uint32_t srcDtypeBytes = 0;
105 : uint8_t dstLogicPos = 0;
106 : uint8_t srcLogicPos = 0;
107 : uint8_t dstPos = 0;
108 : uint8_t srcPos = 0;
109 : uint16_t blockCount = 0;
110 : uint16_t blockLen = 0;
111 : uint16_t srcStride = 0;
112 : uint16_t dstStride = 0;
113 : };
114 :
115 : struct DataCopyApiParams : public DataCopyBaseParams {
116 : DataCopyApiParams() : DataCopyBaseParams() {}
117 4 : DataCopyApiParams(
118 : uint64_t dstAddrIn, uint64_t srcAddrIn, uint32_t dstDtypeBytesIn, uint32_t srcDtypeBytesIn, uint8_t dstPosIn,
119 : uint8_t srcPosIn, uint16_t blockCountIn, uint16_t blockLenIn, uint16_t srcStrideIn, uint16_t dstStrideIn)
120 4 : : DataCopyBaseParams(
121 : dstAddrIn, srcAddrIn, dstDtypeBytesIn, srcDtypeBytesIn, dstPosIn, srcPosIn, blockCountIn, blockLenIn,
122 4 : srcStrideIn, dstStrideIn)
123 4 : {}
124 : };
125 :
126 : struct DataCopyPadApiParams : public DataCopyBaseParams {
127 : DataCopyPadApiParams() : DataCopyBaseParams(), isPad(false), leftPadding(0), rightPadding(0), paddingValue(0) {}
128 : DataCopyPadApiParams(
129 : uint64_t dstAddrIn, uint64_t srcAddrIn, uint32_t dstDtypeBytesIn, uint32_t srcDtypeBytesIn, uint8_t dstPosIn,
130 : uint8_t srcPosIn, uint16_t blockCountIn, uint16_t blockLenIn, uint16_t srcStrideIn, uint16_t dstStrideIn,
131 : bool isPadIn, uint8_t leftPaddingIn, uint8_t rightPaddingIn, uint64_t paddingValueIn)
132 : : DataCopyBaseParams(
133 : dstAddrIn, srcAddrIn, dstDtypeBytesIn, srcDtypeBytesIn, dstPosIn, srcPosIn, blockCountIn, blockLenIn,
134 : srcStrideIn, dstStrideIn),
135 : isPad(isPadIn),
136 : leftPadding(leftPaddingIn),
137 : rightPadding(rightPaddingIn),
138 : paddingValue(paddingValueIn)
139 : {}
140 :
141 : bool isPad = false;
142 : uint8_t leftPadding = 0;
143 : uint8_t rightPadding = 0;
144 : uint64_t paddingValue = 0;
145 : };
146 :
147 : struct DataCopySliceApiParams {
148 : DataCopySliceApiParams() {}
149 52 : DataCopySliceApiParams(
150 : uint64_t dstAddrIn, uint64_t srcAddrIn, uint32_t dstDtypeBytesIn, uint32_t srcDtypeBytesIn, uint64_t sizeIn,
151 : uint8_t posIn, uint32_t dimValueIn, uint32_t shapeDstIn[], uint32_t shapeSrcIn[],
152 : const SliceInfo dstSliceInfoIn[], const SliceInfo srcSliceInfoIn[], bool isGM2UBIn)
153 884 : {
154 52 : dstAddr = dstAddrIn;
155 52 : srcAddr = srcAddrIn;
156 52 : dstDtypeBytes = dstDtypeBytesIn;
157 52 : srcDtypeBytes = srcDtypeBytesIn;
158 52 : sizeNum = sizeIn;
159 52 : logicPos = posIn;
160 52 : pos = static_cast<uint8_t>(GetPhyType(static_cast<TPosition>(posIn)));
161 52 : dimValue = dimValueIn;
162 52 : isGM2UB = isGM2UBIn;
163 156 : for (uint32_t i = 0; i < dimValueIn; i++) {
164 104 : srcShape[i] = shapeSrcIn[i];
165 104 : dstShape[i] = shapeDstIn[i];
166 104 : dstSliceInfo[i] = dstSliceInfoIn[i];
167 104 : srcSliceInfo[i] = srcSliceInfoIn[i];
168 : }
169 52 : }
170 :
171 : uint64_t dstAddr = 0;
172 : uint64_t srcAddr = 0;
173 : uint32_t dstDtypeBytes = 0;
174 : uint32_t srcDtypeBytes = 0;
175 : uint64_t sizeNum = 0;
176 : uint8_t pos = 0;
177 : uint8_t logicPos = 0;
178 : uint32_t dimValue = 0;
179 : bool isGM2UB = false;
180 : uint32_t srcShape[K_MAX_SHAPE_DIM];
181 : uint32_t dstShape[K_MAX_SHAPE_DIM];
182 : SliceInfo dstSliceInfo[K_MAX_SHAPE_DIM];
183 : SliceInfo srcSliceInfo[K_MAX_SHAPE_DIM];
184 : };
185 :
186 : template <typename T, typename U>
187 : inline bool IsConv(DeqScale deqScale)
188 : {
189 : (void)(deqScale);
190 : return false;
191 : }
192 :
193 : template <>
194 : inline bool IsConv<int32_t, half>(DeqScale deqScale)
195 : {
196 : return (
197 : deqScale == DeqScale::DEQ || deqScale == DeqScale::DEQ16 || deqScale == DeqScale::VDEQ ||
198 : deqScale == DeqScale::VDEQ16);
199 : }
200 :
201 : template <>
202 : inline bool IsConv<float, half>(DeqScale deqScale)
203 : {
204 : (void)(deqScale);
205 : return true;
206 : }
207 :
208 : template <>
209 : inline bool IsConv<half, half>(DeqScale deqScale)
210 : {
211 : (void)(deqScale);
212 : return false;
213 : }
214 :
215 : template <>
216 : inline bool IsConv<int32_t, int8_t>(DeqScale deqScale)
217 : {
218 : return (deqScale == DeqScale::DEQ8 || deqScale == DeqScale::VDEQ8);
219 : }
220 :
221 : template <>
222 : inline bool IsConv<int32_t, uint8_t>(DeqScale deqScale)
223 : {
224 : return (deqScale == DeqScale::DEQ8 || deqScale == DeqScale::VDEQ8);
225 : }
226 :
227 : template <>
228 : inline bool IsConv<int32_t, int16_t>(DeqScale deqScale)
229 : {
230 : return (deqScale == DeqScale::DEQ16 || deqScale == DeqScale::VDEQ16);
231 : }
232 :
233 : template <typename T>
234 : inline std::string GetSrcIDString(Hardware srcScope, BlockMode blockMode)
235 : {
236 : std::string srcIDString = "";
237 : if ((srcScope == Hardware::UB) || (srcScope == Hardware::L1) || (srcScope == Hardware::GM)) {
238 : srcIDString += ConstDefiner::Instance().hardwareMap.at(srcScope);
239 : } else if (
240 : (std::is_same<T, float>::value || std::is_same<T, half>::value) &&
241 : (blockMode == BlockMode::BLOCK_MODE_DEPTHWISE)) {
242 : srcIDString += ConstDefiner::Instance().hardwareMap.at(srcScope) +
243 : ConstDefiner::Instance().blockModeMap.at(blockMode) + "f" +
244 : std::to_string(sizeof(T) * ONE_BYTE_BIT_SIZE);
245 : } else {
246 : srcIDString += ConstDefiner::Instance().hardwareMap.at(srcScope) +
247 : ConstDefiner::Instance().blockModeMap.at(blockMode) +
248 : std::to_string(sizeof(T) * ONE_BYTE_BIT_SIZE);
249 : }
250 : return srcIDString;
251 : }
252 :
253 : template <typename T>
254 : inline std::string GetDstIDString(Hardware dstScope, BlockMode blockMode)
255 : {
256 : std::string dstIDString = "";
257 : #if defined(__NPU_ARCH__) && \
258 : ((__NPU_ARCH__ == 2201) || (__NPU_ARCH__ == 3002) || (__NPU_ARCH__ == 3102) || (__NPU_ARCH__ == 3510) || \
259 : (__NPU_ARCH__ == 5102) || (__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3103) || (__NPU_ARCH__ == 3113))
260 : if ((dstScope == Hardware::UB) || (dstScope == Hardware::L1) || (dstScope == Hardware::GM) ||
261 : (dstScope == Hardware::BIAS) || (dstScope == Hardware::FIXBUF)) {
262 : #else
263 : if ((dstScope == Hardware::UB) || (dstScope == Hardware::L1) || (dstScope == Hardware::GM)) {
264 : #endif
265 : dstIDString += ConstDefiner::Instance().hardwareMap.at(dstScope);
266 : } else {
267 : dstIDString += ConstDefiner::Instance().hardwareMap.at(dstScope) +
268 : ConstDefiner::Instance().blockModeMap.at(blockMode) +
269 : std::to_string(sizeof(T) * ONE_BYTE_BIT_SIZE);
270 : }
271 : return dstIDString;
272 : }
273 :
274 : // get unit of dst src busrt length, unit is byte
275 : inline uint16_t GetBurstLenUnit(std::string srcDstId, bool isConv, bool isSrc)
276 : {
277 : uint16_t burstLenUnit = 0;
278 : auto burstLenUnitMap =
279 : isSrc ? ConstDefiner::Instance().srcBurstLenUnitMap : ConstDefiner::Instance().dstBurstLenUnitMap;
280 : if (burstLenUnitMap.find(srcDstId) != burstLenUnitMap.end()) {
281 : burstLenUnit = burstLenUnitMap.at(srcDstId);
282 : if (isConv) {
283 : burstLenUnit /= HALF_FACTOR;
284 : }
285 : } else {
286 : burstLenUnit = DEFAULT_C0_SIZE;
287 : }
288 : return burstLenUnit;
289 : }
290 :
291 : // get unit of dst src stride, unit is byte
292 : inline uint16_t GetStrideUnit(std::string srcDstId, bool isSrc)
293 : {
294 : auto strideUnitMap = isSrc ? ConstDefiner::Instance().srcStrideUnitMap : ConstDefiner::Instance().dstStrideUnitMap;
295 : if (strideUnitMap.find(srcDstId) != strideUnitMap.end()) {
296 : return strideUnitMap.at(srcDstId);
297 : }
298 : return DEFAULT_C0_SIZE;
299 : }
300 :
301 : bool CheckFuncCopyImplForMaskArray(CopyApiParams& chkParams, const uint64_t mask[], const char* intriName);
302 : bool CheckFuncCopyImpl(CopyApiParams& chkParams, const uint64_t mask, const char* intriName);
303 : bool CheckFuncCopyImpl(CopyApiParams& chkParams, const char* intriName);
304 :
305 : bool CheckFuncDataCopyImpl(DataCopyApiParams& chkParams, const char* intriName);
306 : bool CheckFuncDataCopyPadImpl(DataCopyPadApiParams& chkParams, const char* intriName);
307 : bool CheckFuncDataCopySliceImpl(DataCopySliceApiParams& chkParams, const char* intriName);
308 : } // namespace check
309 : } // namespace AscendC
310 : #endif
311 : #endif
|