-
Notifications
You must be signed in to change notification settings - Fork 226
[NVVM] Support - Followup enhancements #1218
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
Draft
abhilash1910
wants to merge
14
commits into
NVIDIA:main
Choose a base branch
from
abhilash1910:nvvm_enhance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a4db20b
add ltoir test support
abhilash1910 fb6cfb3
add options for multi-modules
abhilash1910 7aaed4e
add tests
abhilash1910 64c7f7d
add bitcode test
abhilash1910 42ba301
[pre-commit.ci] auto code formatting
pre-commit-ci[bot] 7ca6899
fix format
abhilash1910 0674ea1
[pre-commit.ci] auto code formatting
pre-commit-ci[bot] 03b1224
refresh
abhilash1910 033f11c
apply bitcode file from cupy_test helpers
abhilash1910 6e411ee
use 2 tuples
abhilash1910 b4c21db
Merge branch 'main' into nvvm_enhance
abhilash1910 aeb26aa
refresh
abhilash1910 b3d6d96
format
abhilash1910 edd6401
[pre-commit.ci] auto code formatting
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import weakref | ||
| from contextlib import contextmanager | ||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Union | ||
| from typing import TYPE_CHECKING, List, Tuple, Union | ||
| from warnings import warn | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -298,6 +298,10 @@ class ProgramOptions: | |
| split_compile: int | None = None | ||
| fdevice_syntax_only: bool | None = None | ||
| minimal: bool | None = None | ||
| # Creating as 2 tuples ((names, source), (names,source)) | ||
| extra_sources: ( | ||
| Union[List[Tuple[str, Union[str, bytes, bytearray]]], Tuple[Tuple[str, Union[str, bytes, bytearray]]]] | None | ||
| ) = None | ||
| numba_debug: bool | None = None # Custom option for Numba debugging | ||
|
|
||
| def __post_init__(self): | ||
|
|
@@ -419,8 +423,6 @@ def __post_init__(self): | |
| self._formatted_options.append("--fdevice-syntax-only") | ||
| if self.minimal is not None and self.minimal: | ||
| self._formatted_options.append("--minimal") | ||
| if self.numba_debug: | ||
| self._formatted_options.append("--numba-debug") | ||
|
Comment on lines
-422
to
-423
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw plz do not remove this and the related tests, however ugly this is needed for future devtool works 😅 |
||
|
|
||
| def _as_bytes(self): | ||
| # TODO: allow tuples once NVIDIA/cuda-python#72 is resolved | ||
|
|
@@ -470,26 +472,33 @@ def close(self): | |
| nvvm.destroy_program(self.handle) | ||
| self.handle = None | ||
|
|
||
| __slots__ = ("__weakref__", "_mnff", "_backend", "_linker", "_options") | ||
| __slots__ = ("__weakref__", "_mnff", "_backend", "_linker", "_options", "_module_count") | ||
|
|
||
| def __init__(self, code, code_type, options: ProgramOptions = None): | ||
| self._mnff = Program._MembersNeededForFinalize(self, None, None) | ||
|
|
||
| self._options = options = check_or_create_options(ProgramOptions, options, "Program options") | ||
| code_type = code_type.lower() | ||
| self._module_count = 0 | ||
|
|
||
| if code_type == "c++": | ||
| assert_type(code, str) | ||
| # TODO: support pre-loaded headers & include names | ||
| # TODO: allow tuples once NVIDIA/cuda-python#72 is resolved | ||
|
|
||
| if options.extra_sources is not None: | ||
| raise ValueError("extra_sources is not supported by the NVRTC backend (C++ code_type)") | ||
|
|
||
| # TODO: allow tuples once NVIDIA/cuda-python#72 is resolved | ||
| self._mnff.handle = handle_return(nvrtc.nvrtcCreateProgram(code.encode(), options._name, 0, [], [])) | ||
| self._mnff.backend = "NVRTC" | ||
| self._backend = "NVRTC" | ||
| self._linker = None | ||
|
|
||
| elif code_type == "ptx": | ||
| assert_type(code, str) | ||
| if options.extra_sources is not None: | ||
| raise ValueError("extra_sources is not supported by the PTX backend.") | ||
|
|
||
| self._linker = Linker( | ||
| ObjectCode._init(code.encode(), code_type), options=self._translate_program_options(options) | ||
| ) | ||
|
|
@@ -505,6 +514,40 @@ def __init__(self, code, code_type, options: ProgramOptions = None): | |
| self._mnff.handle = nvvm.create_program() | ||
| self._mnff.backend = "NVVM" | ||
| nvvm.add_module_to_program(self._mnff.handle, code, len(code), options._name.decode()) | ||
| self._module_count = 1 | ||
| # Add extra modules if provided | ||
| if options.extra_sources is not None: | ||
| if not is_sequence(options.extra_sources): | ||
| raise TypeError( | ||
| "extra_modules must be a sequence of 2-tuples:((name1, source1), (name2, source2), ...)" | ||
| ) | ||
| for i, module in enumerate(options.extra_sources): | ||
| if not isinstance(module, tuple) or len(module) != 2: | ||
| raise TypeError( | ||
| f"Each extra module must be a 2-tuple (name, source)" | ||
| f", got {type(module).__name__} at index {i}" | ||
| ) | ||
|
|
||
| module_name, module_source = module | ||
|
|
||
| if not isinstance(module_name, str): | ||
| raise TypeError(f"Module name at index {i} must be a string,got {type(module_name).__name__}") | ||
|
|
||
| if isinstance(module_source, str): | ||
| # Textual LLVM IR - encode to UTF-8 bytes | ||
| module_source = module_source.encode("utf-8") | ||
| elif not isinstance(module_source, (bytes, bytearray)): | ||
| raise TypeError( | ||
| f"Module source at index {i} must be str (textual LLVM IR), bytes (textual LLVM IR or bitcode), " | ||
| f"or bytearray, got {type(module_source).__name__}" | ||
| ) | ||
|
|
||
| if len(module_source) == 0: | ||
| raise ValueError(f"Module source for '{module_name}' (index {i}) cannot be empty") | ||
|
|
||
| nvvm.add_module_to_program(self._mnff.handle, module_source, len(module_source), module_name) | ||
| self._module_count += 1 | ||
|
|
||
| self._backend = "NVVM" | ||
| self._linker = None | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is safe to use normal Python
list/tuplein typing now