Skip to content

Commit 4282b99

Browse files
committed
centralize check_or_create_options
1 parent 6f346e4 commit 4282b99

3 files changed

Lines changed: 23 additions & 35 deletions

File tree

cuda_core/cuda/core/experimental/_event.pyx

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
from __future__ import annotations
66

7+
from cuda.core.experimental._utils.cuda_utils cimport (
8+
_check_driver_error as raise_if_driver_error,
9+
check_or_create_options,
10+
)
11+
712
from dataclasses import dataclass
813
from typing import TYPE_CHECKING, Optional
914

@@ -13,9 +18,6 @@ from cuda.core.experimental._utils.cuda_utils import (
1318
driver,
1419
handle_return,
1520
)
16-
from cuda.core.experimental._utils.cuda_utils import (
17-
_check_driver_error as raise_if_driver_error,
18-
)
1921

2022
if TYPE_CHECKING:
2123
import cuda.bindings
@@ -47,26 +49,6 @@ cdef class EventOptions:
4749
support_ipc: Optional[bool] = False
4850

4951

50-
cdef inline EventOptions check_or_create_options(options, str options_description):
51-
"""
52-
Create the specified options dataclass from a dictionary of options or None.
53-
"""
54-
cdef EventOptions opts
55-
if options is None:
56-
opts = EventOptions()
57-
elif isinstance(options, dict):
58-
opts = EventOptions(**options)
59-
elif not isinstance(options, EventOptions):
60-
raise TypeError(
61-
f"The {options_description} must be provided as an object "
62-
f"of type {EventOptions.__name__} or as a dict with valid {options_description}. "
63-
f"The provided object is '{options}'."
64-
)
65-
66-
return opts
67-
68-
69-
7052
cdef class Event:
7153
"""Represent a record at a specific point of execution within a CUDA stream.
7254
@@ -108,7 +90,7 @@ cdef class Event:
10890
@classmethod
10991
def _init(cls, device_id: int, ctx_handle: Context, opts=None):
11092
cdef Event self = Event.__new__(Event)
111-
cdef EventOptions options = check_or_create_options(opts, "Event options")
93+
cdef EventOptions options = check_or_create_options(EventOptions, opts, "Event options")
11294
flags = 0x0
11395
self._timing_disabled = False
11496
self._busy_waited = False
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cpdef int _check_driver_error(error) except?-1
6+
cpdef int _check_runtime_error(error) except?-1
7+
cpdef int _check_nvrtc_error(error) except?-1
8+
cpdef check_or_create_options(type cls, options, str options_description=*, bint keep_none=*)

cuda_core/cuda/core/experimental/_utils/cuda_utils.pyx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
22
#
3-
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
3+
# SPDX-License-Identifier: Apache-2.0
44

55
import functools
66
import importlib.metadata
77
from collections import namedtuple
88
from collections.abc import Sequence
9-
from typing import Callable, Dict
9+
from typing import Callable
1010

1111
try:
1212
from cuda.bindings import driver, nvrtc, runtime
@@ -123,27 +123,25 @@ def handle_return(tuple result, handle=None):
123123
return result[1:]
124124

125125

126-
def check_or_create_options(cls, options, options_description, *, keep_none=False):
126+
cpdef check_or_create_options(type cls, options, str options_description="", bint keep_none=False):
127127
"""
128128
Create the specified options dataclass from a dictionary of options or None.
129129
"""
130-
131130
if options is None:
132131
if keep_none:
133132
return options
134-
options = cls()
135-
elif isinstance(options, Dict):
136-
options = cls(**options)
137-
138-
if not isinstance(options, cls):
133+
return cls()
134+
elif isinstance(options, cls):
135+
return options
136+
elif isinstance(options, dict):
137+
return cls(**options)
138+
else:
139139
raise TypeError(
140140
f"The {options_description} must be provided as an object "
141141
f"of type {cls.__name__} or as a dict with valid {options_description}. "
142142
f"The provided object is '{options}'."
143143
)
144144

145-
return options
146-
147145

148146
def _handle_boolean_option(option: bool) -> str:
149147
"""

0 commit comments

Comments
 (0)