Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX avoid torch dlpack crash for negative strides - #34380

Merged
virchan merged 3 commits into
scikit-learn:mainfrom
yinli-systems:codex/fix-torch-negative-strides-dlpack
Jul 9, 2026
Merged

FIX avoid torch dlpack crash for negative strides#34380
virchan merged 3 commits into
scikit-learn:mainfrom
yinli-systems:codex/fix-torch-negative-strides-dlpack

Conversation

@yinli-systems

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #34307.
Related to pytorch/pytorch#188023.

What does this implement/fix? Explain your changes.

move_to currently 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_to first makes the array contiguous with numpy.ascontiguousarray before calling xp.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 -q

Result: 1 passed, 6 skipped.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.
  • The pull request addresses an existing issue that is ready for
    contribution (e.g. not tagged as "Needs Triage", "Needs Decision", ...).
    If you are proposing a new feature, please open an issue to discuss it first.
  • There are no other open pull requests already targeting the same issue.
  • You have followed the pull request checklist.
    In particular, linting and tests should pass.

@yinli-systems

Copy link
Copy Markdown
Contributor Author

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.

@yinli-systems
yinli-systems force-pushed the codex/fix-torch-negative-strides-dlpack branch from 579c68b to 197081f Compare June 28, 2026 10:59

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

The change LGTM besides the following detail.

Comment thread sklearn/utils/_array_api.py Outdated
@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 moved this to In Progress in Array API Jul 1, 2026
@ogrisel

ogrisel commented Jul 1, 2026

Copy link
Copy Markdown
Member

Also, please disclose AI usage when making PRs as requested in the PR template.

@yinli-systems

yinli-systems commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, addressed in the latest push. The negative-stride workaround now uses nested conditions so that array.strides is only accessed after confirming the source namespace is NumPy.

Local validation:

  • git diff --check
  • PYTHONPYCACHEPREFIX=/private/tmp/sklearn-pr34380-pycache /usr/bin/python3 -m py_compile sklearn/utils/_array_api.py sklearn/utils/tests/test_array_api.py

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 sklearn.__check_build._check_build import error before collecting the test.

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 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. Any second review @betatim @virchan @OmarManzoor?

Comment thread doc/whats_new/upcoming_changes/sklearn.utils/34380.fix.rst Outdated
Co-authored-by: Olivier Grisel <[email protected]>

@virchan virchan 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! Thanks, @Kevin-Li-2025!

I applied #34380 (comment) directly, and enabled auto-merge.

@virchan
virchan enabled auto-merge (squash) July 9, 2026 07:54
@virchan
virchan merged commit f801a5e into scikit-learn:main Jul 9, 2026
53 of 54 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Jul 9, 2026
@betatim

betatim commented Jul 9, 2026

Copy link
Copy Markdown
Member

I don't know why the "check job statuses" job was cancelled, I restarted it and it succeeded

@yinli-systems
yinli-systems deleted the codex/fix-torch-negative-strides-dlpack branch August 29, 2026 09:17
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
Co-authored-by: Kevin-Li-2025 <[email protected]>
Co-authored-by: Virgil Chan <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 9, 2026
Co-authored-by: Kevin-Li-2025 <[email protected]>
Co-authored-by: Virgil Chan <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
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.

Using move_to on a numpy array with negative stride crashes the Python process

4 participants