diff --git a/examples/openpilot/compile3.py b/examples/openpilot/compile3.py index 6517620842..3c5a587b78 100644 --- a/examples/openpilot/compile3.py +++ b/examples/openpilot/compile3.py @@ -28,8 +28,8 @@ def compile(onnx_file): inputs = {k:Tensor(v.numpy(), device=Device.DEFAULT).realize() if 'img' in k else v for k,v in inputs.items()} print("created tensors") - run_onnx_jit = TinyJit(lambda **kwargs: - next(iter(run_onnx({k:v.to(Device.DEFAULT) for k,v in kwargs.items()}).values())).cast('float32'), prune=True) + @TinyJit(prune=True) + def run_onnx_jit(**kwargs): return next(iter(run_onnx({k:v.to(Device.DEFAULT) for k,v in kwargs.items()}).values())).cast('float32') for i in range(3): GlobalCounters.reset() print(f"run {i}") diff --git a/tinygrad/engine/jit.py b/tinygrad/engine/jit.py index 96f92a1b88..40d6636e56 100644 --- a/tinygrad/engine/jit.py +++ b/tinygrad/engine/jit.py @@ -1,4 +1,4 @@ -from typing import TypeVar, Generic, Callable, Any +from typing import TypeVar, Generic, Callable, Any, overload import functools from tinygrad.tensor import Tensor, all_tensors from tinygrad.helpers import flatten, merge_dicts, DEBUG, Context, BEAM, getenv, JIT, JIT_BATCH_SIZE, dedup, pluralize, VIZ, disable_gc @@ -219,7 +219,7 @@ def _prepare_jit_inputs(args, kwargs): expected_input_info = [(x[0], tuple(sorted(x[1].keys(), key=lambda v: v.expr)), x[2], x[3]) for x in inputs] return input_buf_uops, var_vals, names, expected_input_info -class TinyJit(Generic[ReturnType]): +class _TinyJit(Generic[ReturnType]): def __init__(self, fxn:Callable[..., ReturnType]|None, captured:CapturedJit|None=None, prune=False): assert fxn or captured, "need either a function or a CapturedJit" self.fxn = fxn @@ -287,3 +287,10 @@ class TinyJit(Generic[ReturnType]): self.cnt += 1 return ret + +# overload signatures support both @TinyJit and @TinyJit(prune=True) syntax +@overload +def TinyJit(fxn:Callable[..., ReturnType], *, prune:bool=False) -> _TinyJit[ReturnType]: ... +@overload +def TinyJit(fxn:None=None, *, prune:bool=False) -> Callable[[Callable[..., ReturnType]], _TinyJit[ReturnType]]: ... +def TinyJit(fxn=None, **kwargs): return (lambda f: _TinyJit(f, **kwargs)) if fxn is None else _TinyJit(fxn, **kwargs)