siboehm / lleaves

Compiler for LightGBM gradient-boosted trees, based on LLVM. Speeds up prediction by ≥10x.
https://lleaves.readthedocs.io/en/latest/
MIT License
333 stars 28 forks source link

Enabling passing unpickled model directly rather than just through the mode.txt file #45

Closed faithfulalabi closed 1 year ago

faithfulalabi commented 1 year ago
siboehm commented 1 year ago

Hi! That's currently not possible and I probably won't support it in the future either. It makes the implementation much simpler to load the model.txt instead of loading a pickled model :) Pickle is very Python specific, and I'm considering rewriting lleaves from scratch in Cpp.

You could try storing the model.txt in an in-memory tmp-file, using code roughly like this:

import lightgbm as lgb
import tempfile

with tempfile.NamedTemporaryFile(mode='w+b', delete=False, suffix='.txt') as tmp_file:
    model.save_model(tmp_file.name)
    print(f"Model saved to: {tmp_file.name}")

loaded_model = lleaves.Model(tmp_file.name)
faithfulalabi commented 1 year ago

Thanks for the fast response and sample code. Was going to do that as a work around but thought I'd ask first. Thanks again 😄