forked from tinygrad/tinygrad
nicer TinyJit decorator (#17293)
This commit is contained in:
@@ -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}")
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user