Skip to content

Backend: follow-up copies for kernel outputs that cannot share its layout - #44

Open
AdvaitTahilyani wants to merge 12 commits into
scale/26-solver-layout-tiesfrom
scale/27-kernel-layout-followups
Open

AdvaitTahilyani wants to merge 12 commits into
scale/26-solver-layout-tiesfrom
scale/27-kernel-layout-followups

Conversation

@AdvaitTahilyani

Copy link
Copy Markdown

Correctness no longer depends on the solver unifying layouts.

resolve_kernel_layouts runs after every layout-setting pass. For any kernel whose kept result or set_region target can't share the kernel's layout, it rewrites the kernel:

  • The kernel writes a temporary in its own layout.
  • A follow-up node copies the temporary into the real array.
  • Whole-result copies run before set_region follow-ups.
  • Mixed-shape results are left as they are.

No original, port or op test triggers a follow-up, apart from the new test. Before this fix, a matmul input fused with a shifted set_region gave wrong results and then hung.

Adds test_kernel_layouts (47 checks) and solver unit tests. Validated with CPU regress at +p1, +p2 and +p4, and on 1 GPU and 4 GPUs.

🤖 Generated with Claude Code

AdvaitTahilyani and others added 4 commits November 5, 2026 07:21
Correctness no longer depends on the solver's ties. After all metadata is
final (solver, live arrays, scan/sort pins), resolve_kernel_layouts checks
every fused kernel against the layout the executor writes in. A kept
result R pinned elsewhere is written as X in the kernel's layout and
redistributed by a follow-up node R = X (CONVERT to its own dtype); if a
kernel's set_regions disagree with anything, each T[r] = v becomes X = v
and a follow-up T[r] = X. Follow-ups run between the kernel and its
children, so the kernel's reads still see values from before its writes;
X retires after its follow-up. Kept results of different shapes abort.

Unit tests: cross-kernel ties, COPY roots, a fixed target with a matmul
operand, two fixed targets, whole + shifted set_regions, a renamed result
read in its kernel, mixed shapes, 2-D and 3-D.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 9a55dcee7c32dd3f241dbca98a7782103fffcb6d)
1-D, 2-D and 3-D past the CPU and GPU minimum tiles: an earlier array at
a view offset written from a matmul operand (1-D), two earlier arrays at
different offsets written by one kernel, whole + shifted set_regions in
one kernel, results of different shapes, {a,b} kept by one kernel and a
written by the next, and copy() of a shifted result. Before the follow-up
nodes the matmul operand case was wrong and the test then hung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2a8c976f1526e6e796a5798693b1ee4579e503b9)
…on mixed shapes

resolve_kernel_layouts queued the set_region follow-ups T[r] = X before the
whole-result copies R = X', so a kept result that is also a set_region
target in its kernel (r = a+b; r[...] = c, with r renamed) ended with its
value from before the set_region. The copies now run first: a kernel writes
R before any set_region into it, and the copies only touch their own R.

A kept result of another shape than the kernel's anchor is left as it is
instead of aborting: R = X cannot reshape, and the frontend only fuses ops
of one shape (check_fusible), so the executor writes every output over one
iteration space anyway.

Unit tests: two renamed results that are also set_region targets plus a
fixed target (copies, then set_regions in kernel order), size-1 results
(tiles are positive after the solver, no follow-ups), mixed shapes left
alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit a2377d690397ab5b418d15c2cb63bf2c6fed4561)
q (a matmul operand, offset 0) and r (tied to an earlier array at offset 5)
are kept by one kernel that also writes r[...] = c and u[...] = q + 1, so r
is renamed and redistributed; r must end up as c. 1-D and 2-D; fails on
9a55dce.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit bbe8c04e41e6d7efc9c0f06c632b868335108289)

@adityapb adityapb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve with nits, once #43's creation-grid fix is in. The layouts are consistent in that case, so resolve_kernel_layouts can't see that hang.

This is a static review, plus the solver fuzzer: 30k random DAGs gave 0 inconsistent kernels after resolve. Line numbers are at this PR's head.

These hold up:

  • the pass runs after every layout-setting pass;
  • the copies run before the set_region writes;
  • readers come after the follow-up chain;
  • temporaries retire after their follow-up;
  • message counts come from metadata that is identical on every PE.
  1. [minor] Mixed-shape kept results are now silently left in the wrong layout (decomposition_solver.hpp:2532).
    • Commit 4ccc979 replaced an error with a skip.
    • The safety argument is the frontend's check_fusible. But examples/ops/test_kernel_layouts.py:104 never actually fuses mismatched shapes, and test_kernel_layout_mixed_shapes locks in the silent skip.
    • Please restore a CkAbort.
  2. Nit: resolve runs after the PE-local live_array_meta merge (dag_group_receive.cpp:514 → :542). The rewrite and the synth_name/synth_node_id counters therefore have to be identical on every PE. A debug assert would catch a divergence before it turns into a hang.
  3. Nit: metadata for the synthetic temporaries is never removed from array_meta, so it grows during long iterative runs.

…e across PEs

- A fused kernel that keeps results of different shapes is an error again
  (detail::kept_shapes_differ, reported by resolve_kernel_layouts,
  CkAbort in receive_dag_now) instead of leaving them in the wrong layout.
  The unit test checks the detection and the reported error.
- layout_hash hashes the solved DAG (roots and their layouts) and the
  synthetic name/node counters. Each PE sends it to the next PE, which
  aborts if it differs from its own, so a PE-local divergence fails
  loudly instead of hanging.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author
  1. [minor] Mixed-shape kept results left in the wrong layout: fixed in dc12d4c. detail::kept_shapes_differ detects them, resolve_kernel_layouts reports an error, and receive_dag_now calls CkAbort again. test_kernel_layout_mixed_shapes now checks the detection and the reported error rather than aborting.
  2. Nit, counters must match on every PE: fixed in dc12d4c with a cheap check that is always on. layout_hash hashes the solved DAG's roots, their layouts, and the synth_name/synth_node_id counters. Each PE sends its hash to the next PE (one message per PE per DAG), and the receiver aborts if the hashes differ.
  3. Nit, synthetic metadata grows: not changed. Erasing a temporary's metadata per PE when its follow-up runs is unsafe, because a lagging chare on that PE can still look it up in an older epoch. Follow-up: epoch-based GC that drops the synthetic entries once every local chare has passed the epoch.

#43's creation-grid fix (d31a115) is on #43 only. This head still needs it merged up.

Validated: unit tests (standalone, CT_MIN_TILE=64); nc build of this head; CPU regress at +p1 and +p2: orig 15/15, ports 25/25, ops 37/37 at each PE count (with the hash check active at +p2).

AdvaitTahilyani added a commit that referenced this pull request Nov 10, 2026
The solver keeps a zero-offset (matmul-pinned) kept result of a shifted
set_region kernel off its grid; the assert now sits after same_shape. The
executor writes such a result on the kernel's tiles, so it now aborts with a
clear message instead of producing wrong values or a hang until #43/#44's
follow-up copies land. TILE/DIAG receivers check that every piece comes from
one source chare of the array_meta layout, and the sender-side offset check
is always on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AdvaitTahilyani and others added 2 commits November 10, 2026 16:19
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author

GPU validation of this head (7898a88, which includes #38's JIT fix) on A100s: +p1 on 1 GPU and +p4 on 4 GPUs (one GPU per process) both pass, with originals 15/15, ports 25/25 and ops 38/38.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants