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_log.h
13 : * \brief
14 : */
15 : #ifndef ASCENDC_MODULE_KERNEL_LOG_INTF_H
16 : #define ASCENDC_MODULE_KERNEL_LOG_INTF_H
17 : #if defined(ASCENDC_CPU_DEBUG) && ASCENDC_CPU_DEBUG == 1
18 : #include <string>
19 : #include <map>
20 : #include <csignal>
21 : #include <cstdio>
22 : #include <cstdint>
23 : #include <unistd.h>
24 : #include "stub_def.h"
25 :
26 : namespace AscendC {
27 : #ifndef __NPU_DEVICE__
28 :
29 : #if defined(UT_TEST) || defined(ST_TEST)
30 : #define ASCENDC_ASSERT(cond, behavior) \
31 : do { \
32 : if (!(cond)) { \
33 : behavior; \
34 : AscendC::KernelRaise::GetInstance().Raise(SIGABRT); \
35 : } \
36 : } while (0)
37 : #else
38 : #define ASCENDC_ASSERT(cond, behavior) \
39 : do { \
40 : if (!(cond)) { \
41 : behavior; \
42 : raise(SIGABRT); \
43 : } \
44 : } while (0)
45 : #endif
46 :
47 : #else // #ifdef __NPU_DEVICE__
48 :
49 : #ifndef ASCC_ASCENDC_ASSERT
50 : #define ASCC_ASCENDC_ASSERT
51 : #define ASCENDC_ASSERT(cond, behavior)
52 : #endif
53 :
54 : #endif // __NPU_DEVICE__
55 :
56 : // Check value in range [low, high]
57 : #define ASCENDC_CHECK_VALUE_RANGE(value, rangeLow, rangeHigh, paramName, apiMsg) \
58 : do { \
59 : if (value < rangeLow || value > rangeHigh) { \
60 : KERNEL_LOG( \
61 : KERNEL_ERROR, \
62 : "Failed to check %s value in %s, its valid range is " \
63 : "%s ~ %s, current value is %s.", \
64 : paramName, apiMsg, std::to_string(rangeLow).c_str(), std::to_string(rangeHigh).c_str(), \
65 : std::to_string(value).c_str()); \
66 : raise(SIGABRT); \
67 : } \
68 : } while (0)
69 :
70 : enum class TPosition : uint8_t;
71 :
72 : template <TPosition pos>
73 : __aicore__ inline uint64_t TransUBAddr(uint64_t addr);
74 :
75 : // Check tensor tposition with condition judgement
76 : #define ASCENDC_CHECK_TPOSITION(cond, tensorName, tPosName, apiMsg, curPos) \
77 : do { \
78 : if (!(cond)) { \
79 : KERNEL_LOG( \
80 : KERNEL_ERROR, \
81 : "Failed to check %s tensor position in %s, supported positions " \
82 : "are %s, current position is %s.", \
83 : tensorName, apiMsg, tPosName, curPos.c_str()); \
84 : raise(SIGABRT); \
85 : } \
86 : } while (0)
87 :
88 : enum KernelFuncType : uint8_t {
89 : NONE_MODE,
90 : MASK_COUNT_MODE, // mask
91 : MASK_BIT_MODE, // mask[]
92 : CALCOUNT_MODE // calcount
93 : };
94 :
95 : enum class LogLevel : uint8_t {
96 : KERNEL_DEBUG = 0,
97 : KERNEL_INFO = 1,
98 : KERNEL_WARN = 2,
99 : KERNEL_ERROR = 3,
100 : };
101 : } // namespace AscendC
102 :
103 : #define KERNEL_LOG(level, format, ...) KERNEL_LOG_##level(format, ##__VA_ARGS__)
104 :
105 : #if __NPU_ARCH__ == 2201
106 :
107 : namespace AscendC {
108 0 : inline std::string GenCoreTypeStr()
109 : {
110 0 : std::string coreTypeStr = "";
111 0 : if (g_coreType == AscendC::AIC_TYPE) {
112 0 : coreTypeStr = "AIC_";
113 0 : } else if (g_coreType == AscendC::AIV_TYPE) {
114 0 : coreTypeStr = "AIV_";
115 : } else {
116 0 : coreTypeStr = "MIX_";
117 : }
118 0 : coreTypeStr += std::to_string(sub_block_idx);
119 0 : return coreTypeStr;
120 0 : }
121 :
122 0 : inline std::string GenBlockStr()
123 : {
124 0 : std::string blockStr = "Block_";
125 0 : blockStr += std::to_string(block_idx);
126 0 : return blockStr;
127 0 : }
128 : } // namespace AscendC
129 :
130 : #define KERNEL_LOG_KERNEL_DEBUG(format, ...) \
131 : do { \
132 : std::string coreTypeStr = AscendC::GenCoreTypeStr(); \
133 : std::string blockStr = AscendC::GenBlockStr(); \
134 : printf( \
135 : "[DEBUG][%s][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), coreTypeStr.c_str(), __FILE__, __LINE__, \
136 : __FUNCTION__, static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
137 : } while (0)
138 :
139 : #define KERNEL_LOG_KERNEL_INFO(format, ...) \
140 : do { \
141 : std::string coreTypeStr = AscendC::GenCoreTypeStr(); \
142 : std::string blockStr = AscendC::GenBlockStr(); \
143 : printf( \
144 : "[INFO][%s][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), coreTypeStr.c_str(), __FILE__, __LINE__, \
145 : __FUNCTION__, static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
146 : } while (0)
147 :
148 : #define KERNEL_LOG_KERNEL_WARN(format, ...) \
149 : do { \
150 : std::string coreTypeStr = AscendC::GenCoreTypeStr(); \
151 : std::string blockStr = AscendC::GenBlockStr(); \
152 : printf( \
153 : "[WARN][%s][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), coreTypeStr.c_str(), __FILE__, __LINE__, \
154 : __FUNCTION__, static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
155 : } while (0)
156 :
157 : #define KERNEL_LOG_KERNEL_ERROR(format, ...) \
158 : do { \
159 : std::string coreTypeStr = AscendC::GenCoreTypeStr(); \
160 : std::string blockStr = AscendC::GenBlockStr(); \
161 : printf( \
162 : "[ERROR][%s][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), coreTypeStr.c_str(), __FILE__, __LINE__, \
163 : __FUNCTION__, static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
164 : } while (0)
165 :
166 : #else
167 :
168 : #define KERNEL_LOG_KERNEL_DEBUG(format, ...) \
169 : do { \
170 : std::string blockStr = "Core_"; \
171 : blockStr += std::to_string(block_idx); \
172 : printf( \
173 : "[DEBUG][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), __FILE__, __LINE__, __FUNCTION__, \
174 : static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
175 : } while (0)
176 :
177 : #define KERNEL_LOG_KERNEL_INFO(format, ...) \
178 : do { \
179 : std::string blockStr = "Core_"; \
180 : blockStr += std::to_string(block_idx); \
181 : printf( \
182 : "[INFO][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), __FILE__, __LINE__, __FUNCTION__, \
183 : static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
184 : } while (0)
185 :
186 : #define KERNEL_LOG_KERNEL_WARN(format, ...) \
187 : do { \
188 : std::string blockStr = "Core_"; \
189 : blockStr += std::to_string(block_idx); \
190 : printf( \
191 : "[WARN][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), __FILE__, __LINE__, __FUNCTION__, \
192 : static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
193 : } while (0)
194 :
195 : #define KERNEL_LOG_KERNEL_ERROR(format, ...) \
196 : do { \
197 : std::string blockStr = "Core_"; \
198 : blockStr += std::to_string(block_idx); \
199 : printf( \
200 : "[ERROR][%s][%s:%d][%s][%u] " format "\n", blockStr.c_str(), __FILE__, __LINE__, __FUNCTION__, \
201 : static_cast<uint32_t>(getpid()), ##__VA_ARGS__); \
202 : } while (0)
203 :
204 : #endif
205 :
206 : #else
207 :
208 : #define KERNEL_LOG(level, format, ...)
209 : #ifndef __NPU_HOST__
210 :
211 : #define ASCENDC_ASSERT(cond, behavior)
212 :
213 : #else // #ifdef __NPU_HOST__
214 :
215 : #ifndef ASCC_ASCENDC_ASSERT
216 : #define ASCC_ASCENDC_ASSERT
217 : #define ASCENDC_ASSERT(cond, behavior) \
218 : do { \
219 : if (!(cond)) { \
220 : behavior; \
221 : raise(SIGABRT); \
222 : } \
223 : } while (0)
224 : #endif
225 :
226 : #endif // __NPU_HOST__
227 : #define ASCENDC_CHECK_VALUE_RANGE(value, rangeLow, rangeHigh, paramName, apiMsg)
228 : #define ASCENDC_CHECK_TPOSITION(cond, tensorName, tPosName, apiMsg, curPos)
229 :
230 : #endif
231 :
232 : #endif // ASCENDC_MODULE_KERNEL_LOG_INTF_H
|