Skip to content

Commit 7fe26fc

Browse files
committed
Complete type annotations for public APIs
1 parent d296dbb commit 7fe26fc

74 files changed

Lines changed: 599 additions & 493 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cuda_core/cuda/core/_context.pyi

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ from __future__ import annotations
55
from collections.abc import Sequence
66
from dataclasses import dataclass
77

8+
import cuda.bindings.driver
89
from cuda.core._device_resources import (DeviceResources, SMResource,
910
WorkqueueResource)
10-
from cuda.core._stream import StreamOptions
11+
from cuda.core._stream import Stream, StreamOptions
1112

1213

1314
class Context:
@@ -20,15 +21,15 @@ class Context:
2021
def close(self):
2122
"""Release this context wrapper's underlying CUDA handles."""
2223

23-
def __init__(self, *args, **kwargs):
24+
def __init__(self, *args, **kwargs) -> None:
2425
...
2526

2627
@property
27-
def handle(self):
28+
def handle(self) -> cuda.bindings.driver.CUcontext | None:
2829
"""Return the underlying CUcontext handle."""
2930

3031
@property
31-
def _handle(self):
32+
def _handle(self) -> cuda.bindings.driver.CUcontext | None:
3233
...
3334

3435
@property
@@ -46,7 +47,7 @@ class Context:
4647
Raises :class:`RuntimeError` if the context has been closed.
4748
"""
4849

49-
def create_stream(self, options: StreamOptions | None=None):
50+
def create_stream(self, options: StreamOptions | None=None) -> Stream:
5051
"""Create a new stream bound to this green context.
5152
5253
This method is only available on green contexts. For primary
@@ -63,7 +64,7 @@ class Context:
6364
Newly created stream object.
6465
"""
6566

66-
def __eq__(self, other):
67+
def __eq__(self, other: object) -> bool:
6768
...
6869

6970
def __hash__(self) -> int:

cuda_core/cuda/core/_context.pyx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from __future__ import annotations
66

77
from collections.abc import Sequence
88
from dataclasses import dataclass
9+
from typing import TYPE_CHECKING
910

1011
from cuda.bindings cimport cydriver
1112
from cuda.core._device_resources cimport DeviceResources, SMResource, WorkqueueResource
@@ -23,6 +24,9 @@ from cuda.core._resource_handles cimport (
2324
from cuda.core._stream import Stream, StreamOptions
2425
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2526

27+
if TYPE_CHECKING:
28+
import cuda.bindings.driver # no-cython-lint
29+
2630

2731
__all__ = ['Context', 'ContextOptions']
2832

@@ -37,7 +41,7 @@ cdef class Context:
3741
Use Device or Stream APIs to obtain context objects.
3842
"""
3943

40-
def __init__(self, *args, **kwargs):
44+
def __init__(self, *args, **kwargs) -> None:
4145
raise RuntimeError("Context objects cannot be instantiated directly. Please use Device or Stream APIs.")
4246

4347
@staticmethod
@@ -58,7 +62,7 @@ cdef class Context:
5862
return Context._from_handle(cls, h_context, device_id)
5963

6064
@property
61-
def handle(self):
65+
def handle(self) -> cuda.bindings.driver.CUcontext | None:
6266
"""Return the underlying CUcontext handle."""
6367
if not self._h_context:
6468
return None
@@ -67,7 +71,7 @@ cdef class Context:
6771
return as_py(self._h_context)
6872

6973
@property
70-
def _handle(self):
74+
def _handle(self) -> cuda.bindings.driver.CUcontext | None:
7175
return self.handle
7276

7377
@property
@@ -91,7 +95,7 @@ cdef class Context:
9195
raise RuntimeError("Cannot query resources on a closed context")
9296
return DeviceResources._init_from_ctx(self._h_context, self._device_id)
9397

94-
def create_stream(self, options: StreamOptions | None = None):
98+
def create_stream(self, options: StreamOptions | None = None) -> Stream:
9599
"""Create a new stream bound to this green context.
96100

97101
This method is only available on green contexts. For primary
@@ -130,7 +134,7 @@ cdef class Context:
130134
)
131135
self._h_context.reset()
132136

133-
def __eq__(self, other):
137+
def __eq__(self, other: object) -> bool:
134138
if not isinstance(other, Context):
135139
return NotImplemented
136140
cdef Context _other = <Context>other

cuda_core/cuda/core/_device.pyi

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class DeviceProperties:
2121
Attributes are read-only and provide information about the device.
2222
"""
2323

24-
def __init__(self, *args, **kwargs):
24+
def __init__(self, *args, **kwargs) -> None:
2525
...
2626

2727
@classmethod
28-
def _init(cls, handle):
28+
def _init(cls, handle: int) -> DeviceProperties:
2929
...
3030

3131
@property
@@ -634,14 +634,14 @@ class Device:
634634
"""
635635
__slots__ = ('_device_id', '_memory_resource', '_has_inited', '_properties', '_resources', '_uuid', '_context', '__weakref__')
636636

637-
def __new__(cls, device_id: Device | int | None=None):
637+
def __new__(cls, device_id: Device | int | None=None) -> Device:
638638
...
639639

640640
def _check_context_initialized(self):
641641
...
642642

643643
@classmethod
644-
def get_all_devices(cls):
644+
def get_all_devices(cls) -> tuple[Device, ...]:
645645
"""
646646
Query the available device instances.
647647
@@ -737,7 +737,7 @@ class Device:
737737
"""Return :obj:`~_memory.MemoryResource` associated with this device."""
738738

739739
@memory_resource.setter
740-
def memory_resource(self, mr):
740+
def memory_resource(self, mr: MemoryResource) -> None:
741741
...
742742

743743
@property
@@ -752,19 +752,19 @@ class Device:
752752
753753
"""
754754

755-
def __int__(self):
755+
def __int__(self) -> int:
756756
"""Return device_id."""
757757

758-
def __repr__(self):
758+
def __repr__(self) -> str:
759759
...
760760

761761
def __hash__(self) -> int:
762762
...
763763

764-
def __eq__(self, other) -> bool:
764+
def __eq__(self, other: object) -> bool:
765765
...
766766

767-
def __reduce__(self):
767+
def __reduce__(self) -> tuple:
768768
...
769769

770770
def set_current(self, ctx: Context | None=None) -> Context | None:
@@ -865,7 +865,7 @@ class Device:
865865
866866
"""
867867

868-
def allocate(self, size, *, stream: Stream | GraphBuilder) -> Buffer:
868+
def allocate(self, size: int, *, stream: Stream | GraphBuilder) -> Buffer:
869869
"""Allocate device memory from a specified stream.
870870
871871
Allocates device memory of `size` bytes on the specified `stream`
@@ -891,7 +891,7 @@ class Device:
891891
892892
"""
893893

894-
def sync(self):
894+
def sync(self) -> None:
895895
"""Synchronize the device.
896896
897897
Note

cuda_core/cuda/core/_device.pyx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ cdef class DeviceProperties:
6262
int _handle
6363
dict _cache
6464

65-
def __init__(self, *args, **kwargs):
65+
def __init__(self, *args, **kwargs) -> None:
6666
raise RuntimeError("DeviceProperties cannot be instantiated directly. Please use Device APIs.")
6767

6868
@classmethod
69-
def _init(cls, handle):
69+
def _init(cls, handle: int) -> DeviceProperties:
7070
cdef DeviceProperties self = DeviceProperties.__new__(cls)
7171
self._handle = handle
7272
self._cache = {}
@@ -976,7 +976,7 @@ class Device:
976976
"__weakref__",
977977
)
978978

979-
def __new__(cls, device_id: Device | int | None = None):
979+
def __new__(cls, device_id: Device | int | None = None) -> Device:
980980
if isinstance(device_id, Device):
981981
return device_id
982982

@@ -997,7 +997,7 @@ class Device:
997997

998998

999999
@classmethod
1000-
def get_all_devices(cls):
1000+
def get_all_devices(cls) -> tuple[Device, ...]:
10011001
"""
10021002
Query the available device instances.
10031003

@@ -1178,7 +1178,7 @@ class Device:
11781178
return self._memory_resource
11791179

11801180
@memory_resource.setter
1181-
def memory_resource(self, mr):
1181+
def memory_resource(self, mr: MemoryResource) -> None:
11821182
from cuda.core._memory import MemoryResource
11831183
assert_type(mr, MemoryResource)
11841184
self._memory_resource = mr
@@ -1196,22 +1196,22 @@ class Device:
11961196
"""
11971197
return default_stream()
11981198

1199-
def __int__(self):
1199+
def __int__(self) -> int:
12001200
"""Return device_id."""
12011201
return self._device_id
12021202

1203-
def __repr__(self):
1203+
def __repr__(self) -> str:
12041204
return f"<Device {self._device_id} ({self.name})>"
12051205

12061206
def __hash__(self) -> int:
12071207
return hash(self.uuid)
12081208

1209-
def __eq__(self, other) -> bool:
1209+
def __eq__(self, other: object) -> bool:
12101210
if not isinstance(other, Device):
12111211
return NotImplemented
12121212
return self._device_id == other._device_id
12131213

1214-
def __reduce__(self):
1214+
def __reduce__(self) -> tuple:
12151215
return Device, (self.device_id,)
12161216

12171217
def set_current(self, ctx: Context | None = None) -> Context | None:
@@ -1400,7 +1400,7 @@ class Device:
14001400
cdef Context ctx = self._context
14011401
return cyEvent._init(cyEvent, self._device_id, ctx._h_context, options, True)
14021402

1403-
def allocate(self, size, *, stream: Stream | GraphBuilder) -> Buffer:
1403+
def allocate(self, size: int, *, stream: Stream | GraphBuilder) -> Buffer:
14041404
"""Allocate device memory from a specified stream.
14051405

14061406
Allocates device memory of `size` bytes on the specified `stream`
@@ -1428,7 +1428,7 @@ class Device:
14281428
self._check_context_initialized()
14291429
return self.memory_resource.allocate(size, stream=stream)
14301430

1431-
def sync(self):
1431+
def sync(self) -> None:
14321432
"""Synchronize the device.
14331433

14341434
Note

cuda_core/cuda/core/_device_resources.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class SMResource:
8080
def flags(self) -> int:
8181
"""Raw flags from the underlying SM resource."""
8282

83-
def split(self, options, *, dry_run: bool=False):
83+
def split(self, options: SMResourceOptions, *, dry_run: bool=False) -> tuple[list[SMResource], SMResource]:
8484
"""Split this SM resource into groups and a remainder.
8585
8686
Parameters
@@ -107,14 +107,14 @@ class WorkqueueResource:
107107
cannot be instantiated directly.
108108
"""
109109

110-
def __init__(self, *args, **kwargs):
110+
def __init__(self, *args, **kwargs) -> None:
111111
...
112112

113113
@property
114114
def handle(self) -> int:
115115
"""Return the address of the underlying config ``CUdevResource`` struct."""
116116

117-
def configure(self, options):
117+
def configure(self, options: WorkqueueResourceOptions) -> None:
118118
"""Configure the workqueue resource in place.
119119
120120
Parameters
@@ -134,7 +134,7 @@ class DeviceResources:
134134
This class cannot be instantiated directly.
135135
"""
136136

137-
def __init__(self, *args, **kwargs):
137+
def __init__(self, *args, **kwargs) -> None:
138138
...
139139

140140
@property

cuda_core/cuda/core/_device_resources.pyx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,12 @@ cdef class SMResource:
471471
"""Raw flags from the underlying SM resource."""
472472
return self._flags
473473

474-
def split(self, options not None, *, bint dry_run=False):
474+
def split(
475+
self,
476+
options: SMResourceOptions,
477+
*,
478+
bint dry_run=False
479+
) -> tuple[list[SMResource], SMResource]:
475480
"""Split this SM resource into groups and a remainder.
476481

477482
Parameters
@@ -508,7 +513,7 @@ cdef class WorkqueueResource:
508513
cannot be instantiated directly.
509514
"""
510515

511-
def __init__(self, *args, **kwargs):
516+
def __init__(self, *args, **kwargs) -> None:
512517
raise RuntimeError(
513518
"WorkqueueResource cannot be instantiated directly. "
514519
"Use dev.resources.workqueue."
@@ -529,7 +534,7 @@ cdef class WorkqueueResource:
529534
"""Return the address of the underlying config ``CUdevResource`` struct."""
530535
return <intptr_t>(&self._wq_config_resource)
531536

532-
def configure(self, options not None):
537+
def configure(self, options: WorkqueueResourceOptions) -> None:
533538
"""Configure the workqueue resource in place.
534539

535540
Parameters
@@ -576,7 +581,7 @@ cdef class DeviceResources:
576581
This class cannot be instantiated directly.
577582
"""
578583

579-
def __init__(self, *args, **kwargs):
584+
def __init__(self, *args, **kwargs) -> None:
580585
raise RuntimeError(
581586
"DeviceResources cannot be instantiated directly. "
582587
"Use dev.resources or ctx.resources."

cuda_core/cuda/core/_dlpack.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class DLDeviceType(IntEnum):
1616
def make_py_capsule(buf: object, versioned: bool) -> object:
1717
...
1818

19-
def classify_dl_device(buf) -> tuple[int, int]:
19+
def classify_dl_device(buf: object) -> tuple[int, int]:
2020
"""Classify a buffer into a DLPack (device_type, device_id) pair.
2121
2222
``buf`` must expose ``is_device_accessible``, ``is_host_accessible``,

cuda_core/cuda/core/_dlpack.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ cdef inline int setup_dl_tensor_layout(DLTensor* dl_tensor, object buf) except -
8989
return 0
9090

9191

92-
def classify_dl_device(buf) -> tuple[int, int]:
92+
def classify_dl_device(buf: object) -> tuple[int, int]:
9393
"""Classify a buffer into a DLPack (device_type, device_id) pair.
9494

9595
``buf`` must expose ``is_device_accessible``, ``is_host_accessible``,

0 commit comments

Comments
 (0)