forked from tinygrad/tinygrad
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbf8e641ac | ||
|
|
ac53c8866e | ||
|
|
c260482d84 | ||
|
|
26d54530d6 |
@@ -50,4 +50,7 @@ exclude = [
|
|||||||
"E303", "E304", "E501", "E702", "E703", "E731", "W191",
|
"E303", "E304", "E501", "E702", "E703", "E731", "W191",
|
||||||
"W291", "W293", "UP039", "C416", "RET506", "RET507", "A",
|
"W291", "W293", "UP039", "C416", "RET506", "RET507", "A",
|
||||||
"FURB110", "RUF018", "F541", "F841"
|
"FURB110", "RUF018", "F541", "F841"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[format]
|
||||||
|
exclude = ["*"]
|
||||||
|
|||||||
@@ -1526,7 +1526,7 @@ class TestSchedule(unittest.TestCase):
|
|||||||
# run_schedule(check_schedule(out, 1))
|
# run_schedule(check_schedule(out, 1))
|
||||||
run_schedule(check_schedule(out, 4))
|
run_schedule(check_schedule(out, 4))
|
||||||
np.testing.assert_allclose(out.numpy(), np.pad(np.log2(np.abs(np.pad(np.log2(a.numpy()), ((0, 1), (0, 1), (0, 1)), constant_values=1.0).sum() + \
|
np.testing.assert_allclose(out.numpy(), np.pad(np.log2(np.abs(np.pad(np.log2(a.numpy()), ((0, 1), (0, 1), (0, 1)), constant_values=1.0).sum() + \
|
||||||
b.numpy())), ((0, 1), (0, 1), (0, 1)), constant_values=1.0).sum(), atol=3e-4, rtol=1e-6)
|
b.numpy())), ((0, 1), (0, 1), (0, 1)), constant_values=1.0).sum(), atol=3e-4, rtol=1e-5)
|
||||||
|
|
||||||
def test_shrink_pad_safe(self):
|
def test_shrink_pad_safe(self):
|
||||||
a = Tensor.ones((3, )).contiguous().realize()
|
a = Tensor.ones((3, )).contiguous().realize()
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ pm_gradient = PatternMatcher([
|
|||||||
(UPat(Ops.EXPAND, name="ret"), lambda ctx, ret: (ctx.r(Ops.ADD, tuple(i for i,(si,so) in enumerate(zip(ret.src[0].shape, ret.arg)) if si!=so)),)),
|
(UPat(Ops.EXPAND, name="ret"), lambda ctx, ret: (ctx.r(Ops.ADD, tuple(i for i,(si,so) in enumerate(zip(ret.src[0].shape, ret.arg)) if si!=so)),)),
|
||||||
(UPat(Ops.MULTI, name="ret"), lambda ctx, ret: ctx.shard(ret.device, ret.axis).src),
|
(UPat(Ops.MULTI, name="ret"), lambda ctx, ret: ctx.shard(ret.device, ret.axis).src),
|
||||||
# there's no gradient for bitcast
|
# there's no gradient for bitcast
|
||||||
(UPat(Ops.BITCAST), lambda ctx: (None,)),
|
(UPat(Ops.BITCAST), lambda: (None,)),
|
||||||
])
|
])
|
||||||
|
|
||||||
def _deepwalk(root:UOp, targets:set[UOp]) -> list[UOp]:
|
def _deepwalk(root:UOp, targets:set[UOp]) -> list[UOp]:
|
||||||
|
|||||||
+1
-1
@@ -96,7 +96,7 @@ def suppress_finalizing(func):
|
|||||||
if not getattr(sys, 'is_finalizing', lambda: True)(): raise # re-raise if not finalizing
|
if not getattr(sys, 'is_finalizing', lambda: True)(): raise # re-raise if not finalizing
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
def unwrap_class_type(cls_t:T): return cls_t.func if isinstance(cls_t, functools.partial) else cls_t
|
def unwrap_class_type(cls_t): return cls_t.func if isinstance(cls_t, functools.partial) else cls_t
|
||||||
|
|
||||||
def pluralize(st:str, cnt:int): return f"{cnt} {st}"+('' if cnt == 1 else 's')
|
def pluralize(st:str, cnt:int): return f"{cnt} {st}"+('' if cnt == 1 else 's')
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
|
from typing import TypeVar
|
||||||
from tinygrad.uop import Ops
|
from tinygrad.uop import Ops
|
||||||
from tinygrad.helpers import T
|
from tinygrad.dtype import dtypes, ConstType
|
||||||
from tinygrad.dtype import dtypes
|
|
||||||
|
|
||||||
|
TMathTrait = TypeVar("TMathTrait", bound="MathTrait")
|
||||||
class MathTrait:
|
class MathTrait:
|
||||||
# required to implement
|
# required to implement
|
||||||
def alu(self:T, op:Ops, *src) -> T: raise NotImplementedError
|
def alu(self:TMathTrait, op:Ops, *src:TMathTrait) -> TMathTrait: raise NotImplementedError
|
||||||
def const_like(self:T, b) -> T: raise NotImplementedError
|
def const_like(self:TMathTrait, b:ConstType) -> TMathTrait: raise NotImplementedError
|
||||||
|
|
||||||
# great functions you get!
|
# great functions you get!
|
||||||
def ufix(self, x): return self.const_like(x) if not isinstance(x, MathTrait) else x
|
def ufix(self:TMathTrait, x:ConstType|TMathTrait) -> TMathTrait: return self.const_like(x) if not isinstance(x, MathTrait) else x
|
||||||
def _binop(self, op, x, reverse): return self.ufix(x).alu(op, self) if reverse else self.alu(op, self.ufix(x))
|
def _binop(self:TMathTrait, op:Ops, x:TMathTrait|ConstType, reverse:bool) -> TMathTrait:
|
||||||
|
return self.ufix(x).alu(op, self) if reverse else self.alu(op, self.ufix(x))
|
||||||
def logical_not(self): return self.ne(True)
|
def logical_not(self): return self.ne(True)
|
||||||
def neg(self):
|
def neg(self):
|
||||||
if (dtype:=getattr(self, 'dtype')) is None: raise TypeError(f"MathTraits __neg__ requires a dtype, {self=}")
|
if (dtype:=getattr(self, 'dtype')) is None: raise TypeError(f"MathTraits __neg__ requires a dtype, {self=}")
|
||||||
|
|||||||
Reference in New Issue
Block a user