Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 321
Various handle-related changes and improvements#463
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
54082298384937299ce8d674cee7650e3ae18a9c85b59918baee4a4f2189cb137a685043654c4bc9595b7db664263f7febc96f6dee4cde77File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| # Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. | ||
| # Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. | ||
| # | ||
| # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE | ||
| from __future__ import annotations | ||
| import ctypes | ||
| import weakref | ||
| from contextlib import contextmanager | ||
| from dataclasses import dataclass | ||
| from typing import List, Optional, Tuple, Union | ||
| from typing import TYPE_CHECKING, List, Optional, Tuple, Union | ||
| from warnings import warn | ||
| if TYPE_CHECKING: | ||
| import cuda.bindings | ||
| from cuda.core.experimental._device import Device | ||
| from cuda.core.experimental._module import ObjectCode | ||
| from cuda.core.experimental._utils import check_or_create_options, driver, handle_return, is_sequence | ||
| @@ -323,6 +328,10 @@ def _exception_manager(self): | ||
| raise e | ||
| nvJitLinkHandleT = int | ||
| LinkerHandleT = Union[nvJitLinkHandleT, "cuda.bindings.driver.CUlinkState"] | ||
leofang marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| class Linker: | ||
| """Represent a linking machinery to link one or multiple object codes into | ||
| :obj:`~cuda.core.experimental._module.ObjectCode` with the specified options. | ||
| @@ -483,10 +492,20 @@ def _input_type_from_code_type(self, code_type: str): | ||
| return input_type | ||
| @property | ||
| def handle(self): | ||
| """Return the linker handle object.""" | ||
| def handle(self) -> LinkerHandleT: | ||
| """Return the underlying handle object. | ||
| .. note:: | ||
| The type of the returned object depends on the backend. | ||
| """ | ||
| return self._mnff.handle | ||
| @property | ||
| def backend(self) -> str: | ||
| """Return this Linker instance's underlying backend.""" | ||
| return "nvJitLink" if self._mnff.use_nvjitlink else "driver" | ||
| def close(self): | ||
| """Destroy this linker.""" | ||
| self._mnff.close() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,13 +2,18 @@ | ||
| # | ||
| # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE | ||
| from __future__ import annotations | ||
| import weakref | ||
| from dataclasses import dataclass | ||
| from typing import List, Optional, Tuple, Union | ||
| from typing import TYPE_CHECKING, List, Optional, Tuple, Union | ||
| from warnings import warn | ||
| if TYPE_CHECKING: | ||
| import cuda.bindings | ||
| from cuda.core.experimental._device import Device | ||
| from cuda.core.experimental._linker import Linker, LinkerOptions | ||
| from cuda.core.experimental._linker import Linker, LinkerHandleT, LinkerOptions | ||
| from cuda.core.experimental._module import ObjectCode | ||
| from cuda.core.experimental._utils import ( | ||
| _handle_boolean_option, | ||
| @@ -331,6 +336,9 @@ def __repr__(self): | ||
| return self._formatted_options | ||
| ProgramHandleT = Union["cuda.bindings.nvrtc.nvrtcProgram", LinkerHandleT] | ||
| class Program: | ||
| """Represent a compilation machinery to process programs into | ||
| :obj:`~_module.ObjectCode`. | ||
| @@ -382,7 +390,7 @@ def __init__(self, code, code_type, options: ProgramOptions = None): | ||
| # TODO: allow tuples once NVIDIA/cuda-python#72 is resolved | ||
| self._mnff.handle = handle_return(nvrtc.nvrtcCreateProgram(code.encode(), b"", 0, [], [])) | ||
| self._backend = "nvrtc" | ||
| self._backend = "NVRTC" | ||
| self._linker = None | ||
| elif code_type == "ptx": | ||
| @@ -391,7 +399,7 @@ def __init__(self, code, code_type, options: ProgramOptions = None): | ||
| self._linker = Linker( | ||
| ObjectCode._init(code.encode(), code_type), options=self._translate_program_options(options) | ||
| ) | ||
| self._backend = "linker" | ||
| self._backend = self._linker.backend | ||
| else: | ||
| raise NotImplementedError | ||
| @@ -445,9 +453,9 @@ def compile(self, target_type, name_expressions=(), logs=None): | ||
| """ | ||
| if target_type not in self._supported_target_type: | ||
| raise NotImplementedError | ||
| raise ValueError(f"the target type {target_type} is not supported") | ||
| if self._backend == "nvrtc": | ||
| if self._backend == "NVRTC": | ||
| if target_type == "ptx" and not self._can_load_generated_ptx(): | ||
| warn( | ||
| "The CUDA driver version is older than the backend version. " | ||
| @@ -489,15 +497,20 @@ def compile(self, target_type, name_expressions=(), logs=None): | ||
| return ObjectCode._init(data, target_type, symbol_mapping=symbol_mapping) | ||
| if self._backend == "linker": | ||
| return self._linker.link(target_type) | ||
| assert self._backend in ("nvJitLink", "driver") | ||
| return self._linker.link(target_type) | ||
leofang marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| @property | ||
| def backend(self): | ||
| """Return the backend type string associated with this program.""" | ||
| def backend(self) -> str: | ||
| """Return this Program instance's underlying backend.""" | ||
| return self._backend | ||
| @property | ||
| def handle(self): | ||
leofang marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """Return the program handle object.""" | ||
| def handle(self) -> ProgramHandleT: | ||
| """Return the underlying handle object. | ||
| .. note:: | ||
| The type of the returned object depends on the backend. | ||
| """ | ||
| return self._mnff.handle | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. | ||
| // | ||
| // SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE | ||
| #pragma once | ||
| #include <type_traits> | ||
| // In cuda.bindings 12.8, the private member name was renamed from "_ptr" to "_pvt_ptr". | ||
leofang marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // We want to have the C++ layer supporting all past 12.x versions, so some tricks are needed. | ||
| // Since there's no std::has_member<T, member_name> so we use SFINAE to create the same effect. | ||
| template <typename T, | ||
| std::enable_if_t<std::is_pointer_v<decltype(std::remove_pointer_t<T>::_pvt_ptr)>, int> = 0> | ||
| inline auto& get_cuda_native_handle(const T& obj) { | ||
| return *(obj->_pvt_ptr); | ||
| } | ||
| template <typename T, | ||
| std::enable_if_t<std::is_pointer_v<decltype(std::remove_pointer_t<T>::_ptr)>, int> = 0> | ||
| inline auto& get_cuda_native_handle(const T& obj) { | ||
| return *(obj->_ptr); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.