Skip to content

Commit

Permalink
Defer PTX file load to runtime (#13690)
Browse files Browse the repository at this point in the history
This PR fixes an issue where cuDF fails to import on machines with no NVIDIA GPU present. 

cc @shwina

Authors:
  - https://github.com/brandon-b-miller
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Bradley Dice (https://github.com/bdice)
  - Ashwin Srinath (https://github.com/shwina)
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #13690
  • Loading branch information
brandon-b-miller authored Jan 17, 2024
1 parent 1bff508 commit c811987
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
16 changes: 11 additions & 5 deletions python/cudf/cudf/core/udf/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

import functools
import os
from typing import Any, Callable, Dict

Expand Down Expand Up @@ -60,10 +61,15 @@
precompiled: cachetools.LRUCache = cachetools.LRUCache(maxsize=32)
launch_arg_getters: Dict[Any, Any] = {}

_PTX_FILE = _get_ptx_file(
os.path.join(os.path.dirname(strings_udf.__file__), "..", "core", "udf"),
"shim_",
)

@functools.cache
def _ptx_file():
return _get_ptx_file(
os.path.join(
os.path.dirname(strings_udf.__file__), "..", "core", "udf"
),
"shim_",
)


@_cudf_nvtx_annotate
Expand Down Expand Up @@ -286,7 +292,7 @@ def _get_kernel(kernel_string, globals_, sig, func):
exec(kernel_string, globals_)
_kernel = globals_["_kernel"]
kernel = cuda.jit(
sig, link=[_PTX_FILE], extensions=[str_view_arg_handler]
sig, link=[_ptx_file()], extensions=[str_view_arg_handler]
)(_kernel)

return kernel
Expand Down
16 changes: 16 additions & 0 deletions python/cudf/cudf/tests/test_no_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
import os
import subprocess


def test_cudf_import_no_device():
env = os.environ.copy()
env["CUDA_VISIBLE_DEVICES"] = "-1"
output = subprocess.run(
["python", "-c", "import cudf"],
env=env,
capture_output=True,
text=True,
cwd="/",
)
assert output.returncode == 0
6 changes: 4 additions & 2 deletions python/cudf/cudf/tests/test_string_udfs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022-2023, NVIDIA CORPORATION.
# Copyright (c) 2022-2024, NVIDIA CORPORATION.

import numba
import numpy as np
Expand All @@ -20,10 +20,12 @@
string_view,
udf_string,
)
from cudf.core.udf.utils import _PTX_FILE, _get_extensionty_size
from cudf.core.udf.utils import _get_extensionty_size, _ptx_file
from cudf.testing._utils import assert_eq, sv_to_udf_str
from cudf.utils._numba import _CUDFNumbaConfig

_PTX_FILE = _ptx_file()


def get_kernels(func, dtype, size):
"""
Expand Down

0 comments on commit c811987

Please sign in to comment.