From 770f39f482c810c9572d73a0de1765b021ade45a Mon Sep 17 00:00:00 2001 From: ksimpson Date: Mon, 4 Nov 2024 10:37:59 -0800 Subject: [PATCH 1/9] move away from internal entry-points, and clean up the fixture use in the testsuite --- cuda_core/tests/conftest.py | 18 ++++++++++-- cuda_core/tests/test_device.py | 6 +--- cuda_core/tests/test_event.py | 45 +++++++++++++++++++----------- cuda_core/tests/test_launcher.py | 4 +-- cuda_core/tests/test_stream.py | 48 ++++++++++++++++---------------- 5 files changed, 72 insertions(+), 49 deletions(-) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 3ff6ce08..ac4e2f22 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -7,10 +7,24 @@ # is strictly prohibited. from cuda.core.experimental._device import Device +from cuda.core.experimental import _device +from cuda import cuda +from cuda.core.experimental._utils import handle_return import pytest -@pytest.fixture(scope="module") +@pytest.fixture(scope="module", autouse=True) +def ensure_no_context(): + device = Device() + device.set_current() + with _device._tls_lock: + if hasattr(_device._tls, 'devices'): + del _device._tls.devices + +@pytest.fixture(scope="function") def init_cuda(): device = Device() device.set_current() - \ No newline at end of file + yield + handle_return(cuda.cuCtxPopCurrent()) + with _device._tls_lock: + del _device._tls.devices \ No newline at end of file diff --git a/cuda_core/tests/test_device.py b/cuda_core/tests/test_device.py index 653dac06..65aa0696 100644 --- a/cuda_core/tests/test_device.py +++ b/cuda_core/tests/test_device.py @@ -24,11 +24,7 @@ def test_device_alloc(init_cuda): assert buffer.size == 1024 assert buffer.device_id == 0 -def test_device_set_current(): - device = Device() - device.set_current() - -def test_device_create_stream(): +def test_device_create_stream(init_cuda): device = Device() stream = device.create_stream() assert stream is not None diff --git a/cuda_core/tests/test_event.py b/cuda_core/tests/test_event.py index b6cfe647..0649df53 100644 --- a/cuda_core/tests/test_event.py +++ b/cuda_core/tests/test_event.py @@ -6,34 +6,47 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda import cuda from cuda.core.experimental._event import EventOptions, Event -from cuda.core.experimental._utils import handle_return from cuda.core.experimental._device import Device import pytest -def test_is_timing_disabled(): +def test_is_timing_disabled(init_cuda): options = EventOptions(enable_timing=False) - event = Event._init(options) + stream = Device().create_stream() + event = stream.record(options=options) assert event.is_timing_disabled == True + + options = EventOptions(enable_timing=True) + stream = Device().create_stream() + event = stream.record(options=options) + assert event.is_timing_disabled == False -def test_is_sync_busy_waited(): - options = EventOptions(busy_waited_sync=True) - event = Event._init(options) +def test_is_sync_busy_waited(init_cuda): + options = EventOptions(enable_timing=False, busy_waited_sync=True) + stream = Device().create_stream() + event = stream.record(options=options) assert event.is_sync_busy_waited == True -def test_sync(): - options = EventOptions() - event = Event._init(options) + options = EventOptions(enable_timing=False) + stream = Device().create_stream() + event = stream.record(options=options) + assert event.is_sync_busy_waited == False + +def test_sync(init_cuda): + options = EventOptions(enable_timing=False) + stream = Device().create_stream() + event = stream.record(options=options) event.sync() assert event.is_done == True -def test_is_done(): - options = EventOptions() - event = Event._init(options) +def test_is_done(init_cuda): + options = EventOptions(enable_timing=False) + stream = Device().create_stream() + event = stream.record(options=options) assert event.is_done == True -def test_handle(): - options = EventOptions() - event = Event._init(options) +def test_handle(init_cuda): + options = EventOptions(enable_timing=False) + stream = Device().create_stream() + event = stream.record(options=options) assert isinstance(event.handle, int) diff --git a/cuda_core/tests/test_launcher.py b/cuda_core/tests/test_launcher.py index 92dfc726..45e1b6fa 100644 --- a/cuda_core/tests/test_launcher.py +++ b/cuda_core/tests/test_launcher.py @@ -13,7 +13,7 @@ from cuda.core.experimental._utils import handle_return import pytest -def test_launch_config_init(): +def test_launch_config_init(init_cuda): config = LaunchConfig(grid=(1, 1, 1), block=(1, 1, 1), stream=None, shmem_size=0) assert config.grid == (1, 1, 1) assert config.block == (1, 1, 1) @@ -50,7 +50,7 @@ def test_launch_config_invalid_values(): with pytest.raises(ValueError): LaunchConfig(grid=(1, 1, 1), block=(0, 1)) -def test_launch_config_stream(): +def test_launch_config_stream(init_cuda): stream = Device().create_stream() config = LaunchConfig(grid=(1, 1, 1), block=(1, 1, 1), stream=stream, shmem_size=0) assert config.stream == stream diff --git a/cuda_core/tests/test_stream.py b/cuda_core/tests/test_stream.py index e0a98c18..885ef8e4 100644 --- a/cuda_core/tests/test_stream.py +++ b/cuda_core/tests/test_stream.py @@ -15,59 +15,59 @@ def test_stream_init(): with pytest.raises(NotImplementedError): Stream() -def test_stream_init_with_options(): - stream = Stream._init(options=StreamOptions(nonblocking=True, priority=0)) +def test_stream_init_with_options(init_cuda): + stream = Device().create_stream(options=StreamOptions(nonblocking=True, priority=0)) assert stream.is_nonblocking is True assert stream.priority == 0 -def test_stream_handle(): - stream = Stream._init(options=StreamOptions()) +def test_stream_handle(init_cuda): + stream = Device().create_stream(options=StreamOptions()) assert isinstance(stream.handle, int) -def test_stream_is_nonblocking(): - stream = Stream._init(options=StreamOptions(nonblocking=True)) +def test_stream_is_nonblocking(init_cuda): + stream = Device().create_stream(options=StreamOptions(nonblocking=True)) assert stream.is_nonblocking is True -def test_stream_priority(): - stream = Stream._init(options=StreamOptions(priority=0)) +def test_stream_priority(init_cuda): + stream = Device().create_stream(options=StreamOptions(priority=0)) assert stream.priority == 0 - stream = Stream._init(options=StreamOptions(priority=-1)) + stream = Device().create_stream(options=StreamOptions(priority=-1)) assert stream.priority == -1 with pytest.raises(ValueError): - stream = Stream._init(options=StreamOptions(priority=1)) + stream = Device().create_stream(options=StreamOptions(priority=1)) -def test_stream_sync(): - stream = Stream._init(options=StreamOptions()) +def test_stream_sync(init_cuda): + stream = Device().create_stream(options=StreamOptions()) stream.sync() # Should not raise any exceptions -def test_stream_record(): - stream = Stream._init(options=StreamOptions()) +def test_stream_record(init_cuda): + stream = Device().create_stream(options=StreamOptions()) event = stream.record() assert isinstance(event, Event) -def test_stream_record_invalid_event(): - stream = Stream._init(options=StreamOptions()) +def test_stream_record_invalid_event(init_cuda): + stream = Device().create_stream(options=StreamOptions()) with pytest.raises(TypeError): stream.record(event="invalid_event") -def test_stream_wait_event(): - stream = Stream._init(options=StreamOptions()) +def test_stream_wait_event(init_cuda): + stream = Device().create_stream(options=StreamOptions()) event = Event._init() stream.record(event) stream.wait(event) # Should not raise any exceptions -def test_stream_wait_invalid_event(): - stream = Stream._init(options=StreamOptions()) +def test_stream_wait_invalid_event(init_cuda): + stream = Device().create_stream(options=StreamOptions()) with pytest.raises(ValueError): stream.wait(event_or_stream="invalid_event") -def test_stream_device(): - stream = Stream._init(options=StreamOptions()) +def test_stream_device(init_cuda): + stream = Device().create_stream(options=StreamOptions()) device = stream.device assert isinstance(device, Device) -def test_stream_context(): - stream = Stream._init(options=StreamOptions()) +def test_stream_context(init_cuda): + stream = Device().create_stream(options=StreamOptions()) context = stream.context assert context is not None From b1c17d565de83fbf7c04d3e95c9d8c521fcb0a58 Mon Sep 17 00:00:00 2001 From: ksimpson Date: Mon, 4 Nov 2024 11:17:39 -0800 Subject: [PATCH 2/9] switch to deint and keep everything at function level scope --- cuda_core/cuda/core/experimental/_context.py | 3 +-- cuda_core/cuda/core/experimental/_device.py | 3 +-- cuda_core/cuda/core/experimental/_launcher.py | 4 +--- cuda_core/cuda/core/experimental/_memory.py | 1 - cuda_core/cuda/core/experimental/_module.py | 2 +- cuda_core/cuda/core/experimental/utils.py | 1 - cuda_core/tests/conftest.py | 21 ++++++++++++------- .../example_tests/test_basic_examples.py | 2 +- cuda_core/tests/example_tests/utils.py | 1 - cuda_core/tests/test_device.py | 9 +++++--- cuda_core/tests/test_event.py | 3 +-- cuda_core/tests/test_launcher.py | 2 -- cuda_core/tests/test_memory.py | 1 - cuda_core/tests/test_module.py | 5 +---- cuda_core/tests/test_program.py | 1 - cuda_core/tests/test_stream.py | 2 +- 16 files changed, 28 insertions(+), 33 deletions(-) diff --git a/cuda_core/cuda/core/experimental/_context.py b/cuda_core/cuda/core/experimental/_context.py index 216953ab..3ea389b9 100644 --- a/cuda_core/cuda/core/experimental/_context.py +++ b/cuda_core/cuda/core/experimental/_context.py @@ -4,8 +4,7 @@ from dataclasses import dataclass -from cuda import cuda, cudart -from cuda.core.experimental._utils import handle_return +from cuda import cuda @dataclass diff --git a/cuda_core/cuda/core/experimental/_device.py b/cuda_core/cuda/core/experimental/_device.py index 65d5fe9b..b1433ef0 100644 --- a/cuda_core/cuda/core/experimental/_device.py +++ b/cuda_core/cuda/core/experimental/_device.py @@ -3,8 +3,7 @@ # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE import threading -from typing import Optional, Union -import warnings +from typing import Union from cuda import cuda, cudart from cuda.core.experimental._utils import handle_return, ComputeCapability, CUDAError, \ diff --git a/cuda_core/cuda/core/experimental/_launcher.py b/cuda_core/cuda/core/experimental/_launcher.py index 4b9533cb..1655da4e 100644 --- a/cuda_core/cuda/core/experimental/_launcher.py +++ b/cuda_core/cuda/core/experimental/_launcher.py @@ -6,11 +6,9 @@ import importlib.metadata from typing import Optional, Union -import numpy as np -from cuda import cuda, cudart +from cuda import cuda from cuda.core.experimental._kernel_arg_handler import ParamHolder -from cuda.core.experimental._memory import Buffer from cuda.core.experimental._module import Kernel from cuda.core.experimental._stream import Stream from cuda.core.experimental._utils import CUDAError, check_or_create_options, handle_return diff --git a/cuda_core/cuda/core/experimental/_memory.py b/cuda_core/cuda/core/experimental/_memory.py index 4ef2cbc3..6207d861 100644 --- a/cuda_core/cuda/core/experimental/_memory.py +++ b/cuda_core/cuda/core/experimental/_memory.py @@ -6,7 +6,6 @@ import abc from typing import Optional, Tuple, TypeVar -import warnings from cuda import cuda from cuda.core.experimental._dlpack import DLDeviceType, make_py_capsule diff --git a/cuda_core/cuda/core/experimental/_module.py b/cuda_core/cuda/core/experimental/_module.py index 60d4db97..4eacea1c 100644 --- a/cuda_core/cuda/core/experimental/_module.py +++ b/cuda_core/cuda/core/experimental/_module.py @@ -4,7 +4,7 @@ import importlib.metadata -from cuda import cuda, cudart +from cuda import cuda from cuda.core.experimental._utils import handle_return diff --git a/cuda_core/cuda/core/experimental/utils.py b/cuda_core/cuda/core/experimental/utils.py index 74f41e4d..0717b41a 100644 --- a/cuda_core/cuda/core/experimental/utils.py +++ b/cuda_core/cuda/core/experimental/utils.py @@ -2,4 +2,3 @@ # # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE -from cuda.core.experimental._memoryview import StridedMemoryView, viewable diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index ac4e2f22..ce3b5b95 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -12,18 +12,25 @@ from cuda.core.experimental._utils import handle_return import pytest -@pytest.fixture(scope="module", autouse=True) -def ensure_no_context(): - device = Device() - device.set_current() - with _device._tls_lock: - if hasattr(_device._tls, 'devices'): - del _device._tls.devices +# @pytest.fixture(scope="module", autouse=True) +# def ensure_no_context(): +# device = Device() +# device.set_current() +# with _device._tls_lock: +# if hasattr(_device._tls, 'devices'): +# del _device._tls.devices @pytest.fixture(scope="function") def init_cuda(): device = Device() device.set_current() + yield + handle_return(cuda.cuCtxPopCurrent()) + with _device._tls_lock: + del _device._tls.devices + +@pytest.fixture(scope="function") +def deinit_cuda(): yield handle_return(cuda.cuCtxPopCurrent()) with _device._tls_lock: diff --git a/cuda_core/tests/example_tests/test_basic_examples.py b/cuda_core/tests/example_tests/test_basic_examples.py index e490892d..c02ea43f 100644 --- a/cuda_core/tests/example_tests/test_basic_examples.py +++ b/cuda_core/tests/example_tests/test_basic_examples.py @@ -20,6 +20,6 @@ 'example', sample_files ) class TestExamples: - def test_example(self, example): + def test_example(self, example, deinit_cuda): filename = os.path.basename(example) run_example(samples_path, example) diff --git a/cuda_core/tests/example_tests/utils.py b/cuda_core/tests/example_tests/utils.py index 5f4e14b0..23a3018c 100644 --- a/cuda_core/tests/example_tests/utils.py +++ b/cuda_core/tests/example_tests/utils.py @@ -6,7 +6,6 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda import cuda import gc import os import sys diff --git a/cuda_core/tests/test_device.py b/cuda_core/tests/test_device.py index 65aa0696..eabaa57e 100644 --- a/cuda_core/tests/test_device.py +++ b/cuda_core/tests/test_device.py @@ -8,10 +8,13 @@ from cuda import cuda, cudart from cuda.core.experimental._device import Device -from cuda.core.experimental._utils import handle_return, ComputeCapability, CUDAError, \ - precondition -import pytest +from cuda.core.experimental._utils import handle_return, ComputeCapability +def test_device_set_current(deinit_cuda): + device = Device() + device.set_current() + assert handle_return(cuda.cuCtxGetCurrent()) is not None + def test_device_repr(): device = Device(0) assert str(device).startswith(' Date: Mon, 4 Nov 2024 11:22:07 -0800 Subject: [PATCH 3/9] remove ruff lint changes from this review --- cuda_core/cuda/core/experimental/_context.py | 3 ++- cuda_core/cuda/core/experimental/_device.py | 3 ++- cuda_core/cuda/core/experimental/_launcher.py | 4 +++- cuda_core/cuda/core/experimental/_memory.py | 1 + cuda_core/cuda/core/experimental/_module.py | 2 +- cuda_core/cuda/core/experimental/utils.py | 1 + 6 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cuda_core/cuda/core/experimental/_context.py b/cuda_core/cuda/core/experimental/_context.py index 3ea389b9..216953ab 100644 --- a/cuda_core/cuda/core/experimental/_context.py +++ b/cuda_core/cuda/core/experimental/_context.py @@ -4,7 +4,8 @@ from dataclasses import dataclass -from cuda import cuda +from cuda import cuda, cudart +from cuda.core.experimental._utils import handle_return @dataclass diff --git a/cuda_core/cuda/core/experimental/_device.py b/cuda_core/cuda/core/experimental/_device.py index b1433ef0..65d5fe9b 100644 --- a/cuda_core/cuda/core/experimental/_device.py +++ b/cuda_core/cuda/core/experimental/_device.py @@ -3,7 +3,8 @@ # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE import threading -from typing import Union +from typing import Optional, Union +import warnings from cuda import cuda, cudart from cuda.core.experimental._utils import handle_return, ComputeCapability, CUDAError, \ diff --git a/cuda_core/cuda/core/experimental/_launcher.py b/cuda_core/cuda/core/experimental/_launcher.py index 1655da4e..4b9533cb 100644 --- a/cuda_core/cuda/core/experimental/_launcher.py +++ b/cuda_core/cuda/core/experimental/_launcher.py @@ -6,9 +6,11 @@ import importlib.metadata from typing import Optional, Union +import numpy as np -from cuda import cuda +from cuda import cuda, cudart from cuda.core.experimental._kernel_arg_handler import ParamHolder +from cuda.core.experimental._memory import Buffer from cuda.core.experimental._module import Kernel from cuda.core.experimental._stream import Stream from cuda.core.experimental._utils import CUDAError, check_or_create_options, handle_return diff --git a/cuda_core/cuda/core/experimental/_memory.py b/cuda_core/cuda/core/experimental/_memory.py index 6207d861..4ef2cbc3 100644 --- a/cuda_core/cuda/core/experimental/_memory.py +++ b/cuda_core/cuda/core/experimental/_memory.py @@ -6,6 +6,7 @@ import abc from typing import Optional, Tuple, TypeVar +import warnings from cuda import cuda from cuda.core.experimental._dlpack import DLDeviceType, make_py_capsule diff --git a/cuda_core/cuda/core/experimental/_module.py b/cuda_core/cuda/core/experimental/_module.py index 4eacea1c..60d4db97 100644 --- a/cuda_core/cuda/core/experimental/_module.py +++ b/cuda_core/cuda/core/experimental/_module.py @@ -4,7 +4,7 @@ import importlib.metadata -from cuda import cuda +from cuda import cuda, cudart from cuda.core.experimental._utils import handle_return diff --git a/cuda_core/cuda/core/experimental/utils.py b/cuda_core/cuda/core/experimental/utils.py index 0717b41a..74f41e4d 100644 --- a/cuda_core/cuda/core/experimental/utils.py +++ b/cuda_core/cuda/core/experimental/utils.py @@ -2,3 +2,4 @@ # # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE +from cuda.core.experimental._memoryview import StridedMemoryView, viewable From dc2fc1469e23d88f0cd3f3dc8109c0f3daa0de9a Mon Sep 17 00:00:00 2001 From: ksimpson Date: Mon, 4 Nov 2024 11:24:33 -0800 Subject: [PATCH 4/9] remove module fixture --- cuda_core/tests/conftest.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index ce3b5b95..770c1f19 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -12,14 +12,6 @@ from cuda.core.experimental._utils import handle_return import pytest -# @pytest.fixture(scope="module", autouse=True) -# def ensure_no_context(): -# device = Device() -# device.set_current() -# with _device._tls_lock: -# if hasattr(_device._tls, 'devices'): -# del _device._tls.devices - @pytest.fixture(scope="function") def init_cuda(): device = Device() From c4dccff09183a193c315bc3688577082567e888f Mon Sep 17 00:00:00 2001 From: ksimpson Date: Fri, 15 Nov 2024 10:18:54 -0800 Subject: [PATCH 5/9] address review comments --- cuda_core/tests/conftest.py | 11 +++++++---- cuda_core/tests/test_device.py | 33 ++++++++++++++++++++------------ cuda_core/tests/test_event.py | 20 ++++++------------- cuda_core/tests/test_launcher.py | 4 +--- cuda_core/tests/test_memory.py | 2 +- cuda_core/tests/test_program.py | 2 +- cuda_core/tests/test_stream.py | 4 ++-- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 770c1f19..67948e32 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -5,10 +5,13 @@ # this software. Any use, reproduction, disclosure, or distribution of # this software and related documentation outside the terms of the EULA # is strictly prohibited. +try: + from cuda.bindings import driver +except ImportError: + from cuda import cuda as driver -from cuda.core.experimental._device import Device +from cuda.core.experimental import Device from cuda.core.experimental import _device -from cuda import cuda from cuda.core.experimental._utils import handle_return import pytest @@ -17,13 +20,13 @@ def init_cuda(): device = Device() device.set_current() yield - handle_return(cuda.cuCtxPopCurrent()) + handle_return(driver.cuCtxPopCurrent()) with _device._tls_lock: del _device._tls.devices @pytest.fixture(scope="function") def deinit_cuda(): yield - handle_return(cuda.cuCtxPopCurrent()) + handle_return(driver.cuCtxPopCurrent()) with _device._tls_lock: del _device._tls.devices \ No newline at end of file diff --git a/cuda_core/tests/test_device.py b/cuda_core/tests/test_device.py index eabaa57e..9aadf0db 100644 --- a/cuda_core/tests/test_device.py +++ b/cuda_core/tests/test_device.py @@ -6,14 +6,23 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda import cuda, cudart -from cuda.core.experimental._device import Device +try: + from cuda.bindings import driver +except ImportError: + from cuda import cuda as driver + +try: + from cuda.bindings import runtime +except ImportError: + from cuda import cudart as runtime + +from cuda.core.experimental import Device from cuda.core.experimental._utils import handle_return, ComputeCapability def test_device_set_current(deinit_cuda): device = Device() device.set_current() - assert handle_return(cuda.cuCtxGetCurrent()) is not None + assert handle_return(driver.cuCtxGetCurrent()) is not None def test_device_repr(): device = Device(0) @@ -35,32 +44,32 @@ def test_device_create_stream(init_cuda): def test_pci_bus_id(): device = Device() - bus_id = handle_return(cudart.cudaDeviceGetPCIBusId(13, device.device_id)) + bus_id = handle_return(runtime.cudaDeviceGetPCIBusId(13, device.device_id)) assert device.pci_bus_id == bus_id[:12].decode() def test_uuid(): device = Device() - driver_ver = handle_return(cuda.cuDriverGetVersion()) + driver_ver = handle_return(driver.cuDriverGetVersion()) if driver_ver >= 11040: - uuid = handle_return(cuda.cuDeviceGetUuid_v2(device.device_id)) + uuid = handle_return(driver.cuDeviceGetUuid_v2(device.device_id)) else: - uuid = handle_return(cuda.cuDeviceGetUuid(device.device_id)) + uuid = handle_return(driver.cuDeviceGetUuid(device.device_id)) uuid = uuid.bytes.hex() expected_uuid = f"{uuid[:8]}-{uuid[8:12]}-{uuid[12:16]}-{uuid[16:20]}-{uuid[20:]}" assert device.uuid == expected_uuid def test_name(): device = Device() - name = handle_return(cuda.cuDeviceGetName(128, device.device_id)) + name = handle_return(driver.cuDeviceGetName(128, device.device_id)) name = name.split(b'\0')[0] assert device.name == name.decode() def test_compute_capability(): device = Device() - major = handle_return(cudart.cudaDeviceGetAttribute( - cudart.cudaDeviceAttr.cudaDevAttrComputeCapabilityMajor, device.device_id)) - minor = handle_return(cudart.cudaDeviceGetAttribute( - cudart.cudaDeviceAttr.cudaDevAttrComputeCapabilityMinor, device.device_id)) + major = handle_return(runtime.cudaDeviceGetAttribute( + runtime.cudaDeviceAttr.cudaDevAttrComputeCapabilityMajor, device.device_id)) + minor = handle_return(runtime.cudaDeviceGetAttribute( + runtime.cudaDeviceAttr.cudaDevAttrComputeCapabilityMinor, device.device_id)) expected_cc = ComputeCapability(major, minor) assert device.compute_capability == expected_cc \ No newline at end of file diff --git a/cuda_core/tests/test_event.py b/cuda_core/tests/test_event.py index f174ca2e..42d1ef95 100644 --- a/cuda_core/tests/test_event.py +++ b/cuda_core/tests/test_event.py @@ -6,19 +6,16 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda.core.experimental._event import EventOptions -from cuda.core.experimental._device import Device +from cuda.core.experimental import Device, EventOptions +import pytest -def test_is_timing_disabled(init_cuda): - options = EventOptions(enable_timing=False) +@pytest.mark.parametrize("enable_timing", [True, False, None]) +def test_timing(init_cuda, enable_timing): + options = EventOptions(enable_timing=enable_timing) stream = Device().create_stream() event = stream.record(options=options) - assert event.is_timing_disabled == True + assert event.is_timing_disabled == (not enable_timing if enable_timing is not None else True) - options = EventOptions(enable_timing=True) - stream = Device().create_stream() - event = stream.record(options=options) - assert event.is_timing_disabled == False def test_is_sync_busy_waited(init_cuda): options = EventOptions(enable_timing=False, busy_waited_sync=True) @@ -44,8 +41,3 @@ def test_is_done(init_cuda): event = stream.record(options=options) assert event.is_done == True -def test_handle(init_cuda): - options = EventOptions(enable_timing=False) - stream = Device().create_stream() - event = stream.record(options=options) - assert isinstance(event.handle, int) diff --git a/cuda_core/tests/test_launcher.py b/cuda_core/tests/test_launcher.py index 8d3d3dfa..796050a8 100644 --- a/cuda_core/tests/test_launcher.py +++ b/cuda_core/tests/test_launcher.py @@ -6,9 +6,7 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda.core.experimental._launcher import LaunchConfig -from cuda.core.experimental._stream import Stream -from cuda.core.experimental._device import Device +from cuda.core.experimental import Device, Stream, LaunchConfig import pytest def test_launch_config_init(init_cuda): diff --git a/cuda_core/tests/test_memory.py b/cuda_core/tests/test_memory.py index 7affb548..67478bd6 100644 --- a/cuda_core/tests/test_memory.py +++ b/cuda_core/tests/test_memory.py @@ -7,8 +7,8 @@ # is strictly prohibited. from cuda import cuda +from cuda.core.experimental import Device from cuda.core.experimental._memory import Buffer, MemoryResource -from cuda.core.experimental._device import Device from cuda.core.experimental._utils import handle_return import ctypes diff --git a/cuda_core/tests/test_program.py b/cuda_core/tests/test_program.py index be289cc2..caa7369e 100644 --- a/cuda_core/tests/test_program.py +++ b/cuda_core/tests/test_program.py @@ -6,7 +6,7 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda.core.experimental._program import Program +from cuda.core.experimental import Program from cuda.core.experimental._module import ObjectCode, Kernel import pytest diff --git a/cuda_core/tests/test_stream.py b/cuda_core/tests/test_stream.py index f01bfa70..831e6888 100644 --- a/cuda_core/tests/test_stream.py +++ b/cuda_core/tests/test_stream.py @@ -6,9 +6,9 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda.core.experimental._stream import Stream, StreamOptions, LEGACY_DEFAULT_STREAM, PER_THREAD_DEFAULT_STREAM, default_stream +from cuda.core.experimental import Device, Stream, StreamOptions +from cuda.core.experimental._stream import LEGACY_DEFAULT_STREAM, PER_THREAD_DEFAULT_STREAM, default_stream from cuda.core.experimental._event import Event -from cuda.core.experimental._device import Device import pytest def test_stream_init(): From d663afb8b7f5be0f0ec151a42c5eb51d97597d86 Mon Sep 17 00:00:00 2001 From: ksimpson Date: Fri, 15 Nov 2024 10:21:12 -0800 Subject: [PATCH 6/9] address review comments --- cuda_core/tests/test_memory.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cuda_core/tests/test_memory.py b/cuda_core/tests/test_memory.py index 67478bd6..0a70e52a 100644 --- a/cuda_core/tests/test_memory.py +++ b/cuda_core/tests/test_memory.py @@ -6,7 +6,11 @@ # this software and related documentation outside the terms of the EULA # is strictly prohibited. -from cuda import cuda +try: + from cuda.bindings import driver +except ImportError: + from cuda import cuda as driver + from cuda.core.experimental import Device from cuda.core.experimental._memory import Buffer, MemoryResource from cuda.core.experimental._utils import handle_return @@ -65,11 +69,11 @@ def __init__(self, device): self.device = device def allocate(self, size, stream=None) -> Buffer: - ptr = handle_return(cuda.cuMemAllocManaged(size, cuda.CUmemAttach_flags.CU_MEM_ATTACH_GLOBAL.value)) + ptr = handle_return(driver.cuMemAllocManaged(size, driver.CUmemAttach_flags.CU_MEM_ATTACH_GLOBAL.value)) return Buffer(ptr=ptr, size=size, mr=self) def deallocate(self, ptr, size, stream=None): - handle_return(cuda.cuMemFree(ptr)) + handle_return(driver.cuMemFree(ptr)) @property def is_device_accessible(self) -> bool: @@ -88,11 +92,11 @@ def __init__(self, device): self.device = device def allocate(self, size, stream=None) -> Buffer: - ptr = handle_return(cuda.cuMemAllocHost(size)) + ptr = handle_return(driver.cuMemAllocHost(size)) return Buffer(ptr=ptr, size=size, mr=self) def deallocate(self, ptr, size, stream=None): - handle_return(cuda.cuMemFreeHost(ptr)) + handle_return(driver.cuMemFreeHost(ptr)) @property def is_device_accessible(self) -> bool: From e31eae4db8747bca18bd518ac4aeb46ffa9a9195 Mon Sep 17 00:00:00 2001 From: Keenan Simpson Date: Mon, 18 Nov 2024 11:38:35 -0800 Subject: [PATCH 7/9] Update cuda_core/tests/conftest.py Co-authored-by: Leo Fang --- cuda_core/tests/conftest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 67948e32..3c7eccd0 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -20,6 +20,9 @@ def init_cuda(): device = Device() device.set_current() yield + _device_unset_current() + +def _device_unset_current(): handle_return(driver.cuCtxPopCurrent()) with _device._tls_lock: del _device._tls.devices @@ -27,6 +30,4 @@ def init_cuda(): @pytest.fixture(scope="function") def deinit_cuda(): yield - handle_return(driver.cuCtxPopCurrent()) - with _device._tls_lock: - del _device._tls.devices \ No newline at end of file + _device_unset_current() \ No newline at end of file From c3e9927ef0bb9e6525ea543172761af6e0f33984 Mon Sep 17 00:00:00 2001 From: Keenan Simpson Date: Mon, 18 Nov 2024 11:38:52 -0800 Subject: [PATCH 8/9] Update cuda_core/tests/test_stream.py Co-authored-by: Leo Fang --- cuda_core/tests/test_stream.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cuda_core/tests/test_stream.py b/cuda_core/tests/test_stream.py index 9aad63fb..e66b1684 100644 --- a/cuda_core/tests/test_stream.py +++ b/cuda_core/tests/test_stream.py @@ -51,10 +51,11 @@ def test_stream_record_invalid_event(init_cuda): stream.record(event="invalid_event") def test_stream_wait_event(init_cuda): - stream = Device().create_stream(options=StreamOptions()) - event = Event._init() - stream.record(event) - stream.wait(event) # Should not raise any exceptions + s1 = Device().create_stream() + s2 = Device().create_stream() + e1 = s1.record() + s2.wait(e1) # Should not raise any exceptions + s2.sync() def test_stream_wait_invalid_event(init_cuda): stream = Device().create_stream(options=StreamOptions()) From 8daac0ecee32db05dc635c85e28946e8550c6e9c Mon Sep 17 00:00:00 2001 From: Keenan Simpson Date: Mon, 18 Nov 2024 11:40:30 -0800 Subject: [PATCH 9/9] Update cuda_core/tests/test_device.py Co-authored-by: Leo Fang --- cuda_core/tests/test_device.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cuda_core/tests/test_device.py b/cuda_core/tests/test_device.py index 9aadf0db..c809bfb3 100644 --- a/cuda_core/tests/test_device.py +++ b/cuda_core/tests/test_device.py @@ -7,13 +7,9 @@ # is strictly prohibited. try: - from cuda.bindings import driver + from cuda.bindings import driver, runtime except ImportError: from cuda import cuda as driver - -try: - from cuda.bindings import runtime -except ImportError: from cuda import cudart as runtime from cuda.core.experimental import Device