Implementation:Interpretml Interpret Libebm Core Utilities
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, EBM_Core |
| Last Updated | 2026-02-07 12:00 GMT |
Overview
Provides common C++ utility functions, safe arithmetic helpers, and type manipulation templates used throughout the EBM native library.
Description
The common.hpp header is a foundational utility header for the libebm library. It defines inline helper functions for safe arithmetic operations (overflow-checked multiplication, addition), array-to-pointer conversion for the struct hack pattern, approximate equality checks for debugging, and various compile-time constants and type traits. It includes macros for conditional inlining (INLINE_RELEASE_UNTEMPLATED vs INLINE_RELEASE_TEMPLATED) that enable inlining only in release builds for better debuggability. The ArrayToPointer function is particularly important as it makes the struct hack pattern formally legal in C++ by converting array access to pointer access, avoiding undefined behavior when accessing memory beyond declared array bounds.
Usage
Included by virtually every other source file in the libebm library. Provides the fundamental building blocks for safe pointer arithmetic, overflow checking, and memory layout operations that the entire EBM native implementation depends on.
Code Reference
Source Location
- Repository: Interpretml_Interpret
- File: shared/libebm/bridge/common.hpp
Signature
template<typename T> GPU_BOTH inline static T* ArrayToPointer(T* const a) noexcept;
template<typename T> GPU_BOTH inline static const T* ArrayToPointer(const T* const a) noexcept;
#ifndef NDEBUG
template<typename T> INLINE_ALWAYS static bool IsApproxEqual(
const T val1, const T val2, const T percentage = T{1e-3});
#endif
// Overflow-safe arithmetic
template<typename T> inline constexpr static bool IsMultiplyError(const T num1, const T num2) noexcept;
template<typename T> inline constexpr static bool IsAddError(const T num1, const T num2) noexcept;
I/O Contract
| Function | Input | Output | Description |
|---|---|---|---|
| ArrayToPointer | T* array | T* pointer | Converts array to pointer to make struct hack legal |
| IsApproxEqual | Two values, percentage | bool | Checks approximate equality (debug only) |
| IsMultiplyError | Two values | bool | Returns true if multiplication would overflow |
| IsAddError | Two values | bool | Returns true if addition would overflow |
Usage Examples
# Called internally via native bindings
from interpret.glassbox import ExplainableBoostingClassifier
ebm = ExplainableBoostingClassifier()
ebm.fit(X, y) # Internally uses common.hpp utilities throughout