Files
Kay Hayen b7355d8b50 Plugins: Added JIT decorator callbacks and source inclusion
* Replaced the uncompiled decorator name hook with JIT decorator
  registrations that can either request bytecode or run a callback with
  the decorated function's source range.

* Added "AstUnparsing" for Python versions without "ast.unparse", and
  preserved source line numbers for bytecode functions.

* Registered Triton, CuTe, and CUDA tile decorators for source inclusion,
  and updated the Numba and CuTe plugins to the new registration API.
2026-11-14 12:21:23 +01:00

77 lines
2.0 KiB
Python

# Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
# nuitka-skip-unless-imports: numba
# nuitka-project: --mode=standalone
# nuitka-project: --enable-plugin=numba
# nuitka-project: --numba-enable-jit=yes
import numba
import numpy as np
from numba import njit, stencil
@njit(cache=True)
def imported_njit(a):
return a.sum()
@numba.njit
def attribute_njit(a):
return a.sum() + 1
def make_closure(offset):
@numba.njit
def closure_njit(a):
return a.sum() + offset
return closure_njit
def make_nested_closure(outer_offset):
def middle(middle_offset):
@numba.njit
def nested_closure_njit(a):
return a.sum() + outer_offset + middle_offset
return nested_closure_njit
return middle
@stencil
def stencil_kernel(a):
return (a[0, 0] + a[1, 0] + a[0, 1] + a[-1, 0] + a[0, -1]) / 5
x = np.arange(100).reshape(10, 10).astype(np.float64)
print("imported njit", imported_njit(x))
print("attribute njit", attribute_njit(x))
print("closure njit", make_closure(5)(x))
print("nested closure njit", make_nested_closure(3)(2)(x))
out = np.empty_like(x)
print("stencil", stencil_kernel(x, out=out).sum())
print("OK.")
# Python test originally created or extracted from other peoples work. The
# parts from me are licensed as below. It is at least Free Software where
# it's copied from other people. In these cases, that will normally be
# indicated.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.