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 : #ifndef DUMP_ARGS_H
11 : #define DUMP_ARGS_H
12 :
13 : #include <string>
14 : #include <vector>
15 : #include <sstream>
16 : #include "adump_pub.h"
17 : #include "adump_api.h"
18 : #include "dump_operator.h"
19 : #include "runtime/rt.h"
20 : #include "kernel_info_collector.h"
21 : #include "log/adx_log.h"
22 : #include "adx_exception_callback.h"
23 :
24 : namespace Adx {
25 : #define IDE_CHECK_RET(ret, action) \
26 : do { \
27 : if ((ret) != ADUMP_SUCCESS) { \
28 : action; \
29 : } \
30 : } while (0)
31 :
32 : constexpr uint16_t TYPE_L0_EXCEPTION_DFX = 4U;
33 : constexpr uint16_t TYPE_L0_EXCEPTION_DFX_ARGS_INFO = 5U;
34 : constexpr uint16_t TYPE_L0_EXCEPTION_DFX_IS_TIK = 6U;
35 : constexpr uint64_t TENSOR_TYPE_MASK = 0x0FFFF; // 0~15 bit
36 : constexpr uint64_t POINTER_TYPE_MASK = 0x0FFFF0000; // 16~31 bit
37 : constexpr uint64_t TENSOR_COUNT_MASK = 0xFFFFFFFF00000000; // high 32 bits
38 : constexpr uint64_t TENSOR_DIMENSION_MASK = 0x0FFFFFFFF; // low 32 bits
39 : constexpr uint32_t TENSOR_COUNT_SHIFT_BITS = 32;
40 : constexpr uint32_t POINTER_TYPE_SHIFT_BITS = 16;
41 : constexpr int32_t ELF_DATA2MSB = 2;
42 : constexpr uint32_t BITS_PER_BYTE = 8U;
43 :
44 : enum class DfxPointerType : uint16_t {
45 : INVALID_POINTER = 0,
46 : LEVEL_1_POINTER = 1,
47 : LEVEL_2_POINTER,
48 : LEVEL_2_POINTER_WITH_SHAPE,
49 : SHAPE_TENSOR_PLACEHOLD
50 : };
51 :
52 : struct InputBuffer {
53 125 : InputBuffer(const void* argAddr, uint64_t len, uint32_t index) : addr(argAddr), length(len), argIndex(index) {}
54 : const void* addr;
55 : uint64_t length;
56 : uint32_t argIndex;
57 : };
58 :
59 : struct TensorBuffer {
60 50 : TensorBuffer(const void* argAddr, uint32_t index, DfxTensorType dfxTensorType, DfxPointerType dfxPointerType)
61 50 : : addr(argAddr), argIndex(index), tensorType(dfxTensorType), pointerType(dfxPointerType)
62 50 : {}
63 : const void* addr{nullptr};
64 : uint64_t size{0U};
65 : uint64_t dataTypeSize{1U}; // 无data type size情况下,默认值为1,size为实际内存大小
66 : bool isDataTypeSizeByte{true}; // 标记dataTypeSize以bit还是byte为单位
67 : uint64_t dimension{0U};
68 : std::vector<uint64_t> shape;
69 : uint32_t argIndex;
70 : DfxTensorType tensorType;
71 : DfxPointerType pointerType;
72 42 : uint64_t GetTotalByteSize() const
73 : {
74 42 : if (isDataTypeSizeByte) {
75 34 : return dataTypeSize * size;
76 : }
77 8 : uint64_t totalBits = dataTypeSize * size;
78 8 : uint32_t remainder = totalBits % BITS_PER_BYTE == 0 ? 0 : 1; // 向上取整
79 8 : return totalBits / BITS_PER_BYTE + remainder;
80 : }
81 : };
82 :
83 : extern uint64_t g_chunk[RING_CHUNK_SIZE + MAX_TENSOR_NUM];
84 :
85 : class DumpArgs {
86 : public:
87 68 : DumpArgs()
88 136 : : sizeInfo_(nullptr),
89 68 : sizeBeginIndex_(0),
90 68 : skipNum_(0),
91 68 : inputNum_(0),
92 68 : argAddr_(nullptr),
93 68 : argSize_(0),
94 68 : dumpWithDfxFlag_(false),
95 68 : isTik_(false),
96 68 : exceptionDfxPtr_(nullptr),
97 68 : exceptionDfxSize_(0),
98 68 : kernelCollector_(std::make_shared<KernelInfoCollector>()){};
99 68 : ~DumpArgs() = default;
100 : int32_t LoadArgsExceptionInfo(const rtExceptionInfo& exception);
101 : int32_t DumpArgsExceptionInfo(const uint32_t deviceId, const std::string& dumpPath);
102 :
103 : bool DumpArgsDumpWithDfxFlag() const;
104 : const std::vector<InputBuffer>& DumpArgsGetInputBuffer() const;
105 : const std::vector<TensorBuffer>& DumpArgsGetTensorBuffer() const;
106 : const std::vector<DumpWorkspace>& DumpArgsGetWorkSpace() const;
107 :
108 : private:
109 : int32_t DumpArgsExceptionFile(const uint32_t deviceId, const std::string& dumpPath);
110 : std::string GetDumpFilePath(const std::string& dumpPath) const;
111 : int32_t CheckParam(
112 : const rtExceptionArgsInfo_t& exceptionArgsInfo, const rtExceptionExpandInfo_t& exceptionExpandInfo) const;
113 : int32_t InitAttributes(
114 : const rtExceptionArgsInfo_t& exceptionArgsInfo, const rtExceptionExpandInfo_t& exceptionExpandInfo);
115 : int32_t InitTensorModeInfo(const uint8_t* exceptionDfxPtr);
116 : int32_t InitTensorModeInfoInner(const uint8_t* exceptionDfxPtr, uint64_t& currDfxSize, uint32_t currArgsIndex);
117 : int32_t LoadArgsInfoWithDfx(const rtExceptionArgsInfo_t& exceptionArgsInfo);
118 : int32_t LoadArgsInfoWithSizeInfo(
119 : const rtExceptionArgsInfo_t& exceptionArgsInfo, const rtExceptionExpandInfo_t& exceptionExpandInfo);
120 : void LogArgsInfo(const void** argOnHost, uint32_t maxArgNum);
121 : int32_t LoadInputBuffer(const void** argOnHost, const uint32_t argIndex, uint64_t& sizeInfoIdx);
122 : int32_t LoadPointerTensor(const void** argOnHost, const uint32_t argIndex, uint64_t& sizeInfoIdx);
123 : void LoadTilingData();
124 :
125 : template <typename T>
126 : int32_t GetPointerValueByBigEndian(
127 : const uint8_t** ptr, T& value, uint64_t& currentDfxSize, uint16_t totalDfxSize) const;
128 : template <typename T>
129 : int32_t GetPointerValueByLittleEndian(
130 : const uint8_t** ptr, T& value, uint64_t& currentDfxSize, uint16_t totalDfxSize) const;
131 : int32_t FindExceptionDfx(const rtExceptionArgsInfo_t& exceptionArgsInfo);
132 : int32_t CheckAddressOverArgs(const uint64_t* address, const void** argOnHost, uint64_t maxArgNum) const;
133 : int32_t GetAddressBias(uint64_t& addrBias, const void* argAddr, void* baseAddr, uint64_t argsSize) const;
134 : void RecordCurrentLog();
135 : std::string taskId_;
136 : std::string streamId_;
137 : std::vector<InputBuffer> inputBuffer_;
138 : std::vector<InputBuffer> tilingData_;
139 : std::vector<TensorBuffer> tensorBuffer_;
140 : std::vector<DumpWorkspace> workspace_;
141 : std::vector<DumpWorkspace> mc2Space_;
142 : uint64_t* sizeInfo_;
143 : uint32_t sizeBeginIndex_;
144 : uint32_t skipNum_;
145 : uint32_t inputNum_;
146 : void* argAddr_;
147 : uint64_t argSize_;
148 : bool dumpWithDfxFlag_;
149 : bool isTik_;
150 : const uint8_t* exceptionDfxPtr_;
151 : uint16_t exceptionDfxSize_;
152 : std::shared_ptr<KernelInfoCollector> kernelCollector_;
153 : std::vector<std::string> logRecord_;
154 : std::ostringstream oss_;
155 : };
156 : } // namespace Adx
157 : #endif
|