Skip to content

Propagate warnings to all workers in joblib - #30380

Merged
lesteve merged 10 commits into
scikit-learn:mainfrom
thomasjpfan:filter_warning
Jan 15, 2025
Merged

lesteve merged 10 commits into
scikit-learn:mainfrom
thomasjpfan:filter_warning

Conversation

@thomasjpfan

Copy link
Copy Markdown
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.

@github-actions

github-actions Bot commented Dec 1, 2024 •

Copy link
Copy Markdown

✔️ Linting Passed

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

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

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

LGTM once the following are addressed.

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

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.

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

Comment thread sklearn/utils/parallel.py Outdated
previous_filters = warnings.filters
warnings.filters = warning_filters
yield
warnings.filters = previous_filters

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.

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
Copy Markdown
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)

Comment thread sklearn/utils/parallel.py Outdated
with (
config_context(**config),
_warning_filter_context(warning_filters=warning_filters),
):

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.

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)

Comment thread sklearn/utils/parallel.py Outdated


def _with_config(delayed_func, config):
def _with_config(delayed_func, config, warning_filters):

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.

Maybe renaming the function _with_config_and_warning_filters to be explicit?

@lesteve

lesteve commented Jan 14, 2025 •

Copy link
Copy Markdown
Member

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)
        )

@lesteve lesteve 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, LGTM!

I push a small tweak and set this to automerge.

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