Implementation:InternLM Lmdeploy Check
| Knowledge Sources | |
|---|---|
| Domains | Error_Handling, Core_Infrastructure |
| Last Updated | 2026-02-07 15:00 GMT |
Overview
Provides glog-inspired assertion and check macros with detailed error reporting, including comparison operators, null-pointer checks, and streamable error messages.
Description
The check system centers on CheckErrorStream, which captures file, line, and expression info, accumulates a user message via operator<<, and calls Report() (marked noreturn) in its destructor to abort with a detailed error message. The TM_CHECK(expr) macro evaluates the expression and, if false, creates a CheckErrorStream. The TM_CHECK_OP family (TM_CHECK_EQ, TM_CHECK_NE, TM_CHECK_LE, TM_CHECK_LT, TM_CHECK_GE, TM_CHECK_GT) performs typed comparison and, on failure, builds a string showing both operand values using CheckOpStringBuilder. The TM_CHECK_NOTNULL(p) macro calls EnsureNotNull which aborts if the pointer is null, otherwise forwards the pointer transparently. Platform-specific macros TM_LIKELY, TM_UNLIKELY, TM_NOINLINE, and TM_UNREACHABLE provide compiler hints.
Usage
Used pervasively throughout TurboMind as the primary runtime assertion mechanism. Every module uses TM_CHECK* macros for precondition validation, invariant checking, and null-pointer guards.
Code Reference
Source Location
- Repository: InternLM_Lmdeploy
- File: src/turbomind/core/check.h
- Lines: 1-143
Signature
namespace turbomind::core {
class CheckErrorStream {
public:
CheckErrorStream(const char* file, int line, const char* expr);
CheckErrorStream(const char* file, int line, const char* expr, std::string* str);
~CheckErrorStream();
template<class T> CheckErrorStream& operator<<(const T& msg);
};
// Macros
#define TM_CHECK(e)
#define TM_CHECK_EQ(a, b)
#define TM_CHECK_NE(a, b)
#define TM_CHECK_LE(a, b)
#define TM_CHECK_LT(a, b)
#define TM_CHECK_GE(a, b)
#define TM_CHECK_GT(a, b)
#define TM_CHECK_NOTNULL(p)
#define TM_LIKELY(expr)
#define TM_UNLIKELY(expr)
#define TM_UNREACHABLE
template<class T>
decltype(auto) EnsureNotNull(const char* file, int line, const char* expr, T&& p);
[[noreturn]] void ReportNullError(const char* file, int line, const char* expr);
} // namespace turbomind::core
Import
#include "src/turbomind/core/check.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| expr | bool | TM_CHECK | Expression to evaluate; aborts if false |
| a, b | comparable types | TM_CHECK_OP | Two values to compare with the specified operator |
| p | pointer | TM_CHECK_NOTNULL | Pointer to validate as non-null |
Outputs
| Name | Type | Description |
|---|---|---|
| (pass-through) | T&& | TM_CHECK_NOTNULL returns the pointer if non-null |
| (abort) | noreturn | Program terminates with error message on failure |
Usage Examples
#include "src/turbomind/core/check.h"
// Basic check
TM_CHECK(ptr != nullptr) << "Pointer must not be null";
// Comparison checks with operand values in error message
TM_CHECK_EQ(a.size(), b.size());
TM_CHECK_LE(offset, buffer.size());
TM_CHECK_GT(n_ranks, 0) << "Need at least one rank";
// Null check that returns the pointer
auto* valid_ptr = TM_CHECK_NOTNULL(maybe_null_ptr);