Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate warnings to all workers in joblib #30380

Merged
merged 10 commits into from
Jan 15, 2025

Conversation

thomasjpfan
Copy link
Member

Reference Issues/PRs

Closes #29294

What does this implement/fix? Explain your changes.

This PR sets the filter warnings from the main process for each joblib worker. It adjusts the context in the same place as the global context.

Copy link

github-actions bot commented Dec 1, 2024

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: e014df3. Link to the linter CI: here

Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM once the following are addressed.

@@ -0,0 +1,2 @@
- Filter warnings from the main process are propagated to joblib workers.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Filter warnings from the main process are propagated to joblib workers.
- Warning filters from the main process are propagated to joblib workers.

previous_filters = warnings.filters
warnings.filters = warning_filters
yield
warnings.filters = previous_filters
Copy link
Member

Choose a reason for hiding this comment

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

For the threading backend, this should result in a null-operation, but since the warnings module is famously not thread-safe, I would rather test this case explicitly as part of our test suite.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a more explicit check here: 06aa344 (#30380)

with (
config_context(**config),
_warning_filter_context(warning_filters=warning_filters),
):
Copy link
Member

Choose a reason for hiding this comment

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

Rather than creating our own context manager, maybe it would be slightly simpler to use warnings.catch_warnings and set warning.filters = warning_filters, i.e. something like this:

with config_context(**config), warnings.catch_warnings():
    warnings.filters = warnings_filters
    return self.function(*args, **kwargs)

@@ -21,10 +22,10 @@
_threadpool_controller = None


def _with_config(delayed_func, config):
def _with_config(delayed_func, config, warning_filters):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe renaming the function _with_config_and_warning_filters to be explicit?

@lesteve
Copy link
Member

lesteve commented Jan 14, 2025

Maybe it is worth to add a test that makes sure that for backend='loky' there is no side-effects on the workers warnings.filters, i.e. you could imagine forgetting to reset the warning filters to their original value after you have executed the fuction, i.e. something like this:

def test_filter_warning_propagates_no_side_effect_with_loky_backend():
    with warnings.catch_warnings():
        warnings.simplefilter("error", category=ConvergenceWarning)

        Parallel(n_jobs=2, backend="loky")(
            delayed(time.sleep)(0) for i in range(10)
        )

        # Make sure that inside the loky workers, warnings filters have been reset to their original
        # value. Using joblib directly should not turn ConvergenceWarning turned into an error
        joblib.Parallel(n_jobs=2, backend="loky")(
            joblib.delayed(warnings.warn)("Convergence warning", ConvergenceWarning) for _ in range(10)
        )

Copy link
Member

@lesteve lesteve left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM!

I push a small tweak and set this to automerge.

@lesteve lesteve enabled auto-merge (squash) January 15, 2025 13:28
@lesteve lesteve merged commit 10253eb into scikit-learn:main Jan 15, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConvergenceWarnings cannot be turned off
3 participants