From 64ccbde3bbe2c23463622fd9eb90f1971edf01ac Mon Sep 17 00:00:00 2001 From: chenyu Date: Fri, 14 Aug 2026 21:07:01 -0400 Subject: [PATCH] clean up STACK with a const [PR] (#17539) --- tinygrad/uop/ops.py | 4 +--- tinygrad/uop/weak.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/tinygrad/uop/ops.py b/tinygrad/uop/ops.py index 6611ef12bf..58df80338a 100644 --- a/tinygrad/uop/ops.py +++ b/tinygrad/uop/ops.py @@ -796,11 +796,9 @@ class UOp(RandMixin, metaclass=UOpMetaClass): case Ops.PAD | Ops.SHRINK: src_args = list(zip(*arg)) case Ops.PERMUTE | Ops.FLIP: src_args = [] case Ops.STACK: - # arg is the other srcs; all are cast to the promoted dtype, spec requires STACK srcs to match its dtype srcs = (self,)+tuple(arg) dtype = cast(DType, dtype_from_uop(Ops.STACK, srcs, None)) - # TODO: why cast here? - return UOp(Ops.STACK, dtype, tuple(u if u.base.is_invalid else u.cast(dtype) for u in srcs)) + return UOp(Ops.STACK, dtype, tuple(u if u.base.is_invalid else UOp.const(u.val, dtype) if u.op is Ops.CONST else u.cast(dtype) for u in srcs)) case _: raise RuntimeError(f"{op} is not a MovementOp") usrcs = [shape_to_shape_arg(arg) for arg in src_args] if len(usrcs) == 0: return UOp(op, src=(self,), arg=arg) diff --git a/tinygrad/uop/weak.py b/tinygrad/uop/weak.py index 398c68fb3c..d5410aa3f6 100644 --- a/tinygrad/uop/weak.py +++ b/tinygrad/uop/weak.py @@ -12,7 +12,7 @@ def lower_weak_node(u:UOp) -> UOp|None: if src == u.src or any(s.dtype in dtypes.weaks for s in src[start:]): return None dt = strong_dtype(least_upper_dtype(select_dtype(u), *(s.dtype for s in src)) if u.op in GroupOp.Binary else unwrap(dtype_from_uop(u.op, src, u.arg))) - return u.replace(dtype=None, src=src[:start]+tuple(s if s.base.is_invalid else s.cast(dt) for s in src[start:])).cast(u.dtype) + return u.replace(dtype=None, src=src[:start]+tuple(s if s.base.is_invalid else commit_weak(s, dt) for s in src[start:])).cast(u.dtype) pm_lower_weak = PatternMatcher([ (UPat(Ops.CONST, dtype=dtypes.weaks, name="u"), lambda u: UOp.const(u.val, select_dtype(u)).cast(u.dtype)), @@ -40,7 +40,7 @@ def lower_weak_srcs(ctx:dict[UOp, UOp]|None, u:UOp) -> UOp|None: return None if ret is u else ret def commit_weak(s:UOp, dt:DType) -> UOp: - # a bare weak CONST commits directly (the value stays mathematical, emission truncates), a weak non-const src takes the demand cast + # a CONST commits directly at dt (the value stays mathematical, emission truncates), a non-const src takes the cast return UOp.const(s.val, dt) if s.op is Ops.CONST else s.cast(dt) def commit_weak_srcs(u:UOp) -> UOp|None: