Skip to content

[R-package] Ensure print.lgb.Booster() works when objective is not explicitly set - #6850

Merged
StrikerRUS merged 11 commits into
lightgbm-org:masterfrom
walkerjameschris:fix/lgb-train-print
Apr 24, 2025
Merged

StrikerRUS merged 11 commits into
lightgbm-org:masterfrom
walkerjameschris:fix/lgb-train-print

Conversation

@walkerjameschris

@walkerjameschris walkerjameschris commented Feb 28, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #6848

This PR fixes an issue where lgb.train() creates a booster, the booster is printed, and no objective is specified. This led to an error due to a NULL value. I have handled this by extracting the default objective (looks to be l2) and printing that in the event a built-in or custom objective is not provided.

…s out of method tests, creating unique tests for different method situations
@walkerjameschris walkerjameschris changed the title Handling NULL objective when booster prints, adding a test for this case, dividing other method tests [R-package] Handling NULL objective when booster prints, adding a tests Feb 28, 2025
@walkerjameschris walkerjameschris changed the title [R-package] Handling NULL objective when booster prints, adding a tests [R-package] Handling NULL objective when booster prints, adding tests Mar 1, 2025
@jameslamb jameslamb added the fix label Mar 2, 2025

@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! Please see my first round of suggestions.

Also... I noticed that you pushed multiple commits to fix linter errors. That's totally fine to do, but you might find it easier to run the linting yourself locally.

For R code:

# install {lintr} (you only need to do this once)
Rscript -e "install.packages('lintr', repos = 'https://cran.r-project.org')"

# run the R linting
Rscript .ci/lint-r-code.R $(pwd)

For other things like trailing whitespace and newlines, you can run that linting locally too. You just need to install pre-commit (https://pre-commit.com/)... for example:

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

Comment thread R-package/R/lgb.Booster.R Resolved
})
.have_same_handle(ret, model)
.has_expected_content_for_finalized_model(log_txt)
}

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 pulling these out! But could you please move them down to right before the first test case using them?

If they were more generically useful or used in more places I'd agree with putting helper functions like this at the top of the file, but in this case they're highly specific to the {print,show,summary}.lgb.Booster() test cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For sure! I will get on that.

@jameslamb jameslamb changed the title [R-package] Handling NULL objective when booster prints, adding tests [R-package] Ensure print.lgb.Booster() works when objective is not explicitly set Mar 6, 2025
@jameslamb

Copy link
Copy Markdown
Member

Some other notes I meant to add:

, params = list(num_threads = .LGB_MAX_THREADS)
)

log_txt <- capture.output(print(model))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am checking that the output of print() for a booster is "Objective: (default)" before .check_methods_work() is called. My rationale for this order of operations is that the model is finalized in .check_methods_work() and .has_expected_content_for_finalized_model() checks that the printout is correctly modified to "Booster handle is invalid" which removes "Objective: (default)" from the output. So we can first check that "Objective: (default)" is reported and then run .check_methods_work().

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.

ohhhhh I forgot about that. Ok makes sense to me! Thanks for the explanation.

@walkerjameschris

Copy link
Copy Markdown
Contributor Author

Could we restart the CI workflow? The failing check does not appear to be related to this issue:

qemu: uncaught target signal 11 (Segmentation fault) - core dumped
/LightGBM/.ci/setup.sh: line 146:    15 Segmentation fault      sudo yum update -y

##[error]Bash exited with code '1'.
Finishing: Setup and run tests

@walkerjameschris

Copy link
Copy Markdown
Contributor Author

Hmm I am still not sure this is related to my PR. Is it best practice for me to continue periodically requesting a CI run in instances such as this?

@walkerjameschris

Copy link
Copy Markdown
Contributor Author

Could we run the CI workflow for this PR? Thanks!

@walkerjameschris

Copy link
Copy Markdown
Contributor Author

I think the CI workflow failed for something unrelated to my PR. Should we try again?

Linting PowerShell code

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseCorrectCasing                  Information  test-windo 21    Keyword 'Exit
                                                 ws.ps1           ' does not ma
                                                                  tch the expec
                                                                  ted case 'exi
                                                                  t'.
PSUseCorrectCasing                  Information  test-windo 28    Keyword 'Exit
                                                 ws.ps1           ' does not ma
                                                                  tch the expec
                                                                  ted case 'exi
                                                                  t'.
PSUseCorrectCasing                  Information  test-windo 62    Keyword 'Exit
                                                 ws.ps1           ' does not ma
                                                                  tch the expec
                                                                  ted case 'exi
                                                                  t'.

Error: Process completed with exit code 1.

@jameslamb

Copy link
Copy Markdown
Member

Hmm I am still not sure this is related to my PR. Is it best practice for me to continue periodically requesting a CI run in instances such as this?

Thanks for your effort here! I agree that this looks unrelated to your changes.

You don't need to ask for any more re-runs, we'll try to get this fixed separately and come back to update your PR. Sorry for the inconvenience.

@walkerjameschris

Copy link
Copy Markdown
Contributor Author

Hmm I am still not sure this is related to my PR. Is it best practice for me to continue periodically requesting a CI run in instances such as this?

Thanks for your effort here! I agree that this looks unrelated to your changes.

You don't need to ask for any more re-runs, we'll try to get this fixed separately and come back to update your PR. Sorry for the inconvenience.

Thanks James, I appreciate it. I’ll sit tight for now.

@jameslamb

Copy link
Copy Markdown
Member

/AzurePipelines run

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

Looks great, thanks so much for this!!

@jameslamb

Copy link
Copy Markdown
Member

/AzurePipelines run

@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

@shiyu1994 Could you please restart VM for CUDA CI? Seems that it's stuck.

@StrikerRUS

Copy link
Copy Markdown
Collaborator

Kindly ping @shiyu1994

@StrikerRUS

Copy link
Copy Markdown
Collaborator

/AzurePipelines run

@StrikerRUS
StrikerRUS merged commit 0bbb02f into lightgbm-org:master Apr 24, 2025
@jameslamb

Copy link
Copy Markdown
Member

Thanks for fixing CUDA CI @shiyu1994, for getting this merged @StrikerRUS , and for taking the time to contribute @walkerjameschris !

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

[R-package] Printing a booster trained with lgb.train() errors unexpectedly

3 participants