mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-08-29 16:56:07 +00:00
weak frompy prerequisite [PR] (#17213)
This commit is contained in:
@@ -95,7 +95,7 @@ class Scheduler:
|
||||
def shift_to(self, rng:UOp, amount:int, new_type:AxisType, top:bool=False, input_new_rng:UOp|None=None):
|
||||
if (old_sz:=rng.src[0].divides(amount)) is None:
|
||||
raise KernelOptError(f"{amount} can't divide {rng.src[0]} in {self.colored_shape()}")
|
||||
new_rng = UOp.range(amount, next(self.opt_range), new_type) if input_new_rng is None else input_new_rng
|
||||
new_rng = UOp.range(amount, next(self.opt_range), new_type, dtype=rng.dtype) if input_new_rng is None else input_new_rng
|
||||
replaced_rng = rng.replace(src=(old_sz,))
|
||||
sub_axis = (new_rng * old_sz + replaced_rng) if top else (replaced_rng * amount + new_rng)
|
||||
self.ast = self.ast.substitute({rng:sub_axis}, name=f"shift {rng.arg[:-1]} {amount} {str(new_type).split('.')[1].lower()}")
|
||||
@@ -191,7 +191,7 @@ class Scheduler:
|
||||
check(rng.arg[-1] is not AxisType.THREAD, "cannot pad thread")
|
||||
new_sz = round_up(int(rng.vmax+1), cast(int, opt.arg))
|
||||
check(rng.vmax+1 > new_sz//4, "pad adds more than quadruple the work")
|
||||
replaced_rng = UOp.range(new_sz, *rng.arg)
|
||||
replaced_rng = UOp.range(new_sz, *rng.arg, dtype=rng.dtype)
|
||||
replaces = {rng:replaced_rng}
|
||||
valid = replaced_rng < rng.vmax+1
|
||||
store_targets = {s.src[0] for s in self.ast.backward_slice_with_self if s.op is Ops.STORE}
|
||||
|
||||
+1
-1
@@ -102,7 +102,7 @@ class dtypes:
|
||||
if isinstance(x, float): return dtypes.default_float
|
||||
if isinstance(x, int): return dtypes.default_int
|
||||
# put this in the last is faster because there are more items than lists/tuples to check
|
||||
if isinstance(x, (list, tuple)): return max(dtypes.from_py(xi) for xi in x) if x else dtypes.default_float
|
||||
if isinstance(x, (list, tuple)): return strong_dtype(max(dtypes.from_py(xi) for xi in x)) if x else dtypes.default_float
|
||||
raise RuntimeError(f"Could not infer dtype of {x} with type {type(x)}")
|
||||
@staticmethod
|
||||
def finfo(dtype:DType) -> tuple[int, int]:
|
||||
|
||||
@@ -240,6 +240,7 @@ class ElementwiseMixin(CreationMixin):
|
||||
if dtypes.is_int(a.dtype) and dtypes.is_int(b.dtype):
|
||||
if rounding_mode == "trunc": return a.alu(Ops.CDIV, b)
|
||||
if rounding_mode == "floor": return a.alu(Ops.FLOORDIV, b)
|
||||
if a.dtype not in dtypes.weaks: a = a.cast(dtypes.default_float)
|
||||
d = a * b.reciprocal()
|
||||
if rounding_mode is None: return d
|
||||
if rounding_mode == "trunc": return d.trunc()
|
||||
@@ -390,7 +391,7 @@ class ElementwiseMixin(CreationMixin):
|
||||
```
|
||||
"""
|
||||
t, x = self._broadcasted(x)
|
||||
return t._inverse().maximum(x._inverse())._inverse()
|
||||
return t.cast(dtype := least_upper_dtype(t.dtype, x.dtype))._inverse().maximum(x.cast(dtype)._inverse())._inverse()
|
||||
|
||||
def copysign(self, other: Self | ConstType) -> Self:
|
||||
"""
|
||||
@@ -549,7 +550,7 @@ class ElementwiseMixin(CreationMixin):
|
||||
"""
|
||||
base, exponent = self._broadcasted(x, reverse=reverse)
|
||||
# TODO: int pow
|
||||
if not base.is_floating_point() and isinstance(x, ConstType) and not (isinstance(x, int) and x >= 0):
|
||||
if not dtypes.is_float(least_upper_dtype(base.dtype, exponent.dtype)) and isinstance(x, ConstType) and not (isinstance(x, int) and x >= 0):
|
||||
raise RuntimeError("base needs to be float")
|
||||
return base.alu(Ops.POW, exponent)
|
||||
|
||||
|
||||
@@ -784,7 +784,7 @@ class OpMixin(ElementwiseMixin, ReduceMixin):
|
||||
if self.ndim == 0: return self._split_cumalu(axis, Ops.MAX), type(self).zeros(self.shape, dtype=dtypes.int32, buffer=False)
|
||||
values, n = self._split_cumalu(axis, Ops.MAX), int(self.shape[axis])
|
||||
x, values_t = self.transpose(axis, -1), values.transpose(axis, -1)
|
||||
match = x.unsqueeze(-1).eq(values_t.unsqueeze(-2)) * type(self).ones(n, n, buffer=False).triu()
|
||||
match = x.unsqueeze(-1).eq(values_t.unsqueeze(-2)) * type(self).ones(n, n, dtype=dtypes.bool, buffer=False).triu()
|
||||
idx = (-(match * type(self).arange(n, 0, -1).reshape(n, 1)).max(-2) + n).cast(dtypes.int32)
|
||||
return values, idx.transpose(-1, axis)
|
||||
|
||||
|
||||
@@ -59,10 +59,10 @@ spec_shared = PatternMatcher([
|
||||
# STACK is everywhere too
|
||||
(UPat(Ops.STACK, dtype=dtypes.void, src=()), lambda: True),
|
||||
(UPat(Ops.STACK, src=(UPat(),), allow_any_len=True, name="s"),
|
||||
lambda s: all_same([x.shape for x in s.src]) and all(matches_dtype(x, s.dtype) for x in s.src)),
|
||||
lambda s: all_same([x.shape for x in s.src]) and all(matches_dtype(x, s.dtype) or x.dtype in dtypes.weaks for x in s.src)),
|
||||
|
||||
# ALUs: operands match the result dtype, except comparisons/WHERE; renderer-lowered shifts may use a uint32 count
|
||||
# a weak dtype matches any dtype (TODO: make python scalars weak consts)
|
||||
# a weak dtype matches any dtype until lowering commits its operand
|
||||
(UPat(Ops.WHERE, name="w", src=(UPat(dtype=dtypes.bool), UPat(), UPat())),
|
||||
lambda w: all(matches_dtype(s, w.dtype) or s.dtype in dtypes.weaks for s in w.src[1:])),
|
||||
(UPat(GroupOp.Comparison, dtype=dtypes.bool, src=(UPat.var("x"), UPat.var("y"))),
|
||||
|
||||
Reference in New Issue
Block a user