forked from tinygrad/tinygrad
some tri cleanups [PR] (#17829)
This commit is contained in:
@@ -78,8 +78,7 @@ class CreationMixin(DTypeMixin, MovementMixin):
|
||||
from tinygrad.uop.ops import UOp
|
||||
new_shape = argfix(shape)
|
||||
dt = to_dtype(dtype) if dtype is not None else fill_value.dtype if isinstance(fill_value, UOp) else dtypes.from_py(fill_value)
|
||||
val = cls.const(fill_value, dt)
|
||||
val = val.reshape((1,)*len(new_shape)).expand(new_shape)
|
||||
val = cls.const(fill_value, dt).expand(new_shape)
|
||||
if not buffer: return val
|
||||
ret = val.empty_like(dt if dtype is not None else None, device)
|
||||
return cls._wrap_uop(ret._uop.after(ret._uop.store(val._uop)))
|
||||
|
||||
@@ -812,7 +812,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, dtype=dtypes.bool, buffer=False).triu()
|
||||
match = x.unsqueeze(-1).eq(values_t.unsqueeze(-2)) * self._tri(n, n)
|
||||
idx = (-(match * type(self).arange(n, 0, -1).reshape(n, 1)).max(-2) + n).cast(dtypes.int32)
|
||||
return values, idx.transpose(-1, axis)
|
||||
|
||||
@@ -859,7 +859,7 @@ class OpMixin(ElementwiseMixin, ReduceMixin):
|
||||
last_dim_size = x.shape[-1]
|
||||
x_unsqueezed = x.unsqueeze(-2)
|
||||
x_cummax = x.cummax(-1)[0].detach()
|
||||
mask = type(self).ones(last_dim_size, last_dim_size, buffer=False, dtype=dtypes.bool).tril()
|
||||
mask = self._tri(last_dim_size, last_dim_size, 1).logical_not()
|
||||
ret = mask.where(x_unsqueezed - x_cummax.unsqueeze(-1), self.dtype.min).exp().sum(-1).log() + x_cummax
|
||||
return ret.transpose(-1, axis)
|
||||
|
||||
@@ -956,7 +956,7 @@ class OpMixin(ElementwiseMixin, ReduceMixin):
|
||||
x = blue_box.cat(flipped_green_box.flip(flip_dims), dim=crossover_dim)
|
||||
x = x.flatten(dim, dim+n_stages-1).shrink_to(self.shape)
|
||||
# compute indices for sorted values
|
||||
mask = type(self).ones(orig_len, orig_len, dtype=dtypes.bool, buffer=False).tril()
|
||||
mask = self._tri(orig_len, orig_len, 1).logical_not()
|
||||
mask = mask.reshape((None, None) + (1,)*(self.ndim-dim-1))
|
||||
def compute_counts(t:Self): return (mask & t.unsqueeze(dim).eq(t.unsqueeze(dim+1))).sum(dim+1)
|
||||
count_orig, count_sorted = compute_counts(self), compute_counts(x)
|
||||
|
||||
+2
-3
@@ -1037,7 +1037,7 @@ def get_onnx_ops() -> dict[str, types.FunctionType|dict[OpSetId, types.FunctionT
|
||||
attn_scores = mask.where(attn_scores, mask_filter_value)
|
||||
|
||||
if unidirectional:
|
||||
causal_mask = Tensor.ones((seq_len, seq_len), dtype=dtypes.bool, buffer=False).tril()
|
||||
causal_mask = Tensor._tri(seq_len, seq_len, 1).logical_not()
|
||||
attn_scores = causal_mask.where(attn_scores, mask_filter_value)
|
||||
|
||||
output = attn_scores.softmax(-1) @ v
|
||||
@@ -1069,8 +1069,7 @@ def get_onnx_ops() -> dict[str, types.FunctionType|dict[OpSetId, types.FunctionT
|
||||
qk_matmul_return_val = scores
|
||||
|
||||
if is_causal:
|
||||
causal_mask = Tensor.ones(Q.shape[-2], K.shape[-2], dtype=dtypes.bool, buffer=False).tril(0)
|
||||
scores = scores.masked_fill(causal_mask.logical_not(), -float("inf"))
|
||||
scores = scores.masked_fill(Tensor._tri(Q.shape[-2], K.shape[-2], 1), -float("inf"))
|
||||
|
||||
if attn_mask is not None:
|
||||
mask_to_add = attn_mask.where(0, -float("inf")) if attn_mask.dtype == dtypes.bool else attn_mask
|
||||
|
||||
+1
-1
@@ -615,7 +615,7 @@ class UOp(RandMixin, metaclass=UOpMetaClass):
|
||||
if isinstance(b, tuple): return UOp.stack(*[UOp.const(c, dtype) for c in b])
|
||||
# .cast folds away at exactly the dtypes a CONST derives (bool/weakint/weakfloat): bare there, the pair everywhere else
|
||||
return UOp(Ops.CONST, arg=dtype.const(b), src=()).cast(dtype)
|
||||
# weak CONST with width on the CAST. TODO: this is the final const
|
||||
# a forced CAST for bool: .cast(bool) folds, so UOp.const cannot state the width
|
||||
@staticmethod
|
||||
def cconst(b:ConstLike, dtype:DType): return UOp(Ops.CAST, src=(UOp.const(b),), arg=dtype)
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user