Skip to content

[Driver] The cache identity is the compiler's own toolchain and TOGSim API; a hit keeps its autotuned tile - #423

Merged
YWHyuk merged 2 commits into
pytorchsim-triton-backendfrom
driver/cache-identity
Oct 23, 2026
Merged

YWHyuk merged 2 commits into
pytorchsim-triton-backendfrom
driver/cache-identity

Conversation

@YWHyuk

@YWHyuk YWHyuk commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Closes #422.

Why

The kernel cache had two holes, both silent:

  • Toolchain rebuilds: a rebuilt toolchain at the same compiler commit was served from the cache. _tool_paths imported pytorchsim_triton_compiler.config.CHECKS, which the compiler has never had, so identity()["tools"] was always None.
  • TOG header: the TOG's ABI digest read $TOGSIM_DIR/include/togsim_runtime.h as PyTorchSim resolves it. The compiler compiles a TOG against togsim.hpp in the TOGSim it resolves, and versions it with toolchain.togsim_api_version().
  • Autotuned tile: a cache hit dropped the autotuned tile. meta.json was written after the autotune retries and never read, so a hit built its launcher from the caller's untuned fixed_config against the tuned ELF. contract.grid_of then derived the wrong grid.

What changed

commit before after
de24504 tools is always None (with a warning); tog_abi = sha of PyTorchSim's togsim_runtime.h One probe in the compiler's interpreter returns toolchain.TOOLS, togsim_api_version() and TOGSIM_INCLUDE. The identity stats those tools. tog_abi = {api, headers}: the compiler's stated API version plus a digest of every header in its TOGSim include dir. A probe that cannot answer raises with the compiler's message. config.togsim_include_dir (no other reader) is gone.
64a147e A hit keeps the caller's untuned fixed_config timing.load_tuned_blocks takes fixed_config from meta.json before the launcher is built. select._time_tile compiles every candidate afresh and never retries, so it needs no change.

The identity's shape changes, so every cached kernel rebuilds once.

Tests

test parent (8de3ba3) this branch
test_cache_identity::test_tools_are_the_compilers FAIL: tools is None PASS
test_cache_identity::test_tog_abi_is_the_compilers_togsim FAIL: a bare digest b6eb37a7e3659bb7 PASS
test_cache_identity::test_a_compiler_that_cannot_answer_stops_the_run FAIL: identity answered with /bin/false as the compiler PASS
test_tuned_blocks_on_hit FAIL: launcher keeps {'XBLOCK': 1024} PASS (256, grid 16)

Verification

  • Container: topk-ptb:pts-base-92028c8, an x86 PTS base on PTB 92028c8. The published 1b35878 images are arm64-only. The compiler is three commits behind the pin. toolchain.TOOLS, togsim_api_version() and TOGSIM_INCLUDE are the same at both.
  • Existing tests on this branch:
    • test_patch_scope, test_select, tests/ops/elementwise/test_add.py, and test_add with TORCHSIM_TIMING_MODE=1 (TOGSim simulated, 1,974 cycles) pass.
    • test_compiler_failure fails the same way on the parent and here: tile_spad_over_budget.py compiled. Its fixture no longer overflows, which predates this PR.
  • No real autotune retry: I found no test kernel that goes through an autotune retry. softmax and layernorm do not, so the hit path is pinned by the unit test only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UnJWSTNtd6944XAzy4bhk5

…he compiler itself resolves

_tool_paths imported pytorchsim_triton_compiler.config.CHECKS, which the compiler has never had, so
identity()["tools"] was always None: a rebuilt llc, opt, mlir-translate or riscv gcc at the same
compiler commit never missed the cache, and the only trace was a warning. _tog_abi digested
$TOGSIM_DIR/include/togsim_runtime.h as PyTorchSim resolves it, while the compiler compiles a TOG
against togsim.hpp in the TOGSim it resolves and versions it with toolchain.togsim_api_version().

One probe in the compiler's own interpreter now returns toolchain.TOOLS, togsim_api_version() and
TOGSIM_INCLUDE. The identity stats those tools and keys the TOG on the stated API version plus a
digest of every header in that include directory; config.togsim_include_dir, whose only reader this
was, is gone. A probe that cannot answer raises with what the compiler said, instead of leaving the
cache guarded by the commit alone. The identity's shape changes, so cached kernels rebuild once.

Pin: tests/system/test_cache_identity.py. On the parent: tools is None; tog_abi is a bare digest
(b6eb37a7e3659bb7), not the compiler's API version; a compiler interpreter of /bin/false gives an
identity anyway. All three pass here (#422).
torchsim_compile writes meta.json after the autotune retries shrink a kernel's fixed_config, but
nothing read it back: a later hit built its launcher from the caller's meta, whose fixed_config is
the untuned one, while kernel.py and the ELF in the workdir are the tuned ones. contract.grid_of then
derives the grid from the wrong block, so the launch runs the wrong number of programs.

On a hit, timing.load_tuned_blocks takes fixed_config from meta.json before the launcher is built.
select._time_tile compiles every candidate afresh and never retries, so its meta.json is the meta it
uses; it needs no change.

Pin: tests/system/test_tuned_blocks_on_hit.py, a hit on a workdir whose meta.json holds XBLOCK 256
for a caller holding 1024. On the parent the launcher keeps {'XBLOCK': 1024}; here it gets 256 and
grid_of gives 16 programs (#422).
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.

1 participant