Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
convert_arrays: run the pinned copy with the array's device current
ndarray.get() takes the current device's current stream before switching
to the array's device, so with another device current the transfer and
the preceding contiguous copy could run on unordered streams. Keep the
array's device current for the whole function, as get(order="A") does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
maxwbuckley and claude committed Nov 13, 2026
commit bf036d88576add52926687ee272726445f1a9689
34 changes: 19 additions & 15 deletions python/cuml/cuml/internals/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,22 +471,26 @@ def _cupy_to_numpy(obj):
_PINNED_OUTPUT_MIN_BYTES <= obj.nbytes <= _PINNED_OUTPUT_MAX_BYTES
):
return obj.get(order="A")
if not (obj.flags.c_contiguous or obj.flags.f_contiguous):
# `get(order="A")` would make the same device-side copy, on the
# array's own device
with obj.device:
# `get()` takes the current stream of the *current* device, then
# switches to the array's device. Make the array's device current for
# the whole function so the contiguous copy below and the transfer run
# on the same stream, as they do inside a single `get(order="A")`.
with obj.device:
if not (obj.flags.c_contiguous or obj.flags.f_contiguous):
obj = cp.ascontiguousarray(obj)
order = (
"F" if obj.flags.f_contiguous and not obj.flags.c_contiguous else "C"
)
try:
mem = cp.cuda.alloc_pinned_memory(obj.nbytes)
except cp.cuda.runtime.CUDARuntimeError:
# Out of pinned memory: fall back to pageable
return obj.get(order="A")
out = np.frombuffer(mem, dtype=obj.dtype, count=obj.size)
out = out.reshape(obj.shape, order=order)
obj.get(out=out)
order = (
"F"
if obj.flags.f_contiguous and not obj.flags.c_contiguous
else "C"
)
try:
mem = cp.cuda.alloc_pinned_memory(obj.nbytes)
except cp.cuda.runtime.CUDARuntimeError:
# Out of pinned memory: fall back to pageable
return obj.get(order="A")
out = np.frombuffer(mem, dtype=obj.dtype, count=obj.size)
out = out.reshape(obj.shape, order=order)
obj.get(out=out)
return out


Expand Down
19 changes: 19 additions & 0 deletions python/cuml/tests/test_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,25 @@ def test_convert_arrays_numpy_pinned(monkeypatch, layout):
assert cp.any(X != 0)


@pytest.mark.skipif(
cp.cuda.runtime.getDeviceCount() < 2, reason="needs two GPUs"
)
def test_convert_arrays_numpy_pinned_other_device(monkeypatch):
"""A non-contiguous output on a device that isn't current, whose current
stream is non-blocking, is copied after its contiguous copy completes"""
import cuml.internals.outputs as outputs

monkeypatch.setattr(outputs, "_PINNED_OUTPUT_MIN_BYTES", 0)
monkeypatch.setattr(outputs, "_PINNED_OUTPUT_MAX_BYTES", 1 << 62)
with cp.cuda.Device(1), cp.cuda.Stream(non_blocking=True):
X = cp.random.default_rng(42).standard_normal((4096, 4096))[:, ::2]
expected = cp.asnumpy(X)
with cp.cuda.Device(0):
for _ in range(5):
out = convert_arrays(X, "numpy")
np.testing.assert_array_equal(out, expected)


@pytest.mark.parametrize("limit", ["below-min", "above-max", "empty"])
def test_convert_arrays_numpy_pageable_fallback(monkeypatch, limit):
"""Outputs outside the size limits keep the pageable `get()` path"""
Expand Down
Loading