Skip to content

Flag kernels whose temporaries are sized while they run, instead of refusing the program (#121) - #122

Merged
ThrudPrimrose merged 2 commits into
mainfrom
thesis/runtime-sized-scratch
Nov 27, 2026
Merged

ThrudPrimrose merged 2 commits into
mainfrom
thesis/runtime-sized-scratch

Conversation

@nicolasribeiro2028

@nicolasribeiro2028 nicolasribeiro2028 commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Addresses #121. Draft: the full test suite is still running locally.

Problem

In 5 bench kernels a temporary is sized by a value the kernel reads or computes while it runs (lavamd: count = int(neighbor_counts[l]), then np.empty(1 + count)). widen_scratch cannot size such a buffer for the caller, so default_scopes raised UnsupportedNest and the program stopped at stage 3.

Only Tactics' own per-kernel checks need that size: the NumPy oracle and make_inputs. The built kernel (the CPF unit) allocates its temporaries itself, and its abi_order leaves them out.

Change

  1. Flag. widen_scratch raises RuntimeSizedScratch(UnsupportedNest) naming every such array. prepare catches it and gives the kernel no NumPy oracle (unspelled, the same path as a kernel NumPy cannot spell), with its arguments minus the scratch. KernelScope.runtime_sized lists the arrays.
  2. Run normally. The kernel's check is left to the whole program, as for any kernel without an oracle. default_build gives it its first configuration instead of a sweep: under codegen="sweep" the sweep raised ValueError on any kernel without an oracle.
  3. Check. The whole program is checked against the bench's reference. Placement keeps a flagged kernel on the CPU (a GPU kernel cannot allocate per iteration), and run-on-gpu on one is refused with the reason.

The whole-program check then failed on 2 kernels for an unrelated reason, fixed here too. A scalar parameter that canonicalization turns into a symbol (ls3df_scf's dvol and tol, warpx_esirkepov_deposition's do_ionization) arrives as a 0-d input, not a size, so init raised KeyError. time_program now passes such inputs as symbol values (program_symbols).

Results

Local arm64 Linux container, CPU target, preset S, default stages. Tactics afc49d9, dace-extended 1.0.4, HPCAgent-Bench abef4b636.

Kernel main This branch
lavamd refused at stage 3 7 stages, whole program correct
cp2k_grid_integrate refused at stage 3 7 stages, correct
ls3df_scf refused at stage 3 7 stages, correct
warpx_esirkepov_deposition refused at stage 3 7 stages, correct
qe_cegterg refused at stage 3 7 stages; not checked: no reference in the container (also on main)
  • "Correct" is against the bench's NumPy reference: the container cannot run the numba and C references (SealError: mount('proc', ...)).
  • GPU target, stages 1-4 (no build): on lavamd, cp2k_grid_integrate and qe_cegterg the flagged kernel is placed on the CPU, and forcing it onto the GPU is refused.
  • Tests: tests/test_runtime_sized_scratch.py and tests/test_program_scalar_symbols.py (tagged reproducers). The test files of the touched modules pass (112 tests); mypy and mypy tests are clean; ruff and the header check pass. pyright reports the same 50 errors on these files as on main.

Follow-ups

  • qe_cegterg stopped at stage 5 (KeyError: 'tmp165') on a renaming bug that hits any kernel exporting a symbol. Fixed in f750122, explained in the comment below.
  • Not checked: the full test suite (still running), qe_cegterg's whole-program output (needs Daint), GPU builds, timings.

🤖 Generated with Claude Code

nicolasribeiro2028 and others added 2 commits November 27, 2026 17:52
…efusing the program (#121)

A temporary sized by a value the kernel reads or computes while it runs (lavamd's
neighbor_counts[l]) has no caller that can allocate it, so widen_scratch raised
UnsupportedNest and default_scopes stopped the program at stage 3. Only Tactics' own
per-kernel checks need that size; the built kernel allocates its temporaries itself.

- widen_scratch raises RuntimeSizedScratch, naming every such array; prepare catches it
  and gives the kernel no NumPy oracle (unspelled), with its arguments minus the scratch.
- default_scopes reports the arrays in KernelScope.runtime_sized; the kernel's check is
  left to the whole program, and default_build gives it its first configuration instead
  of a sweep (the sweep raised ValueError on any kernel without an oracle).
- Placement keeps a flagged kernel on the CPU; run-on-gpu on one is refused with why.
- The whole-program run passes a scalar input the program takes as a symbol (ls3df_scf's
  dvol, warpx_esirkepov_deposition's do_ionization), which raised KeyError at init.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ning temporaries

shorten_temporaries renamed every generated program container with replace_dict, the
__sym_out_<s> scalar a loop kernel exports its symbol through among them (qe_cegterg's
__sym_out_nbase_iter became tmp165). replace_dict rewrites the kernel's abi_order (a
string-list property) and, through ExternalCall.rename_symbols, its body, but not its
_out_ connectors, so node_boundary took the output for a symbol and stage 5 raised
KeyError: 'tmp165' in abi_ready_copy. A container bound to an ExternalCall is part of
the kernel's interface and now keeps its name.

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

Copy link
Copy Markdown
Collaborator Author

f750122: the qe_cegterg fix (stage 5 KeyError: 'tmp165')

With the first commit, qe_cegterg got past stage 3 and stopped at stage 5. This commit fixes that. The bug is not specific to qe_cegterg or to #121: it hits any kernel that exports a symbol the program reads after it. It only showed up because this kernel now reaches stage 5.

What broke. qe_cegterg's Davidson loop becomes one loop kernel, extcall_for42. The loop updates nbase_iter and notcnv_iter, which the program reads after it, so DaCe's nest_sdfg_subgraph exports them through two program scalars, __sym_out_nbase_iter and __sym_out_notcnv_iter. The kernel names each one in three places:

Where Name
its output connectors _out___sym_out_nbase_iter (prefixed)
its abi_order __sym_out_nbase_iter (bare)
its body (standalone_sdfg) __sym_out_nbase_iter (bare)

Why. After stage 3, default_scopes → bump() → normalize_labels → shorten_temporaries gives every generated program container a short name: __sym_out_nbase_iter → tmp165, __sym_out_notcnv_iter → tmp166. The rename is a replace_dict on the program, which reaches the kernel node in two ways:

  • replace_properties_dict rewrites every string-list property, and abi_order is one.
  • ExternalCall.rename_symbols treats every abi_order name with no connector of the same name as a symbol, and renames it in the body too. The connectors carry the _in_/_out_ prefix, so a bare name never matches one.

The connectors keep the old name. node_boundary then sorts abi_order against the connectors, finds tmp165 and tmp166 in neither, and takes them for symbols. abi_ready_copy raises KeyError: 'tmp165', because the body has no such symbol.

Fix. temporary_renames leaves out every container an ExternalCall of the program reads or writes (new kernel_bound in tactics/ir/names.py). Those names are the kernel's interface, and shorten_temporaries has no way to rename all three spellings together. shorten_temporaries already renames a nested SDFG's connectors with its containers (rename_connectors), but ExternalCall kernels had no such path. The only visible cost is that a container bound to a kernel keeps a long generated name in the tree, such as __sym_out_nbase_iter.

Test. tests/test_kernel_symbol_outputs.py (tagged reproducer) uses a five-line program whose sequential loop counts into m, which z[0:m] reads after it. On 7038cd3 it fails as the bug does ({'__sym_out_m'} <= {'N', 'm', 'tmp2', 'x', 'y'}); with the fix it passes. mypy, mypy tests, ruff, the header check and tests/test_normalize.py pass.

qe_cegterg now. It runs all 7 stages in the local container (CPU, preset S; Tactics afc49d9 + this PR, dace-extended 1.0.4, HPCAgent-Bench abef4b636). Its whole-program output is not checked there: the container has no reference for it. The numba and C references cannot run (SealError: mount('proc', ...), as for every kernel there), and the bench's NumPy function raises KeyError: 'gamma_only', which is not among the program's parameters. That also happens on unchanged main, so it is not from this PR. The C or numba reference should work on Daint; not run there yet.

🤖 Generated with Claude Code

@ThrudPrimrose
ThrudPrimrose marked this pull request as ready for review November 27, 2026 17:19
@ThrudPrimrose
ThrudPrimrose merged commit a6a6bc5 into main Nov 27, 2026
5 of 7 checks passed
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