|
1 | | -def add(n1: int, n2: int) -> int: |
2 | | - """Add the arguments.""" |
3 | | - return n1 + n2 |
| 1 | +# https://github.com/search?q=gumerov+translation+language%3APython&type=code&l=Python |
| 2 | +from collections.abc import Mapping, Sequence |
| 3 | +from functools import cache, wraps |
| 4 | +from types import ModuleType |
| 5 | +from typing import Any, Callable, ParamSpec, TypeVar |
| 6 | + |
| 7 | +from array_api_compat import array_namespace |
| 8 | +from frozendict import frozendict |
| 9 | + |
| 10 | +P = ParamSpec("P") |
| 11 | +T = TypeVar("T") |
| 12 | + |
| 13 | + |
| 14 | +def _default_decorator( |
| 15 | + module: ModuleType, /, *args: Any, **kwargs: Any |
| 16 | +) -> Callable[[Callable[P, T]], Callable[P, T]]: |
| 17 | + if "numpy" in module.__name__: |
| 18 | + import numba |
| 19 | + |
| 20 | + return numba.jit(*args, **kwargs) |
| 21 | + elif "torch" in module.__name__: |
| 22 | + import torch |
| 23 | + |
| 24 | + return torch.compile |
| 25 | + else: |
| 26 | + return getattr(module, "jit", lambda x: x)(*args, **kwargs) |
| 27 | + |
| 28 | + |
| 29 | +def jit( |
| 30 | + decorator: Mapping[str, Callable[[Callable[P, T]], Callable[P, T]]] | None = None, |
| 31 | + /, |
| 32 | + *, |
| 33 | + decorator_args: Mapping[str, Sequence[Any]] | None = None, |
| 34 | + decorator_kwargs: Mapping[str, Mapping[str, Any]] | None = None, |
| 35 | +) -> Callable[[Callable[P, T]], Callable[P, T]]: |
| 36 | + """ |
| 37 | + Just-in-time compilation decorator with multiple backends. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + decorator : Mapping[str, Callable[[Callable[P, T]], Callable[P, T]]] | None, optional |
| 42 | + The JIT decorator to use for each array namespace, by default None |
| 43 | + decorator_args : Mapping[str, Sequence[Any]] | None, optional |
| 44 | + Additional positional arguments for the decorator for each array namespace, by default None |
| 45 | + decorator_kwargs : Mapping[str, Mapping[str, Any]] | None, optional |
| 46 | + Additional keyword arguments for the decorator for each array namespace, by default None |
| 47 | +
|
| 48 | + Returns |
| 49 | + ------- |
| 50 | + Callable[[Callable[P, T]], Callable[P, T]] |
| 51 | + The JIT decorator that can be applied to a function. |
| 52 | +
|
| 53 | + """ |
| 54 | + |
| 55 | + def new_decorator(f: Callable[P, T]) -> Callable[P, T]: |
| 56 | + decorator_args_ = frozendict(decorator_args or {}) |
| 57 | + decorator_kwargs_ = frozendict(decorator_kwargs or {}) |
| 58 | + decorator_ = decorator or {} |
| 59 | + |
| 60 | + @cache |
| 61 | + def jit_cached(xp: ModuleType) -> Callable[P, T]: |
| 62 | + decorator_args__ = decorator_args_.get(xp, ()) |
| 63 | + decorator_kwargs__ = decorator_kwargs_.get(xp, {}) |
| 64 | + name = xp.__name__ |
| 65 | + if name in decorator_: |
| 66 | + decorator_current = decorator_[name] |
| 67 | + else: |
| 68 | + decorator_current = _default_decorator(xp, *decorator_args_, **decorator_kwargs_) |
| 69 | + |
| 70 | + return decorator_current(f, *decorator_args__, **decorator_kwargs__) |
| 71 | + |
| 72 | + @wraps(f) |
| 73 | + def inner(*args_inner: P.args, **kwargs_inner: P.kwargs) -> T: |
| 74 | + try: |
| 75 | + xp = array_namespace(*args_inner) |
| 76 | + except TypeError as e: |
| 77 | + if e.args[0] == "Unrecognized array input": |
| 78 | + return f(*args_inner, **kwargs_inner) |
| 79 | + raise |
| 80 | + f_jit = jit_cached(xp) |
| 81 | + return f_jit(*args_inner, **kwargs_inner) |
| 82 | + |
| 83 | + return inner |
| 84 | + |
| 85 | + return new_decorator |
0 commit comments