Skip to content

Commit 1a72510

Browse files
author
Diptorup Deb
committed
Adds an experimental KernelDispacther to numba_dpex.
- The numba_dpex.experimental module adds a new dispatcher class for numba_dpex kernels. The new dispatcher is a numba.dispatcher.Dispathcer subclass. - Introduce a new compiler class that is used to compile a numba_dpex.kernel decorated function to spirv and then store the spirv module as the compiled "overload". - Adds an experimental `call_kernel` dpjit function that will be used to submit or launch kernels. The `call_kernel` function generates LLVM IR code for all the functionality currenty done in pure Python in JitKernel.__call__.
1 parent e54c058 commit 1a72510

File tree

7 files changed

+744
-0
lines changed

7 files changed

+744
-0
lines changed

numba_dpex/core/descriptor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ def _inherit_if_not_set(flags, options, name, default=targetconfig._NotSet):
3838
class DpexTargetOptions(CPUTargetOptions):
3939
experimental = _option_mapping("experimental")
4040
release_gil = _option_mapping("release_gil")
41+
no_compile = _option_mapping("no_compile")
4142

4243
def finalize(self, flags, options):
4344
super().finalize(flags, options)
4445
_inherit_if_not_set(flags, options, "experimental", False)
4546
_inherit_if_not_set(flags, options, "release_gil", False)
47+
_inherit_if_not_set(flags, options, "no_compile", True)
4648

4749

4850
class DpexKernelTarget(TargetDescriptor):

numba_dpex/experimental/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from numba.core.imputils import Registry
6+
7+
from .decorators import kernel
8+
from .kernel_dispatcher import KernelDispatcher
9+
from .launcher import call_kernel
10+
from .models import *
11+
from .types import KernelDispatcherType
12+
13+
registry = Registry()
14+
lower_constant = registry.lower_constant
15+
16+
17+
@lower_constant(KernelDispatcherType)
18+
def dpex_dispatcher_const(context, builder, ty, pyval):
19+
return context.get_dummy_value()
20+
21+
22+
__all__ = ["kernel", "KernelDispatcher", "call_kernel"]

numba_dpex/experimental/decorators.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# SPDX-FileCopyrightText: 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import inspect
6+
7+
from numba.core import sigutils
8+
9+
from .kernel_dispatcher import KernelDispatcher
10+
11+
12+
def kernel(func_or_sig=None, debug=False, cache=False, **options):
13+
"""A decorator to define a kernel function.
14+
15+
A kernel function is conceptually equivalent to a SYCL kernel function, and
16+
gets compiled into either an OpenCL or a LevelZero SPIR-V binary kernel.
17+
A kernel decorated Python function has the following restrictions:
18+
19+
* The function can not return any value.
20+
* All array arguments passed to a kernel should adhere to compute
21+
follows data programming model.
22+
"""
23+
# FIXME: The options need to be evaluated and checked here like it is
24+
# done in numba.core.decorators.jit
25+
26+
def _kernel_dispatcher(pyfunc, sigs=None):
27+
return KernelDispatcher(
28+
pyfunc=pyfunc,
29+
debug_flags=debug,
30+
enable_cache=cache,
31+
specialization_sigs=sigs,
32+
targetoptions=options,
33+
)
34+
35+
if func_or_sig is None:
36+
return _kernel_dispatcher
37+
elif isinstance(func_or_sig, str):
38+
raise NotImplementedError(
39+
"Specifying signatures as string is not yet supported by numba-dpex"
40+
)
41+
elif isinstance(func_or_sig, list) or sigutils.is_signature(func_or_sig):
42+
# String signatures are not supported as passing usm_ndarray type as
43+
# a string is not possible. Numba's sigutils relies on the type being
44+
# available in Numba's `types.__dict__` and dpex types are not
45+
# registered there yet.
46+
if isinstance(func_or_sig, list):
47+
for sig in func_or_sig:
48+
if isinstance(sig, str):
49+
raise NotImplementedError(
50+
"Specifying signatures as string is not yet supported "
51+
"by numba-dpex"
52+
)
53+
# Specialized signatures can either be a single signature or a list.
54+
# In case only one signature is provided convert it to a list
55+
if not isinstance(func_or_sig, list):
56+
func_or_sig = [func_or_sig]
57+
58+
def _specialized_kernel_dispatcher(pyfunc):
59+
return KernelDispatcher(
60+
pyfunc=pyfunc,
61+
debug_flags=debug,
62+
enable_cache=cache,
63+
specialization_sigs=func_or_sig,
64+
)
65+
66+
return _specialized_kernel_dispatcher
67+
else:
68+
func = func_or_sig
69+
if not inspect.isfunction(func):
70+
raise ValueError(
71+
"Argument passed to the kernel decorator is neither a "
72+
"function object, nor a signature. If you are trying to "
73+
"specialize the kernel that takes a single argument, specify "
74+
"the return type as void explicitly."
75+
)
76+
return _kernel_dispatcher(func)

0 commit comments

Comments
 (0)