Skip to content

fix(python): preserve loss link function when loading model from blob/stream - #3184

Closed
webzuweb wants to merge 3 commits into
catboost:masterfrom
webzuweb:fix/load-model-preserve-poisson-link
Closed

webzuweb wants to merge 3 commits into
catboost:masterfrom
webzuweb:fix/load-model-preserve-poisson-link

Conversation

@webzuweb

@webzuweb webzuweb commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #3103 — load_model(blob=...) and load_model(stream=...) silently dropped the model's default prediction link function, so predict() returned RawFormulaVal (log-scale) instead of the exponentiated target for losses with a link function (Poisson, Tweedie).

Root cause

The file-path load path (_load_model) resets _init_params and repopulates it from the loaded model, then invalidates _canonized_params. The blob/stream paths (_load_from_blob, _load_from_stream) only called _set_trained_model_attributes() and left _init_params empty/stale. _get_default_prediction_type() reads loss_function from the canonized params, so it fell back to 'RawFormulaVal'.

Changes

  • Extract the shared post-load refresh into _refresh_params_from_model().
  • Call it from all three load paths (_load_model, _load_from_blob, _load_from_stream), each resetting _init_params = {} before loading so stale params from a previous load don't leak.

Test

Added test_load_model_preserves_poisson_link: trains a small Poisson model, saves it, loads via file path / blob / stream, and asserts the predictions match (and are exponentiated, not raw).

Verification

path  : 1.318882
blob  : 1.318882   # previously 0.276785 (raw)
stream: 1.318882   # previously 0.276785 (raw)

…/stream

load_model(blob=...) and load_model(stream=...) did not repopulate
_init_params (and did not reset _canonized_params), unlike the file-path
load path. As a result _get_default_prediction_type() fell back to
'RawFormulaVal' and predict() returned log-scale raw values for losses
with a link function (Poisson, Tweedie) instead of the exponentiated
target.

Extract the shared post-load param refresh into _refresh_params_from_model()
and call it from all three load paths.

Fixes catboost#3103.

@andrey-khropov andrey-khropov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Let's move self._init_params = {} to _refresh_params_from_model to avoid copy-paste.

  • Please use test_output_path(OUTPUT_MODEL_PATH) for the model file as it is done in other tests. There's no need to delete it yourself, it will be deleted automatically by the test framework.

…use test_output_path

Address review comments: consolidate the _init_params = {} reset inside
_refresh_params_from_model (called from all three load paths) and use the
standard test_output_path(OUTPUT_MODEL_PATH) helper in the regression test.
@webzuweb

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both points addressed:

  • self._init_params = {} moved into _refresh_params_from_model(), so all three load paths share it without copy-paste.
  • Test now uses test_output_path(OUTPUT_MODEL_PATH) and drops the manual NamedTemporaryFile/cleanup.

_load_from_blob now refreshes _init_params from the model (needed for
the Poisson-link fix), but __setstate__ relies on _load_from_blob and
then merges the pickled state on top. Reset _init_params after the blob
load so the restored params match __getstate__ exactly instead of
leaking model-derived defaults (fixes test_set_params_with_synonyms).
@webzuweb

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up: _load_from_blob now refreshes _init_params from the model (required for the Poisson-link fix), but __setstate__ reuses _load_from_blob and then merges the pickled state on top. I reset _init_params right after the blob load so a pickle round-trip restores the exact init params captured by __getstate__ instead of leaking model-derived defaults. This fixes the test_set_params_with_synonyms regression that surfaced in CI.

@robot-magpie

robot-magpie Bot commented Oct 2, 2026

Copy link
Copy Markdown

@andrey-khropov has imported your pull request. If you are a Yandex employee, you can view this diff.

@robot-magpie

robot-magpie Bot commented Oct 2, 2026

Copy link
Copy Markdown

✅ This pull request is being closed because it has been successfully merged into our internal monorepository.
Your changes will be pushed to this repository soon. Thank you for your contribution!

@robot-magpie robot-magpie Bot closed this Oct 2, 2026
robot-piglet pushed a commit that referenced this pull request Oct 2, 2026
…/stream

## Summary

Fixes #3103 — `load_model(blob=...)` and `load_model(stream=...)` silently dropped the model's default prediction link function, so `predict()` returned `RawFormulaVal` (log-scale) instead of the exponentiated target for losses with a link function (Poisson, Tweedie).

## Root cause

The file-path load path (`_load_model`) resets `_init_params` and repopulates it from the loaded model, then invalidates `_canonized_params`. The blob/stream paths (`_load_from_blob`, `_load_from_stream`) only called `_set_trained_model_attributes()` and left `_init_params` empty/stale. `_get_default_prediction_type()` reads `loss_function` from the canonized params, so it fell back to `'RawFormulaVal'`.

## Changes

- Extract the shared post-load refresh into `_refresh_params_from_model()`.
- Call it from all three load paths (`_load_model`, `_load_from_blob`, `_load_from_stream`), each resetting `_init_params = {}` before loading so stale params from a previous load don't leak.

## Test

Added `test_load_model_preserves_poisson_link`: trains a small Poisson model, saves it, loads via file path / blob / stream, and asserts the predictions match (and are exponentiated, not raw).

## Verification

```
path  : 1.318882
blob  : 1.318882   # previously 0.276785 (raw)
stream: 1.318882   # previously 0.276785 (raw)
```

---

Pull Request resolved: #3184
commit_hash:f98e62fa70d063a5b44bb0e8da88df7e5db07653
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

load_model from blob or stream ignores the Poisson link function and returns RawFormulaVal

2 participants