From 46b0766d3a20d24b0a7596046a42581b97fe0d60 Mon Sep 17 00:00:00 2001 From: Rishav Sanjay <83537945+rishavsanjay@users.noreply.github.com> Date: Sun, 13 Sep 2026 02:01:39 +0530 Subject: [PATCH 1/5] ci: apply sliced assign audit fixes --- .github/workflows/_tmp_sliced_assign_fix.yml | 159 +++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 .github/workflows/_tmp_sliced_assign_fix.yml diff --git a/.github/workflows/_tmp_sliced_assign_fix.yml b/.github/workflows/_tmp_sliced_assign_fix.yml new file mode 100644 index 0000000000000..a8acc8bd568a6 --- /dev/null +++ b/.github/workflows/_tmp_sliced_assign_fix.yml @@ -0,0 +1,159 @@ +name: temporary sliced assign fix +on: + push: + branches: [codex/sliced-assign-final] +permissions: + contents: write +jobs: + patch-test-push: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: codex/sliced-assign-final + fetch-depth: 2 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install test tools + run: python -m pip install -q numpy pytest ruff + - name: Apply audited fixes and regressions + shell: bash + run: | + python - <<'PY' + from pathlib import Path + + def replace_once(path, old, new): + p = Path(path) + text = p.read_text() + count = text.count(old) + assert count == 1, f"{path}: expected one match, got {count}" + p.write_text(text.replace(old, new)) + + replace_once("tinygrad/schedule/prepare.py", '''def supports_sliced_copy_destination(x:UOp) -> bool: + return (isinstance(x.device, str) and ":" not in x.device and + not x.device.startswith(("PYTHON", "NPY", "DISK", "CL", "WEBGPU", "NULL"))) + + def store_cross_device_copy(ctx:dict[UOp, int], dst:UOp, x:UOp, cpy:UOp): + if cpy.is_self_copy or dst.device != cpy.device or not supports_sliced_copy_destination(dst): return None + if ctx.get(cpy) != 1 or contiguous_buffer_view(dst) is None or contiguous_buffer_view(x) is None: return None + return dst.store(x) + ''', '''def supports_sliced_copy_destination(x:UOp) -> bool: + return (isinstance(x.device, str) and + not x.device.startswith(("PYTHON", "NPY", "DISK", "CL", "WEBGPU", "NULL"))) + + def store_cross_device_copy(ctx:dict[UOp, int], dst:UOp, x:UOp, cpy:UOp): + if cpy.is_self_copy or dst.device != cpy.device or not supports_sliced_copy_destination(dst): return None + # COPY lowering encodes a concrete transfer length. Keep symbolic-sized stores on the ordinary COPY+STORE path. + if not all_int(dst.shape+x.shape): return None + if ctx.get(cpy) != 1 or contiguous_buffer_view(dst) is None or contiguous_buffer_view(x) is None: return None + return dst.store(x) + ''') + + replace_once("tinygrad/schedule/__init__.py", '''def copy_kernel_to_copy_uop(call:UOp, dst:UOp, src:UOp, r:UOp|None=None, offset:UOp|None=None): + if dst.device == src.device and not (isinstance(dst.device, str) and dst.device.startswith("DISK")): return None + copy = UOp(Ops.COPY, src=(src,), arg=dst.device) + ''', '''def copy_kernel_to_copy_uop(call:UOp, dst:UOp, src:UOp, r:UOp|None=None, offset:UOp|None=None): + if dst.device == src.device and not (isinstance(dst.device, str) and dst.device.startswith("DISK")): return None + # Runtime COPYs have a fixed byte count. Never lower a symbolic RANGE using the PARAM's max-sized storage. + if r is not None and (len(r.src) != 1 or r.src[0].op is not Ops.CONST or r.src[0].val != src.arg.size): return None + copy = UOp(Ops.COPY, src=(src,), arg=dst.device) + ''') + + replace_once("tinygrad/engine/jit.py", '''def graph_class(dev): return dev.graph.func if isinstance(dev.graph, functools.partial) else dev.graph + + class GraphRunner: + ''', '''def graph_class(dev): return dev.graph.func if isinstance(dev.graph, functools.partial) else dev.graph + + def _rebindable_buffer_expr(u:UOp) -> bool: + # Keep this grammar in lock-step with engine.realize._resolve. + if u.op is Ops.PARAM: return True + if u.op in (Ops.MSELECT, Ops.SHRINK, Ops.BITCAST): return _rebindable_buffer_expr(u.src[0]) + if u.op is Ops.MSTACK: return all(_rebindable_buffer_expr(x) for x in u.src) + return False + + class GraphRunner: + ''') + replace_once("tinygrad/engine/jit.py", ''' buffer_replace = [(p, b) for p, b in enumerate(get_call_arg_uops(call)) if any(u.op is Ops.PARAM for u in b.toposort())] + ''', ''' buffer_replace = [(p, b) for p, b in enumerate(get_call_arg_uops(call)) if _rebindable_buffer_expr(b)] + ''') + + replace_once("tinygrad/tensor.py", '''def creation_copy_is_realized(u:UOp): + # all copies from disk/numpy are realized into a real buffer + if is_creation_device(u.src[0]): return tag_uop(u) + ''', '''def creation_copy_is_realized(u:UOp): + # Creation-device copies and explicitly held cross-device snapshots need independent storage. + # Ephemeral copies may still be sunk directly into a destination slice by the scheduler. + if is_creation_device(u.src[0]) or (not u.is_self_copy and _tensor_holds(u)): return tag_uop(u) + ''') + + replace_once("test/unit/test_setitem_schedule.py", '''from tinygrad import Tensor, Device, TinyJit, dtypes, GlobalCounters + ''', '''from tinygrad import Tensor, Device, TinyJit, Variable, dtypes, GlobalCounters + ''') + replace_once("test/unit/test_setitem_schedule.py", '''if __name__ == '__main__': + unittest.main() + ''', ''' def test_cross_device_indexed_destination_uses_single_copy(self): + dst = Tensor(list(range(8)), dtype=dtypes.int32, device="CPU:1").realize() + src = Tensor(list(range(10, 18)), dtype=dtypes.int32, device="CPU").realize() + out = dst[2:4].assign(src[5:7].to(dst.device)) + linear = out.schedule_linear() + self.assertEqual([call.src[0].op for call in linear.src], [Ops.COPY]) + run_linear(linear) + self.assertListEqual(dst.tolist(), [0, 1, 15, 16, 4, 5, 6, 7]) + + # The optimization must be symmetric with respect to logical device indices. + dst2 = Tensor(list(range(8)), dtype=dtypes.int32, device="CPU").realize() + src2 = Tensor(list(range(20, 28)), dtype=dtypes.int32, device="CPU:1").realize() + out2 = dst2[3:5].assign(src2[1:3].to(dst2.device)) + linear2 = out2.schedule_linear() + self.assertEqual([call.src[0].op for call in linear2.src], [Ops.COPY]) + run_linear(linear2) + self.assertListEqual(dst2.tolist(), [0, 1, 2, 21, 22, 5, 6, 7]) + + + def test_symbolic_length_cross_device_slice_does_not_overcopy(self): + @TinyJit + def write(dst:Tensor, src:Tensor, v): + dst.shrink(((7, 7+v),)).assign(src.shrink(((9, 9+v),)).to(dst.device)).realize() + for i, value in enumerate((1, 4, 2, 3, 1)): + before = [-100-i]*20 + values = list(range(100*i, 100*i+24)) + dst = Tensor(before, dtype=dtypes.int32, device="CPU").realize() + src = Tensor(values, dtype=dtypes.int32, device="CPU:1").realize() + write(dst, src, Variable("slice_len", 1, 4).bind(value)) + expected = before[:] + expected[7:7+value] = values[9:9+value] + self.assertListEqual(dst.tolist(), expected) + + def test_optimized_copy_keeps_held_snapshot(self): + src = Tensor([10, 11, 12, 13], dtype=dtypes.int32, device="CPU:1").realize() + dst = Tensor.zeros(6, dtype=dtypes.int32, device="CPU").realize() + snapshot = src[1:3].to(dst.device) + dst[2:4].assign(snapshot).realize() + src[1:3].assign(Tensor([90, 91], dtype=dtypes.int32, device=src.device)).realize() + self.assertListEqual(snapshot.tolist(), [11, 12]) + self.assertListEqual(dst.tolist(), [0, 0, 11, 12, 0, 0]) + + if __name__ == '__main__': + unittest.main() + ''') + PY + - name: Show diff + run: git diff --check && git diff + - name: Ruff changed files + run: ruff check tinygrad/schedule/prepare.py tinygrad/schedule/__init__.py tinygrad/engine/jit.py tinygrad/tensor.py test/unit/test_setitem_schedule.py test/unit/test_jit.py + - name: Targeted tests + env: + DEV: CPU + run: | + python -m pytest -q test/unit/test_setitem_schedule.py -x + python -m pytest -q test/unit/test_jit.py::TestJit::test_graph_rebinds_copy_views + - name: Commit tested patch and remove temporary workflow + shell: bash + run: | + git rm .github/workflows/_tmp_sliced_assign_fix.yml + git add tinygrad/schedule/prepare.py tinygrad/schedule/__init__.py tinygrad/engine/jit.py tinygrad/tensor.py test/unit/test_setitem_schedule.py test/unit/test_jit.py + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git commit --amend -m "scheduler: harden sliced assign optimization" + git push --force-with-lease origin HEAD:codex/sliced-assign-final From d195b703697709327f57acf170f3e9f0dc4004b2 Mon Sep 17 00:00:00 2001 From: Rishav Sanjay <83537945+rishavsanjay@users.noreply.github.com> Date: Sun, 13 Sep 2026 02:07:19 +0530 Subject: [PATCH 2/5] ci: prepare temporary sliced assign validation --- .github/workflows/_tmp_sliced_assign_fix.yml | 72 +++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/.github/workflows/_tmp_sliced_assign_fix.yml b/.github/workflows/_tmp_sliced_assign_fix.yml index a8acc8bd568a6..ea73832de99ab 100644 --- a/.github/workflows/_tmp_sliced_assign_fix.yml +++ b/.github/workflows/_tmp_sliced_assign_fix.yml @@ -1,17 +1,17 @@ -name: temporary sliced assign fix +name: temporary sliced assign validation on: - push: + pull_request: branches: [codex/sliced-assign-final] + types: [opened, synchronize, reopened] permissions: - contents: write + contents: read jobs: - patch-test-push: + patch-test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: ref: codex/sliced-assign-final - fetch-depth: 2 - uses: actions/setup-python@v5 with: python-version: '3.12' @@ -55,7 +55,7 @@ jobs: copy = UOp(Ops.COPY, src=(src,), arg=dst.device) ''', '''def copy_kernel_to_copy_uop(call:UOp, dst:UOp, src:UOp, r:UOp|None=None, offset:UOp|None=None): if dst.device == src.device and not (isinstance(dst.device, str) and dst.device.startswith("DISK")): return None - # Runtime COPYs have a fixed byte count. Never lower a symbolic RANGE using the PARAM's max-sized storage. + # A runtime COPY has a fixed byte count. The RANGE must therefore cover the exact concrete source PARAM. if r is not None and (len(r.src) != 1 or r.src[0].op is not Ops.CONST or r.src[0].val != src.arg.size): return None copy = UOp(Ops.COPY, src=(src,), arg=dst.device) ''') @@ -66,10 +66,10 @@ jobs: ''', '''def graph_class(dev): return dev.graph.func if isinstance(dev.graph, functools.partial) else dev.graph def _rebindable_buffer_expr(u:UOp) -> bool: - # Keep this grammar in lock-step with engine.realize._resolve. + # This mirrors the PARAM-bearing forms engine.realize._resolve can rewrite. if u.op is Ops.PARAM: return True if u.op in (Ops.MSELECT, Ops.SHRINK, Ops.BITCAST): return _rebindable_buffer_expr(u.src[0]) - if u.op is Ops.MSTACK: return all(_rebindable_buffer_expr(x) for x in u.src) + if u.op is Ops.MSTACK: return any(_rebindable_buffer_expr(x) for x in u.src) return False class GraphRunner: @@ -90,8 +90,7 @@ jobs: replace_once("test/unit/test_setitem_schedule.py", '''from tinygrad import Tensor, Device, TinyJit, dtypes, GlobalCounters ''', '''from tinygrad import Tensor, Device, TinyJit, Variable, dtypes, GlobalCounters ''') - replace_once("test/unit/test_setitem_schedule.py", '''if __name__ == '__main__': - unittest.main() + replace_once("test/unit/test_setitem_schedule.py", ''' def test_cross_device_slice_copy_jit_rebinds_destination_view(self): ''', ''' def test_cross_device_indexed_destination_uses_single_copy(self): dst = Tensor(list(range(8)), dtype=dtypes.int32, device="CPU:1").realize() src = Tensor(list(range(10, 18)), dtype=dtypes.int32, device="CPU").realize() @@ -101,16 +100,6 @@ jobs: run_linear(linear) self.assertListEqual(dst.tolist(), [0, 1, 15, 16, 4, 5, 6, 7]) - # The optimization must be symmetric with respect to logical device indices. - dst2 = Tensor(list(range(8)), dtype=dtypes.int32, device="CPU").realize() - src2 = Tensor(list(range(20, 28)), dtype=dtypes.int32, device="CPU:1").realize() - out2 = dst2[3:5].assign(src2[1:3].to(dst2.device)) - linear2 = out2.schedule_linear() - self.assertEqual([call.src[0].op for call in linear2.src], [Ops.COPY]) - run_linear(linear2) - self.assertListEqual(dst2.tolist(), [0, 1, 2, 21, 22, 5, 6, 7]) - - def test_symbolic_length_cross_device_slice_does_not_overcopy(self): @TinyJit def write(dst:Tensor, src:Tensor, v): @@ -134,12 +123,30 @@ jobs: self.assertListEqual(snapshot.tolist(), [11, 12]) self.assertListEqual(dst.tolist(), [0, 0, 11, 12, 0, 0]) - if __name__ == '__main__': - unittest.main() + def test_cross_device_empty_and_buffer_end_slices(self): + dst = Tensor(list(range(8)), dtype=dtypes.int32, device="CPU").realize() + src = Tensor(list(range(10, 18)), dtype=dtypes.int32, device="CPU:1").realize() + dst[4:4].assign(src[2:2].to(dst.device)).realize() + self.assertListEqual(dst.tolist(), list(range(8))) + dst[6:8].assign(src[6:8].to(dst.device)).realize() + self.assertListEqual(dst.tolist(), [0, 1, 2, 3, 4, 5, 16, 17]) + + def test_cross_device_slice_copy_jit_rebinds_destination_view(self): + ''') + + replace_once("test/unit/test_jit.py", ''' def test_graph_rebinds_copy_views(self): + from tinygrad.engine.jit import GraphRunner + ''', ''' def test_graph_rebinds_copy_views(self): + from tinygrad.engine.jit import GraphRunner, _rebindable_buffer_expr + ''') + replace_once("test/unit/test_jit.py", ''' dst = UOp.param(0, dtypes.int32, 6, "CPU") + ''', ''' self.assertTrue(_rebindable_buffer_expr(UOp.param(0, dtypes.int32, 6, "CPU").shrink(((1, 3),)))) + self.assertFalse(_rebindable_buffer_expr(UOp.param(0, dtypes.int32, 6, "CPU").reshape((2, 3)))) + dst = UOp.param(0, dtypes.int32, 6, "CPU") ''') PY - - name: Show diff - run: git diff --check && git diff + - name: Validate diff + run: git diff --check - name: Ruff changed files run: ruff check tinygrad/schedule/prepare.py tinygrad/schedule/__init__.py tinygrad/engine/jit.py tinygrad/tensor.py test/unit/test_setitem_schedule.py test/unit/test_jit.py - name: Targeted tests @@ -148,12 +155,11 @@ jobs: run: | python -m pytest -q test/unit/test_setitem_schedule.py -x python -m pytest -q test/unit/test_jit.py::TestJit::test_graph_rebinds_copy_views - - name: Commit tested patch and remove temporary workflow - shell: bash - run: | - git rm .github/workflows/_tmp_sliced_assign_fix.yml - git add tinygrad/schedule/prepare.py tinygrad/schedule/__init__.py tinygrad/engine/jit.py tinygrad/tensor.py test/unit/test_setitem_schedule.py test/unit/test_jit.py - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git commit --amend -m "scheduler: harden sliced assign optimization" - git push --force-with-lease origin HEAD:codex/sliced-assign-final + - name: Save tested patch + if: always() + run: git diff > /tmp/sliced-assign.patch + - uses: actions/upload-artifact@v4 + if: always() + with: + name: sliced-assign-tested-patch + path: /tmp/sliced-assign.patch From 7dc5ed4ddcc3cf4123aa39d8776c1d9338e512f5 Mon Sep 17 00:00:00 2001 From: Rishav Sanjay <83537945+rishavsanjay@users.noreply.github.com> Date: Sun, 13 Sep 2026 02:07:38 +0530 Subject: [PATCH 3/5] ci: trigger sliced assign validation --- .ci-trigger-sliced-assign | 1 + 1 file changed, 1 insertion(+) create mode 100644 .ci-trigger-sliced-assign diff --git a/.ci-trigger-sliced-assign b/.ci-trigger-sliced-assign new file mode 100644 index 0000000000000..89519c7ae66fa --- /dev/null +++ b/.ci-trigger-sliced-assign @@ -0,0 +1 @@ +temporary trigger From f77ccf272d90a242658416753e4e3fa4319a3647 Mon Sep 17 00:00:00 2001 From: Rishav Sanjay <83537945+rishavsanjay@users.noreply.github.com> Date: Sun, 13 Sep 2026 02:12:33 +0530 Subject: [PATCH 4/5] ci: retrigger sliced assign validation --- .ci-trigger-sliced-assign | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci-trigger-sliced-assign b/.ci-trigger-sliced-assign index 89519c7ae66fa..6901f375c5b51 100644 --- a/.ci-trigger-sliced-assign +++ b/.ci-trigger-sliced-assign @@ -1 +1 @@ -temporary trigger +temporary trigger 2 From bb7817b36269a826e3bfc3d1c67bdb744d12d79d Mon Sep 17 00:00:00 2001 From: Rishav Sanjay <83537945+rishavsanjay@users.noreply.github.com> Date: Sun, 13 Sep 2026 02:15:17 +0530 Subject: [PATCH 5/5] ci: run corrected sliced assign validation --- .ci-trigger-sliced-assign | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci-trigger-sliced-assign b/.ci-trigger-sliced-assign index 6901f375c5b51..5ffd0cf11fe98 100644 --- a/.ci-trigger-sliced-assign +++ b/.ci-trigger-sliced-assign @@ -1 +1 @@ -temporary trigger 2 +temporary trigger 3