Skip to content

Add unit tests for batched L-BFGS-B#6274

Merged
y0z merged 16 commits intooptuna:masterfrom
Kaichi-Irie:add-unittest-batched-lbfgsb-for-PR
Sep 26, 2025
Merged

Add unit tests for batched L-BFGS-B#6274
y0z merged 16 commits intooptuna:masterfrom
Kaichi-Irie:add-unittest-batched-lbfgsb-for-PR

Conversation

@Kaichi-Irie
Copy link
Copy Markdown
Contributor

@Kaichi-Irie Kaichi-Irie commented Sep 12, 2025

Motivation

Add unit tests for batched_lbfgs module. This is the followup PR of this PR:

Description of the changes

This change add the unit tests for batched_lbfgsb with/without greenlet and an end-to-end test where greenlet is unavailable and batched_lbfgsb should be fallen back.

@nabenabe0928 nabenabe0928 changed the title Add unittest for batched lbfgsb Add unit tests for batched L-BFGS-B Sep 12, 2025
@nabenabe0928 nabenabe0928 marked this pull request as draft September 12, 2025 09:28
@nabenabe0928 nabenabe0928 marked this pull request as ready for review September 19, 2025 08:23
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you make this PR only for this test file?

@nabenabe0928 nabenabe0928 added the CI Continuous integration. label Sep 22, 2025
@nabenabe0928 nabenabe0928 added this to the v4.6.0 milestone Sep 22, 2025
@Kaichi-Irie Kaichi-Irie force-pushed the add-unittest-batched-lbfgsb-for-PR branch from f7c6520 to da11c03 Compare September 22, 2025 05:04
import optuna._gp.batched_lbfgsb as my_module

importlib.reload(my_module)
assert my_module._greenlet_imports.is_successful() is False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert my_module._greenlet_imports.is_successful() is False
assert not my_module._greenlet_imports.is_successful()

_verify_results(X0, func_and_grad, kwargs_ours, kwargs_scipy)


def test_greenlet_import_behavior(monkeypatch: pytest.MonkeyPatch) -> None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need this test? I feel like it's tested in the other tests. It would be better avoid duplications to not doubly manage similar tests.

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.

Thanks for your comment. As you pointed out, this test is verbose, so I will remove it.

import optuna._gp.batched_lbfgsb as my_module

importlib.reload(my_module)
assert my_module._greenlet_imports.is_successful() is use_greenlet
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use == to check the value validity.

Suggested change
assert my_module._greenlet_imports.is_successful() is use_greenlet
assert my_module._greenlet_imports.is_successful() == use_greenlet

"lower_bound,upper_bound", [(-np.inf, None), (None, np.inf), (-np.inf, np.inf), (None, None)]
)
@pytest.mark.parametrize("dim, n_localopts", [(10, 10), (1, 10), (10, 1), (1, 1)])
def test_batched_lbfgsb_without_bounds(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not using use_greenlet in this function?

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Mostly LGTM. Please address my comments 🙇

@Kaichi-Irie
Copy link
Copy Markdown
Contributor Author

Thank you for your review! I addressed the issues you mentioned.

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the update, LGTM!

@nabenabe0928 nabenabe0928 removed their assignment Sep 25, 2025
bounds[:, 1] = upper_bound
kwargs_ours.update(bounds=bounds)
kwargs_scipy.update(bounds=bounds)
_verify_results(X0, func_and_grad, kwargs_ours, kwargs_scipy)
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.

Is batched_lbfgsb_func=my_module.batched_lbfgsb not required here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No, we do not need it.
The fallback automatically happens in batched_l_bfgs_b.

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 modified the implementation to explicitly pass batched_lbfgsb_func to _verify_results.

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

LGTM:)

@y0z y0z added test Unit test. and removed CI Continuous integration. labels Sep 26, 2025
Copy link
Copy Markdown
Member

@y0z y0z left a comment

Choose a reason for hiding this comment

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

Thank you for your update.
LGTM.

@y0z y0z merged commit 1685846 into optuna:master Sep 26, 2025
14 checks passed
@Kaichi-Irie Kaichi-Irie deleted the add-unittest-batched-lbfgsb-for-PR branch October 17, 2025 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test Unit test.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants