Merged
add simd_vf dump and printf interface #1605
ChenZhoujieAdd on Apr 14
add simd_vf dump and printf interface #1605
Merged
ChenZhoujieAdd on Apr 14
Total: 8 files changed+648-1
@@ -15,6 +15,21 @@
15#ifndef IMPL_UTILS_DEBUG_ASC_DEBUG_TYPES_H15#ifndef IMPL_UTILS_DEBUG_ASC_DEBUG_TYPES_H
16#define IMPL_UTILS_DEBUG_ASC_DEBUG_TYPES_H16#define IMPL_UTILS_DEBUG_ASC_DEBUG_TYPES_H
17 17 
18+#define SIMD_VF_PRINTF_UBUF_MAX_SIZE 1024
zhanghao_0689

internal的名字要区分一下

likedislike
19+#define SIMD_VF_MAGIC_NUMBER 0xF0A00B0F
20+ 
21+struct BlockVFBufInfo {
22+ uint32_t magic = SIMD_VF_MAGIC_NUMBER;
23+ uint32_t length = SIMD_VF_PRINTF_UBUF_MAX_SIZE;
24+ uint32_t writeLen = 0;
25+ uint16_t pidx = 0;
26+ uint8_t flag = 0;
27+ uint8_t resv1 = 0;
28+ uint16_t blockIdx = 0;
29+ uint16_t resv2 = 0;
30+ uint8_t buffer[SIMD_VF_PRINTF_UBUF_MAX_SIZE];
31+};
32+ 
18namespace __asc_aicore {33namespace __asc_aicore {
19 34 
20enum class DumpTensorDataType : uint32_t {35enum class DumpTensorDataType : uint32_t {
@@ -163,7 +178,131 @@ enum class TraceId : uint32_t {
163 BroadCast = 0x8601,178 BroadCast = 0x8601,
164};179};
165#endif180#endif
181+ 
182+struct BlockRingBufInfo {
183+ uint32_t length = 0U; // total size per block (include head and r/w info)
184+ uint32_t coreId = 0U; // current core id
185+ uint32_t blockNum = 0U; // total core num
186+ uint32_t ringBufLen = 0U; // fifo buff size (print tlv storage)
187+ uint16_t magic = 0U; // magic number
ApeiriaNode_Booker

看看能不能共用,不能共用的话,可以放到单独的文件里

likedislike
188+ uint16_t flag = 0U; // 0: simd, 1: simt
189+ uint32_t rsv = 0U; // reserve
190+ uint64_t ringBufAddr = 0U; // start addr of fifo buff
191+ uint32_t resvMem[6]; // reserved
192+};
193+ 
194+struct RingBufWriteInfo {
195+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_BUFI); // DumpType = DUMP_BUFI
196+ uint32_t length = 0U; // u64 + u64
197+ uint64_t bufOffset = 0U; // the offset of write addr relative to ringBufAddr
198+ uint64_t packIdx = 0U; // print pack counter
199+};
200+ 
201+struct RingBufReadInfo {
202+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_BUFO); // DumpType = DUMP_BUFO
203+ uint32_t length = 0U; // u64 + u64
204+ uint64_t bufOffset = 0U; // the offset of read addr relative to ringBufAddr
205+ uint64_t resv = 0U;
206+};
166} // namespace __asc_aicore207} // namespace __asc_aicore
208+ 
209+namespace __asc_simd_vf {
210+enum class DumpTensorDataType : uint32_t {
211+ ACL_FLOAT = 0,
212+ ACL_FLOAT16 = 1,
213+ ACL_INT8 = 2,
214+ ACL_INT32 = 3,
215+ ACL_UINT8 = 4,
216+ ACL_INT16 = 6,
217+ ACL_UINT16 = 7,
218+ ACL_UINT32 = 8,
219+ ACL_INT64 = 9,
220+ ACL_UINT64 = 10,
221+ ACL_DOUBLE = 11,
222+ ACL_BOOL = 12,
223+ ACL_STRING = 13,
224+ ACL_COMPLEX64 = 16,
225+ ACL_COMPLEX128 = 17,
226+ ACL_BF16 = 27,
227+ ACL_INT4 = 29,
228+ ACL_UINT1 = 30,
229+ ACL_COMPLEX32 = 33,
230+ ACL_HIFLOAT8 = 34,
231+ ACL_FLOAT8_E5M2 = 35,
232+ ACL_FLOAT8_E4M3FN = 36,
233+ ACL_FLOAT8_E8M0 = 37,
234+ ACL_FLOAT6_E3M2 = 38,
235+ ACL_FLOAT6_E2M3 = 39,
236+ ACL_FLOAT4_E2M1 = 40,
237+ ACL_FLOAT4_E1M2 = 41,
238+ ACL_MAX = 42,
239+};
240+ 
241+enum class DumpType : uint8_t {
242+ DUMP_DEFAULT = 0,
243+ DUMP_SCALAR,
244+ DUMP_TENSOR,
245+ DUMP_SHAPE,
246+ DUMP_ASSERT,
247+ DUMP_META,
248+ DUMP_TIME_STAMP,
249+ DUMP_SIMT,
250+ DUMP_BUFI,
251+ DUMP_BUFO,
252+ DUMP_SKIP
253+};
254+ 
255+struct BlockRingBufInfo {
256+ uint32_t length = 0U; // total size per block (include head and r/w info)
257+ uint32_t coreId = 0U; // current core id
258+ uint32_t blockNum = 0U; // total core num
259+ uint32_t ringBufLen = 0U; // fifo buff size (print tlv storage)
260+ uint16_t magic = 0U; // magic number
261+ uint16_t flag = 0U; // 0: simd, 1: simt
262+ uint32_t rsv = 0U; // reserve
263+ uint64_t ringBufAddr = 0U; // start addr of fifo buff
264+ uint32_t resvMem[6]; // reserved
265+};
266+ 
267+struct RingBufWriteInfo {
268+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_BUFI); // DumpType = DUMP_BUFI
269+ uint32_t length = 0U; // u64 + u64
270+ uint64_t bufOffset = 0U; // the offset of write addr relative to ringBufAddr
271+ uint64_t packIdx = 0U; // print pack counter
272+};
273+ 
274+struct RingBufReadInfo {
275+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_BUFO); // DumpType = DUMP_BUFO
276+ uint32_t length = 0U; // u64 + u64
277+ uint64_t bufOffset = 0U; // the offset of read addr relative to ringBufAddr
278+ uint64_t resv = 0U;
279+};
280+ 
281+struct PrintTlv {
282+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_SCALAR);
283+ uint32_t length = 0U;
284+ uint32_t blockIdx = 0U; // blockIdx
285+ uint32_t resv = 0U; // reserved
286+ uint64_t fmtOffset = 0U; // offset of fmt string from the start of fmtOffset addr
287+};
288+ 
289+struct DumpTensorTlv {
290+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_TENSOR); // DumpType = DUMP_TENSOR
291+ uint32_t length = 0U; // Length of (addr dataType desc bufferId position dumpSize dumpData align)
292+ uint32_t tensorAddr = 0U; // Address of Tensor
293+ uint32_t dataType = 0U; // Data type: int32_t/half/...
294+ uint32_t desc = 0U; // Usr id
295+ uint32_t bufferId = 0U; // 0
296+ uint16_t position = 0U; // Position GM,UB,L1,L0C
297+ uint16_t blockIdx = 0U; // blockIdx
298+ uint32_t dim = 0U; // shape dim
299+ uint32_t shape[8]; // dim <= 8
300+ uint32_t resv1 = 0U; // reserved
301+ uint32_t dumpSize = 0U; // Length of dumpData
302+ // dumpData[dumpSize], Tensor data
303+};
304+} // namespace __asc_simd_vf
305+ 
167#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_TYPES__)306#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_TYPES__)
168#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__307#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__
169#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_TYPES__308#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_TYPES__
@@ -33,6 +33,11 @@ inline __gm__ uint8_t* __gm__ g_sysPrintFifoSpace = nullptr;
33#endif33#endif
34 34 
35namespace __asc_aicore {35namespace __asc_aicore {
36+__aicore__ inline void asc_copy_ub2gm_align(__gm__ void* dst, __ubuf__ void* src, uint32_t size)
37+{
38+ copy_ubuf_to_gm_align_v2(dst, src, 1, size, 0, 0, 0, 0);
39+}
40+ 
36__aicore__ inline void enable_asc_diagnostics()41__aicore__ inline void enable_asc_diagnostics()
37{42{
38#if (!defined(ASCENDC_DUMP) || (ASCENDC_DUMP != 0)) || defined(ASCENDC_TIME_STAMP_ON)43#if (!defined(ASCENDC_DUMP) || (ASCENDC_DUMP != 0)) || defined(ASCENDC_TIME_STAMP_ON)
@@ -68,6 +68,172 @@ __aicore__ inline void mem_copy_ub_to_gm_impl(__gm__ T* dst, __ubuf__ T* src, co
68 68 
69} // namespace __asc_aicore69} // namespace __asc_aicore
70 70 
71+namespace __asc_simd_vf {
72+__simd_callee__ constexpr uint32_t align_print_tlv_len(const uint32_t dataLen)
73+{
74+ constexpr uint32_t alignBytes = 8;
75+ return ((dataLen + (alignBytes - 1)) & ~(alignBytes - 1)) + alignBytes;
76+}
77+ 
78+template <typename T>
79+__simd_callee__ inline void set_scalar_param_vf(__ubuf__ uint8_t* paramAddr, uint32_t paramIdx, T scalar)
80+{
81+ set_scalar_param_vf_impl(paramAddr, paramIdx, scalar);
82+}
83+ 
84+__simd_callee__ inline void set_string_param_vf(
85+ __ubuf__ uint8_t* paramAddr, uint32_t paramIdx, __ubuf__ const char* s, uint32_t& offset)
86+{
87+ __ubuf__ uint64_t* stringAddr = reinterpret_cast<__ubuf__ uint64_t*>(paramAddr) + paramIdx;
88+ __ubuf__ uint8_t* dstStrAddr = paramAddr + offset;
89+ 
90+ // write string value offset
91+ *stringAddr = static_cast<uint64_t>(offset - sizeof(uint64_t) * paramIdx);
92+ 
93+ // write string content: GM -> UBuf
94+ uint32_t strLen = get_cstring_len_vf(s);
95+ for (uint32_t i = 0; i < strLen; i++) {
96+ *(dstStrAddr + i) = *(s + i);
97+ }
98+ offset += strLen;
99+}
100+ 
101+__simd_callee__ inline void set_param_vf(__ubuf__ uint8_t* paramAddr, uint32_t paramIdx, uint32_t& offset)
102+{
103+ (void)paramAddr;
104+ (void)paramIdx;
105+ (void)offset;
106+ return;
107+}
108+ 
109+template <typename... Args>
110+__simd_callee__ inline void set_param_vf(
111+ __ubuf__ uint8_t* paramAddr, uint32_t paramIdx, uint32_t& offset, Args&&... args);
112+ 
113+template <typename... Args>
114+__simd_callee__ inline void set_param_vf_impl(
115+ __ubuf__ uint8_t* paramAddr, uint32_t paramIdx, uint32_t& offset, __ubuf__ const char* s, Args&&... args)
116+{
117+ set_string_param_vf(paramAddr, paramIdx, s, offset);
118+ set_param_vf(paramAddr, paramIdx + 1, offset, args...);
119+}
120+ 
121+template <typename T, typename... Args>
122+__simd_callee__ inline void set_param_vf_impl(
123+ __ubuf__ uint8_t* paramAddr, uint32_t paramIdx, uint32_t& offset, T scalar, Args&&... args)
124+{
125+ set_scalar_param_vf(paramAddr, paramIdx, scalar);
126+ set_param_vf(paramAddr, paramIdx + 1, offset, args...);
127+}
128+ 
129+template <typename... Args>
130+__simd_callee__ inline void set_param_vf(
131+ __ubuf__ uint8_t* paramAddr, uint32_t paramIdx, uint32_t& offset, Args&&... args)
132+{
133+ set_param_vf_impl(paramAddr, paramIdx, offset, args...);
134+}
135+ 
136+__simd_callee__ inline uint32_t get_args_len_vf(uint32_t& argsNum)
137+{
138+ (void)argsNum;
139+ return 0;
140+}
141+ 
142+template <typename... Args>
143+__simd_callee__ inline uint32_t get_args_len_vf(uint32_t& argsNum, Args&&... args);
144+ 
145+template <typename... Args>
146+__simd_callee__ inline uint32_t get_args_len_vf_impl(uint32_t& argsNum, __ubuf__ const char* s, Args&&... args)
147+{
148+ constexpr uint32_t paramSize = sizeof(uint64_t);
149+ const uint32_t strLen = get_cstring_len_vf(s);
150+ argsNum += 1;
151+ return paramSize + strLen + get_args_len_vf(argsNum, args...);
152+}
153+ 
154+template <typename T, typename... Args>
155+__simd_callee__ inline uint32_t get_args_len_vf_impl(uint32_t& argsNum, T scalar, Args&&... args)
156+{
157+ constexpr uint32_t paramSize = sizeof(uint64_t);
158+ argsNum += 1;
159+ return paramSize + get_args_len_vf(argsNum, args...);
160+}
161+ 
162+template <typename... Args>
163+__simd_callee__ inline uint32_t get_args_len_vf(uint32_t& argsNum, Args&&... args)
164+{
165+ return get_args_len_vf_impl(argsNum, args...);
166+}
167+ 
168+template <typename... Args>
169+__simd_callee__ inline uint32_t get_print_tlv_len_simd(uint32_t& argsNum, __ubuf__ const char* fmt, Args&&... args)
170+{
171+ constexpr uint32_t printInfoLen = sizeof(PrintTlv);
172+ const uint32_t argsLen = get_args_len_vf(argsNum, args...);
173+ const uint32_t fmtLen = get_cstring_len_vf(fmt);
174+ return align_print_tlv_len(printInfoLen + argsLen + fmtLen);
175+}
176+ 
177+__simd_callee__ inline void set_print_tlv_info_vf(
178+ DumpType debugType, __ubuf__ PrintTlv* printTlv, const uint32_t& tlvLen, const uint32_t& argsNum, uint16_t blockIdx)
179+{
180+ printTlv->type = static_cast<uint32_t>(debugType);
181+ printTlv->length = tlvLen - sizeof(uint32_t[2]); // exclude type and length
182+ printTlv->blockIdx = blockIdx; // set in aicore
183+ printTlv->resv = static_cast<uint32_t>(0U);
184+ printTlv->fmtOffset = (argsNum + 1) * sizeof(uint64_t); // include fmt offset
185+}
186+ 
187+__simd_callee__ inline void copy_fmt_to_ubuf(__ubuf__ uint8_t* dst, __ubuf__ const char* src, uint32_t len)
zhanghao_0689

入参类型定义一致

likedislike
188+{
189+ // Workaround for -O2 optimization issue: copy byte-by-byte to avoid miscompilation
190+ for (uint32_t i = 0; i < len; i += 2) {
191+ dst[i] = src[i];
192+ dst[i + 1] = src[i + 1];
zhanghao_0689

这里会有越界读写

likedislike
193+ }
194+}
195+ 
196+template <typename... Args>
197+__simd_callee__ inline void set_print_tlv_data_vf(
198+ __ubuf__ PrintTlv* printTlv, __ubuf__ const char* fmt, Args&&... args)
199+{
200+ const uint32_t strLen = get_cstring_len_vf(fmt);
201+ __ubuf__ uint8_t* paramAddr = reinterpret_cast<__ubuf__ uint8_t*>(printTlv + 1);
202+ __ubuf__ uint8_t* fmtAddr = paramAddr + printTlv->fmtOffset - sizeof(uint64_t);
203+ 
204+ copy_fmt_to_ubuf(fmtAddr, fmt, strLen);
205+ 
206+ uint32_t strParamOffset = printTlv->fmtOffset + strLen;
207+ set_param_vf(paramAddr, 0, strParamOffset, args...);
208+}
209+ 
210+template <class... Args>
211+__simd_callee__ inline void scalar_printf_impl(DumpType debugType, __ubuf__ const char* fmt, Args&&... args)
212+{
213+ __ubuf__ BlockVFBufInfo* blockInfo = get_printf_ubuf_addr(0);
214+ 
215+ uint32_t argsNum = 0;
216+ const uint32_t tlvLen = get_print_tlv_len_simd(argsNum, fmt, args...);
217+ 
218+ // construct PrintTlv TLV in BlockVFBufInfo.buffer (UBuf)
219+ __ubuf__ PrintTlv* printTlv =
220+ reinterpret_cast<__ubuf__ PrintTlv*>((__ubuf__ uint8_t*)(blockInfo->buffer) + blockInfo->writeLen);
221+ set_print_tlv_info_vf(debugType, printTlv, tlvLen, argsNum, blockInfo->blockIdx);
222+ set_print_tlv_data_vf(printTlv, fmt, args...);
223+ 
224+ blockInfo->magic = SIMD_VF_MAGIC_NUMBER;
225+ blockInfo->writeLen += tlvLen;
226+ blockInfo->pidx += 1;
227+}
228+ 
229+template <class... Args>
230+__simd_callee__ inline void printf_impl(__ubuf__ const char* fmt, Args&&... args)
231+{
232+ enable_asc_diagnostics();
233+ scalar_printf_impl(DumpType::DUMP_SCALAR, fmt, args...);
234+}
235+} // namespace __asc_simd_vf
236+ 
71#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_DUMP_UTILS__)237#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_DUMP_UTILS__)
72#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__238#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__
73#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_DUMP_UTILS__239#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_DUMP_UTILS__
@@ -45,6 +45,125 @@ __aicore__ inline void set_scalar_param_impl(__gm__ uint8_t* paramAddr, uint32_t
45 45 
46} // namespace __asc_aicore46} // namespace __asc_aicore
47 47 
48+namespace __asc_simd_vf {
49+template <AscendC::Hardware hardware, typename T, typename U>
50+__simd_callee__ inline void set_dump_tlv_info_vf(U& src, __ubuf__ DumpTensorTlv* dumpTlv,
51+ uint32_t alignDumpLen, uint32_t desc, uint32_t dumpSize, uint16_t blockIdx)
52+{
53+ dumpTlv->type = static_cast<uint32_t>(DumpType::DUMP_TENSOR);
54+ dumpTlv->length = sizeof(DumpTensorTlv) - sizeof(uint32_t[2]) + alignDumpLen;
55+ dumpTlv->tensorAddr = 0U; // set in aicore
56+ dumpTlv->dataType = static_cast<uint32_t>(get_dump_datatype<T>());
57+ dumpTlv->desc = desc;
58+ dumpTlv->blockIdx = blockIdx; // set in aicore
59+ dumpTlv->bufferId = static_cast<uint32_t>(0U);
60+ dumpTlv->position = static_cast<uint16_t>(hardware);
61+ dumpTlv->dim = static_cast<uint32_t>(0U);
62+ for (uint32_t i = 0; i < 8; ++i) {
63+ dumpTlv->shape[i] = static_cast<uint32_t>(0U);
64+ }
65+ dumpTlv->resv1 = static_cast<uint32_t>(0U);
66+ dumpTlv->dumpSize = dumpSize * sizeof(T);
67+}
68+ 
69+template <AscendC::Hardware hardware, typename T, typename U>
70+__simd_callee__ inline void set_dump_tlv_data_vf(U& src, __ubuf__ DumpTensorTlv* dumpTlv,
71+ uint32_t alignDumpLen, uint32_t dumpSize)
72+{
73+ __ubuf__ T* dumpDstAddr = reinterpret_cast<__ubuf__ T*>(dumpTlv + 1);
74+ 
75+ // Scalar copy; performance optimization deferred
76+ for (uint32_t i = 0; i < dumpSize; i++) {
77+ dumpDstAddr[i] = src[i];
78+ }
79+}
80+ 
81+template <AscendC::Hardware hardware, typename T, typename U>
82+__simd_callee__ inline void set_dump_tlv_data_reg(U& src, __ubuf__ DumpTensorTlv* dumpTlv,
83+ uint32_t alignDumpLen, uint32_t dumpSize)
84+{
85+ __ubuf__ T* dumpDstAddr = reinterpret_cast<__ubuf__ T*>(dumpTlv + 1);
86+ 
87+ // Store interface supports unaligned transfer; dumpDstAddr is likely unaligned
88+ uint32_t count = dumpTlv->dumpSize / sizeof(T);
89+ Store(dumpDstAddr, src, count);
90+}
91+ 
92+template <AscendC::Hardware hardware, typename T, typename U>
93+__simd_callee__ inline void asc_dump_impl_reg(U& src, uint32_t desc, uint32_t dumpSize)
94+{
95+ __ubuf__ BlockVFBufInfo *blockInfo = get_printf_ubuf_addr(0);
96+
97+ constexpr uint16_t dataBlockSize = 32;
98+ uint32_t alignDumpLen = align_up(dumpSize * sizeof(T), dataBlockSize);
99+ uint32_t tlvLen = sizeof(DumpTensorTlv) + alignDumpLen;
100+ 
101+ __ubuf__ DumpTensorTlv* dumpTlv =
102+ (__ubuf__ DumpTensorTlv*)((__ubuf__ uint8_t*)(blockInfo->buffer) + blockInfo->writeLen);
103+ set_dump_tlv_info_vf<hardware, T>(src, dumpTlv, alignDumpLen, desc, dumpSize, blockInfo->blockIdx);
104+ set_dump_tlv_data_reg<hardware, T>(src, dumpTlv, alignDumpLen, dumpSize);
105+ 
106+ blockInfo->magic = 0xF0A00B0F;
107+ blockInfo->writeLen += tlvLen;
108+ blockInfo->pidx += 1;
109+}
110+ 
111+template <AscendC::Hardware hardware, typename T, typename U>
112+__simd_callee__ inline void asc_dump_impl(U& src, uint32_t desc, uint32_t dumpSize)
113+{
114+ __ubuf__ BlockVFBufInfo *blockInfo = get_printf_ubuf_addr(0);
115+
116+ constexpr uint16_t dataBlockSize = 32;
117+ uint32_t alignDumpLen = align_up(dumpSize * sizeof(T), dataBlockSize);
118+ uint32_t tlvLen = sizeof(DumpTensorTlv) + alignDumpLen;
119+ 
120+ __ubuf__ DumpTensorTlv* dumpTlv =
121+ (__ubuf__ DumpTensorTlv*)((__ubuf__ uint8_t*)(blockInfo->buffer) + blockInfo->writeLen);
122+ set_dump_tlv_info_vf<hardware, T>(src, dumpTlv, alignDumpLen, desc, dumpSize, blockInfo->blockIdx);
123+ set_dump_tlv_data_vf<hardware, T>(src, dumpTlv, alignDumpLen, dumpSize);
124+ 
125+ blockInfo->magic = 0xF0A00B0F;
126+ blockInfo->writeLen += tlvLen;
127+ blockInfo->pidx += 1;
128+}
129+ 
130+template <typename T, typename U>
131+__simd_callee__ inline void asc_dump_reg(U& input, uint32_t desc, uint32_t dumpSize)
132+{
133+ enable_asc_diagnostics();
134+ asc_dump_impl_reg<AscendC::Hardware::UB, T>(input, desc, dumpSize);
135+}
136+ 
137+template <typename T>
138+__simd_callee__ inline void asc_dump_ubuf(__ubuf__ T* input, uint32_t desc, uint32_t dumpSize) {
139+ enable_asc_diagnostics();
140+ asc_dump_impl<AscendC::Hardware::UB, T>(input, desc, dumpSize);
141+}
142+ 
143+template <typename T>
144+__simd_callee__ inline void set_scalar_param_vf_impl(__ubuf__ uint8_t* paramAddr, uint32_t paramIdx, T scalar)
145+{
146+ __ubuf__ uint64_t *scalarAddr = (__ubuf__ uint64_t *)paramAddr + paramIdx;
147+ *scalarAddr = 0;
148+ 
149+ if constexpr (is_same_in_list<T, half, float>()) {
150+ *((__ubuf__ float *)scalarAddr) = static_cast<float>(scalar);
151+ } else if constexpr (is_same_in_list<T, double>()) {
152+ *((__ubuf__ double *)scalarAddr) = static_cast<double>(scalar);
153+ } else if constexpr (std::is_signed<T>::value) {
154+ *((__ubuf__ int64_t *)scalarAddr) = static_cast<int64_t>(scalar);
155+ } else if constexpr(std::is_unsigned<T>::value) {
156+ *((__ubuf__ uint64_t *)scalarAddr) = static_cast<uint64_t>(scalar);
157+ } else if constexpr(is_same_in_list<T, bfloat16_t, float8_e5m2_t, float8_e8m0_t, float8_e4m3_t, hifloat8_t>()) {
158+ *((__ubuf__ float *)scalarAddr) = to_float(scalar);
159+ } else if constexpr(std::is_pointer<T>::value) {
160+ *((__ubuf__ uint64_t *)scalarAddr) = (uintptr_t)scalar;
161+ } else if constexpr(std::is_enum<T>::value) {
162+ *((__ubuf__ uint64_t *)scalarAddr) = static_cast<uint64_t>(scalar);
163+ }
164+}
165+} // namespace __asc_simd_vf
166+ 
48#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_PRINTF_UTILS__)167#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_PRINTF_UTILS__)
49#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__168#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__
50#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_PRINTF_UTILS__169#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_AICORE_PRINTF_UTILS__
@@ -16,6 +16,85 @@
16#define IMPL_UTILS_DEBUG_NPU_ARCH_3510_ASC_DEBUG_UTILS_H16#define IMPL_UTILS_DEBUG_NPU_ARCH_3510_ASC_DEBUG_UTILS_H
17 17 
18#include "impl/utils/sys_macros.h"18#include "impl/utils/sys_macros.h"
19+ 
20+namespace __asc_simd_vf {
21+template <typename T, typename U, typename... Args>
22+__simd_callee__ constexpr bool is_same_in_list()
23+{
24+ if constexpr (sizeof...(Args) > 0) {
25+ return std::is_same<T, U>::value || is_same_in_list<T, Args...>();
26+ }
27+ return std::is_same<T, U>::value;
28+}
29+ 
30+__simd_callee__ inline uint32_t get_cstring_len_vf(__ubuf__ const char* s)
31+{
32+ uint32_t i = 0;
33+ while (*(s + i) != '\0') {
34+ i++;
35+ }
36+ return i + 1;
37+}
38+ 
39+__simd_callee__ constexpr inline uint32_t div_ceil(uint32_t a, uint32_t b)
40+{
41+ if (b == 0) {
42+ return 0;
43+ }
44+ return (a + b - 1) / b;
45+}
46+ 
47+__simd_callee__ constexpr inline uint32_t align_up(uint32_t a, uint32_t b)
48+{
49+ return div_ceil(a, b) * b;
50+}
51+ 
52+template <typename T>
53+__simd_callee__ constexpr inline DumpTensorDataType get_dump_datatype_impl();
54+ 
55+template <typename T>
56+__simd_callee__ constexpr inline DumpTensorDataType get_dump_datatype()
57+{
58+ return get_dump_datatype_impl<T>();
59+}
60+ 
61+__simd_callee__ inline void enable_asc_diagnostics()
62+{
63+#if defined(__ENABLE_ASCENDC_PRINTF__) && (ASCENDC_SIMD_VF_DEBUG == 1)
64+#if (!defined(ASCENDC_DUMP) || (ASCENDC_DUMP != 0)) || defined(ASCENDC_TIME_STAMP_ON)
65+ static const struct AscTlv __asc_debug_meta_section__ __attribute__ ((used, section (".ascend.meta"))) =
66+ {4, 4, 1};
67+#endif
68+#endif
69+}
70+ 
71+__simd_callee__ __ubuf__ inline BlockVFBufInfo* get_printf_ubuf_addr(uint64_t addr, uint16_t blockIdx = 0)
72+{
73+ constexpr uint32_t safeStaticLen = 32;
74+ static __ubuf__ uint64_t info[safeStaticLen];
75+ if (addr != 0) {
76+ info[0] = addr;
77+ // clear data workspace when initializing
78+ auto* bufInfo = reinterpret_cast<__ubuf__ BlockVFBufInfo*>(addr);
79+ bufInfo->writeLen = 0;
80+ bufInfo->pidx = 0;
81+ bufInfo->blockIdx = blockIdx;
82+ }
83+ return reinterpret_cast<__ubuf__ BlockVFBufInfo*>(info[0]);
84+}
85+ 
86+__simd_callee__ inline void asc_copy_ub2gm_align(__gm__ void* dst, __ubuf__ void* src, uint32_t size)
87+{
88+ __cce_scalar::copy_ubuf_to_gm_align_v2(dst, src, 1, size, 0, 0, 0, 0);
89+}
90+ 
91+__simd_callee__ inline void asc_copy_ub2ub(__ubuf__ void* dst, __ubuf__ void* src, uint32_t size)
92+{
93+ __cce_scalar::copy_ubuf_to_ubuf(dst, src, 1, size / 32, 0, 0);
94+}
95+} // namespace __asc_simd_vf
96+ 
97+ 
19namespace __asc_aicore {98namespace __asc_aicore {
20__aicore__ inline void asc_entire_dcci_impl(__gm__ uint64_t* ptr)99__aicore__ inline void asc_entire_dcci_impl(__gm__ uint64_t* ptr)
21{100{
@@ -112,7 +191,121 @@ __aicore__ constexpr inline DumpTensorDataType get_dump_datatype_impl()
112 }191 }
113}192}
114 193 
194+__aicore__ __ubuf__ inline BlockVFBufInfo* get_printf_ubuf_addr_aicore(uint64_t addr, uint16_t blockIdx = 0)
195+{
196+ return __asc_simd_vf::get_printf_ubuf_addr(addr, blockIdx);
115}197}
198+ 
199+__aicore__ __gm__ inline BlockRingBufInfo* get_block_ring_buf_info()
200+{
201+ const uint32_t blockIdx = asc_debug_get_core_idx_impl();
202+ const uint32_t blockLength = reinterpret_cast<__gm__ BlockRingBufInfo*>(g_sysPrintFifoSpace)->length;
203+ __gm__ BlockRingBufInfo* blockInfo =
204+ reinterpret_cast<__gm__ BlockRingBufInfo*>(g_sysPrintFifoSpace + blockLength * blockIdx);
205+ return blockInfo;
206+}
207+ 
208+__aicore__ inline __gm__ RingBufReadInfo* get_ring_buf_read_info(
209+ __gm__ BlockRingBufInfo* block_ring_buf_info)
210+{
211+ __gm__ uint8_t* blockHead = reinterpret_cast<__gm__ uint8_t*>(block_ring_buf_info);
212+ return reinterpret_cast<__gm__ RingBufReadInfo*>(blockHead + sizeof(BlockRingBufInfo));
213+}
214+ 
215+__aicore__ inline __gm__ RingBufWriteInfo* get_ring_buf_write_info(
216+ __gm__ BlockRingBufInfo* block_ring_buf_info)
217+{
218+ __gm__ uint8_t* ringBufAddr = reinterpret_cast<__gm__ uint8_t*>(block_ring_buf_info->ringBufAddr);
219+ return reinterpret_cast<__gm__ RingBufWriteInfo*>(ringBufAddr + block_ring_buf_info->ringBufLen);
220+}
221+ 
222+__aicore__ __gm__ inline uint8_t* call_get_ring_buf_tlv(__gm__ BlockRingBufInfo* block_ring_buf_info)
223+{
224+ __gm__ RingBufWriteInfo* writeInfo = get_ring_buf_write_info(block_ring_buf_info);
225+ __gm__ uint8_t* ringBufAddr = reinterpret_cast<__gm__ uint8_t*>(block_ring_buf_info->ringBufAddr);
226+ return ringBufAddr + writeInfo->bufOffset;
227+}
228+ 
229+__aicore__ inline void update_write_info(
230+ __gm__ RingBufWriteInfo* writeInfo, const uint32_t& tlvLen, uint32_t packageNum = 1)
231+{
232+ writeInfo->bufOffset += tlvLen;
233+ writeInfo->packIdx += packageNum;
234+ asc_entire_dcci_impl(reinterpret_cast<__gm__ uint64_t*>(writeInfo));
235+}
236+ 
237+__aicore__ inline void asc_vf_debug_ub2gm()
238+{
239+ __ubuf__ BlockVFBufInfo* blockInfo = get_printf_ubuf_addr_aicore(0);
240+ __ubuf__ uint8_t* tlv = reinterpret_cast<__ubuf__ uint8_t*>(blockInfo->buffer);
241+ 
242+ __gm__ BlockRingBufInfo* blockRingBufInfo = get_block_ring_buf_info();
243+ __gm__ uint8_t* dstTlv = reinterpret_cast<__gm__ uint8_t*>(call_get_ring_buf_tlv(blockRingBufInfo));
244+ const uint32_t tlvLen = blockInfo->writeLen;
245+ 
246+ sync_all_impl();
247+ constexpr uint32_t sizeU32 = sizeof(uint32_t);
248+ const uint32_t totalWords = (tlvLen + sizeU32 - 1) / sizeU32;
249+ auto* dstWords = reinterpret_cast<__gm__ uint32_t*>(dstTlv);
250+ auto* srcWords = reinterpret_cast<__ubuf__ uint32_t*>(tlv);
251+ for (uint32_t i = 0; i < totalWords; ++i) {
252+ dstWords[i] = srcWords[i];
253+ }
254+ sync_all_impl();
255+ 
256+ asc_entire_dcci_impl(reinterpret_cast<__gm__ uint64_t*>(dstTlv));
257+ 
258+ __gm__ RingBufWriteInfo* writeInfo = get_ring_buf_write_info(blockRingBufInfo);
259+ update_write_info(writeInfo, tlvLen, blockInfo->pidx);
260+}
261+}
262+ 
263+namespace __asc_simd_vf {
264+template <typename T>
265+__simd_callee__ constexpr inline DumpTensorDataType get_dump_datatype_impl()
266+{
267+ if constexpr (std::is_same<T, bool>::value) {
268+ return DumpTensorDataType::ACL_BOOL;
269+ } else if (std::is_same<T, uint8_t>::value) {
270+ return DumpTensorDataType::ACL_UINT8;
271+ } else if (std::is_same<T, int8_t>::value) {
272+ return DumpTensorDataType::ACL_INT8;
273+ } else if (std::is_same<T, int16_t>::value) {
274+ return DumpTensorDataType::ACL_INT16;
275+ } else if (std::is_same<T, uint16_t>::value) {
276+ return DumpTensorDataType::ACL_UINT16;
277+ } else if (std::is_same<T, int32_t>::value) {
278+ return DumpTensorDataType::ACL_INT32;
279+ } else if (std::is_same<T, uint32_t>::value) {
280+ return DumpTensorDataType::ACL_UINT32;
281+ } else if (std::is_same<T, uint64_t>::value) {
282+ return DumpTensorDataType::ACL_UINT64;
283+ } else if (std::is_same<T, int64_t>::value) {
284+ return DumpTensorDataType::ACL_INT64;
285+ } else if (std::is_same<T, float>::value) {
286+ return DumpTensorDataType::ACL_FLOAT;
287+ } else if (std::is_same<T, half>::value) {
288+ return DumpTensorDataType::ACL_FLOAT16;
289+ } else if (std::is_same<T, bfloat16_t>::value) {
290+ return DumpTensorDataType::ACL_BF16;
291+ } else if (std::is_same<T, hifloat8_t>::value) {
292+ return DumpTensorDataType::ACL_HIFLOAT8;
293+ } else if (std::is_same<T, float8_e5m2_t>::value) {
294+ return DumpTensorDataType::ACL_FLOAT8_E5M2;
295+ } else if (std::is_same<T, float8_e4m3_t>::value) {
296+ return DumpTensorDataType::ACL_FLOAT8_E4M3FN;
297+ } else if (std::is_same<T, float8_e8m0_t>::value) {
298+ return DumpTensorDataType::ACL_FLOAT8_E8M0;
299+ } else if (std::is_same<T, float4_e2m1x2_t>::value) {
300+ return DumpTensorDataType::ACL_FLOAT4_E2M1;
301+ } else if (std::is_same<T, float4_e1m2x2_t>::value) {
302+ return DumpTensorDataType::ACL_FLOAT4_E1M2;
303+ } else {
304+ return DumpTensorDataType::ACL_MAX;
305+ }
306+}
307+}
308+ 
116#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_UTILS_IMPL__)309#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_UTILS_IMPL__)
117#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__310#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__
118#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_UTILS_IMPL__311#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_DEBUG_UTILS_IMPL__
@@ -48,7 +48,16 @@ class GlobalTensor;
48template <auto funcPtr, typename... Args> __aicore__ inline void asc_vf_call(Args &&... args)48template <auto funcPtr, typename... Args> __aicore__ inline void asc_vf_call(Args &&... args)
49{49{
50#if (defined(__NPU_ARCH__) && __NPU_ARCH__ == 3510) 50#if (defined(__NPU_ARCH__) && __NPU_ARCH__ == 3510)
51- funcPtr(args...);51+#if ASCENDC_SIMD_VF_DEBUG == 1
ApeiriaNode_Booker

这个宏如果需要可以评一下

likedislike
52+ constexpr uint64_t RESERVED_UB_SIZE = 8 * 1024;
53+ uint64_t ascReservedAddr = get_shmem_sz() - RESERVED_UB_SIZE;
54+ uint16_t blockIdx = asc_debug_get_block_idx();
55+ get_printf_ubuf_addr_aicore(ascReservedAddr, blockIdx);
56+#endif
57+ funcPtr(args...);
58+#if ASCENDC_SIMD_VF_DEBUG == 1
59+ asc_vf_debug_ub2gm();
60+#endif
52#endif61#endif
53}62}
54 63 
@@ -38,6 +38,14 @@ template<typename T>
38__aicore__ inline void asc_dump_l1buf(__cbuf__ T* input, uint32_t desc, uint32_t dumpSize);38__aicore__ inline void asc_dump_l1buf(__cbuf__ T* input, uint32_t desc, uint32_t dumpSize);
39}39}
40 40 
41+namespace __asc_simd_vf {
42+template <typename T, typename U>
43+__simd_callee__ inline void asc_dump_reg(U& input, uint32_t desc, uint32_t dumpSize);
44+ 
45+template <typename T>
46+__simd_callee__ inline void asc_dump_ubuf(__ubuf__ T* input, uint32_t desc, uint32_t dumpSize);
47+}
48+ 
41#if defined(__UNDEF_ASCENDC_INCLUDE_COMPILER_INTERNAL_HEADERS_ASC_DUMP_H__)49#if defined(__UNDEF_ASCENDC_INCLUDE_COMPILER_INTERNAL_HEADERS_ASC_DUMP_H__)
42#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__50#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__
43#undef __UNDEF_ASCENDC_INCLUDE_COMPILER_INTERNAL_HEADERS_ASC_DUMP_H__51#undef __UNDEF_ASCENDC_INCLUDE_COMPILER_INTERNAL_HEADERS_ASC_DUMP_H__
@@ -53,6 +53,14 @@ __aicore__ inline void printf(__gm__ const char* fmt, Args&&... args)
53}53}
54} // namespace __asc_aicore54} // namespace __asc_aicore
55#endif55#endif
56+ 
57+namespace __asc_simd_vf {
58+template <class... Args>
59+__simd_callee__ inline void printf(__ubuf__ const char* fmt, Args&&... args)
60+{
61+ printf_impl(fmt, args...);
62+}
63+}
56#endif64#endif
57 65 
58#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_PRINTF_H__)66#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_ASC_PRINTF_H__)