Skip to content

Add common test for mixed y string and array API X for estimators - #34142

Merged
ogrisel merged 23 commits into
scikit-learn:mainfrom
lucyleeow:tst_est_str
Jul 1, 2026
Merged

ogrisel merged 23 commits into
scikit-learn:mainfrom
lucyleeow:tst_est_str

Conversation

@lucyleeow

Copy link
Copy Markdown
Member

Reference Issues/PRs

closes #34094

What does this implement/fix? Explain your changes.

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?

@lucyleeow
lucyleeow marked this pull request as draft May 28, 2026 04:40
@ogrisel

ogrisel commented May 28, 2026

Copy link
Copy Markdown
Member

Thanks for starting this PR. I think we should expand the documentation to document the expectations we encode in this new test.

@lucyleeow

Copy link
Copy Markdown
Member Author

I would like to clean up _yield_array_api_checks and all the checks that use _check_array_api_core. I think I can add logic to _check_name for handling partials, such that we do not need 3 separate functions (and potentially more if we want to add pandas/list checks later) to be able to separately specify which ones would fail in PER_ESTIMATOR_XFAIL_CHECKS. This should allow me to clean up _yield_array_api_checks.
_check_array_api_core could do with some clean up too as it is getting quite long.

I think the changes I have in mind are too significant to be part of this PR, so although I find the current test code messy, I think it is best if I make the refactoring and clean up in a separate PR.

@lucyleeow
lucyleeow marked this pull request as ready for review June 3, 2026 09:52
@github-actions github-actions Bot removed the CUDA CI label Jun 3, 2026
@lucyleeow

Copy link
Copy Markdown
Member Author

ping maybe @OmarManzoor @betatim ?

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

@lucyleeow thank you for the PR. A few questions?

Comment on lines 987 to 993
GaussianNB: {
"check_array_api_mixed_inputs": "mixed array API input support not added yet",
"check_array_api_string_and_numeric_inputs": (
"mixed string and numeric array API input support not added yet"
),
"check_array_api_same_namespace": "check_same_namespace not yet added",
},

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 you tried out GaussianMixture instead of GaussianNB. Does this not work with GaussianNB?

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.

Interestingly GaussianNB passes check_array_api_string_and_numeric_inputs but fails check_array_api_mixed_inputs.
The failure is because torch.asarray does not work on an array api strict boolean array (though it does work on numpy boolean array). I am looking into whether/how this is a torch bug.

Details
In [6]: import array_api_strict as xp

In [7]: a_strict = xp.asarray([True,False])

In [8]: torch.asarray(a_strict)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 1
----> 1 torch.asarray(a_strict)

ValueError: buffer length (2 bytes) after offset (0 bytes) must be a multiple of element size (4)

AFAICT the test should pass for GaussianNB

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.

Would it help to use move_to at the point where this fails?

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 can't quite tell if this is a torch bug but I opened an issue: pytorch/pytorch#188128

I don't quite understand why locally I get a different error vs what we get in CI, from https://github.com/scikit-learn/scikit-learn/actions/runs/28084989224/job/83148860207?pr=34142:

>       return torch.asarray(obj, dtype=dtype, device=device, copy=copy, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E       RuntimeError: could not retrieve buffer from object

copy       = None
device     = device(type='cpu')
dtype      = None
kwargs     = {}
obj        = Array([ True,
        True,
        True,
        True,
       False,
       False,
       False,
        True,
      ...lse,
        True,
       False,
        True], dtype=array_api_strict.bool, device=array_api_strict.Device('device1'))

This should be equivalent to torch.asarray(a_strict) - I think cpu is the default device.

I have the same pytorch version as in CI (2.12), though I installed through pip and CI is through conda-forge. What do other people get?

Would it help to use move_to at the point where this fails?

Yes torch.from_dlpack(a_strict, device='cpu') will give a boolean tensor. Is this what we want to do? 🤔 I think one problem is that move_to does not always handle lists (#34046)

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.

Okay, this is indeed a pytorch bug.

Why don't I keep the xfail with the reason being the pytorch bug and it can be removed once fixed in pytorch and released.

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.

Yes I think we can keep the xfail and merge this PR

@lucyleeow

Copy link
Copy Markdown
Member Author

Thanks all! I think this is ready for another review 🙏

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

Minor comments related to the documentation otherwise looks good now. Thank you @lucyleeow

Comment thread doc/modules/array_api.rst Resolved
Comment thread doc/modules/array_api.rst Resolved
Comment thread sklearn/utils/estimator_checks.py Resolved
Comment thread doc/modules/array_api.rst Resolved
Co-authored-by: Omar Salman <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
@ogrisel ogrisel added the CUDA CI label Jul 1, 2026
@github-actions github-actions Bot removed the CUDA CI label Jul 1, 2026

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

@ogrisel
ogrisel enabled auto-merge (squash) July 1, 2026 15:20
@ogrisel
ogrisel merged commit d1f1734 into scikit-learn:main Jul 1, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Jul 1, 2026
@ogrisel

ogrisel commented Jul 1, 2026

Copy link
Copy Markdown
Member

Thanks @lucyleeow! Now we have to fix those XFAILed test cases :)

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.

TST Add common test for mixed y string and array API X for estimators

5 participants