Skip to content

TST Add common test for mixed namespace/device array inputs to estimators - #33525

Merged
lucyleeow merged 39 commits into
scikit-learn:mainfrom
lucyleeow:tst_mixed_input_estimator
Apr 25, 2026
Merged

lucyleeow merged 39 commits into
scikit-learn:mainfrom
lucyleeow:tst_mixed_input_estimator

Conversation

@lucyleeow

@lucyleeow lucyleeow commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Towards #28668
Related to #32755

What does this implement/fix? Explain your changes.

Adds a common test for mixed array inputs to estimators:

  • Re-uses most of check_array_api_input. The following small things are changed:
    • dtype_name used to default to "float64", now the default is None and _max_precision_float_dtype is used. AFAICT all uses of this check was used with yield_namespace_device_dtype_combinations, thus we never used the default value. Regardless _max_precision_float_dtype seems sensible.
    • sample_weight check now has a guard where it is only checked if the estimator has a sample_weight parameter in fit
  • Added a check_array_api_mixed_inputs function, which is almost the same as check_array_api_input, except with different signature. I could have made these a single function but decided not to because (though not 100% on this):
    • I can specifically add check_array_api_mixed_inputs to PER_ESTIMATOR_XFAIL_CHECKS
    • the 2 checks take inputs from yield_namespace_device_dtype_combinations and yield_namespace_device_dtype_combinations which output different things, thus it is a bit cleaner to handle this by having the two check_array_api* functions have different signatures
    • Edited to add: we can have different defaults for check_array_api_input and check_array_api_mixed_inputs - e.g., check_sample_weight is by default turned on for check_array_api_mixed_inputs

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

Comment thread sklearn/utils/estimator_checks.py Outdated
X, y = make_classification(n_samples=30, n_features=10, random_state=42)
if dtype_name is None:
dtype_name = _max_precision_float_dtype(
to_ns_and_device.xp, to_ns_and_device.device

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
to_ns_and_device.xp, to_ns_and_device.device
xp_to, to_ns_and_device.device

I think this is why the CI jobs are failing. We have to pass the actual namespace and not the name of the namespace.

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 it worth naming the attribute to_ns_and_device.xp_name to make it easier to remember that it contains the name of the namespace, not the actual namespace?

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

I like it!

A few comments below. They are mostly about tweaking text and naming.

fit_kwargs = {}
fit_kwargs_xp = {}
if check_sample_weight:
if check_sample_weight and has_fit_parameter(estimator_orig, "sample_weight"):

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.

Why do we now need the has_fit_parameter? It makes sense to check this, but I couldn't work out why we didn't use to need this additional check

@lucyleeow lucyleeow Mar 20, 2026 •

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.

The default was check_sample_weight=False and searching for check_array_api_input uses in the codebase, I don't think we ever set check_sample_weight=True - so I think we never used it?

For the mixed check check_array_api_mixed_inputs - I set the default to be check_sample_weight=True - thus I added the guard...
Maybe it was added initially for the extra utility but we never felt it was needed? 🤷

Comment thread sklearn/utils/estimator_checks.py Outdated
Comment on lines +1116 to +1119
input_ns = get_namespace(X_xp)[0].__name__

# Fitted attributes which are arrays must have the same
# namespace as the one of the training data.
X_ns = xp_to.__name__

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.

Instead of using X_ns could we use to_ns_and_device.xp?

I've got myself confused about to_ns_and_device and from_ns_and_device, which one is which. Maybe we can rename them to make it easier to keep track? Maybe X_ns_and_device and other_ns_and_device? The thing I get confused about is reasoning which namespace/device X should be in and which one y and friends should be in.

@lucyleeow lucyleeow Mar 25, 2026 •

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.

Yeah I would tend to agree with you. I wanted to be consistent with naming used in #32755

For the 'reference' ns we are converting to - I think X_ns_and_device / y_pred_ns_and_device would be nice (though wordy) as it 're-iterates' the "everything follows X/y_pred" idea.

For the other one, that we are converting from, I would be okay with "other". We could also use "aux" as in auxillary parameters or "coerced" ?

Or more general naming we could use "target_ns_and_device" and "source_ns_and_device" ?

Open to other and others suggestions as well.

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.

I think target and source would still end up causing confusions because I think we would need the X namespace to be target and the other one to be source. But since X seems like the main input, it would cause confusion with the word source. Maybe we can use X_ns and other_ns as that seems to be clearer considering we already know that we want to convert to the namespace of X. Otherwise we can simply keep the naming as it is currently.

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.

@lucyleeow Just to confirm, we are keeping the current naming?

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.

Whoops thanks for the reminder!

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.

Thanks for your patience @OmarManzoor @betatim , I've updated the naming to X_ns_and_device and other_ns_and_device, and xp_X etc accordingly.

If you're happy this is ready to go in. I would like to also update the naming in test_common.py for metrics to be y_pred_ns_and_device and other_ns_and_device for consistency, but can do this in a separate PR.

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.

Have you pushed these changes because I don't see them? Also there doesn't seem to be a new commit.

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.

Indeed, forgot to push. Done now, thanks!

Comment thread sklearn/utils/estimator_checks.py Outdated
X, y = make_classification(n_samples=30, n_features=10, random_state=42)
if dtype_name is None:
dtype_name = _max_precision_float_dtype(
to_ns_and_device.xp, to_ns_and_device.device

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 it worth naming the attribute to_ns_and_device.xp_name to make it easier to remember that it contains the name of the namespace, not the actual namespace?

Comment thread sklearn/utils/estimator_checks.py Resolved
Comment thread sklearn/utils/estimator_checks.py Resolved
@betatim betatim moved this to In Progress in Array API Mar 19, 2026
@ogrisel

ogrisel commented Mar 20, 2026

Copy link
Copy Markdown
Member

There is a bit of conflict resolution work to do after the array API testing infra refactoring.

@lucyleeow

Copy link
Copy Markdown
Member Author

Linux x86-64 pylatest_conda_forge_mkl is failing due to doctest check of check_estimator function:

>>> from sklearn.utils.estimator_checks import check_estimator
>>> from sklearn.linear_model import LogisticRegression
>>> check_estimator(LogisticRegression())

While LogisticRegression is listed as failing check_array_api_mixed_inputs in PER_ESTIMATOR_XFAIL_CHECKS, the example calls check_estimator without getting expected fail tests.

Not sure what the best option is here. We could:

@ogrisel

ogrisel commented Apr 1, 2026 •

Copy link
Copy Markdown
Member

Not sure what the best option is here. We could:
change the estimator checked to one that has mixed array support (I see #33633)
skip doctest for this line until we add mixed array support to LogisticRegression

Either solution would be fine with me. @lucyleeow please implement the easiest ;)

EDIT: third option: pass expected_failed_checks={LogisticRegression:{"check_array_api_mixed_inputs": "TODO"}} to the doctest but this is a bit ugly.

EDIT2: fourth option: fix LogisticRegression as part of this PR directly.

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

This looks good but there are a few cases that already XPASS, let's remove them from the expected XFAIL list.

),
},
GridSearchCV: {
"check_array_api_mixed_inputs": "mixed array API input support not added yet",

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
"check_array_api_mixed_inputs": "mixed array API input support not added yet",

Comment thread sklearn/utils/estimator_checks.py Resolved
Comment thread sklearn/utils/estimator_checks.py Outdated
assert array_device(result_xp) == array_device(X_xp)

result_xp_np = move_to(result_xp, xp=np, device="cpu")
result_xp_np = _convert_to_numpy(result_xp, xp=xp_to)

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.

Let's use move_to here as well.

Comment thread sklearn/utils/estimator_checks.py Resolved
@github-actions github-actions Bot removed the CUDA CI label Apr 13, 2026
@adrinjalali adrinjalali added this to Labs Apr 13, 2026
@adrinjalali adrinjalali moved this to In progress in Labs Apr 13, 2026

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

This looks good to me. At least I've looked at it often enough that I can't spot any shortcomings anymore :D

@lucyleeow lucyleeow added the Waiting for Second Reviewer First reviewer is done, need a second one! label Apr 13, 2026

@OmarManzoor OmarManzoor left a comment

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.

Only a few minor comments, otherwise looks good. Thank you for the PR @lucyleeow

Comment thread sklearn/utils/estimator_checks.py Resolved
Comment thread sklearn/utils/estimator_checks.py Resolved
Comment thread sklearn/utils/estimator_checks.py Resolved

@OmarManzoor OmarManzoor left a comment

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.

LGTM. Thank you for the PR @lucyleeow

Just one minor suggestion to maintain naming consistency

Comment thread sklearn/utils/estimator_checks.py Resolved
@lucyleeow
lucyleeow enabled auto-merge (squash) April 25, 2026 00:26
@lucyleeow

Copy link
Copy Markdown
Member Author

Thanks all!

@lucyleeow
lucyleeow merged commit 35bf8c6 into scikit-learn:main Apr 25, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Apr 25, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Apr 25, 2026
@lucyleeow
lucyleeow deleted the tst_mixed_input_estimator branch April 27, 2026 02:00
@betatim

betatim commented Apr 27, 2026

Copy link
Copy Markdown
Member

Whoop whoop! Long term effort paying off :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants