|
5 | 5 | from cupy.cuda import Stream |
6 | 6 |
|
7 | 7 |
|
8 | | -def test_to_device_with_stream(): |
| 8 | +@pytest.mark.parametrize( |
| 9 | + "make_stream", |
| 10 | + [ |
| 11 | + lambda: Stream(), |
| 12 | + lambda: Stream(non_blocking=True), |
| 13 | + lambda: Stream(null=True), |
| 14 | + lambda: Stream(ptds=True), |
| 15 | + ], |
| 16 | +) |
| 17 | +def test_to_device_with_stream(make_stream): |
9 | 18 | devices = xp.__array_namespace_info__().devices() |
10 | | - streams = [ |
11 | | - Stream(), |
12 | | - Stream(non_blocking=True), |
13 | | - Stream(null=True), |
14 | | - Stream(ptds=True), |
15 | | - 123, # dlpack stream |
16 | | - ] |
17 | 19 |
|
18 | 20 | a = xp.asarray([1, 2, 3]) |
19 | 21 | for dev in devices: |
20 | | - for stream in streams: |
21 | | - b = to_device(a, dev, stream=stream) |
22 | | - assert device(b) == dev |
| 22 | + # Streams are device-specific and must be created within |
| 23 | + # the context of the device... |
| 24 | + with dev: |
| 25 | + stream = make_stream() |
| 26 | + # ... however, to_device() does not need to be inside the |
| 27 | + # device context. |
| 28 | + b = to_device(a, dev, stream=stream) |
| 29 | + assert device(b) == dev |
| 30 | + |
| 31 | + |
| 32 | +def test_to_device_with_dlpack_stream(): |
| 33 | + devices = xp.__array_namespace_info__().devices() |
| 34 | + |
| 35 | + a = xp.asarray([1, 2, 3]) |
| 36 | + for dev in devices: |
| 37 | + # Streams are device-specific and must be created within |
| 38 | + # the context of the device... |
| 39 | + with dev: |
| 40 | + s1 = Stream() |
| 41 | + |
| 42 | + # ... however, to_device() does not need to be inside the |
| 43 | + # device context. |
| 44 | + b = to_device(a, dev, stream=s1.ptr) |
| 45 | + assert device(b) == dev |
0 commit comments