|
| 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