Merged
新增支持超大Tensor的Dump功能 #6371
jcmrn0930Add on Sep 23
新增支持超大Tensor的Dump功能 #6371
Merged
jcmrn0930Add on Sep 23
Total: 5 files changed+555-13
@@ -520,7 +520,7 @@ __aicore__ inline void SetDumpDataL0C2GM(__gm__ uint8_t* dst, const LocalTensor<
520 param); // recover data in UB520 param); // recover data in UB
521 PipeBarrier<PIPE_ALL>();521 PipeBarrier<PIPE_ALL>();
522 uint32_t tailLen = copyLen * 2 - alignDumpDataLen;522 uint32_t tailLen = copyLen * 2 - alignDumpDataLen;
523- ClearGmData(gmBackAddr + alignDumpDataLen, tailLen);523+ ClearGmData(dst + alignDumpDataLen, tailLen);
524}524}
525 525 
526template <typename T>526template <typename T>
@@ -555,6 +555,123 @@ __aicore__ inline void WriteRingBufTlvData(
555 MemCopyGm2Gm(dst, reinterpret_cast<__gm__ const uint8_t*>(src.GetPhyAddr()), dumpSize * sizeof(T));555 MemCopyGm2Gm(dst, reinterpret_cast<__gm__ const uint8_t*>(src.GetPhyAddr()), dumpSize * sizeof(T));
556}556}
557 557 
558+#ifndef ASCENDC_CPU_DEBUG
559+template <typename T>
560+__aicore__ inline void WriteSuperTensorData(
561+ const GlobalTensor<T>& src, __gm__ uint8_t* dst, uint32_t alignDumpLen, uint32_t dumpSize)
562+{
563+ PipeBarrier<PIPE_ALL>();
564+ MemCopyGm2Gm(dst, reinterpret_cast<__gm__ const uint8_t*>(src.GetPhyAddr()), dumpSize);
565+}
566+ 
567+template <typename T>
568+__aicore__ inline void WriteSuperTensorData(
569+ const LocalTensor<T>& src, __gm__ uint8_t* dst, uint32_t alignDumpLen, uint32_t dumpSize)
570+{
571+ PipeBarrier<PIPE_ALL>();
572+ Hardware position = CheckDumpTensorPosition(src);
573+ if (position == Hardware::UB) {
574+ DataCopyParams params = {1, static_cast<uint16_t>(alignDumpLen / ONE_BLK_SIZE), 0, 0};
575+ DataCopyUB2GMImpl(dst, reinterpret_cast<__ubuf__ uint8_t*>(src.GetPhyAddr()), params);
576+ } else if (position == Hardware::L1) {
577+ SetDumpDataL12GM(dst, src, alignDumpLen);
578+ } else if (position == Hardware::L0C) {
579+ SetDumpDataL0C2GM(dst, src, alignDumpLen);
580+ }
581+ PipeBarrier<PIPE_ALL>();
582+ dcci(reinterpret_cast<__gm__ uint64_t*>(dst), cache_line_t::ENTIRE_DATA_CACHE);
583+}
584+ 
585+template <template <typename> class Tensor, typename T>
586+__aicore__ inline void WriteSuperTensorHead(
587+ const Tensor<T>& src, __gm__ __asc_aicore::DumpSuperTensorTlv* dumpTlv, Hardware position, uint32_t desc,
588+ uint64_t tensorLength, uint32_t dumpSize, uint32_t alignDumpLen, const uint32_t* shape, uint32_t shapeDim)
589+{
590+ dumpTlv->type = static_cast<uint32_t>(__asc_aicore::DumpType::DUMP_SUPER_TENSOR);
591+ dumpTlv->length = sizeof(__asc_aicore::DumpSuperTensorTlv) - sizeof(uint32_t[2]) + alignDumpLen;
592+ dumpTlv->tensorAddr = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(src.GetPhyAddr()));
593+ dumpTlv->dataType = static_cast<uint32_t>(GetTensorDataType<T>());
594+ dumpTlv->desc = desc;
595+ dumpTlv->bufferId = 0U;
596+ dumpTlv->position = static_cast<uint16_t>(position);
597+ dumpTlv->blockIdx = static_cast<uint16_t>(GetBlockIdxImpl());
598+ dumpTlv->dim = shape != nullptr && shapeDim <= K_MAX_SHAPE_DIM ? shapeDim : 0U;
599+ for (uint32_t i = 0; i < K_MAX_SHAPE_DIM; ++i) {
600+ dumpTlv->shape[i] = dumpTlv->dim == 0U ? 0U : (i < dumpTlv->dim ? shape[i] : 1U);
601+ }
602+ dumpTlv->resv = 0U;
603+ dumpTlv->tensorLength = tensorLength;
604+ dumpTlv->tensorOffset = 0U;
605+ dumpTlv->dumpSize = dumpSize;
606+ dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv), cache_line_t::ENTIRE_DATA_CACHE);
607+}
608+ 
609+template <template <typename> class Tensor, typename T>
610+__aicore__ inline void DumpSuperTensorRingBufImpl(
611+ const Tensor<T>& src, uint32_t desc, uint64_t tensorLength, const uint32_t* shape, uint32_t shapeDim,
612+ __gm__ BlockRingBufInfo* blockRingBufInfo)
613+{
614+ Hardware position = Hardware::GM;
615+ if constexpr (IsSameType<Tensor<T>, LocalTensor<T>>::value) {
616+ position = CheckDumpTensorPosition(src);
617+ }
618+ const bool needBackup = position == Hardware::L1 || position == Hardware::L0C;
619+ const uint32_t alignment = position == Hardware::L0C ? 16U * 16U * sizeof(uint32_t) : ONE_BLK_SIZE;
620+ // The legacy and common headers describe the same FIFO ABI. Keep the 2002 core lookup.
621+ static_assert(sizeof(BlockRingBufInfo) == sizeof(__asc_aicore::DebugBlockHeadInfo));
622+ static_assert(sizeof(RingBufReadInfo) == sizeof(__asc_aicore::DebugBlockReadInfo));
623+ static_assert(sizeof(RingBufWriteInfo) == sizeof(__asc_aicore::DebugBlockWriteInfo));
624+ auto* blockInfo = reinterpret_cast<__gm__ __asc_aicore::DebugBlockHeadInfo*>(blockRingBufInfo);
625+ uint64_t tensorOffset = 0U;
626+ while (tensorOffset < tensorLength) {
627+ const bool isFirst = tensorOffset == 0U;
628+ const uint32_t headerLen =
629+ isFirst ? sizeof(__asc_aicore::DumpSuperTensorTlv) : sizeof(__asc_aicore::DumpSuperTensorBodyTlv);
630+ if (blockInfo->ringBufLen <= headerLen) {
631+ return;
632+ }
633+ uint32_t payloadCapacity = (blockInfo->ringBufLen - headerLen) / (needBackup ? 2U : 1U);
634+ // L1/L0C reuse the UB scratch tensor; UB DMA blockLen is a uint16_t.
635+ const uint32_t maxCopyLen = needBackup ? ONE_DUMP_BACKUP_SIZE : 0xFFFFU * ONE_BLK_SIZE;
636+ if (position != Hardware::GM && payloadCapacity > maxCopyLen) {
637+ payloadCapacity = maxCopyLen;
638+ }
639+ payloadCapacity = payloadCapacity / alignment * alignment;
640+ if (payloadCapacity == 0U) {
641+ return;
642+ }
643+ const uint64_t remaining = tensorLength - tensorOffset;
644+ const uint32_t dumpSize = remaining > payloadCapacity ? payloadCapacity : static_cast<uint32_t>(remaining);
645+ const uint32_t alignDumpLen = AlignUp(dumpSize, alignment);
646+ const uint32_t tlvLen = headerLen + alignDumpLen;
647+ const uint32_t reserveLen = tlvLen + (needBackup ? alignDumpLen : 0U);
648+ if (!__asc_aicore::check_ringbuf_space(blockInfo, reserveLen)) {
649+ return;
650+ }
651+ __gm__ uint8_t* tlvAddr = __asc_aicore::get_ringbuf_tlv_addr(blockInfo);
652+ if (isFirst) {
653+ auto* dumpTlv = reinterpret_cast<__gm__ __asc_aicore::DumpSuperTensorTlv*>(tlvAddr);
654+ WriteSuperTensorHead(src, dumpTlv, position, desc, tensorLength, dumpSize, alignDumpLen, shape, shapeDim);
655+ } else {
656+ auto* dumpTlv = reinterpret_cast<__gm__ __asc_aicore::DumpSuperTensorBodyTlv*>(tlvAddr);
657+ dumpTlv->type = static_cast<uint32_t>(__asc_aicore::DumpType::DUMP_SUPER_TENSOR_BODY);
658+ dumpTlv->length = tlvLen - sizeof(uint32_t[2]);
659+ dumpTlv->resv1 = 0U;
660+ dumpTlv->resv2 = 0U;
661+ dumpTlv->tensorLength = tensorLength;
662+ dumpTlv->tensorOffset = tensorOffset;
663+ dumpTlv->dumpSize = dumpSize;
664+ dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv), cache_line_t::ENTIRE_DATA_CACHE);
665+ }
666+ auto chunkSrc = src[static_cast<uint32_t>(tensorOffset / sizeof(T))];
667+ WriteSuperTensorData(chunkSrc, tlvAddr + headerLen, alignDumpLen, dumpSize);
668+ // Publish only the TLV, after the borrowed UB has been restored.
669+ __asc_aicore::update_write_info(__asc_aicore::get_block_write_info(blockInfo), tlvLen);
670+ tensorOffset += dumpSize;
671+ }
672+}
673+#endif
674+ 
558template <template <typename> class Tensor, typename T>675template <template <typename> class Tensor, typename T>
559__aicore__ inline void DumpTensorRingBufImpl(676__aicore__ inline void DumpTensorRingBufImpl(
560 const Tensor<T>& src, uint32_t desc, uint32_t dumpSize, const uint32_t* shape, const uint32_t shapeDim)677 const Tensor<T>& src, uint32_t desc, uint32_t dumpSize, const uint32_t* shape, const uint32_t shapeDim)
@@ -580,9 +697,19 @@ __aicore__ inline void DumpTensorRingBufImpl(
580 return;697 return;
581 }698 }
582 constexpr uint32_t blockSize = 16 * 16 * sizeof(uint32_t);699 constexpr uint32_t blockSize = 16 * 16 * sizeof(uint32_t);
583- uint32_t alignDumpDataLen = AlignUp(dumpSize * sizeof(T), ONE_BLK_SIZE);700+ const uint64_t tensorLength = static_cast<uint64_t>(dumpSize) * sizeof(T);
584- uint32_t tlvLen = sizeof(DumpTensorTlvInfoHead) + alignDumpDataLen;701+ const uint64_t alignDumpLen64 = (tensorLength + ONE_BLK_SIZE - 1U) / ONE_BLK_SIZE * ONE_BLK_SIZE;
585- uint32_t maxResvLen = sizeof(DumpTensorTlvInfoHead) + AlignUp(alignDumpDataLen, blockSize) * 2; // resv tmp space702+ const uint64_t maxResvLen64 =
703+ sizeof(DumpTensorTlvInfoHead) + (alignDumpLen64 + blockSize - 1U) / blockSize * blockSize * 2U;
704+ if (maxResvLen64 > blockRingBufInfo->ringBufLen) {
705+#ifndef ASCENDC_CPU_DEBUG
706+ DumpSuperTensorRingBufImpl(src, desc, tensorLength, shape, shapeDim, blockRingBufInfo);
707+#endif
708+ return;
709+ }
710+ const uint32_t alignDumpDataLen = static_cast<uint32_t>(alignDumpLen64);
711+ const uint32_t tlvLen = sizeof(DumpTensorTlvInfoHead) + alignDumpDataLen;
712+ const uint32_t maxResvLen = static_cast<uint32_t>(maxResvLen64);
586 if (!CheckAndWaitRingBufSpace(blockRingBufInfo, maxResvLen)) { // Reserved Backup Capacity713 if (!CheckAndWaitRingBufSpace(blockRingBufInfo, maxResvLen)) { // Reserved Backup Capacity
587 return;714 return;
588 }715 }
@@ -54,8 +54,8 @@ __aicore__ inline void set_dump_tlv_info(
54 asc_entire_dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv));54 asc_entire_dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv));
55}55}
56 56 
57-__aicore__ inline void set_dump_shape_info(57+template <typename Tlv>
58- __gm__ DumpTensorTlv* dumpTlv, const uint32_t shapeDim, const uint32_t* shape)58+__aicore__ inline void set_dump_shape_info(__gm__ Tlv* dumpTlv, const uint32_t shapeDim, const uint32_t* shape)
59{59{
60 if (shapeDim <= 0 || shapeDim > K_MAX_SHAPE_DIM || shape == nullptr) {60 if (shapeDim <= 0 || shapeDim > K_MAX_SHAPE_DIM || shape == nullptr) {
61 return;61 return;
@@ -67,9 +67,8 @@ __aicore__ inline void set_dump_shape_info(
67 asc_entire_dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv));67 asc_entire_dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv));
68}68}
69 69 
70-template <AscendC::Hardware hardware, typename T, typename U>70+template <AscendC::Hardware hardware, typename T, typename U, typename Tlv>
71-__aicore__ inline uint32_t set_dump_tlv_data(71+__aicore__ inline uint32_t set_dump_tlv_data(U src, __gm__ Tlv* dumpTlv, uint32_t alignDumpLen, uint32_t dump_size)
72- U src, __gm__ DumpTensorTlv* dumpTlv, uint32_t alignDumpLen, uint32_t dump_size)
73{72{
74 __gm__ T* dumpDstAddr = reinterpret_cast<__gm__ T*>(dumpTlv + 1);73 __gm__ T* dumpDstAddr = reinterpret_cast<__gm__ T*>(dumpTlv + 1);
75 74 
@@ -107,6 +106,118 @@ __aicore__ inline uint32_t set_dump_tlv_data(
107 return ret;106 return ret;
108}107}
109 108 
109+template <AscendC::Hardware hardware>
110+__aicore__ constexpr inline bool is_super_tensor_hardware_supported()
111+{
112+ return hardware == AscendC::Hardware::GM || hardware == AscendC::Hardware::UB ||
113+ hardware == AscendC::Hardware::L1 || hardware == AscendC::Hardware::L0C ||
114+ hardware == AscendC::Hardware::BIAS || hardware == AscendC::Hardware::FIXBUF;
115+}
116+ 
117+template <AscendC::Hardware hardware>
118+__aicore__ constexpr inline uint32_t get_super_tensor_payload_alignment()
119+{
120+ if constexpr (hardware == AscendC::Hardware::L0C) {
121+ return 16U * 16U * sizeof(uint32_t);
122+ }
123+ return ASC_ONE_DATABLOCK_SIZE;
124+}
125+ 
126+template <AscendC::Hardware hardware>
127+__aicore__ inline uint32_t get_super_tensor_payload_capacity(uint32_t ringBufLen, uint32_t headerLen)
128+{
129+ constexpr uint32_t payloadAlignment = get_super_tensor_payload_alignment<hardware>();
130+ if (ringBufLen <= headerLen) {
131+ return 0U;
132+ }
133+ return ((ringBufLen - headerLen) / payloadAlignment) * payloadAlignment;
134+}
135+ 
136+template <AscendC::Hardware hardware, typename T, typename U>
137+__aicore__ inline void set_super_tensor_tlv_info(
138+ U src, __gm__ DumpSuperTensorTlv* dumpTlv, uint32_t alignDumpLen, uint32_t desc, uint64_t tensorLength,
139+ uint32_t dumpSize, const uint32_t* shape, uint32_t shapeDim)
140+{
141+ dumpTlv->type = static_cast<uint32_t>(DumpType::DUMP_SUPER_TENSOR);
142+ dumpTlv->length = sizeof(DumpSuperTensorTlv) - sizeof(uint32_t[2]) + alignDumpLen;
143+ dumpTlv->tensorAddr = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(src));
144+ dumpTlv->dataType = static_cast<uint32_t>(get_dump_datatype<T>());
145+ dumpTlv->desc = desc;
146+ dumpTlv->bufferId = 0U;
147+ dumpTlv->position = static_cast<uint16_t>(hardware);
148+ dumpTlv->blockIdx = static_cast<uint16_t>(asc_debug_get_block_idx());
149+ dumpTlv->dim = 0U;
150+ for (uint32_t i = 0; i < K_MAX_SHAPE_DIM; ++i) {
151+ dumpTlv->shape[i] = 0U;
152+ }
153+ dumpTlv->resv = 0U;
154+ dumpTlv->tensorLength = tensorLength;
155+ dumpTlv->tensorOffset = 0U;
156+ dumpTlv->dumpSize = dumpSize;
157+ set_dump_shape_info(dumpTlv, shapeDim, shape);
158+ asc_entire_dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv));
159+}
160+ 
161+__aicore__ inline void set_super_tensor_body_tlv_info(
162+ __gm__ DumpSuperTensorBodyTlv* dumpTlv, uint32_t alignDumpLen, uint64_t tensorLength, uint64_t tensorOffset,
163+ uint32_t dumpSize)
164+{
165+ dumpTlv->type = static_cast<uint32_t>(DumpType::DUMP_SUPER_TENSOR_BODY);
166+ dumpTlv->length = sizeof(DumpSuperTensorBodyTlv) - sizeof(uint32_t[2]) + alignDumpLen;
167+ dumpTlv->resv1 = 0U;
168+ dumpTlv->resv2 = 0U;
169+ dumpTlv->tensorLength = tensorLength;
170+ dumpTlv->tensorOffset = tensorOffset;
171+ dumpTlv->dumpSize = dumpSize;
172+ asc_entire_dcci(reinterpret_cast<__gm__ uint64_t*>(dumpTlv));
173+}
174+ 
175+template <AscendC::Hardware hardware, typename T, typename U>
176+__aicore__ inline void asc_dump_super_tensor_impl(
177+ U src, uint32_t desc, uint64_t tensorLength, const uint32_t* shape, uint32_t shapeDim,
178+ __gm__ DebugBlockHeadInfo* blockInfo)
179+{
180+ constexpr uint32_t payloadAlignment = get_super_tensor_payload_alignment<hardware>();
181+ uint64_t tensorOffset = 0U;
182+ bool isFirst = true;
183+ while (tensorOffset < tensorLength) {
184+ const uint32_t headerLen = isFirst ? sizeof(DumpSuperTensorTlv) : sizeof(DumpSuperTensorBodyTlv);
185+ const uint32_t payloadCapacity = get_super_tensor_payload_capacity<hardware>(blockInfo->ringBufLen, headerLen);
186+ if (payloadCapacity == 0U) {
187+ return;
188+ }
189+ const uint64_t remaining = tensorLength - tensorOffset;
190+ const uint32_t dumpSize = remaining > payloadCapacity ? payloadCapacity : static_cast<uint32_t>(remaining);
191+ const uint32_t alignDumpLen = align_up(dumpSize, payloadAlignment);
192+ const uint32_t tlvLen = headerLen + alignDumpLen;
193+ if (!check_ringbuf_space(blockInfo, tlvLen)) {
194+ return;
195+ }
196+ 
197+ __gm__ uint8_t* tlvAddr = get_ringbuf_tlv_addr(blockInfo);
198+ U chunkSrc = src + tensorOffset / sizeof(T);
199+ if (isFirst) {
200+ auto* dumpTlv = reinterpret_cast<__gm__ DumpSuperTensorTlv*>(tlvAddr);
201+ set_super_tensor_tlv_info<hardware, T>(
202+ src, dumpTlv, alignDumpLen, desc, tensorLength, dumpSize, shape, shapeDim);
203+ if (set_dump_tlv_data<hardware, T>(chunkSrc, dumpTlv, alignDumpLen, dumpSize / sizeof(T)) != 0U) {
204+ return;
205+ }
206+ } else {
207+ auto* dumpTlv = reinterpret_cast<__gm__ DumpSuperTensorBodyTlv*>(tlvAddr);
208+ set_super_tensor_body_tlv_info(dumpTlv, alignDumpLen, tensorLength, tensorOffset, dumpSize);
209+ if (set_dump_tlv_data<hardware, T>(chunkSrc, dumpTlv, alignDumpLen, dumpSize / sizeof(T)) != 0U) {
210+ return;
211+ }
212+ }
213+ 
214+ __gm__ DebugBlockWriteInfo* writeInfo = get_block_write_info(blockInfo);
215+ update_write_info(writeInfo, tlvLen);
216+ tensorOffset += dumpSize;
217+ isFirst = false;
218+ }
219+}
220+ 
110template <AscendC::Hardware hardware, typename T, typename U>221template <AscendC::Hardware hardware, typename T, typename U>
111__aicore__ inline void asc_dump_impl(222__aicore__ inline void asc_dump_impl(
112 U src, uint32_t desc, uint32_t dump_size, const uint32_t* shape, const uint32_t shapeDim)223 U src, uint32_t desc, uint32_t dump_size, const uint32_t* shape, const uint32_t shapeDim)
@@ -116,9 +227,20 @@ __aicore__ inline void asc_dump_impl(
116 if (dump_size <= 0 || blockInfo == nullptr) {227 if (dump_size <= 0 || blockInfo == nullptr) {
117 return;228 return;
118 }229 }
119- constexpr uint16_t dataBlockSize = 32;230+ constexpr uint32_t dataBlockSize = 32U;
120- uint32_t alignDumpLen = align_up(dump_size * sizeof(T), dataBlockSize);231+ const uint64_t tensorLength = static_cast<uint64_t>(dump_size) * sizeof(T);
121- uint32_t tlvLen = sizeof(DumpTensorTlv) + alignDumpLen;232+ const uint64_t alignDumpLen64 = ((tensorLength + dataBlockSize - 1U) / dataBlockSize) * dataBlockSize;
233+ const uint64_t tlvLen64 = sizeof(DumpTensorTlv) + alignDumpLen64;
234+ if (tlvLen64 > blockInfo->ringBufLen) {
235+#if __NPU_ARCH__ == 3510 || __NPU_ARCH__ == 2201 || __NPU_ARCH__ == 2002 || __NPU_ARCH__ == 5102
236+ if constexpr (is_super_tensor_hardware_supported<hardware>()) {
237+ asc_dump_super_tensor_impl<hardware, T>(src, desc, tensorLength, shape, shapeDim, blockInfo);
238+ }
239+#endif
240+ return;
241+ }
242+ const uint32_t alignDumpLen = static_cast<uint32_t>(alignDumpLen64);
243+ const uint32_t tlvLen = static_cast<uint32_t>(tlvLen64);
122 if (!check_ringbuf_space(blockInfo, tlvLen)) {244 if (!check_ringbuf_space(blockInfo, tlvLen)) {
123 return;245 return;
124 }246 }
@@ -113,9 +113,14 @@ enum class DumpType : uint8_t {
113 DUMP_SIMT,113 DUMP_SIMT,
114 DUMP_BUFI,114 DUMP_BUFI,
115 DUMP_BUFO,115 DUMP_BUFO,
116- DUMP_SKIP116+ DUMP_SKIP,
117+ DUMP_SUPER_TENSOR,
118+ DUMP_SUPER_TENSOR_BODY
117};119};
118 120 
121+static_assert(static_cast<uint32_t>(DumpType::DUMP_SUPER_TENSOR) == 11U, "unexpected super tensor dump type");
122+static_assert(static_cast<uint32_t>(DumpType::DUMP_SUPER_TENSOR_BODY) == 12U, "unexpected super tensor body dump type");
123+ 
119struct DebugBlockHeadInfo {124struct DebugBlockHeadInfo {
120 uint32_t length = 0U; // total size per block (include head and r/w info)125 uint32_t length = 0U; // total size per block (include head and r/w info)
121 uint32_t coreId = 0U; // current core id126 uint32_t coreId = 0U; // current core id
@@ -180,6 +185,33 @@ struct DumpShapeTlv {
180 uint32_t resv;185 uint32_t resv;
181};186};
182 187 
188+struct DumpSuperTensorTlv {
189+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_SUPER_TENSOR);
190+ uint32_t length = 0U;
191+ uint32_t tensorAddr = 0U;
192+ uint32_t dataType = 0U;
193+ uint32_t desc = 0U;
194+ uint32_t bufferId = 0U;
195+ uint16_t position = 0U;
196+ uint16_t blockIdx = 0U;
197+ uint32_t dim = 0U;
198+ uint32_t shape[8];
199+ uint32_t resv = 0U;
200+ uint64_t tensorLength = 0U;
201+ uint64_t tensorOffset = 0U;
202+ uint32_t dumpSize = 0U;
203+};
204+ 
205+struct DumpSuperTensorBodyTlv {
206+ uint32_t type = static_cast<uint32_t>(DumpType::DUMP_SUPER_TENSOR_BODY);
207+ uint32_t length = 0U;
208+ uint32_t resv1 = 0U;
209+ uint32_t resv2 = 0U;
210+ uint64_t tensorLength = 0U;
211+ uint64_t tensorOffset = 0U;
212+ uint32_t dumpSize = 0U;
213+};
214+ 
183struct TimeStampTlv {215struct TimeStampTlv {
184 uint32_t type = static_cast<uint32_t>(DumpType::DUMP_TIME_STAMP); // DumpType = DUMP_TIME_STAMP216 uint32_t type = static_cast<uint32_t>(DumpType::DUMP_TIME_STAMP); // DumpType = DUMP_TIME_STAMP
185 uint32_t length = 0U; // Length of (descId resv cycle pc entry)217 uint32_t length = 0U; // Length of (descId resv cycle pc entry)
@@ -0,0 +1,46 @@
1+/**
2+ * Copyright (c) 2026 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+#if __NPU_ARCH__ == 3510 && !defined(ASCENDC_CPU_DEBUG)
12+#include "impl/utils/debug/asc_aicore_dump_impl.h"
13+ 
14+using AscendC::Hardware;
15+ 
16+__aicore__ inline void CheckSuperTensorDumpInstantiation(__gm__ __asc_aicore::DebugBlockHeadInfo* blockInfo)
17+{
18+ __asc_aicore::asc_dump_super_tensor_impl<Hardware::GM, uint32_t>(
19+ static_cast<__gm__ uint32_t*>(nullptr), 0, 64, nullptr, 0, blockInfo);
20+#if defined(__DAV_VEC__)
21+ __asc_aicore::asc_dump_super_tensor_impl<Hardware::UB, uint32_t>(
22+ static_cast<__ubuf__ uint32_t*>(nullptr), 0, 64, nullptr, 0, blockInfo);
23+#elif defined(__DAV_CUBE__)
24+ __asc_aicore::asc_dump_super_tensor_impl<Hardware::L1, uint32_t>(
25+ static_cast<__cbuf__ uint32_t*>(nullptr), 0, 64, nullptr, 0, blockInfo);
26+ __asc_aicore::asc_dump_super_tensor_impl<Hardware::L0C, uint32_t>(
27+ static_cast<__cc__ uint32_t*>(nullptr), 0, 1024, nullptr, 0, blockInfo);
28+ __asc_aicore::asc_dump_super_tensor_impl<Hardware::BIAS, uint32_t>(
29+ static_cast<__biasbuf__ uint32_t*>(nullptr), 0, 64, nullptr, 0, blockInfo);
30+ __asc_aicore::asc_dump_super_tensor_impl<Hardware::FIXBUF, uint32_t>(
31+ static_cast<__fbuf__ uint32_t*>(nullptr), 0, 64, nullptr, 0, blockInfo);
32+#endif
33+}
34+#endif
35+ 
36+#if __NPU_ARCH__ == 2002 && !defined(ASCENDC_CPU_DEBUG)
37+#include "kernel_operator.h"
38+ 
39+__aicore__ inline void CheckSuperTensorDump2002Instantiation(
40+ const AscendC::GlobalTensor<uint32_t>& gm, const AscendC::LocalTensor<uint32_t>& local,
41+ __gm__ AscendC::BlockRingBufInfo* blockInfo)
42+{
43+ AscendC::DumpSuperTensorRingBufImpl(gm, 0, 8192, nullptr, 0, blockInfo);
44+ AscendC::DumpSuperTensorRingBufImpl(local, 0, 8192, nullptr, 0, blockInfo);
45+}
46+#endif
@@ -0,0 +1,215 @@
1+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3+# CANN Open Software License Agreement Version 2.0 (the "License").
4+# Please refer to the License for details. You may not use this file except in compliance with the License.
5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7+# See LICENSE in the root of the software repository for the full text of the License.
8+ 
9+"""Host regression for the actual 2002 sender, with device copies/FIFO consumption simulated.
10+ 
11+Run: python3 tests/api/basic_api/dump_super_tensor/test_2002_sender.py
12+Requires a C++17 compiler. This does not replace device compilation or board tests.
13+"""
14+ 
15+from pathlib import Path
16+import os
17+import subprocess
18+import tempfile
19+ 
20+ROOT = Path(__file__).resolve().parents[4]
21+SOURCE = ROOT / "impl/basic_api/dav_m200/kernel_operator_dump_tensor_impl.h"
22+ 
23+ 
24+def section(text, start, end):
25+ assert start in text, f"Missing sender implementation: {start}"
26+ return text[text.index(start) : text.index(end, text.index(start))]
27+ 
28+ 
29+def test_2002_sender():
30+ source = SOURCE.read_text()
31+ types = (ROOT / "impl/utils/debug/asc_debug_types.h").read_text()
32+ structs = section(types, "struct DebugBlockHeadInfo {", "struct TimeStampTlv {")
33+ copies = section(
34+ source,
35+ "__aicore__ inline void ClearGmData(",
36+ "template <typename T>\n__aicore__ inline void WriteRingBufTlvData(",
37+ )
38+ sender = section(source, "template <typename T>\n__aicore__ inline void WriteSuperTensorData(", "\n#endif")
39+ entry = section(
40+ source,
41+ "template <template <typename> class Tensor, typename T>\n__aicore__ inline void DumpTensorRingBufImpl(\n const Tensor<T>& src, uint32_t desc, uint32_t dumpSize, const uint32_t* shape, const uint32_t shapeDim)\n{",
42+ "__aicore__ inline void WriteRingBufShapeInfo(const ShapeInfo& shapeInfo)\n{",
43+ )
44+ code = PRELUDE + structs + STUBS + copies + sender + entry + MAIN
45+ with tempfile.TemporaryDirectory(prefix="dump-2002-") as directory:
46+ cpp = Path(directory) / "sender.cpp"
47+ binary = Path(directory) / "sender"
48+ cpp.write_text(code)
49+ subprocess.run(
50+ [
51+ os.environ.get("CXX", "g++"),
52+ "-std=c++17",
53+ "-O1",
54+ "-g",
55+ "-fsanitize=address,undefined",
56+ "-fno-omit-frame-pointer",
57+ str(cpp),
58+ "-o",
59+ str(binary),
60+ ],
61+ check=True,
62+ )
63+ subprocess.run([str(binary)], check=True, env={**os.environ, "ASAN_OPTIONS": "detect_leaks=0"})
64+ 
65+ 
66+PRELUDE = r"""
67+#include <algorithm>
68+#include <cassert>
69+#include <cstdint>
70+#include <cstring>
71+#include <iostream>
72+#include <type_traits>
73+#include <vector>
74+#define __aicore__
75+#define __gm__
76+#define __ubuf__
77+#define __cbuf__
78+#define __cc__
79+#define ASCENDC_ASSERT(x, ...) assert(x)
80+constexpr uint32_t ONE_BLK_SIZE=32, ONE_DUMP_BACKUP_SIZE=1024, K_MAX_SHAPE_DIM=8;
81+constexpr int PIPE_ALL=0;
82+enum class Hardware {GM, UB, L1, L0C, MAX};
83+enum class TPosition {VECIN};
84+enum class DumpType {DUMP_BUFI=1,DUMP_BUFO=2,DUMP_SKIP=3,DUMP_SCALAR=4,DUMP_TENSOR=5,DUMP_SHAPE=6,
85+ DUMP_SUPER_TENSOR=11,DUMP_SUPER_TENSOR_BODY=12};
86+namespace Internal {enum class DumpTensorDataType {ACL_UINT32=4, ACL_MAX=99};}
87+namespace __asc_aicore {
88+using ::DumpType;
89+"""
90+STUBS = r"""
91+}
92+using BlockRingBufInfo=__asc_aicore::DebugBlockHeadInfo;
93+using RingBufWriteInfo=__asc_aicore::DebugBlockWriteInfo;
94+using RingBufReadInfo=__asc_aicore::DebugBlockReadInfo;
95+using DumpTensorTlvInfoHead=__asc_aicore::DumpTensorTlv;
96+namespace cache_line_t {constexpr int ENTIRE_DATA_CACHE=0;}
97+void dcci(uint64_t*,int) {}
98+template<int> void PipeBarrier() {}
99+template<class A,class B> using IsSameType=std::is_same<A,B>;
100+template<class T> constexpr auto GetTensorDataType(){return Internal::DumpTensorDataType::ACL_UINT32;}
101+uint32_t AlignUp(uint32_t n,uint32_t a){return (n+a-1)/a*a;}
102+unsigned GetBlockIdxImpl(){return 7;}
103+void EnablePrintf(){}
104+uint8_t ub[1024];
105+template<class T> struct GlobalTensor {
106+ T* ptr; T* GetPhyAddr() const{return ptr;}
107+ GlobalTensor operator[](uint64_t n) const{return {ptr+n};}
108+};
109+template<class T> struct LocalTensor {
110+ T* ptr=nullptr; Hardware pos=Hardware::UB;
111+ T* GetPhyAddr() const{return ptr;} Hardware GetPosition()const{return pos;}
112+ LocalTensor operator[](uint32_t n)const{return {ptr+n,pos};}
113+};
114+Hardware GetPhyType(Hardware p){return p;}
115+template<class T> Hardware CheckDumpTensorPosition(const LocalTensor<T>& s){return s.pos;}
116+template<class T> void InitTmpTensor(LocalTensor<T>& s,uint8_t){s.ptr=reinterpret_cast<T*>(ub);}
117+struct DataCopyParams {uint16_t blockCount=1,blockLen=0,srcStride=0,dstStride=0;};
118+enum class BlockMode {BLOCK_MODE_MATRIX};
119+struct DataCopyEnhancedParams {BlockMode blockMode;};
120+template<class A,class B> void DataCopyUB2GMImpl(A* d,B* s,DataCopyParams p){memcpy(d,s,p.blockLen*32);}
121+template<class A,class B> void DataCopyGM2UBImpl(A* d,B* s,DataCopyParams p){memcpy(d,s,p.blockLen*32);}
122+template<class A,class B> void DataCopyL12UBImpl(A* d,B* s,DataCopyParams p){memcpy(d,s,p.blockLen*32);}
123+template<class A,class B> void DataCopyL0C2UBImpl(A* d,B* s,DataCopyParams p,DataCopyEnhancedParams){memcpy(d,s,p.blockLen*1024);}
124+void MemCopyGm2Gm(uint8_t* d,const uint8_t* s,const uint32_t& n){memcpy(d,s,n);}
125+BlockRingBufInfo block;
126+RingBufWriteInfo writer;
127+std::vector<uint8_t> ring, collected;
128+unsigned reserved=0,commits=0,failAfter=~0u,normalCalls=0,wraps=0;
129+BlockRingBufInfo* GetBlockRingBufInfo(){return &block;}
130+namespace __asc_aicore {
131+uint32_t align_up(uint32_t n,uint32_t a){return AlignUp(n,a);}
132+void asc_entire_dcci(uint64_t*){}
133+bool check_ringbuf_space(DebugBlockHeadInfo* b,const uint32_t& n){
134+ assert(n<=b->ringBufLen);
135+ if(commits==failAfter)return false;
136+ if(writer.bufOffset+n>b->ringBufLen){writer.bufOffset=0;++wraps;}
137+ reserved=n;
138+ std::fill(ring.begin()+writer.bufOffset+n,ring.end(),0xA5);
139+ return true;
140+}
141+uint8_t* get_ringbuf_tlv_addr(DebugBlockHeadInfo*){return ring.data()+writer.bufOffset;}
142+DebugBlockWriteInfo* get_block_write_info(DebugBlockHeadInfo*){return &writer;}
143+void update_write_info(DebugBlockWriteInfo*,const uint32_t& n){
144+ assert(n<=reserved);
145+ for(size_t i=writer.bufOffset+reserved;i<ring.size();++i)assert(ring[i]==0xA5);
146+ collected.insert(collected.end(),ring.data()+writer.bufOffset,ring.data()+writer.bufOffset+n);
147+ writer.bufOffset+=n;++writer.packIdx;++commits;
148+}
149+}
150+bool CheckAndWaitRingBufSpace(BlockRingBufInfo*,const uint32_t& n){++normalCalls;return n<=block.ringBufLen;}
151+uint8_t* GetRingBufTlv(BlockRingBufInfo*){return ring.data();}
152+RingBufWriteInfo* GetRingBufWriteInfo(BlockRingBufInfo*){return &writer;}
153+void UpdateWriteInfo(RingBufWriteInfo*,const uint32_t&){}
154+template<class... A>void WriteRingBufTlvHead(A...){}
155+template<class... A>void WriteRingBufTlvShape(A...){}
156+template<class... A>void WriteRingBufTlvData(A...){}
157+"""
158+MAIN = r"""
159+void reset(unsigned size){
160+ block.ringBufLen=size;ring.assign(size+4096,0xA5);collected.clear();writer={};
161+ reserved=commits=normalCalls=wraps=0;failAfter=~0u;
162+ std::fill(std::begin(ub),std::end(ub),0x71);
163+}
164+void verify(const std::vector<uint32_t>& src,uint32_t count,Hardware pos){
165+ size_t cursor=0;uint64_t offset=0;std::vector<uint8_t> result;
166+ while(cursor<collected.size()){
167+ uint32_t header,size,span;uint64_t total,off;
168+ if(offset==0){
169+ __asc_aicore::DumpSuperTensorTlv h;memcpy(&h,&collected[cursor],sizeof(h));
170+ assert(h.type==11&&h.desc==17&&h.position==static_cast<uint16_t>(pos)&&h.blockIdx==7);
171+ assert(h.dim==8&&h.shape[0]==count&&h.shape[7]==1);
172+ assert(h.tensorAddr==static_cast<uint32_t>(reinterpret_cast<uintptr_t>(src.data())));
173+ assert(h.dataType==4);
174+ header=sizeof(h);size=h.dumpSize;span=h.length+8;total=h.tensorLength;off=h.tensorOffset;
175+ }else{
176+ __asc_aicore::DumpSuperTensorBodyTlv h;memcpy(&h,&collected[cursor],sizeof(h));
177+ assert(h.type==12);header=sizeof(h);size=h.dumpSize;span=h.length+8;total=h.tensorLength;off=h.tensorOffset;
178+ }
179+ assert(total==uint64_t(count)*4&&off==offset&&size>0&&header+size<=span);
180+ result.insert(result.end(),collected.begin()+cursor+header,collected.begin()+cursor+header+size);
181+ offset+=size;cursor+=span;
182+ }
183+ assert(offset==uint64_t(count)*4&&memcmp(result.data(),src.data(),offset)==0);
184+ for(auto b:ub)assert(b==0x71);
185+}
186+int main(){
187+ for(auto pos:{Hardware::GM,Hardware::UB,Hardware::L1,Hardware::L0C}){
188+ for(unsigned count:{1025u,4097u,16385u})for(unsigned ringSize:{4096u,8192u}){
189+ reset(ringSize);std::vector<uint32_t> src(AlignUp(count*4,1024)/4);
190+ for(unsigned i=0;i<src.size();++i)src[i]=i*1234567u;
191+ uint32_t shape[8]={count,1,1,1,1,1,1,1};
192+ if(pos==Hardware::GM)DumpTensorRingBufImpl(GlobalTensor<uint32_t>{src.data()},17,count,shape,8);
193+ else DumpTensorRingBufImpl(LocalTensor<uint32_t>{src.data(),pos},17,count,shape,8);
194+ assert(commits>0);verify(src,count,pos);
195+ if(count*4>ringSize*2)assert(wraps>0);
196+ }
197+ }
198+ reset(4096);std::vector<uint32_t> src(4096,42);failAfter=1;
199+ DumpTensorRingBufImpl(GlobalTensor<uint32_t>{src.data()},17,4096,nullptr,0);assert(commits==1);
200+ reset(4096);failAfter=1;
201+ DumpTensorRingBufImpl(GlobalTensor<uint32_t>{src.data()},17,0x40000001u,nullptr,0);
202+ assert(commits==1);__asc_aicore::DumpSuperTensorTlv h;
203+ memcpy(&h,collected.data(),sizeof(h));assert(h.tensorLength==0x100000004ULL);
204+ reset(4096);DumpTensorRingBufImpl(GlobalTensor<uint32_t>{src.data()},17,16,nullptr,0);
205+ assert(normalCalls==1&&commits==0);
206+ reset(4096);DumpTensorRingBufImpl(GlobalTensor<uint32_t>{src.data()},17,0,nullptr,0);
207+ assert(normalCalls==0&&commits==0);
208+ reset(96);DumpTensorRingBufImpl(GlobalTensor<uint32_t>{src.data()},17,4096,nullptr,0);assert(commits==0);
209+ reset(2048);DumpTensorRingBufImpl(LocalTensor<uint32_t>{src.data(),Hardware::L0C},17,4096,nullptr,0);assert(commits==0);
210+ std::cout<<"2002 sender: 24 round trips, wrap, timeout, >4GiB, ordinary/zero/tiny FIFO passed\n";
211+}
212+"""
213+ 
214+if __name__ == "__main__":
215+ test_2002_sender()