Skip to content
Open
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
136 changes: 108 additions & 28 deletions python/CuTeDSL/cutlass/cutlass_dsl/cuda_jit_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import sys
import weakref
import threading
from typing import Any, List, Optional, Tuple, Union
from typing import Any, Callable, List, Optional, Tuple, Union

import cuda.bindings.runtime as cuda_runtime
import cuda.bindings.driver as cuda_driver
Expand All @@ -45,6 +45,10 @@
from .._mlir import ir, execution_engine


_cudaGetDevice = cuda_runtime.cudaGetDevice
_CUDA_SUCCESS = cuda_runtime.cudaError_t.cudaSuccess


class CudaDialectJitModule:
"""Holds the execution engine and cuda libraries."""

Expand All @@ -54,12 +58,47 @@ def __init__(
capi_func: Any,
execution_args: ExecutionArgs,
cuda_library: list["cuda_runtime.cudaLibrary_t"],
load_to_device: Optional[Callable[[int], None]] = None,
) -> None:
self.engine = engine
self.capi_func = capi_func
self.execution_args = execution_args
self.cuda_library = cuda_library
self._unloaded = False
# Per-device kernel setup (kernel lookup + device scoped attributes) is
# done lazily, only for the devices this module actually runs on.
self._load_to_device = load_to_device
self._loaded_devices: set[int] = set()
# One lock per device, so that a setup waiting for one device does not
# hold back the first call under another one.
self._device_locks: dict[int, threading.Lock] = {}
self._device_locks_guard = threading.Lock()

def load_to_device(self, device: int) -> None:
"""Resolves the kernels and applies device scoped attributes for ``device``.

Idempotent and thread safe. Only ``device`` is touched: setting a
device scoped attribute makes the driver load the module into that
device's context, which waits for work already running there, so it
must not be done for devices this module is not about to run on. For
the same reason the setups of different devices do not wait for each
other.
"""
if self._load_to_device is None or device in self._loaded_devices:
return
with self._device_locks_guard:
device_lock = self._device_locks.setdefault(device, threading.Lock())
with device_lock:
if device in self._loaded_devices:
return
self._load_to_device(device)
self._loaded_devices.add(device)

def load_to_current_device(self) -> None:
"""Calls :meth:`load_to_device` for the calling thread's current device."""
if self._load_to_device is None:
return
self.load_to_device(checkCudaErrors(cuda_runtime.cudaGetDevice()))

def is_unloaded(self) -> bool:
return self._unloaded
Expand All @@ -85,6 +124,30 @@ def __del__(self) -> None:
self.unload()


class CudaDialectJitExecutor(JitExecutor):
"""A :class:`JitExecutor` for a CUDA library, which is context free.

The same executor may be called under any device. The device scoped part of
the library setup is done on the first call under each device.
"""

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
# Host only modules have nothing to set up per device.
self._per_device_setup = (
getattr(self.jit_module, "_load_to_device", None) is not None
)

def run_compiled_program(self, exe_args: list[Any]) -> int | None:
# This is on the launch path: one cudaGetDevice and one set lookup when
# the device is already set up.
if self._per_device_setup:
err, device = _cudaGetDevice()
if err != _CUDA_SUCCESS or device not in self.jit_module._loaded_devices:
self.jit_module.load_to_current_device()
return super().run_compiled_program(exe_args)


class CudaDialectJitCompiledFunction(JitCompiledFunction):
"""Holds a compiled function and its module."""

Expand Down Expand Up @@ -253,8 +316,16 @@ def _get_cuda_init_and_load(self) -> Tuple[Any, Any]:

return cuda_init, cuda_load_to_device

def _load_cuda_library(self) -> List["cuda_runtime.cudaLibrary_t"]:
"""Loads the CUDA library from the engine."""
def _load_cuda_library(
self,
) -> Tuple[List["cuda_runtime.cudaLibrary_t"], Callable[[int], None]]:
"""Loads the CUDA library from the engine.

Returns the library and a function that finishes the setup for one
device (see :meth:`CudaDialectJitModule.load_to_device`). No device is
set up here: with several visible devices, doing so for all of them
makes the first call wait for the work running on every other device.
"""

cuda_init, cuda_load_to_device = self._get_cuda_init_and_load()

Expand All @@ -272,26 +343,24 @@ def _load_cuda_library(self) -> List["cuda_runtime.cudaLibrary_t"]:

checkCudaErrors((cuda_runtime.cudaError_t(err.value),))

device_id = ctypes.c_int32(0)
pointer_to_device_id = ctypes.pointer(device_id)

cuda_load_args: List[Any] = [
pointer_to_pointer_to_library,
pointer_to_device_id,
pointer_to_err,
]
packed_args = (ctypes.c_void_p * len(cuda_load_args))()
for i, arg in enumerate(cuda_load_args):
packed_args[i] = ctypes.cast(arg, ctypes.c_void_p)

for dev in range(self.num_devices):
device_id.value = dev
cuda_load_to_device(packed_args)
checkCudaErrors((cuda_runtime.cudaError_t(err.value),))

checkCudaErrors((cuda_runtime.cudaError_t(err.value),))

return [cuda_runtime.cudaLibrary_t(library.value)]
# The closure keeps the library handle and the engine function alive.
# The device and error slots are per call: different devices may be set
# up at the same time.
def load_to_device(dev: int) -> None:
device_id = ctypes.c_int32(dev)
load_err = ctypes.c_int32(0)
cuda_load_args: List[Any] = [
pointer_to_pointer_to_library,
ctypes.pointer(device_id),
ctypes.pointer(load_err),
]
packed_load_args = (ctypes.c_void_p * len(cuda_load_args))()
for i, arg in enumerate(cuda_load_args):
packed_load_args[i] = ctypes.cast(arg, ctypes.c_void_p)
cuda_load_to_device(packed_load_args)
checkCudaErrors((cuda_runtime.cudaError_t(load_err.value),))

return [cuda_runtime.cudaLibrary_t(library.value)], load_to_device

def to(self, device: Optional[int] = None) -> JitExecutor:
"""Returns an executable function bound to the given device.
Expand All @@ -300,10 +369,13 @@ def to(self, device: Optional[int] = None) -> JitExecutor:
the kernel will run.

Since CudaJitCompiledFunction uses CUDA libraries, which are context free,
binding to a device is not necessary and the device is ignored. Device is
kept in for compatibility with the JitCompiledFunction.
the returned executor is not bound to a device. The device scoped part of
the library setup is done once per device: here for ``device`` when it is
given as an index, otherwise on the first call under each device. Pass the
index to pay that cost ahead of the first launch on that device.

:param device: Specifies the device for the executor. If None the current device is used.
:param device: Device index to set the library up for now. If None, each
device is set up on the first call under it.
:type device: Optional[Union[int, CUdevice]]
:return: A callable executor function.
:rtype: JitExecutor
Expand All @@ -315,17 +387,25 @@ def to(self, device: Optional[int] = None) -> JitExecutor:
isinstance(self.jit_module, CudaDialectJitModule)
and self.jit_module.is_unloaded()
):
cuda_library = self._load_cuda_library() if self.has_gpu_module else []
cuda_library, load_to_device = (
self._load_cuda_library() if self.has_gpu_module else ([], None)
)
self.jit_module = CudaDialectJitModule(
self.engine,
self.capi_func,
self.execution_args,
cuda_library,
load_to_device,
)

if isinstance(device, int) and isinstance(
self.jit_module, CudaDialectJitModule
):
self.jit_module.load_to_device(device)

# F-SPEC: the derived executor validates re-entries against the
# same sealed record as the handle it came from.
return JitExecutor(
return CudaDialectJitExecutor(
self.jit_module, None, self.jit_time_profiling, spec=self._pyir_spec
)

Expand Down
Loading