Execute the generated function
Load the emitted source into an isolated namespace and score every held-out row.
Open source / cross-runtime inference
lgbm-to-code turns a supported LightGBM ensemble into readable Python, C++17, or JavaScript. The important part is not emitting source. It is checking that the emitted source follows the same tree paths and returns the same raw score.
01 / THE BOUNDARY
Training still belongs in Python with LightGBM. At deployment, the package reads the fitted model’s dumped trees and writes ordinary conditionals for the target runtime.
The output is intentionally plain. A generated function accepts an ordered feature vector and adds the leaf value reached in each tree. There is no serialized model loader and no inference package to install in the target environment.
02 / PARITY
The current suite trains a 17-tree LightGBM regression model on 120 rows, preserves 40 rows for comparison, and includes missing values. It generates all three targets, then executes or compiles each one.
Load the emitted source into an isolated namespace and score every held-out row.
Build the emitted source with g++ and parse its double-precision output.
Restore missing values as NaN and execute every row through the generated module.
Each result is compared with LightGBM’s predict(..., raw_score=True)
using both relative and absolute tolerances of 1e−12. This verifies the tested
model and rows; it is not a claim that every possible LightGBM model is supported.
03 / OUTPUT CONTRACT
Tree traversal and output semantics are separate responsibilities. The generated function returns the ensemble’s raw score so the caller chooses the correct transform.
04 / MISSING VALUES
A threshold alone does not describe a LightGBM split. The generated condition must also preserve how that node treats NaN or zero and which child receives missing input.
Python, C++17, and JavaScript each receive an explicit missing-value expression.
Nodes marked zero-as-missing route both values through the recorded default branch.
The emitted comparison changes with the node’s missing-value direction.
05 / SCOPE
Generated code favors auditability and portability, not minimum code size or maximum throughput. The caller still owns feature ordering, schema validation, objective transforms, and deployment-specific performance testing.
06 / INSPECT IT
The current repository contains the cross-runtime verification work described here. PyPI hosts the earlier packaged release, so review the changelog and repository state when choosing which behavior to depend on.