Skip to content

Ensure cuda is a namespace package & Improve warnings #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed cuda_bindings/cuda/__init__.pxd
Empty file.
14 changes: 0 additions & 14 deletions cuda_bindings/cuda/__init__.py

This file was deleted.

5 changes: 5 additions & 0 deletions cuda_bindings/cuda/ccuda.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ from cuda.bindings.cydriver cimport *

cdef extern from *:
"""
#ifdef _MSC_VER
#pragma message ( "The cuda.ccuda module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.cydriver module instead." )
#else
#warning The cuda.ccuda module is deprecated and will be removed in a future release, \
please switch to use the cuda.bindings.cydriver module instead.
#endif
"""
8 changes: 0 additions & 8 deletions cuda_bindings/cuda/ccuda.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from cuda.bindings.cydriver cimport *

cdef extern from *:
"""
#pragma message ( "The cuda.ccuda module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.cydriver module instead." )
"""


from cuda.bindings import cydriver
__pyx_capi__ = cydriver.__pyx_capi__
del cydriver
5 changes: 5 additions & 0 deletions cuda_bindings/cuda/ccudart.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ from cuda.bindings.cyruntime cimport *

cdef extern from *:
"""
#ifdef _MSC_VER
#pragma message ( "The cuda.ccudart module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.cyruntime module instead." )
#else
#warning The cuda.ccudart module is deprecated and will be removed in a future release, \
please switch to use the cuda.bindings.cyruntime module instead.
#endif
"""
8 changes: 0 additions & 8 deletions cuda_bindings/cuda/ccudart.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from cuda.bindings.cyruntime cimport *

cdef extern from *:
"""
#pragma message ( "The cuda.ccudart module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.cyruntime module instead." )
"""


from cuda.bindings import cyruntime
__pyx_capi__ = cyruntime.__pyx_capi__
del cyruntime
5 changes: 5 additions & 0 deletions cuda_bindings/cuda/cnvrtc.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ from cuda.bindings.cynvrtc cimport *

cdef extern from *:
"""
#ifdef _MSC_VER
#pragma message ( "The cuda.cnvrtc module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.cynvrtc module instead." )
#else
#warning The cuda.cnvrtc module is deprecated and will be removed in a future release, \
please switch to use the cuda.bindings.cynvrtc module instead.
#endif
"""
8 changes: 0 additions & 8 deletions cuda_bindings/cuda/cnvrtc.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from cuda.bindings.cynvrtc cimport *

cdef extern from *:
"""
#pragma message ( "The cuda.cnvrtc module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.cynvrtc module instead." )
"""


from cuda.bindings import cynvrtc
__pyx_capi__ = cynvrtc.__pyx_capi__
del cynvrtc
7 changes: 6 additions & 1 deletion cuda_bindings/cuda/cuda.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ from cuda.bindings.driver import *

cdef extern from *:
"""
#ifdef _MSC_VER
#pragma message ( "The cuda.cuda module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.driver module instead." )
#else
#warning The cuda.cuda module is deprecated and will be removed in a future release, \
please switch to use the cuda.bindings.driver module instead.
#endif
"""


_warnings.warn("The cuda.cuda module is deprecated and will be removed in a future release, "
"please switch to use the cuda.bindings.driver module instead.", DeprecationWarning, stacklevel=2)
"please switch to use the cuda.bindings.driver module instead.", FutureWarning, stacklevel=2)
7 changes: 6 additions & 1 deletion cuda_bindings/cuda/cudart.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ from cuda.bindings.runtime import *

cdef extern from *:
"""
#ifdef _MSC_VER
#pragma message ( "The cuda.cudart module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.runtime module instead." )
#else
#warning The cuda.cudart module is deprecated and will be removed in a future release, \
please switch to use the cuda.bindings.runtime module instead.
#endif
"""


_warnings.warn("The cuda.cudart module is deprecated and will be removed in a future release, "
"please switch to use the cuda.bindings.runtime module instead.", DeprecationWarning, stacklevel=2)
"please switch to use the cuda.bindings.runtime module instead.", FutureWarning, stacklevel=2)
7 changes: 6 additions & 1 deletion cuda_bindings/cuda/nvrtc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ from cuda.bindings.nvrtc import *

cdef extern from *:
"""
#ifdef _MSC_VER
#pragma message ( "The cuda.nvrtc module is deprecated and will be removed in a future release, " \
"please switch to use the cuda.bindings.nvrtc module instead." )
#else
#warning The cuda.nvrtc module is deprecated and will be removed in a future release, \
please switch to use the cuda.bindings.nvrtc module instead.
#endif
"""


_warnings.warn("The cuda.nvrtc module is deprecated and will be removed in a future release, "
"please switch to use the cuda.bindings.nvrtc module instead.", DeprecationWarning, stacklevel=2)
"please switch to use the cuda.bindings.nvrtc module instead.", FutureWarning, stacklevel=2)
1 change: 1 addition & 0 deletions cuda_bindings/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Environment :: GPU :: NVIDIA CUDA",
]
dynamic = [
Expand Down
75 changes: 74 additions & 1 deletion cuda_bindings/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021-2024 NVIDIA Corporation. All rights reserved.
# Copyright 2021-2025 NVIDIA Corporation. All rights reserved.
#
# Please refer to the NVIDIA end user license agreement (EULA) associated
# with this source code for terms and conditions that govern your use of
Expand All @@ -10,6 +10,7 @@
import contextlib
import glob
import os
import pathlib
import platform
import shutil
import sys
Expand All @@ -23,6 +24,8 @@
from setuptools import find_packages, setup
from setuptools.command.bdist_wheel import bdist_wheel
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
from setuptools.command.editable_wheel import _TopLevelFinder, editable_wheel
from setuptools.extension import Extension

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -402,9 +405,79 @@ def build_extension(self, ext):
super().build_extension(ext)


################################################################################
# Adapted from NVIDIA/numba-cuda
# TODO: Remove this block once we get rid of cuda.__version__ and the .pth files

REDIRECTOR_PTH = "_cuda_bindings_redirector.pth"
REDIRECTOR_PY = "_cuda_bindings_redirector.py"
SITE_PACKAGES = pathlib.Path("site-packages")


class build_py_with_redirector(build_py): # noqa: N801
"""Include the redirector files in the generated wheel."""

def copy_redirector_file(self, source, destination="."):
destination = pathlib.Path(self.build_lib) / destination
self.copy_file(str(source), str(destination), preserve_mode=0)

def run(self):
super().run()
self.copy_redirector_file(SITE_PACKAGES / REDIRECTOR_PTH)
self.copy_redirector_file(SITE_PACKAGES / REDIRECTOR_PY)

def get_source_files(self):
src = super().get_source_files()
src.extend(
[
str(SITE_PACKAGES / REDIRECTOR_PTH),
str(SITE_PACKAGES / REDIRECTOR_PY),
]
)
return src

def get_output_mapping(self):
mapping = super().get_output_mapping()
build_lib = pathlib.Path(self.build_lib)
mapping[str(build_lib / REDIRECTOR_PTH)] = REDIRECTOR_PTH
mapping[str(build_lib / REDIRECTOR_PY)] = REDIRECTOR_PY
return mapping


class TopLevelFinderWithRedirector(_TopLevelFinder):
"""Include the redirector files in the editable wheel."""

def get_implementation(self):
for item in super().get_implementation(): # noqa: UP028
yield item

with open(SITE_PACKAGES / REDIRECTOR_PTH) as f:
yield (REDIRECTOR_PTH, f.read())

with open(SITE_PACKAGES / REDIRECTOR_PY) as f:
yield (REDIRECTOR_PY, f.read())


class editable_wheel_with_redirector(editable_wheel):
def _select_strategy(self, name, tag, build_lib):
# The default mode is "lenient" - others are "strict" and "compat".
# "compat" is deprecated. "strict" creates a tree of links to files in
# the repo. It could be implemented, but we only handle the default
# case for now.
if self.mode is not None and self.mode != "lenient":
raise RuntimeError(f"Only lenient mode is supported for editable install. Current mode is {self.mode}")

return TopLevelFinderWithRedirector(self.distribution, name)


################################################################################


cmdclass = {
"bdist_wheel": WheelsBuildExtensions,
"build_ext": ParallelBuildExtensions,
"build_py": build_py_with_redirector,
"editable_wheel": editable_wheel_with_redirector,
}

# ----------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions cuda_bindings/site-packages/_cuda_bindings_redirector.pth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import _cuda_bindings_redirector
28 changes: 28 additions & 0 deletions cuda_bindings/site-packages/_cuda_bindings_redirector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2025 NVIDIA Corporation. All rights reserved.

import sys
from types import ModuleType


class LazyCudaModule(ModuleType):

def __getattr__(self, name):
if name == '__version__':
import warnings
warnings.warn(
"accessing cuda.__version__ is deprecated, " "please switch to use cuda.bindings.__version__ instead",
FutureWarning,
stacklevel=2,
)
from cuda.bindings import __version__

return __version__

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


# Important: We need to populate the cuda namespace module first, otherwise
# we'd lose access to any of its submodules. This is a cheap op because there
# is nothing under cuda.bindings.
import cuda.bindings
sys.modules['cuda'].__class__ = LazyCudaModule
Loading