Skip to content
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

CUDA: Add support for the jit_module function #6515

Closed
wants to merge 9 commits into from

Conversation

gmarkall
Copy link
Member

This PR adds the jit_module decorator to CUDA as numba.cuda.jit_module. It is slightly different to the numba.jit_module function, in that it compiles functions as device functions by default, but can be made to compile functions as kernels by passing device=False to it. This is handy for jitting some functions as device functions and others as kernels by calling it twice in a module, and the example added to the documentation reflects this.

Whilst writing tests I noticed that FakeCUDAKernel from the simulator stores the Python function as fn instead of py_func, which is a discrepancy between it and the real dispatcher - to make this more consistent I've moved fn to py_func and provided a property fn with a deprecation warning to support the old name - though, it's unlikely this was ever used by anyone as it's not advertised and specific to the simulator only.

@gmarkall gmarkall added 2 - In Progress CUDA CUDA related issue/PR labels Nov 24, 2020
@gmarkall
Copy link
Member Author

A question related to this PR: how would others feel about the jit_module decorator allowing jitting of other modules, to support the UDF use case described by @dscole in #2630 (comment)? I am also aware of other users that are thinking of potential UDF usecases too. I imagine implementing this with something like the following change (as a sketch - some error checking would make sense, e.g. to check a module is passed):

diff --git a/numba/cuda/decorators.py b/numba/cuda/decorators.py
index 9923bf08c..267f02951 100644
--- a/numba/cuda/decorators.py
+++ b/numba/cuda/decorators.py
@@ -163,7 +163,7 @@ def convert_types(restype, argtypes):
     return restype, argtypes
 
 
-def jit_module(**kwargs):
+def jit_module(module=None, **kwargs):
     """ Automatically ``jit``-wraps functions defined in a Python module. By
     default, wrapped functions are treated as device functions rather than
     kernels - pass ``device=False`` to treat functions as kernels.
@@ -185,9 +185,10 @@ def jit_module(**kwargs):
     if 'device' not in kwargs:
         kwargs['device'] = True
 
-    # Get the module jit_module is being called from
-    frame = inspect.stack()[1]
-    module = inspect.getmodule(frame[0])
+    if module is None:
+        # Get the module jit_module is being called from
+        frame = inspect.stack()[1]
+        module = inspect.getmodule(frame[0])
     # Replace functions in module with jit-wrapped versions
     for name, obj in module.__dict__.items():
         if inspect.isfunction(obj) and inspect.getmodule(obj) == module:

With this, one could do:

from numba import cuda
import user_functions

cuda.jit_module(user_functions)

@cuda.jit
def wrapper(x):
    i = cuda.grid(1)
    if i < len(x):
        x = user_functions.computation(x)

where user_functions contains something like:

import math

def computation(x):
    # Some arbitrary mathematical operation
    return math.cos(x) + x ** x

Here the user code itself needn't be aware of Numba at all, whereas presently (in this PR and in the CPU target jit_module function) the user is required to call jit_module in their module.

@gmarkall gmarkall added this to the PR Backlog milestone Nov 24, 2020
@stuartarchibald stuartarchibald added the Effort - medium Medium size effort needed label Nov 24, 2020
@@ -7,3 +7,10 @@

compile_ptx = None
compile_ptx_for_current_device = None


class DeviceFunctionTemplate:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining why it's an empty marker class.

@gmarkall gmarkall added 4 - Waiting on author Waiting for author to respond to review and removed 3 - Ready for Review labels Mar 28, 2023
@github-actions
Copy link

This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks!

@github-actions github-actions bot added the stale Marker label for stale issues. label Jun 27, 2023
@gmarkall
Copy link
Member Author

Still interested in this, just haven't got it to the top of my pile in a long while :-)

@github-actions github-actions bot removed the stale Marker label for stale issues. label Jun 28, 2023
@github-actions
Copy link

This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks!

@github-actions github-actions bot added the stale Marker label for stale issues. label Sep 27, 2023
@gmarkall
Copy link
Member Author

gmarkall commented Oct 2, 2023

I still intend to address this one day when it bubbles to the top of my queue.

@gmarkall gmarkall removed the stale Marker label for stale issues. label Oct 2, 2023
Copy link

github-actions bot commented Jan 1, 2024

This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks!

@github-actions github-actions bot added the stale Marker label for stale issues. label Jan 1, 2024
@github-actions github-actions bot added the abandoned - stale PRs automatically closed due to stale status label Jan 9, 2024
@github-actions github-actions bot closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - Waiting on author Waiting for author to respond to review abandoned - stale PRs automatically closed due to stale status CUDA CUDA related issue/PR Effort - medium Medium size effort needed stale Marker label for stale issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants