-
Notifications
You must be signed in to change notification settings - Fork 47
Implement a thread-local means to access kernel launch config. #288
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
Open
tpn
wants to merge
4
commits into
NVIDIA:main
Choose a base branch
from
tpn:280-launch-config-contextvar
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b229ca7
Implement a thread-local means to access kernel launch config.
tpn 2faf9cd
Add args and dispatcher to LaunchConfig.
tpn 6a88b46
Implement ensure_current_launch_config().
tpn fea9f79
Add support for pre-kernel-launch callbacks to launch config.
tpn 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
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
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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from contextvars import ContextVar | ||
| from contextlib import contextmanager | ||
| from typing import ( | ||
| Any, | ||
| Callable, | ||
| List, | ||
| Tuple, | ||
| Optional, | ||
| TYPE_CHECKING, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from numba.cuda.dispatcher import CUDADispatcher, _Kernel | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class LaunchConfig: | ||
|
Contributor
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. There seems to be quite some overlap with |
||
| """ | ||
| Helper class used to encapsulate kernel launch configuration for storing | ||
| and retrieving from a thread-local ContextVar. | ||
| """ | ||
|
|
||
| dispatcher: "CUDADispatcher" | ||
| args: Tuple[Any, ...] | ||
| griddim: Tuple[int, int, int] | ||
| blockdim: Tuple[int, int, int] | ||
| stream: Any | ||
| sharedmem: int | ||
| pre_launch_callbacks: List[Callable[["_Kernel", "LaunchConfig"], None]] | ||
| """ | ||
| List of functions to call before launching a kernel. The functions are | ||
| called with the kernel and the launch config as arguments. This enables | ||
| just-in-time modifications to the kernel's configuration prior to launch, | ||
| such as appending extensions for dynamic types that were created after the | ||
| @cuda.jit decorator appeared (i.e. as part of rewriting). | ||
| """ | ||
|
|
||
| def __str__(self) -> str: | ||
| a = ", ".join(map(str, self.args)) | ||
| g = "×".join(map(str, self.griddim)) | ||
| b = "×".join(map(str, self.blockdim)) | ||
| cb = ", ".join(map(str, self.pre_launch_callbacks)) | ||
| return ( | ||
| f"<LaunchConfig args=[{a}], grid={g}, block={b}, " | ||
| f"stream={self.stream}, smem={self.sharedmem}B, " | ||
| f"pre_launch_callbacks=[{cb}]>" | ||
| ) | ||
|
|
||
|
|
||
| _launch_config_var: ContextVar[Optional[LaunchConfig]] = ContextVar( | ||
| "_launch_config_var", | ||
| default=None, | ||
| ) | ||
|
|
||
|
|
||
| def current_launch_config() -> Optional[LaunchConfig]: | ||
| """ | ||
| Read the launch config visible in *this* thread/asyncio task. | ||
| Returns None if no launch config is set. | ||
| """ | ||
| return _launch_config_var.get() | ||
|
|
||
|
|
||
| def ensure_current_launch_config() -> LaunchConfig: | ||
| """ | ||
| Ensure that a launch config is set for *this* thread/asyncio task. | ||
| Returns the launch config. Raises RuntimeError if no launch config is set. | ||
| """ | ||
| launch_config = current_launch_config() | ||
| if launch_config is None: | ||
| raise RuntimeError("No launch config set for this thread/asyncio task") | ||
| return launch_config | ||
|
|
||
|
|
||
| @contextmanager | ||
| def launch_config_ctx( | ||
| *, | ||
| dispatcher: "CUDADispatcher", | ||
| args: Tuple[Any, ...], | ||
| griddim: Tuple[int, int, int], | ||
| blockdim: Tuple[int, int, int], | ||
| stream: Any, | ||
| sharedmem: int, | ||
| ): | ||
| """ | ||
| Install a LaunchConfig for the dynamic extent of the with-block. | ||
| The previous value (if any) is restored automatically. | ||
| """ | ||
| pre_launch_callbacks = [] | ||
| launch_config = LaunchConfig( | ||
| dispatcher, | ||
| args, | ||
| griddim, | ||
| blockdim, | ||
| stream, | ||
| sharedmem, | ||
| pre_launch_callbacks, | ||
| ) | ||
| token = _launch_config_var.set(launch_config) | ||
| try: | ||
| yield launch_config | ||
| finally: | ||
| _launch_config_var.reset(token) | ||
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.
Specialized kernels cannot be recompiled, so a new launch configuration would not be able to affect the compilation of a new version - so this check could be kept outside the context manager.