diff --git a/cuda_core/cuda/core/experimental/__init__.py b/cuda_core/cuda/core/experimental/__init__.py index 9b978398..12fed225 100644 --- a/cuda_core/cuda/core/experimental/__init__.py +++ b/cuda_core/cuda/core/experimental/__init__.py @@ -5,5 +5,6 @@ from cuda.core.experimental._device import Device from cuda.core.experimental._event import EventOptions from cuda.core.experimental._launcher import LaunchConfig, launch +from cuda.core.experimental._linker import Linker, LinkerOptions from cuda.core.experimental._program import Program from cuda.core.experimental._stream import Stream, StreamOptions diff --git a/cuda_core/cuda/core/experimental/_linker.py b/cuda_core/cuda/core/experimental/_linker.py new file mode 100644 index 00000000..cf4c6ccd --- /dev/null +++ b/cuda_core/cuda/core/experimental/_linker.py @@ -0,0 +1,269 @@ +import weakref +from dataclasses import dataclass +from typing import List, Optional + +from cuda.bindings import nvjitlink +from cuda.core.experimental._module import ObjectCode +from cuda.core.experimental._utils import check_or_create_options + + +@dataclass +class LinkerOptions: + """Customizable :obj:`LinkerOptions` for nvJitLink. + + Attributes + ---------- + arch : str + Pass SM architecture value. Can use compute_ value instead if only generating PTX. + This is a required option. + Acceptable value type: str + Maps to: -arch=sm_ + max_register_count : int, optional + Maximum register count. + Default: None + Acceptable value type: int + Maps to: -maxrregcount= + time : bool, optional + Print timing information to InfoLog. + Default: False + Acceptable value type: bool + Maps to: -time + verbose : bool, optional + Print verbose messages to InfoLog. + Default: False + Acceptable value type: bool + Maps to: -verbose + link_time_optimization : bool, optional + Perform link time optimization. + Default: False + Acceptable value type: bool + Maps to: -lto + ptx : bool, optional + Emit PTX after linking instead of CUBIN; only supported with -lto. + Default: False + Acceptable value type: bool + Maps to: -ptx + optimization_level : int, optional + Set optimization level. Only 0 and 3 are accepted. + Default: None + Acceptable value type: int + Maps to: -O + debug : bool, optional + Generate debug information. + Default: False + Acceptable value type: bool + Maps to: -g + lineinfo : bool, optional + Generate line information. + Default: False + Acceptable value type: bool + Maps to: -lineinfo + ftz : bool, optional + Flush denormal values to zero. + Default: False + Acceptable value type: bool + Maps to: -ftz= + prec_div : bool, optional + Use precise division. + Default: True + Acceptable value type: bool + Maps to: -prec-div= + prec_sqrt : bool, optional + Use precise square root. + Default: True + Acceptable value type: bool + Maps to: -prec-sqrt= + fma : bool, optional + Use fast multiply-add. + Default: True + Acceptable value type: bool + Maps to: -fma= + kernels_used : List[str], optional + Pass list of kernels that are used; any not in the list can be removed. This option can be specified multiple + times. + Default: None + Acceptable value type: list of str + Maps to: -kernels-used= + variables_used : List[str], optional + Pass list of variables that are used; any not in the list can be removed. This option can be specified multiple + times. + Default: None + Acceptable value type: list of str + Maps to: -variables-used= + optimize_unused_variables : bool, optional + Assume that if a variable is not referenced in device code, it can be removed. + Default: False + Acceptable value type: bool + Maps to: -optimize-unused-variables + xptxas : List[str], optional + Pass options to PTXAS. This option can be called multiple times. + Default: None + Acceptable value type: list of str + Maps to: -Xptxas= + split_compile : int, optional + Split compilation maximum thread count. Use 0 to use all available processors. Value of 1 disables split + compilation (default). + Default: 1 + Acceptable value type: int + Maps to: -split-compile= + split_compile_extended : int, optional + A more aggressive form of split compilation available in LTO mode only. Accepts a maximum thread count value. + Use 0 to use all available processors. Value of 1 disables extended split compilation (default). Note: This + option can potentially impact performance of the compiled binary. + Default: 1 + Acceptable value type: int + Maps to: -split-compile-extended= + no_cache : bool, optional + Do not cache the intermediate steps of nvJitLink. + Default: False + Acceptable value type: bool + Maps to: -no-cache + """ + + arch: str + max_register_count: Optional[int] = None + time: Optional[bool] = None + verbose: Optional[bool] = None + link_time_optimization: Optional[bool] = None + ptx: Optional[bool] = None + optimization_level: Optional[int] = None + debug: Optional[bool] = None + lineinfo: Optional[bool] = None + ftz: Optional[bool] = None + prec_div: Optional[bool] = None + prec_sqrt: Optional[bool] = None + fma: Optional[bool] = None + kernels_used: Optional[List[str]] = None + variables_used: Optional[List[str]] = None + optimize_unused_variables: Optional[bool] = None + xptxas: Optional[List[str]] = None + split_compile: Optional[int] = None + split_compile_extended: Optional[int] = None + no_cache: Optional[bool] = None + + def __post_init__(self): + self.formatted_options = [] + if self.arch is not None: + self.formatted_options.append(f"-arch={self.arch}") + if self.max_register_count is not None: + self.formatted_options.append(f"-maxrregcount={self.max_register_count}") + if self.time is not None: + self.formatted_options.append("-time") + if self.verbose is not None: + self.formatted_options.append("-verbose") + if self.link_time_optimization is not None: + self.formatted_options.append("-lto") + if self.ptx is not None: + self.formatted_options.append("-ptx") + if self.optimization_level is not None: + self.formatted_options.append(f"-O{self.optimization_level}") + if self.debug is not None: + self.formatted_options.append("-g") + if self.lineinfo is not None: + self.formatted_options.append("-lineinfo") + if self.ftz is not None: + self.formatted_options.append(f"-ftz={'true' if self.ftz else 'false'}") + if self.prec_div is not None: + self.formatted_options.append(f"-prec-div={'true' if self.prec_div else 'false'}") + if self.prec_sqrt is not None: + self.formatted_options.append(f"-prec-sqrt={'true' if self.prec_sqrt else 'false'}") + if self.fma is not None: + self.formatted_options.append(f"-fma={'true' if self.fma else 'false'}") + if self.kernels_used is not None: + for kernel in self.kernels_used: + self.formatted_options.append(f"-kernels-used={kernel}") + if self.variables_used is not None: + for variable in self.variables_used: + self.formatted_options.append(f"-variables-used={variable}") + if self.optimize_unused_variables is not None: + self.formatted_options.append("-optimize-unused-variables") + if self.xptxas is not None: + for opt in self.xptxas: + self.formatted_options.append(f"-Xptxas={opt}") + if self.split_compile is not None: + self.formatted_options.append(f"-split-compile={self.split_compile}") + if self.split_compile_extended is not None: + self.formatted_options.append(f"-split-compile-extended={self.split_compile_extended}") + if self.no_cache is not None: + self.formatted_options.append("-no-cache") + + +class Linker: + __slots__ = ("__weakref__", "_handle", "_options") + + def __init__(self, *object_codes: ObjectCode, options: LinkerOptions = None): + self._options = options = check_or_create_options(LinkerOptions, options, "Linker options") + self._handle = nvjitlink.create(len(options.formatted_options), options.formatted_options) + + if len(object_codes) == 0: + raise ValueError("At least one ObjectCode object must be provided") + + for code in object_codes: + assert isinstance(code, ObjectCode) + self._add_code_object(code) + + weakref.finalize(self, self.close) + + def _add_code_object(self, object_code: ObjectCode): + data = object_code._module + assert isinstance(data, bytes) + nvjitlink.add_data( + self._handle, + self._input_type_from_code_type(object_code._code_type), + data, + len(data), + f"{object_code._handle}_{object_code._code_type}", + ) + + def link(self, target_type) -> ObjectCode: + nvjitlink.complete(self._handle) + if target_type not in ("cubin", "ptx"): + raise ValueError(f"Unsupported target type: {target_type}") + code = None + if target_type == "cubin": + cubin_size = nvjitlink.get_linked_cubin_size(self._handle) + code = bytearray(cubin_size) + nvjitlink.get_linked_cubin(self._handle, code) + else: + ptx_size = nvjitlink.get_linked_ptx_size(self._handle) + code = bytearray(ptx_size) + nvjitlink.get_linked_ptx(self._handle, code) + + return ObjectCode(bytes(code), target_type) + + def get_error_log(self) -> str: + log_size = nvjitlink.get_error_log_size(self._handle) + log = bytearray(log_size) + nvjitlink.get_error_log(self._handle, log) + return log.decode() + + def get_info_log(self) -> str: + log_size = nvjitlink.get_info_log_size(self._handle) + log = bytearray(log_size) + nvjitlink.get_info_log(self._handle, log) + return log.decode() + + def _input_type_from_code_type(self, code_type: str) -> nvjitlink.InputType: + # this list is based on the supported values for code_type in the ObjectCode class definition. + # nvjitlink supports other options for input type + if code_type == "ptx": + return nvjitlink.InputType.PTX + elif code_type == "cubin": + return nvjitlink.InputType.CUBIN + elif code_type == "fatbin": + return nvjitlink.InputType.FATBIN + elif code_type == "ltoir": + return nvjitlink.InputType.LTOIR + elif code_type == "object": + return nvjitlink.InputType.OBJECT + else: + raise ValueError(f"Unknown code_type associated with ObjectCode: {code_type}") + + @property + def handle(self) -> int: + return self._handle + + def close(self): + if self._handle is not None: + nvjitlink.destroy(self._handle) + self._handle = None diff --git a/cuda_core/docs/source/api.rst b/cuda_core/docs/source/api.rst index 1cb9811b..e10b36a8 100644 --- a/cuda_core/docs/source/api.rst +++ b/cuda_core/docs/source/api.rst @@ -31,3 +31,8 @@ CUDA compilation toolchain :toctree: generated/ Program + Linker + + :template: dataclass.rst + + LinkerOptions \ No newline at end of file diff --git a/cuda_core/docs/source/release.md b/cuda_core/docs/source/release.md index 48e24786..4c615eb3 100644 --- a/cuda_core/docs/source/release.md +++ b/cuda_core/docs/source/release.md @@ -6,4 +6,5 @@ maxdepth: 3 --- 0.1.0 + 0.2.0 ``` diff --git a/cuda_core/docs/source/release/0.2.0-notes.md b/cuda_core/docs/source/release/0.2.0-notes.md new file mode 100644 index 00000000..1a047511 --- /dev/null +++ b/cuda_core/docs/source/release/0.2.0-notes.md @@ -0,0 +1,11 @@ +# `cuda.core` Release notes + +Released on Nov , 2024 + +## Hightlights +- Addition of the Linker class which gives object oriented and pythonic access to the nvJitLink API. + +## Limitations + +-The Linker class only supports cuda >=12. For cuda <12, use low level cuLink API. + diff --git a/cuda_core/tests/test_linker.py b/cuda_core/tests/test_linker.py new file mode 100644 index 00000000..7db6ed9f --- /dev/null +++ b/cuda_core/tests/test_linker.py @@ -0,0 +1,99 @@ +import pytest + +from cuda.core.experimental import Linker, LinkerOptions, Program +from cuda.core.experimental._module import ObjectCode + +ARCH = "sm_80" # use sm_80 for testing the oop nvJitLink wrapper +empty_entrypoint_kernel = "__global__ void A() {}" +empty_kernel = "__device__ void B() {}" +addition_kernel = "__device__ int C(int a, int b) { return a + b; }" + + +@pytest.fixture(scope="function") +def compile_ptx_functions(init_cuda): + object_code_a_ptx = Program(empty_entrypoint_kernel, "c++").compile("ptx") + object_code_b_ptx = Program(empty_kernel, "c++").compile("ptx") + object_code_c_ptx = Program(addition_kernel, "c++").compile("ptx") + + return object_code_a_ptx, object_code_b_ptx, object_code_c_ptx + + +@pytest.fixture(scope="function") +def compile_ltoir_functions(init_cuda): + object_code_a_ltoir = Program(empty_entrypoint_kernel, "c++").compile("ltoir", options=("-dlto",)) + object_code_b_ltoir = Program(empty_kernel, "c++").compile("ltoir", options=("-dlto",)) + object_code_c_ltoir = Program(addition_kernel, "c++").compile("ltoir", options=("-dlto",)) + + return object_code_a_ltoir, object_code_b_ltoir, object_code_c_ltoir + + +@pytest.mark.parametrize( + "options", + [ + LinkerOptions(arch=ARCH), + LinkerOptions(arch=ARCH, max_register_count=32), + LinkerOptions(arch=ARCH, time=True), + LinkerOptions(arch=ARCH, verbose=True), + LinkerOptions(arch=ARCH, optimization_level=3), + LinkerOptions(arch=ARCH, debug=True), + LinkerOptions(arch=ARCH, lineinfo=True), + LinkerOptions(arch=ARCH, ftz=True), + LinkerOptions(arch=ARCH, prec_div=True), + LinkerOptions(arch=ARCH, prec_sqrt=True), + LinkerOptions(arch=ARCH, fma=True), + LinkerOptions(arch=ARCH, kernels_used=["kernel1"]), + LinkerOptions(arch=ARCH, variables_used=["var1"]), + LinkerOptions(arch=ARCH, optimize_unused_variables=True), + LinkerOptions(arch=ARCH, xptxas=["-v"]), + LinkerOptions(arch=ARCH, split_compile=0), + LinkerOptions(arch=ARCH, split_compile_extended=1), + LinkerOptions(arch=ARCH, no_cache=True), + ], +) +def test_linker_init(compile_ptx_functions, options): + linker = Linker(*compile_ptx_functions, options=options) + object_code = linker.link("cubin") + assert isinstance(object_code, ObjectCode) + + +def test_linker_init_invalid_arch(): + options = LinkerOptions(arch=None) + with pytest.raises(TypeError): + Linker(options) + + +def test_linker_link_ptx(compile_ltoir_functions): + options = LinkerOptions(arch=ARCH, link_time_optimization=True, ptx=True) + linker = Linker(*compile_ltoir_functions, options=options) + linked_code = linker.link("ptx") + assert isinstance(linked_code, ObjectCode) + + +def test_linker_link_cubin(compile_ptx_functions): + options = LinkerOptions(arch=ARCH) + linker = Linker(*compile_ptx_functions, options=options) + linked_code = linker.link("cubin") + assert isinstance(linked_code, ObjectCode) + + +def test_linker_link_invalid_target_type(compile_ptx_functions): + options = LinkerOptions(arch=ARCH) + linker = Linker(*compile_ptx_functions, options=options) + with pytest.raises(ValueError): + linker.link("invalid_target") + + +def test_linker_get_error_log(compile_ptx_functions): + options = LinkerOptions(arch=ARCH) + linker = Linker(*compile_ptx_functions, options=options) + linker.link("cubin") + log = linker.get_error_log() + assert isinstance(log, str) + + +def test_linker_get_info_log(compile_ptx_functions): + options = LinkerOptions(arch=ARCH) + linker = Linker(*compile_ptx_functions, options=options) + linker.link("cubin") + log = linker.get_info_log() + assert isinstance(log, str)