Skip to content

Commit bc084aa

Browse files
authored
[Doc] Update logging docs (#1395)
1 parent e7e4e65 commit bc084aa

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ add_subdirectory(${TVM_SOURCE} tvm EXCLUDE_FROM_ALL)
228228
add_compile_definitions(DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
229229

230230
add_library(tilelang_objs OBJECT ${TILE_LANG_SRCS})
231+
232+
# Set debug mode compile definitions
233+
# We open the deubg option of TVM, i.e. TVM_LOG_DEBUG
231234
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
235+
message(STATUS "Building TileLang with DEBUG mode")
232236
target_compile_definitions(tilelang_objs PRIVATE "TVM_LOG_DEBUG")
233237
endif()
234238

docs/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
[GitHub](https://github.com/tile-ai/tilelang)
44

5-
Tile Language (tile-lang) is a concise domain-specific language designed to streamline
6-
the development of high-performance GPU/CPU kernels (e.g., GEMM, Dequant GEMM, FlashAttention, LinearAttention).
7-
By employing a Pythonic syntax with an underlying compiler infrastructure on top of TVM,
8-
tile-lang allows developers to focus on productivity without sacrificing the
5+
Tile Language (tile-lang) is a concise domain-specific language designed to streamline
6+
the development of high-performance GPU/CPU kernels (e.g., GEMM, Dequant GEMM, FlashAttention, LinearAttention).
7+
By employing a Pythonic syntax with an underlying compiler infrastructure on top of TVM,
8+
tile-lang allows developers to focus on productivity without sacrificing the
99
low-level optimizations necessary for state-of-the-art performance.
1010

1111
:::{toctree}
@@ -24,6 +24,7 @@ get_started/targets
2424

2525
tutorials/debug_tools_for_tilelang
2626
tutorials/auto_tuning
27+
tutorials/logging
2728
:::
2829

2930
:::{toctree}

docs/tutorials/logging.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
Logging in Tilelang/TVM
2+
===================================================
3+
<div style="text-align: left;">
4+
<em>Author:</em> <a href="https://github.com/SiriusNEO">SiriusNEO</a>
5+
</div>
6+
7+
## TVM Logging Overview
8+
9+
Tilelang currently utilizes the logging system from TVM. The implementation can be found in:
10+
11+
- [include/tvm/runtime/logging.h](https://github.com/apache/tvm/blob/main/include/tvm/runtime/logging.h): Macro definitions
12+
- [src/runtime/logging.cc](https://github.com/apache/tvm/blob/main/src/runtime/logging.cc): Logging logic implementation
13+
14+
The design style is inspired by [Google's glog](https://google.github.io/glog/stable/).
15+
16+
## Logging Categories
17+
18+
There are three primary macro types:
19+
20+
```c++
21+
LOG(INFO) << "aaa";
22+
DLOG(INFO) << "aaa";
23+
VLOG(1) << "aaa";
24+
```
25+
26+
- **LOG**: Standard logging preserved in code for displaying necessary information at different levels during runtime. Most Tilelang C++ error reporting is implemented via `LOG(FATAL) << "error msg"`.
27+
- **DLOG**: Debug logging for developer debugging output. DLOG is controlled at build time by the TVM_LOG_DEBUG environment variable and is **eliminated in Release builds through dead code elimination**.
28+
- The key difference between LOG(DEBUG) and DLOG is this build-time elimination. We recommend using DLOG over LOG(DEBUG), as the latter has overlapping functionality and gets compiled into the release runtime.
29+
- **VLOG**: [Verbose logging](https://google.github.io/glog/stable/logging/#verbose-logging), primarily for debugging. Its main feature is customizable verbosity levels. For example, VLOG(n) where n can be 1, 2, 3, 4, 5, or 6, enabling complex tracing requirements. In contrast, LOG and DLOG typically use predefined verbose levels like INFO and DEBUG.
30+
- In practical Tilelang development, VLOG is used less frequently.
31+
- TVM's VLOG is implemented using DLOG, thus inheriting DLOG's characteristics.
32+
33+
Additional useful macros include various **CHECK** variants:
34+
35+
```c++
36+
CHECK(cond) << "error msg";
37+
DCHECK(cond) << "error msg";
38+
ICHECK(cond) << "error msg";
39+
```
40+
41+
The implementation routes errors to LogFatal:
42+
43+
```c++
44+
#define CHECK(x) \
45+
if (!(x)) \
46+
::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \
47+
<< "Check failed: (" #x << ") is false: "
48+
```
49+
- **DCHECK**: Debug mode CHECK, only compiled in debug builds
50+
- **ICHECK**: Internal Check that should exist in Release builds. When ICHECK fails, the entire system should report an error.
51+
52+
## Logging Verbose Levels
53+
54+
TVM defines 5 levels for LOG and DLOG (adding DEBUG compared to glog):
55+
56+
```c++
57+
#define TVM_LOG_LEVEL_DEBUG 0
58+
#define TVM_LOG_LEVEL_INFO 1
59+
#define TVM_LOG_LEVEL_WARNING 2
60+
#define TVM_LOG_LEVEL_ERROR 3
61+
#define TVM_LOG_LEVEL_FATAL 4
62+
```
63+
64+
## Using Logging in TileLang Development
65+
66+
### Guidelines
67+
68+
For temporary debugging output in your code, there are no restrictions (you can even use std::cout). Just remember to remove it before submitting a PR.
69+
70+
For meaningful logging that should remain in the Tilelang codebase:
71+
72+
- Critical correctness checks: Use ICHECK with sufficient error messages to facilitate debugging when issues arise.
73+
- Complex Pass debugging: For passes requiring intermediate output that may need future review (e.g., LayoutInference), use DLOG.
74+
- General INFO/WARNING messages: Use standard LOG.
75+
76+
### Enabling Log Output in Tilelang
77+
78+
To specify current log level at runtime, we need to set the environment variable `TVM_LOG_LEVEL`. An example usage is:
79+
80+
```c++
81+
TVM_LOG_DEBUG=1 python3 code.py
82+
```
83+
84+
which enables all DEBUG/INFO (level <= 1) logs for all files.
85+
86+
#### Detailed Rules for TVM_LOG_DEBUG Specification
87+
88+
The parsing logic is in `logging.cc`. Reference: [HyperAI Zhihu Article](https://zhuanlan.zhihu.com/p/1933106843468665163).
89+
90+
Launch Python with `TVM_LOG_DEBUG=<spec>`, where `<spec>` is a comma-separated list of level assignments in the form `<file_name>=<level>`. Important notes:
91+
92+
- The special filename DEFAULT sets the LOG level for all files.
93+
- `<level>` can be set to -1 to disable LOG for that file.
94+
- `<file_name>` is the C++ source filename (e.g., .cc, not .h) relative to the `src/` directory in the TVM repository. The `src/` prefix is optional when specifying file paths.
95+
96+
### Enabling Debug Mode
97+
98+
To enable DLOG/DCHECK, developers need to first build Tilelang in Debug mode:
99+
100+
```bash
101+
cmake .. -DCMAKE_BUILD_TYPE=Debug -DUSE_CUDA=ON
102+
```
103+
104+
Tilelang's CMake logic automatically adds the `TVM_LOG_DEBUG` macro, compiling all DLOG statements:
105+
106+
```cmake
107+
target_compile_definitions(tilelang_objs PRIVATE "TVM_LOG_DEBUG")
108+
```
109+
110+
Then you also need to specify the runtime environment variables. For example, to use `DLOG(INFO) << "xxx"` for debugging, run your code with INFO level (1): `TVM_LOG_DEBUG=1`.
111+
112+
:::{note}
113+
**Important**: There are two TVM_LOG_DEBUG variables. (1) Compile-time macro: Determines whether debug content (like DLOG) is compiled into the .so file. Referenced in C++ source via #ifdef TVM_LOG_DEBUG. This is automatically enabled when using Debug build mode in CMake. (2) Runtime environment variable: Controls logging level at runtime. TVM provides a specification for this variable, allowing control over per-file logging levels.
114+
115+
These two should ideally have different names, but TVM uses the same name for both, which can cause confusion.
116+
:::
117+
118+

0 commit comments

Comments
 (0)