rstudio / bundle

Prepare objects for serialization with a consistent interface
https://rstudio.github.io/bundle/
Other
28 stars 4 forks source link

Add bundle support for LightGBM #55

Closed EKtheSage closed 1 year ago

EKtheSage commented 1 year ago

I was able to save LightGBM to pin using vetiver, loading it from pin board seems to work too, but when it comes to prediction, I got an error below, it seems that the model was incorrectly saved.

v_mod <- vetiver_model(model_final, 'lightgbm_model',
                       description = 'lightgbm model',
                       save_prototype = FALSE)

board %>% 
  vetiver_pin_write(v_mod, check_renv = TRUE)

mod <- board %>% 
      vetiver_pin_read('lightgbm_model')

test_dat %>%
      dplyr::slice(1)  %>% 
      mutate(.pred = predict(mod$model, test_dat %>%
                               select(!!x) %>% as.matrix()))
Error in `mutate()`:
ℹ In argument: `.pred = predict(...)`.
Caused by error in `predictor$predict()`:
! Attempting to use a Booster which no longer exists. This can happen if you have called Booster$finalize() or 
if this Booster was saved with saveRDS(). To avoid this error in the future, use saveRDS.lgb.Booster() or 
Booster$save_model() to save lightgbm Boosters.
juliasilge commented 1 year ago

Thanks so much for the report @EKtheSage! This model needs to be "bundled" because it can't be saved directly as .rds.

simonpcouch commented 1 year ago

Some additional context at https://github.com/rstudio/bundle/issues/24!

lightgbm 4.0.0 will include support for serialization via .rds in its upcoming release. 4.0.0 is already in PyPI and is in submission on CRAN. This should resolve itself soon, so I'd recommend we not move forward with lightgbm bundle methods. :)

juliasilge commented 1 year ago

Ah, fantastic! Thank you so much @simonpcouch 🙌

I'll close this and we can reopen if we find further problems.

@EKtheSage you will want to wait to see when the new version of lightgbm makes it to CRAN: https://cran.r-project.org/package=lightgbm

jameslamb commented 1 year ago

👋🏻 hi all, James from LightGBM here.

I heard from a friend who went to posit::conf that you all are waiting on {lightgbm} 4.x to get to CRAN, to take advantage of the ability to saveRDS() / readRDS() LightGBM models, added here: https://github.com/microsoft/LightGBM/pull/4685

Just wanted to add... here's the relevant issue to follow to be notified of progress towards {lightgbm} v4.x making it to CRAN: https://github.com/microsoft/LightGBM/issues/5987

We (and {xgboost}, https://github.com/dmlc/xgboost/issues/9497#issuecomment-1724129196) are struggling to meet CRAN's changed practices around the use of multithreading in package code. Sorry for the delay.

juliasilge commented 1 year ago

Thank you so much for the update @jameslamb! 🙌 Good luck as you work on the CRAN submission.

simonpcouch commented 10 months ago

LightGBM 4.2.0 on CRAN! https://cran.r-project.org/web/packages/lightgbm/index.html.

jameslamb commented 10 months ago

Hey @simonpcouch thanks so much to you and your team for the help with this release!!!

Just note, I haven't announced this on social media or come back here to this issue yet because CRAN isn't done checking the submission.

Screen Shot 2023-12-11 at 1 14 58 PM

(https://cran.r-project.org/web/checks/check_results_lightgbm.html)

I'm also paranoid about calling a release to CRAN "done" so soon because I've observed cases where checks pass for every check flavor in that table, and then a few days later an error is reported for one of the extra checks like rchk or valgrind (https://cran.r-project.org/web/checks/check_issue_kinds.html).

But so far so good! 😁

jameslamb commented 10 months ago

@simonpcouch v4.2.0 of {lightgbm} passed all the CRAN main checks and binaries have been built for all the platforms CRAN supports: https://github.com/microsoft/LightGBM/pull/6191#issuecomment-1863190427

You can safely rely on saveRDS() / readRDS() of Booster objects from {lightgbm} now 😁

We'll remove the old lightgbm::readRDS.lgb.Booster() and lightgbm::saveRDS.lgb.Booster() in an upcoming release.

@ me any time if you have questions or need help! And thanks again for all your help getting LightGBM v4.2.0 out.