Skip to content

Commit 8b81c00

Browse files
Draft of Python API for NVBench
The prototype is based on pybind11 to minimize boiler-plate code needed to deal with move-only semantics of many nvbench classes.
1 parent c463a78 commit 8b81c00

File tree

8 files changed

+706
-0
lines changed

8 files changed

+706
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.30...4.0)
2+
3+
project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX)
4+
5+
set(CMAKE_CXX_STANDARD 20)
6+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
7+
8+
find_package(Python REQUIRED COMPONENTS Development.Module)
9+
find_package(CUDAToolkit REQUIRED)
10+
11+
include(FetchContent)
12+
13+
FetchContent_Declare(
14+
pybind11
15+
URL https://github.com/pybind/pybind11/archive/refs/tags/v2.13.6.tar.gz
16+
URL_HASH SHA256=e08cb87f4773da97fa7b5f035de8763abc656d87d5773e62f6da0587d1f0ec20
17+
FIND_PACKAGE_ARGS NAMES pybind11
18+
)
19+
FetchContent_MakeAvailable(pybind11)
20+
21+
find_package(nvbench CONFIG REQUIRED)
22+
23+
pybind11_add_module(_nvbench MODULE src/py_nvbench.cpp)
24+
target_link_libraries(_nvbench PUBLIC nvbench::nvbench)
25+
target_link_libraries(_nvbench PRIVATE CUDA::cudart_static)
26+
set_target_properties(_nvbench PROPERTIES INSTALL_RPATH "$ORIGIN")
27+
28+
install(TARGETS _nvbench DESTINATION cuda/nvbench)
29+
install(IMPORTED_RUNTIME_ARTIFACTS nvbench::nvbench DESTINATION cuda/nvbench)

python/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# CUDA Kernel Benchmarking Package
2+
3+
This package provides Python API to CUDA Kernel Benchmarking Library `NVBench`.
4+
5+
## Building
6+
7+
### Build `NVBench` project
8+
9+
```
10+
cd nvbench/python
11+
cmake -B nvbench_build --preset nvbench-ci -S $(pwd)/.. -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DNVBench_ENABLE_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX=$(pwd)/nvbench_install
12+
cmake --build nvbench_build/ --config Release --target install
13+
14+
nvbench_DIR=$(pwd)/nvbench_install/lib/cmake CUDACXX=/usr/local/cuda/bin/nvcc pip install -e .
15+
```
16+
17+
### Verify that package works
18+
19+
```
20+
python test/run_1.py
21+
```

python/cuda/nvbench/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import importlib.metadata
2+
3+
from cuda.bindings.path_finder import ( # type: ignore[import-not-found]
4+
_load_nvidia_dynamic_library,
5+
)
6+
7+
try:
8+
__version__ = importlib.metadata.version("pynvbench")
9+
except Exception:
10+
__version__ = "0.0.0dev"
11+
12+
for libname in ("cupti", "nvperf_target", "nvperf_host"):
13+
_load_nvidia_dynamic_library(libname)
14+
15+
from ._nvbench import * # noqa: E402, F403
16+
from ._nvbench import register, run_all_benchmarks # noqa: E402
17+
18+
__all__ = ["register", "run_all_benchmarks"]

python/pyproject.toml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[build-system]
2+
requires = ["scikit-build-core>=0.10", "setuptools_scm"]
3+
build-backend = "scikit_build_core.build"
4+
5+
[project]
6+
name = "pynvbench"
7+
description = "CUDA Kernel Benchmarking Package"
8+
authors = [{ name = "NVIDIA Corporation" }]
9+
classifiers = [
10+
"Programming Language :: Python :: 3 :: Only",
11+
"Environment :: GPU :: NVIDIA CUDA",
12+
"License :: OSI Approved :: Apache Software License",
13+
]
14+
requires-python = ">=3.9"
15+
dependencies = [
16+
# pathfinder
17+
"cuda-bindings",
18+
19+
# Library expects to find shared libraries
20+
# libcupti, libnvperf_target, libnvperf_host
21+
# pathfinder is used to find it in the Python layout
22+
"nvidia-cuda-cupti-cu12",
23+
24+
# The shared library
25+
# libnvidia-ml must be installed system-wide
26+
# (Debian package provider: libnvidia-compute)
27+
]
28+
dynamic = ["version"]
29+
readme = { file = "README.md", content-type = "text/markdown" }
30+
31+
[project.optional-dependencies]
32+
test = ["pytest", "cupy-cuda12x", "numba"]
33+
34+
[project.urls]
35+
Homepage = "https://developer.nvidia.com/"
36+
37+
[tool.scikit-build]
38+
minimum-version = "build-system.requires"
39+
build-dir = "build/{wheel_tag}"
40+
41+
[tool.scikit-build.cmake]
42+
version = ">=3.30.4"
43+
args = []
44+
build-type = "Release"
45+
source-dir = "."
46+
47+
[tool.scikit-build.ninja]
48+
version = ">=1.11"
49+
make-fallback = true
50+
51+
[tool.scikit-build.metadata.version]
52+
provider = "scikit_build_core.metadata.setuptools_scm"
53+
54+
[tool.setuptools_scm]
55+
root = ".."
56+
57+
[tool.scikit-build.wheel.packages]
58+
"cuda/nvbench" = "cuda/nvbench"

python/src/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
```
3+
g++ py_nvbench.cpp \
4+
-shared -fPIC \
5+
-I ${HOME}/repos/pybind11/include \
6+
-I ${HOME}/repos/pynvbench/nvbench_dir/include \
7+
-I /usr/local/cuda/include \
8+
$(python3-config --includes) \
9+
$(python3-config --libs) \
10+
-L ${HOME}/repos/pynvbench/nvbench_dir/lib/ \
11+
-lnvbench \
12+
-Wl,-rpath,${HOME}/repos/pynvbench/nvbench_dir/lib \
13+
-L /usr/local/cuda/lib64/ \
14+
-lcudart \
15+
-Wl,-rpath,/usr/local/cuda/lib64 \
16+
-o _nvbench$(python3-config --extension-suffix)
17+
```

0 commit comments

Comments
 (0)