Skip to content

[python-package] Load parameters from model string - #6852

Merged
StrikerRUS merged 22 commits into
lightgbm-org:masterfrom
Samsagax:fix-param-from-string
Apr 24, 2025
Merged

StrikerRUS merged 22 commits into
lightgbm-org:masterfrom
Samsagax:fix-param-from-string

Conversation

@Samsagax

@Samsagax Samsagax commented Mar 2, 2025 •

Copy link
Copy Markdown
Contributor

This fixes #6851 by using the same workaround as when loading the model from a file (#5424).

@jameslamb jameslamb 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.

Thanks for taking the time to contribute!

As I just mentioned in #6851 (comment), think the issue goes deeper than just this and that #6101 will be the right fix.

That said, if you want to pursue this PR for just the in-memory string part (not also fixing copy()), then I'm open to it.... but please add a new test covering this behavior.

Otherwise, if you don't have the time/interest in that, we can close this and you can subscribe to #6101.

@Samsagax

Samsagax commented Mar 3, 2025

Copy link
Copy Markdown
Contributor Author

I'll add the requested tests for this based on the minimal example in #6851 . I would like to make this working even if it doesn't fix the copy part since I'll be using the string from memory in my use case (string retrieved from a database elsewhere).

Thanks for taking the time to review it.

@Samsagax
Samsagax force-pushed the fix-param-from-string branch from ef2cc77 to 299b83a Compare March 3, 2025 14:10
@Samsagax
Samsagax requested a review from jameslamb March 3, 2025 14:10
@Samsagax

Samsagax commented Mar 4, 2025

Copy link
Copy Markdown
Contributor Author

Ok. Test case added. Should also be useful for #6101

Thanks for having the time to review this.

@jameslamb jameslamb 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.

Thanks very much! I'm glad this worked and I think it's a really nice improvement.

I left some suggestions to make the tests stricter. Can you please also move that test case to right after the test on the similar logic for loading from a model file?

https://github.com/microsoft/LightGBM/blob/6437645c4a0c17046be59e4f57d09952e2e0185f/tests/python_package_test/test_engine.py#L1461

Comment thread tests/python_package_test/test_basic.py Resolved
Comment thread tests/python_package_test/test_basic.py Resolved
Comment thread tests/python_package_test/test_basic.py Resolved
Comment thread tests/python_package_test/test_basic.py Resolved
Comment thread tests/python_package_test/test_basic.py Resolved
@Samsagax
Samsagax requested a review from jameslamb March 6, 2025 16:08
@Samsagax

Samsagax commented Mar 6, 2025

Copy link
Copy Markdown
Contributor Author

I think I've done everything as requested. Should I rebase/squash commits into a more recipe-like structure?

@jameslamb jameslamb 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.

Thanks, looks great!

As a quick note... I already see the linting failed: https://github.com/microsoft/LightGBM/actions/runs/13703032288/job/38321483346?pr=6852

You can replicate that locally (and auto-fix the formatting issues) like this:

pip install pre-commit
pre-commit run --all-files

No need to squash commits... we squash on merges here, so every pull request = 1 commit on master: https://github.com/microsoft/LightGBM/commits/master/

You should merge in the latest changes whenever updating though... that helps ensure this is tested against the latest state of the target branch. I just did that, by clicking this button:

image

Comment thread tests/python_package_test/test_basic.py Resolved
@Samsagax

Samsagax commented Mar 6, 2025

Copy link
Copy Markdown
Contributor Author

Don't really know why r packaging is failing. But seems to be failing in master too.

@jameslamb jameslamb changed the title [python-package] Fill in params when loading from string [python-package] Load parameters from model string Mar 7, 2025
@jameslamb

Copy link
Copy Markdown
Member

Don't really know why r packaging is failing. But seems to be failing in master too.

Yes you're right, that job failing is unrelated to your changes. Documented at #6855

@jameslamb jameslamb 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.

Thanks very much! I've changed the label to breaking, because this is technically a breaking change from the perspective of anyone who was relying on params passed through the Booster constructor when loading a file, like this:

bst = Booster(model_str=model_str, params={...})

@jmoralez do you have some time to review this too? You're so familiar with these code paths from #5424, I think that'd be really helpful.

I approve of these changes as-is (so nothing else you need to do @Samsagax), but just leaving a "comment" review so my approval won't count towards a merge until we get another approval.

@Samsagax
Samsagax requested review from jameslamb and jmoralez March 28, 2025 22:01
@Samsagax

Copy link
Copy Markdown
Contributor Author

Don't know why is failing. But seems the same failure as main.

@jameslamb

Copy link
Copy Markdown
Member

But seems the same failure as main.

Yes, you don't need to do anything else at this point. There are a couple of CI issues here, we're working on them in #6872

@StrikerRUS StrikerRUS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks!

@StrikerRUS

Copy link
Copy Markdown
Collaborator

/AzurePipelines run

@StrikerRUS

Copy link
Copy Markdown
Collaborator

/AzurePipelines run

@StrikerRUS
StrikerRUS dismissed jameslamb’s stale review April 24, 2025 16:31

To unblock the merge.

I approve of these changes as-is (so nothing else you need to do @Samsagax), but just leaving a "comment" review so my approval won't count towards a merge until we get another approval.

@StrikerRUS
StrikerRUS merged commit 06e94ad into lightgbm-org:master Apr 24, 2025
@jameslamb jameslamb mentioned this pull request Jan 17, 2026
43 tasks done
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity since it was closed.
To start a new related discussion, open a new issue at https://github.com/lightgbm-org/LightGBM/issues including a reference to this.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[python-package] Model parameters are lost when loading Booster from model serialized string

4 participants