mirror of
https://github.com/NVIDIA/nvbench.git
synced 2026-06-29 10:47:36 +00:00
* Add type annotations for future functionality
```python
class Timer:
def start(self) -> None: ...
def stop(self) -> None: ...
```
and overloaded `State.exec` so:
- normal mode accepts `Callable[[Launch], None]`
- `timer=True` accepts `Callable[[Launch, Timer], None]`
No implementation yet. Type annotation checked with
```
(py313) :~/repos/nvbench/python$ python -m mypy --ignore-missing-imports /tmp/check_timer.py
/tmp/check_timer.py:24: error: No overload variant of "exec" of "State" matches argument types "Callable[[Launch], None]", "bool" [call-overload]
/tmp/check_timer.py:24: note: Possible overload variants:
/tmp/check_timer.py:24: note: def exec(self, Callable[[Launch], None], /, *, batched: bool | None = ..., sync: bool | None = ..., timer: Literal[False] = ...) -> None
/tmp/check_timer.py:24: note: def exec(self, Callable[[Launch, Timer], None], /, *, timer: Literal[True], sync: bool | None = ...) -> None
/tmp/check_timer.py:25: error: Argument 1 to "exec" of "State" has incompatible type "Callable[[Launch, Timer], None]"; expected "Callable[[Launch], None]" [arg-type]
/tmp/check_timer.py:26: error: No overload variant of "exec" of "State" matches argument types "Callable[[Launch, int], None]", "bool" [call-overload]
/tmp/check_timer.py:26: note: Possible overload variants:
/tmp/check_timer.py:26: note: def exec(self, Callable[[Launch], None], /, *, batched: bool | None = ..., sync: bool | None = ..., timer: Literal[False] = ...) -> None
/tmp/check_timer.py:26: note: def exec(self, Callable[[Launch, Timer], None], /, *, timer: Literal[True], sync: bool | None = ...) -> None
Found 3 errors in 1 file (checked 1 source file)
(py313) :~/repos/nvbench/python$ nl -ba /tmp/check_timer.py
1 # /tmp/check_nvbench_timer.py
2 import cuda.bench as bench
3
4 def normal_ok(launch: bench.Launch) -> None:
5 pass
6
7 def timer_ok(launch: bench.Launch, timer: bench.Timer) -> None:
8 timer.start()
9 timer.stop()
10
11 def missing_timer(launch: bench.Launch) -> None:
12 pass
13
14 def extra_timer(launch: bench.Launch, timer: bench.Timer) -> None:
15 pass
16
17 def wrong_timer_type(launch: bench.Launch, timer: int) -> None:
18 pass
19
20 def state_bench(state: bench.State) -> None:
21 state.exec(normal_ok)
22 state.exec(normal_ok, timer=False)
23 state.exec(timer_ok, timer=True)
24 state.exec(missing_timer, timer=True) # should fail
25 state.exec(extra_timer) # should fail
26 state.exec(wrong_timer_type, timer=True) # should fail
```
* Implement cuda.bench.Timer object
The Timer class is not user-constructible. It exposes two nullary
methods timer.start() and timer.stop().
The instance of Timer class would be provided to launchable object
passed to State.exec with timer=True.
* Implement support for State.exec( launch_fn, timer=True)
* Change type annotation for batch to default to None
None is interpreted as `not timer`, i.e., it effectively
defaults to True (as before) for usage without timer set,
but starts defaulting to `False` is `timer=True` is set.
The batched keyword type is `bool | None`.
* Implement default batched=None behavior
API allows one to specify all 3 keywords, sync, batched,
and timer. batched is None by default, run-time interpreted
as `(not timer)`.
* Update tests for new behavior of batched/time combination
* Add python/examples/exec_tag_timer.py
* Expand Timer class and methods docstrings
* Reworked python/example/exec_tag_timer.py to align with C++ example.
* Replace ::cuda::std::name with cuda::std::name
* Resolve review feedback
151 lines
4.0 KiB
Python
151 lines
4.0 KiB
Python
# Copyright 2025-2026 NVIDIA Corporation
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 with the LLVM exception
|
|
# (the "License"); you may not use this file except in compliance with
|
|
# the License.
|
|
#
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://llvm.org/foundation/relicensing/LICENSE.txt
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""CUDA Kernel Benchmarking Library Python API."""
|
|
|
|
import functools
|
|
import importlib
|
|
import importlib.metadata
|
|
import warnings
|
|
|
|
from ._decorators import axis as axis
|
|
from ._decorators import make_register as _make_register
|
|
from ._decorators import option as option
|
|
|
|
try:
|
|
__version__ = importlib.metadata.version("cuda-bench")
|
|
except Exception as e:
|
|
__version__ = "0.0.0dev"
|
|
warnings.warn(
|
|
"Could not retrieve version of cuda-bench package dynamically from its metadata. "
|
|
f"Exception {e} was raised. "
|
|
f"Version is set to fall-back value '{__version__}' instead."
|
|
)
|
|
|
|
|
|
_NVBENCH_EXPORTS = (
|
|
"Benchmark",
|
|
"CudaStream",
|
|
"Launch",
|
|
"NVBenchRuntimeError",
|
|
"State",
|
|
"Timer",
|
|
"run_all_benchmarks",
|
|
)
|
|
|
|
_PUBLIC_EXPORTS = (
|
|
*_NVBENCH_EXPORTS,
|
|
"axis",
|
|
"option",
|
|
"register",
|
|
)
|
|
|
|
_NVBENCH_TEST_EXPORTS = (
|
|
"_test_cpp_exception",
|
|
"_test_py_exception",
|
|
)
|
|
|
|
__all__ = list(_PUBLIC_EXPORTS)
|
|
|
|
# Optional test override used by decorator tests.
|
|
_register = None
|
|
|
|
|
|
# Detect CUDA runtime version and load appropriate extension
|
|
def _get_cuda_major_version():
|
|
"""Detect the CUDA runtime major version."""
|
|
try:
|
|
import cuda.bindings
|
|
|
|
# Get CUDA version from cuda-bindings package version
|
|
# cuda-bindings version is in format like "12.9.1" or "13.0.0"
|
|
version_str = cuda.bindings.__version__
|
|
major = int(version_str.split(".")[0])
|
|
return major
|
|
except ImportError:
|
|
raise ImportError(
|
|
"cuda-bindings is required for runtime CUDA version detection. "
|
|
"Install with: pip install cuda-bench[cu12] or pip install cuda-bench[cu13]"
|
|
)
|
|
|
|
|
|
def _bind_nvbench_module(module):
|
|
for name in _NVBENCH_EXPORTS:
|
|
globals()[name] = getattr(module, name)
|
|
# Set module of exposed objects
|
|
globals()[name].__module__ = __name__
|
|
|
|
for name in _NVBENCH_TEST_EXPORTS:
|
|
globals()[name] = getattr(module, name)
|
|
|
|
# Expose the module as _nvbench for backward compatibility (e.g., for tests)
|
|
globals()["_nvbench"] = module
|
|
|
|
|
|
@functools.lru_cache(maxsize=1)
|
|
def _load_nvbench_module():
|
|
cuda_major = _get_cuda_major_version()
|
|
extra_name = f"cu{cuda_major}"
|
|
module_fullname = f"cuda.bench.{extra_name}._nvbench"
|
|
|
|
try:
|
|
module = importlib.import_module(module_fullname)
|
|
except ImportError as e:
|
|
raise ImportError(
|
|
f"No cuda-bench extension found for CUDA {cuda_major}.x. "
|
|
f"This wheel may not include support for your CUDA version. "
|
|
f"Supported CUDA versions: 12, 13. "
|
|
f"Original error: {e}"
|
|
) from e
|
|
|
|
_bind_nvbench_module(module)
|
|
return module
|
|
|
|
|
|
def __getattr__(name):
|
|
if name == "_nvbench":
|
|
return _load_nvbench_module()
|
|
|
|
if name in _NVBENCH_EXPORTS + _NVBENCH_TEST_EXPORTS:
|
|
_load_nvbench_module()
|
|
return globals()[name]
|
|
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
|
|
|
|
def __dir__():
|
|
return sorted(
|
|
set(globals())
|
|
| set(_PUBLIC_EXPORTS)
|
|
| set(_NVBENCH_TEST_EXPORTS)
|
|
| {"_nvbench"}
|
|
)
|
|
|
|
|
|
__doc__ = """
|
|
CUDA Kernel Benchmarking Library Python API
|
|
"""
|
|
|
|
|
|
def _get_register():
|
|
if _register is not None:
|
|
return _register
|
|
return _load_nvbench_module().register
|
|
|
|
|
|
register = _make_register(_get_register)
|
|
register.__module__ = __name__
|