Skip to content
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

Allow Pynvjitlinker Enabled via Config at Runtime #91

Merged
merged 5 commits into from
Dec 20, 2024
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
30 changes: 16 additions & 14 deletions numba_cuda/numba/cuda/cudadrv/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
from .linkable_code import LinkableCode, LTOIR, Fatbin, Object
from numba.cuda.cudadrv import enums, drvapi, nvrtc

try:
from pynvjitlink.api import NvJitLinker, NvJitLinkError
except ImportError:
NvJitLinker, NvJitLinkError = None, None

USE_NV_BINDING = config.CUDA_USE_NVIDIA_BINDING

if USE_NV_BINDING:
Expand Down Expand Up @@ -92,20 +97,6 @@ def _readenv(name, ctor, default):
if not hasattr(config, "CUDA_ENABLE_PYNVJITLINK"):
config.CUDA_ENABLE_PYNVJITLINK = ENABLE_PYNVJITLINK

if ENABLE_PYNVJITLINK:
try:
from pynvjitlink.api import NvJitLinker, NvJitLinkError
except ImportError:
raise ImportError(
"Using pynvjitlink requires the pynvjitlink package to be available"
)

if config.CUDA_ENABLE_MINOR_VERSION_COMPATIBILITY:
raise ValueError(
"Can't set CUDA_ENABLE_MINOR_VERSION_COMPATIBILITY and "
"CUDA_ENABLE_PYNVJITLINK at the same time"
)


def make_logger():
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -3061,6 +3052,17 @@ def __init__(
lto=False,
additional_flags=None,
):
if NvJitLinker is None:
raise ImportError(
"Using pynvjitlink requires the pynvjitlink package to be "
"available"
)

if config.CUDA_ENABLE_MINOR_VERSION_COMPATIBILITY:
raise ValueError(
"Can't set CUDA_ENABLE_MINOR_VERSION_COMPATIBILITY and "
"CUDA_ENABLE_PYNVJITLINK at the same time"
)

if cc is None:
raise RuntimeError("PyNvJitLinker requires CC to be specified")
Expand Down
79 changes: 76 additions & 3 deletions numba_cuda/numba/cuda/tests/cudadrv/test_nvjitlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
from numba.cuda.testing import skip_on_cudasim
from numba.cuda.testing import CUDATestCase
from numba.cuda.cudadrv.driver import PyNvJitLinker
from numba.cuda import get_current_device

from numba import cuda
from numba import config
from numba.tests.support import run_in_subprocess, override_config

try:
import pynvjitlink # noqa: F401
PYNVJITLINK_INSTALLED = True
except ImportError:
PYNVJITLINK_INSTALLED = False


import itertools
import os
import io
import contextlib
import warnings

from numba.cuda import get_current_device
from numba import cuda
from numba import config

TEST_BIN_DIR = os.getenv("NUMBA_CUDA_TEST_BIN_DIR")
if TEST_BIN_DIR:
Expand Down Expand Up @@ -251,5 +260,69 @@ def kernel():
pass


@unittest.skipIf(
not PYNVJITLINK_INSTALLED, reason="Pynvjitlink is not installed"
)
class TestLinkerUsage(CUDATestCase):
isVoid marked this conversation as resolved.
Show resolved Hide resolved
"""Test that whether pynvjitlink can be enabled by both environment variable
and modification of config at runtime.
"""

src = """if 1:
import os
from numba import cuda, config

{config}

TEST_BIN_DIR = os.getenv("NUMBA_CUDA_TEST_BIN_DIR")
if TEST_BIN_DIR:
test_device_functions_cubin = os.path.join(
TEST_BIN_DIR, "test_device_functions.cubin"
)

sig = "uint32(uint32, uint32)"
add_from_numba = cuda.declare_device("add_from_numba", sig)

@cuda.jit(link=[test_device_functions_cubin], lto=True)
def kernel(result):
result[0] = add_from_numba(1, 2)

result = cuda.device_array(1)
kernel[1, 1](result)
assert result[0] == 3
"""

def test_linker_enabled_envvar(self):
env = os.environ.copy()
env['NUMBA_CUDA_ENABLE_PYNVJITLINK'] = "1"
run_in_subprocess(self.src.format(config=""), env=env)

def test_linker_disabled_envvar(self):
env = os.environ.copy()
env.pop('NUMBA_CUDA_ENABLE_PYNVJITLINK', None)
with self.assertRaisesRegex(
AssertionError, "LTO and additional flags require PyNvJitLinker"
):
# Actual error raised is `ValueError`, but `run_in_subprocess`
# reraises as AssertionError.
run_in_subprocess(self.src.format(config=""), env=env)

def test_linker_enabled_config(self):
env = os.environ.copy()
env.pop('NUMBA_CUDA_ENABLE_PYNVJITLINK', None)
run_in_subprocess(self.src.format(
config="config.CUDA_ENABLE_PYNVJITLINK = True"), env=env)

def test_linker_disabled_config(self):
env = os.environ.copy()
env.pop('NUMBA_CUDA_ENABLE_PYNVJITLINK', None)
with override_config("CUDA_ENABLE_PYNVJITLINK", False):
with self.assertRaisesRegex(
AssertionError, "LTO and additional flags require PyNvJitLinker"
):
run_in_subprocess(self.src.format(
config="config.CUDA_ENABLE_PYNVJITLINK = False"), env=env)


if __name__ == "__main__":
unittest.main()
Loading