ITADN

Question about inference discrepancies between LightGBM and lleaves

#94OpenSkqLiao 创建于 2025-09-18
S
SkqLiaocommented
Hi, thank you for open-sourcing lleaves! It’s an excellent project that brings significant speedups to LightGBM inference, and I really appreciate the work that went into it. I have a question regarding small discrepancies in inference results. My understanding is that lleaves compiles LightGBM models into more efficient machine code, but the underlying algorithm should be identical, so I initially expected the outputs to match exactly. However, in my tests on the NYC taxi dataset, I observed the following differences: fp64 error: ~3e-14 fp32 error: ~1e-05 My hypothesis is that these differences might be due to floating-point arithmetic not being strictly associative/commutative, so aggressive optimizations or changes in computation order could introduce slight numerical errors. But I am not sure if this explanation is correct. My questions are: Is this level of discrepancy expected when using lleaves compared to LightGBM? What are the most likely causes of these errors (e.g., floating-point reordering, compiler optimizations, etc.)? Is there any way to eliminate or further reduce these discrepancies, or are they fundamentally unavoidable? Thanks again for your great work on this project, and I look forward to your insights! Test code: ```python import lightgbm import lleaves import pandas as pd import numpy as np df = pd.read_csv("../examples/data/airline_data_factorized.csv") df = df.drop(columns=["Time"]) def init_model(use_fp64): lgbm_model = lightgbm.Booster(model_file="./tests/models/NYC_taxi/model.txt") llvm_model = lleaves.Model(model_file="./tests/models/NYC_taxi/model.txt") llvm_model.compile(use_fp64=use_fp64) return lgbm_model, llvm_model def test(use_fp64): df0 = df.astype(np.float64 if use_fp64 else np.float32) lgbm_model, llvm_model = init_model(use_fp64=use_fp64) res1 = lgbm_model.predict(df0) res2 = llvm_model.predict(df0) print(np.abs(res1 - res2).max()) test(use_fp64=True) # 2.842170943040401e-14 test(use_fp64=False) # 1.349401425443375e-05 ```
0 条评论