From 5b649616ff80211d53802ffdb920788a1e0574fa Mon Sep 17 00:00:00 2001 From: qazal <77887910+Qazalin@users.noreply.github.com> Date: Thu, 2 Oct 2025 03:39:43 +0300 Subject: [PATCH] rangeify: detect and assert cycles (#12405) * rangeify: assert cycles * rng=2 * any --- .github/workflows/test.yml | 2 -- tinygrad/schedule/rangeify.py | 7 +++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 98446abb3c..067246897f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -532,10 +532,8 @@ jobs: llvm: "true" - name: Test CPU=1 RANGEIFY=1 # TODO: add more passing tests here - # rangeify diamond cycle gives the wrong answer run: | CPU=1 CPU_LLVM=0 RANGEIFY=1 python3 -m pytest -n auto --durations 20 \ - -k "not test_assign_diamond_cycle" \ test/test_tiny.py test/test_rangeify.py test/test_ops.py test/test_symbolic_ops.py test/test_symbolic_jit.py test/test_tensor_variable.py \ test/test_outerworld_range.py test/test_randomness.py test/test_nn.py test/test_arange.py test/test_tensor.py test/test_optim.py \ test/test_setitem.py test/test_assign.py test/test_multitensor.py diff --git a/tinygrad/schedule/rangeify.py b/tinygrad/schedule/rangeify.py index 649a67a64b..e9dfd47323 100644 --- a/tinygrad/schedule/rangeify.py +++ b/tinygrad/schedule/rangeify.py @@ -603,7 +603,14 @@ def renumber_range(ctx:LocalAddBufferContext, r:UOp): ctx.range += 1 return ret +def find_bufs(x:UOp): + idxs = [s for s in x.toposort(gate=lambda x: x.op is not Ops.ASSIGN) if s.op is Ops.INDEX] + read_from: dict[UOp, Ops] = {} + if any((buf:=idx.as_buf()).op is Ops.BUFFER and read_from.setdefault(buf, op:=idx.src[0].op) is not op for idx in idxs): + raise RuntimeError(f"cycle detected while indexing {buf}") + to_define_global = PatternMatcher([ + (UPat(Ops.STORE, name="x"), find_bufs), (UPat(Ops.BUFFER, name="buf"), debuf), (UPat(Ops.BIND, name="b"), unbind_kernel), (UPat((Ops.ASSIGN, Ops.MSTACK, Ops.MSELECT), name="assign"), handle_assign),