xgboost icon indicating copy to clipboard operation
xgboost copied to clipboard

Issues downgrading xgboost 2 model

Open farridav opened this issue 1 month ago • 3 comments

I'm attempting to downgrade a trained xgboost 2 model down to 1.1 (as another system needs the model to be compatible with 1.1)

These are the steps that i taking

In an environment with xgboost==2.0.3 installed

# Model starts in pickle format
model: xgboost.Booster = pickle.load(open(in_pickle_path, "rb"))
model.save_model(out_json_path)

In an environment with xgboost==1.1 installed

booster = xgboost.Booster()
model = booster.load_model(out_json_path)

Which results in the following error:

XGBoostError: [14:46:41] /workspace/include/xgboost/json.h:65: Invalid cast, from Null to Array
Stack trace:
  [bt] (0) .../xgboost/lib/libxgboost.so(+0xfa146) [0x77622c2fa146]
  [bt] (1) .../xgboost/lib/libxgboost.so(+0x22f7dd) [0x77622c42f7dd]
  [bt] (2) .../xgboost/lib/libxgboost.so(+0x188af2) [0x77622c388af2]
  [bt] (3) .../xgboost/lib/libxgboost.so(+0x16a97f) [0x77622c36a97f]
  [bt] (4) .../xgboost/lib/libxgboost.so(+0x1a9e6a) [0x77622c3a9e6a]
  [bt] (5) .../xgboost/lib/libxgboost.so(XGBoosterLoadModel+0x796) [0x77622c292096]
  [bt] (6) /lib/x86_64-linux-gnu/libffi.so.8(+0x7e2e) [0x7762b6191e2e]
  [bt] (7) /lib/x86_64-linux-gnu/libffi.so.8(+0x4493) [0x7762b618e493]
  [bt] (8) .../lib-dynload/_ctypes.cpython-311-x86_64-linux-gnu.so(+0x13cd6) [0x7762b7a97cd6]

Is this the right approach? or is there something I need to do inbetween ? i also seem to struggle to re-import the model into other xgboost versions ive tried (e.g 1.6, for various other errors)

farridav avatar May 09 '24 13:05 farridav

It is not possible to "downgrade" a trained model. This is analogous to Microsoft Word 97 being unable to load .docx files from Word 2019. Is it possible to upgrade the other system to latest XGBoost? If not, you would have to train a new model using XGBoost 1.1.

hcho3 avatar May 10 '24 02:05 hcho3

Unfortunately not, and I read in the docs that the model is forwards compatible across versions (admittedly I'm going backwards)

https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html#a-note-on-backward-compatibility-of-models-and-memory-snapshots

It seems there is something that might need tweaking to transition between 2.0.3 and 1.1 though I've not yet found out what it is..

farridav avatar May 10 '24 05:05 farridav

I read in the docs that the model is forwards compatible across versions

The docs say that the model is backwards compatible, which is in the opposite direction of forward compatibility. So the following is true:

  • XGBoost 1.1 can load a model that is trained with XGBoost 1.1
  • XGBoost 2.0.3 can load a model that is trained with XGBoost 1.1 (backwards compatible)
  • XGBoost 1.1 cannot load a model that is trained with XGBoost 2.0.3 (not forward compatible)

Unfortunately not

In this case, you should probably train a new model in an environment with xgboost==1.1.

hcho3 avatar May 10 '24 06:05 hcho3

Feel free to reopen if there are further questions.

trivialfis avatar May 17 '24 07:05 trivialfis