Skip to content

Commit 935bb0b

Browse files
Merge pull request #237 from oleksandr-pavlyk/add-pynvbench
Python package pynvbench introduced that exposes `cuda.bench` namespace. Repository provides a set of examples.
2 parents fa8dd48 + b5e4b4b commit 935bb0b

24 files changed

+2780
-0
lines changed

python/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
nvbench_build
3+
nvbench_install
4+
__pycache__

python/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 3.30...4.0)
2+
3+
# CUDA is transitive dependency of nvbench
4+
project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX CUDA)
5+
6+
find_package(Python REQUIRED COMPONENTS Development.Module)
7+
find_package(CUDAToolkit REQUIRED)
8+
9+
# Get CMake package manager
10+
set(_cpm_download_location ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)
11+
file(
12+
DOWNLOAD
13+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake
14+
${_cpm_download_location}
15+
EXPECTED_HASH SHA256=2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a
16+
)
17+
include(${_cpm_download_location})
18+
19+
CPMAddPackage(
20+
NAME nvbench
21+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..
22+
OPTIONS "NVBench_INSTALL_RULES ON"
23+
FIND_PACKAGE_ARGS CONFIG REQUIRED
24+
)
25+
26+
CPMAddPackage("gh:pybind/[email protected]")
27+
28+
pybind11_add_module(_nvbench MODULE src/py_nvbench.cpp)
29+
target_link_libraries(_nvbench PUBLIC nvbench::nvbench)
30+
target_link_libraries(_nvbench PRIVATE CUDA::cudart_static)
31+
32+
set_target_properties(_nvbench PROPERTIES INSTALL_RPATH "$ORIGIN")
33+
set_target_properties(_nvbench PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
34+
set_target_properties(_nvbench PROPERTIES POSITION_INDEPENDENT_CODE ON)
35+
36+
install(TARGETS _nvbench DESTINATION cuda/bench)
37+
38+
# Determine target that nvbench::nvbench is an alias of,
39+
# necessary because ALIAS targets cannot be installed
40+
get_target_property(_aliased_target_name nvbench::nvbench ALIASED_TARGET)
41+
install(IMPORTED_RUNTIME_ARTIFACTS ${_aliased_target_name} DESTINATION cuda/bench)

python/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# CUDA Kernel Benchmarking Package
2+
3+
This package provides Python API to CUDA Kernel Benchmarking Library `NVBench`.
4+
5+
## Building
6+
7+
### Ensure recent version of CMake
8+
9+
Since `nvbench` requires a rather new version of CMake (>=3.30.4), either build CMake from sources, or create a conda environment with a recent version of CMake, using
10+
11+
```
12+
conda create -n build_env --yes cmake ninja
13+
conda activate build_env
14+
```
15+
16+
### Ensure CUDA compiler
17+
18+
Since building `NVBench` library requires CUDA compiler, ensure that appropriate environment variables
19+
are set. For example, assuming CUDA toolkit is installed system-wide, and assuming Ampere GPU architecture:
20+
21+
```bash
22+
export CUDACXX=/usr/local/cuda/bin/nvcc
23+
export CUDAARCHS=86
24+
``
25+
26+
### Build Python project
27+
28+
Now switch to python folder, configure and install NVBench library, and install the package in editable mode:
29+
30+
```bash
31+
cd nvbench/python
32+
pip install -e .
33+
```
34+
35+
### Verify that package works
36+
37+
```bash
38+
python test/run_1.py
39+
```
40+
41+
### Run examples
42+
43+
```bash
44+
# Example benchmarking numba.cuda kernel
45+
python examples/throughput.py
46+
```
47+
48+
```bash
49+
# Example benchmarking kernels authored using cuda.core
50+
python examples/axes.py
51+
```
52+
53+
```bash
54+
# Example benchmarking algorithms from cuda.cccl.parallel
55+
python examples/cccl_parallel_segmented_reduce.py
56+
```
57+
58+
```bash
59+
# Example benchmarking CuPy function
60+
python examples/cupy_extract.py
61+
```

python/cuda/bench/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2025 NVIDIA Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 with the LLVM exception
4+
# (the "License"); you may not use this file except in compliance with
5+
# the License.
6+
#
7+
# You may obtain a copy of the License at
8+
#
9+
# http://llvm.org/foundation/relicensing/LICENSE.txt
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import importlib.metadata
18+
import warnings
19+
20+
from cuda.pathfinder import ( # type: ignore[import-not-found]
21+
load_nvidia_dynamic_lib,
22+
)
23+
24+
try:
25+
__version__ = importlib.metadata.version("pynvbench")
26+
except Exception as e:
27+
__version__ = "0.0.0dev"
28+
warnings.warn(
29+
"Could not retrieve version of pynvbench package dynamically from its metadata. "
30+
f"Exception {e} was raised. "
31+
f"Version is set to fall-back value '{__version__}' instead."
32+
)
33+
34+
for libname in ("cupti", "nvperf_target", "nvperf_host"):
35+
load_nvidia_dynamic_lib(libname)
36+
37+
from cuda.bench._nvbench import ( # noqa: E402
38+
Benchmark as Benchmark,
39+
)
40+
from cuda.bench._nvbench import ( # noqa: E402
41+
CudaStream as CudaStream,
42+
)
43+
from cuda.bench._nvbench import ( # noqa: E402
44+
Launch as Launch,
45+
)
46+
from cuda.bench._nvbench import ( # noqa: E402
47+
NVBenchRuntimeError as NVBenchRuntimeError,
48+
)
49+
from cuda.bench._nvbench import ( # noqa: E402
50+
State as State,
51+
)
52+
from cuda.bench._nvbench import ( # noqa: E402
53+
register as register,
54+
)
55+
from cuda.bench._nvbench import ( # noqa: E402
56+
run_all_benchmarks as run_all_benchmarks,
57+
)
58+
59+
del load_nvidia_dynamic_lib

0 commit comments

Comments
 (0)