diff --git a/.ci-trigger-sliced-assign b/.ci-trigger-sliced-assign new file mode 100644 index 0000000000000..5ffd0cf11fe98 --- /dev/null +++ b/.ci-trigger-sliced-assign @@ -0,0 +1 @@ +temporary trigger 3 diff --git a/.github/workflows/_tmp_sliced_assign_fix.yml b/.github/workflows/_tmp_sliced_assign_fix.yml new file mode 100644 index 0000000000000..ea73832de99ab --- /dev/null +++ b/.github/workflows/_tmp_sliced_assign_fix.yml @@ -0,0 +1,165 @@ +name: temporary sliced assign validation +on: + pull_request: + branches: [codex/sliced-assign-final] + types: [opened, synchronize, reopened] +permissions: + contents: read +jobs: + patch-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: codex/sliced-assign-final + - 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 + # 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) + ''') + + 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: + # 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 any(_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", ''' 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() + 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]) + + 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]) + + 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: 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 + 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: 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