Repository navigation
TST Add common test for mixed namespace/device array inputs to estimators - #33525
Conversation
| 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 |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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"): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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? 🤷
| 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__ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@lucyleeow Just to confirm, we are keeping the current naming?
There was a problem hiding this comment.
Whoops thanks for the reminder!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Have you pushed these changes because I don't see them? Also there doesn't seem to be a new commit.
There was a problem hiding this comment.
Indeed, forgot to push. Done now, thanks!
| 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 |
There was a problem hiding this comment.
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?
|
There is a bit of conflict resolution work to do after the array API testing infra refactoring. |
|
Linux x86-64 pylatest_conda_forge_mkl is failing due to doctest check of scikit-learn/sklearn/utils/estimator_checks.py Lines 853 to 855 in cc4843a While Not sure what the best option is here. We could:
|
Either solution would be fine with me. @lucyleeow please implement the easiest ;) EDIT: third option: pass EDIT2: fourth option: fix |
ogrisel
left a comment
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
| "check_array_api_mixed_inputs": "mixed array API input support not added yet", |
| 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) |
betatim
left a comment
There was a problem hiding this comment.
This looks good to me. At least I've looked at it often enough that I can't spot any shortcomings anymore :D
OmarManzoor
left a comment
There was a problem hiding this comment.
Only a few minor comments, otherwise looks good. Thank you for the PR @lucyleeow
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thank you for the PR @lucyleeow
Just one minor suggestion to maintain naming consistency
Co-authored-by: Omar Salman <[email protected]>
|
Thanks all! |
|
Whoop whoop! Long term effort paying off :D |
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:
check_array_api_input. The following small things are changed:dtype_nameused to default to "float64", now the default isNoneand_max_precision_float_dtypeis used. AFAICT all uses of this check was used withyield_namespace_device_dtype_combinations, thus we never used the default value. Regardless_max_precision_float_dtypeseems sensible.sample_weightcheck now has a guard where it is only checked if the estimator has asample_weightparameter infitcheck_array_api_mixed_inputsfunction, which is almost the same ascheck_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):check_array_api_mixed_inputstoPER_ESTIMATOR_XFAIL_CHECKSyield_namespace_device_dtype_combinationsandyield_namespace_device_dtype_combinationswhich output different things, thus it is a bit cleaner to handle this by having the twocheck_array_api*functions have different signaturescheck_array_api_inputandcheck_array_api_mixed_inputs- e.g.,check_sample_weightis by default turned on forcheck_array_api_mixed_inputsAI usage disclosure
I used AI assistance for:
Any other comments?