FIX avoid torch dlpack crash for negative strides - #34380
Conversation
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
|
Status note: all test/lint/docs/codecov checks are passing. The only red check is the repository changelog gate ("A reviewer will let you know if it is required or can be bypassed"), so I am leaving it for reviewer guidance rather than adding a changelog entry preemptively. |
579c68b to
197081f
Compare
ogrisel
left a comment
There was a problem hiding this comment.
The change LGTM besides the following detail.
|
Also, please disclose AI usage when making PRs as requested in the PR template. |
|
Thanks, addressed in the latest push. The negative-stride workaround now uses nested conditions so that Local validation:
I also attempted the targeted pytest in the temporary checkout, but this fresh clone has not been built with Meson, so pytest stops at the expected Also, to disclose AI usage as requested in the PR template: I used AI assistance while preparing this PR, mainly to help reason about the edge case and organize/check local validation. I reviewed the final code changes and validation output myself before submitting. |
ogrisel
left a comment
There was a problem hiding this comment.
LGTM. Any second review @betatim @virchan @OmarManzoor?
Co-authored-by: Olivier Grisel <[email protected]>
virchan
left a comment
There was a problem hiding this comment.
LGTM! Thanks, @Kevin-Li-2025!
I applied #34380 (comment) directly, and enabled auto-merge.
|
I don't know why the "check job statuses" job was cancelled, I restarted it and it succeeded |
Co-authored-by: Kevin-Li-2025 <[email protected]> Co-authored-by: Virgil Chan <[email protected]> Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Kevin-Li-2025 <[email protected]> Co-authored-by: Virgil Chan <[email protected]> Co-authored-by: Olivier Grisel <[email protected]>
Reference Issues/PRs
Fixes #34307.
Related to pytorch/pytorch#188023.
What does this implement/fix? Explain your changes.
move_tocurrently transfers NumPy arrays to torch through DLPack when possible. PyTorch aborts the Python process when importing a NumPy array with negative strides through DLPack, before scikit-learn can fall back to a safer conversion path.This adds a narrow workaround for the affected path: when the source array is NumPy, the target namespace is torch, and any stride is negative,
move_tofirst makes the array contiguous withnumpy.ascontiguousarraybefore callingxp.from_dlpack. Other namespaces and non-negative-strided NumPy arrays continue to use the existing path.The regression test runs in a subprocess because the previous failure mode aborts the Python process instead of raising a Python exception.
Any other comments?
Validation run locally:
MPLCONFIGDIR=/private/tmp/mplconfig LOKY_MAX_CPU_COUNT=8 PATH="$PWD/.venv/bin:$PATH" .venv/bin/python -m pytest sklearn/utils/tests/test_array_api.py::test_move_to_numpy_negative_strides_to_torch sklearn/utils/tests/test_array_api.py::test_move_to_array_api_conversions sklearn/utils/tests/test_array_api.py::test_move_to_sparse -qResult:
1 passed, 6 skipped.