From 7c7e3768d534b741b24f59dfaf914adc958d19c0 Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sun, 11 Oct 2026 10:45:21 +0900 Subject: [PATCH 1/6] [Codegen] The compiler is given the machine the TOGSim YAML describes, whatever the environment says compiler_env() wrote the target for the running TOGSim YAML (target_path) but handed it to the compiler with setdefault, so an inherited TORCHSIM_COMPILE_TARGET won. The compiler's own env.sh exports one -- tests/targets/tpuv6e.json -- and so did Dockerfile.base, so every kernel this route compiled, on the development machine and in all three CI configs, was compiled for 256 lanes and a 512 KB scratchpad: the 8x8 and 32x32 configs pinned their blocks from their YAML and TOGSim timed their YAML, while the ELF, its DMA descriptors and its spad layout were tpuv6e's. The docstring said the YAML is the machine; the code did not. The target is now set unconditionally. On the 32x32 and 8x8 configs the compiler then really targets 32 and 8 lanes (with PyTorchSim-Triton-Backend's fix for its one hard-coded 256, which the pin bump brings): test_add, test_batchnorm, test_gqa, test_matmul (11 checks, tiles retried down to the 32 KB-a-lane budget) and test_mlp pass on both, test_gpt2 passes on 8x8, and test_gpt2 on 32x32 reaches a form the compiler has no lowering for at that width ("a window copy no XLU op carries"), which the 256-lane compile had hidden. The gate on those two configs now reports the compiler's real coverage of small machines. --- PyTorchSimCodegen/compiler_bridge.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/PyTorchSimCodegen/compiler_bridge.py b/PyTorchSimCodegen/compiler_bridge.py index 67607e33f..e0fa37bdb 100644 --- a/PyTorchSimCodegen/compiler_bridge.py +++ b/PyTorchSimCodegen/compiler_bridge.py @@ -92,17 +92,13 @@ def target_path(): def compiler_env(): - """The environment for a compiler subprocess: this machine, no PYTHONPATH, and - no device backend autoload. - - TORCHSIM_COMPILE_TARGET NAMES THE WHOLE MACHINE, and used to name three of its seven - fields while the compiler supplied the rest from a shipped default. One - description, written from the YAML this process is running. - """ + """The environment for a compiler subprocess: this machine, no PYTHONPATH, and no device + backend autoload. TORCHSIM_COMPILE_TARGET is the target written from the TOGSim YAML this + process runs, whatever the toolchain's environment set it to: the YAML is the machine.""" env = dict(os.environ) env.pop("PYTHONPATH", None) env["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0" - env.setdefault("TORCHSIM_COMPILE_TARGET", target_path()) + env["TORCHSIM_COMPILE_TARGET"] = target_path() return env From c176808f116f43a6e0e3358158e1eb6f8b1f53c9 Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sun, 11 Oct 2026 11:02:19 +0900 Subject: [PATCH 2/6] [Deps] The compiler pin moves to cc80a49: a linear layout's lane count is the target's PyTorchSim-Triton-Backend #44 replaces its one hard-coded 256 (the lane bits of a wrapped linear layout) with the target's lane count, which the previous commit's honest TORCHSIM_COMPILE_TARGET needs: with it the compiler lowers gemm and bmm for 32-lane and 8-lane machines instead of dying in transform. --- thirdparty/pytorchsim-triton-backend.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/pytorchsim-triton-backend.json b/thirdparty/pytorchsim-triton-backend.json index f8ad8c50a..18d57b359 100644 --- a/thirdparty/pytorchsim-triton-backend.json +++ b/thirdparty/pytorchsim-triton-backend.json @@ -2,6 +2,6 @@ "description": "Pin for the compiler the codegen route lowers through: PyTorchSim-Triton-Backend, with its LLVM, Triton and the vcix-accelerator environment (RISC-V toolchain, pk, Spike, gem5) and the accelerator model. CI builds its base image FROM ghcr.io/psal-postech/pytorchsim-triton-backend:, the image that repository publishes for every commit on its main and develop, so ref must be such a commit. scripts/build_from_source.sh checks the same ref out for a local toolchain. The repository is private; its package grants this repository read access.", "pytorchsim_triton_backend": { "repository": "PSAL-POSTECH/PyTorchSim-Triton-Backend", - "ref": "17ec55a85955ba330c07189171b3bb2e01783f6f" + "ref": "cc80a49e3b5c5389c7e95a4d79525c5866bf4868" } } From 99b9a068a7da5896f00ae7ce77769805b1ba2541 Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sun, 11 Oct 2026 11:33:36 +0900 Subject: [PATCH 3/6] [Codegen] A bmm template retries the smaller mapped tiles like an mm template _mm_template declined any kernel named bmm, so a bmm template whose tile overflowed the scratchpad had no retry: _smaller_tiles fell through to the fixed_config halvings, which a template has none of, and the compile failed with "no smaller tile is left to try". On the 256-lane machine the bmm tiles fit; on the 8x8 config, now that kernels are compiled for the machine the YAML describes, Mixtral's fused bmm (21504 bytes a lane over a budget of 16384) stopped the gate. A bmm template has the same M, N, K constants and BLOCK_M/N/K constexprs as an mm template; only its grid carries the batch in its second and third axes. _retile now recomputes the first axis and keeps the rest, and bmm takes the same mapped-tile sequence (and, in timing mode, the same autotune). Verified on the 8x8 config with the compiler at cc80a49: test_mistral passes after two retries, test_gpt2 after eight, test_bmm 7 of 7 with none; on 256x256 test_bmm 7 of 7. --- PyTorchSimCodegen/codecache.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/PyTorchSimCodegen/codecache.py b/PyTorchSimCodegen/codecache.py index 73e3b2b1f..f87a8dbf9 100644 --- a/PyTorchSimCodegen/codecache.py +++ b/PyTorchSimCodegen/codecache.py @@ -131,9 +131,9 @@ def _product(tile, keep): def _mm_template(src_code, meta): - """(M, N, K, input bytes, output bytes) of an mm template kernel -- one program per output tile, its - blocks stated as constexprs -- else None.""" - if (meta.get("template_grid") is None or "bmm" in meta.get("kernel_name", "") + """(M, N, K, input bytes, output bytes) of an mm or bmm template kernel -- one program per output + tile (and batch), its blocks stated as constexprs -- else None.""" + if (meta.get("template_grid") is None or not all(r.search(src_code) for r in _TILE_RE.values())): return None dims = [_DIM_RE[k].search(src_code) for k in "MNK"] @@ -145,13 +145,14 @@ def _mm_template(src_code, meta): def _retile(src_code, meta, mnk, tile): - """The same mm kernel at block `tile` (M, N, K): its constexprs and its grid.""" + """The same mm or bmm kernel at block `tile` (M, N, K): its constexprs and its grid, the batch + axes of the grid kept.""" (m, n, k), (bm, bn, bk) = mnk, tile vals = {"BLOCK_M": bm, "BLOCK_N": bn, "BLOCK_K": bk, "EVEN_K": k % bk == 0} for key, v in vals.items(): src_code = _TILE_RE[key].sub(lambda mt, v=v: f"{mt.group(1)}{v}", src_code) meta = copy.deepcopy(meta) - meta["template_grid"] = [-(-m // bm) * -(-n // bn), 1, 1] + meta["template_grid"] = [-(-m // bm) * -(-n // bn)] + list(meta["template_grid"][1:]) return src_code, meta From 72aed1df294952c5e51f34ada270640b8da940a1 Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sun, 11 Oct 2026 12:01:48 +0900 Subject: [PATCH 4/6] [Codegen] A retried mm tile keeps BLOCK_K dividing K when the body was rendered without K masks Inductor's mm and bmm templates mask the K loads only when rendered with EVEN_K False ({% if not EVEN_K %} in kernel/templates/triton_mm.py.jinja); with EVEN_K True the body loads every column of the K block. _retile rewrote the BLOCK_K and EVEN_K constexprs of such a body but could not add the masks it never had, so a retry from a tile that divided K to one that did not read past K. BERT on the 32x32 config, the first config whose kernels were really compiled for 32 lanes: the addmm 32x768x768 was rendered at BLOCK_K=64 (EVEN_K), overflowed the 16 KB-a-lane budget and was retried at BLOCK_K=1024, which reads 256 columns of garbage; the per-kernel verify found 24531 of 24576 elements wrong (max 1.23). Llama on 32x32 the same way (max 6.2). The tile table offers the power of two above K as a BLOCK_K, so any machine where a retry crosses that boundary was exposed; the 256-lane configs had not been because their first tile fit. _template_tiles now reads the EVEN_K the source was rendered with and, when it is True, keeps only the tiles whose BLOCK_K divides K. A body rendered with masks takes any tile as before. Verified on the 32x32 config with the compiler at cc80a49: test_bert passes with the per-kernel verify on (max 1.7e-6), test_llama passes after four retries (max 7.7e-7, was 6.2), test_matmul 11 of 11; on 8x8 test_gpt2 and test_mistral pass. --- PyTorchSimCodegen/codecache.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/PyTorchSimCodegen/codecache.py b/PyTorchSimCodegen/codecache.py index f87a8dbf9..d3fe2c30b 100644 --- a/PyTorchSimCodegen/codecache.py +++ b/PyTorchSimCodegen/codecache.py @@ -183,14 +183,17 @@ def _time_tile(src_code, meta, kernel_name, workdir, timeout): def _template_tiles(src_code, meta): - """Every mapped tile of an mm template kernel, best first, as (M, N, K) blocks; [] for - any other kernel.""" + """Every mapped tile of an mm template kernel this source can take, best first, as (M, N, K) + blocks; [] for any other kernel. A body rendered with EVEN_K loads K unmasked, so it takes only + a BLOCK_K that divides K.""" shape = _mm_template(src_code, meta) if shape is None: return [] from .inductor_patches import _gemm_tiles m, n, k, size, out_size = shape - return [(c.block_m, c.block_n, c.block_k) for c in _gemm_tiles(m, n, k, size, out_size)] + even = _TILE_RE["EVEN_K"].search(src_code).group(2) == "True" + return [(c.block_m, c.block_n, c.block_k) for c in _gemm_tiles(m, n, k, size, out_size) + if not even or k % c.block_k == 0] def _smaller_tiles(src_code, meta): From 2b6862955b114d680e383760559a88d99869db5d Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sun, 11 Oct 2026 12:57:11 +0900 Subject: [PATCH 5/6] [Build] Pin the compiler at 65fb058: a lane axis longer than the machine folds onto its lanes PyTorchSim-Triton-Backend #45. A scan as long as the lanes shifts through a tile one and a half lane-fulls long, which the compiler refused on any target ("a window copy no XLU op carries"): GPT-2's and Llama-4's position cumsum on the 32x32 config in the gate, and a 256-wide scan on the 256-lane target. The fold onto the lanes now takes any length and the bank reserves a whole row per lane-full. --- thirdparty/pytorchsim-triton-backend.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/pytorchsim-triton-backend.json b/thirdparty/pytorchsim-triton-backend.json index 18d57b359..974d96d15 100644 --- a/thirdparty/pytorchsim-triton-backend.json +++ b/thirdparty/pytorchsim-triton-backend.json @@ -2,6 +2,6 @@ "description": "Pin for the compiler the codegen route lowers through: PyTorchSim-Triton-Backend, with its LLVM, Triton and the vcix-accelerator environment (RISC-V toolchain, pk, Spike, gem5) and the accelerator model. CI builds its base image FROM ghcr.io/psal-postech/pytorchsim-triton-backend:, the image that repository publishes for every commit on its main and develop, so ref must be such a commit. scripts/build_from_source.sh checks the same ref out for a local toolchain. The repository is private; its package grants this repository read access.", "pytorchsim_triton_backend": { "repository": "PSAL-POSTECH/PyTorchSim-Triton-Backend", - "ref": "cc80a49e3b5c5389c7e95a4d79525c5866bf4868" + "ref": "65fb0585aec61464571146e58790555a0a49056c" } } From c24d0e46f0b287e2aa2beea0ab40e92f331c0686 Mon Sep 17 00:00:00 2001 From: student-Jungmin Date: Sun, 11 Oct 2026 14:13:28 +0900 Subject: [PATCH 6/6] [Codegen] The persistence decision reads the target's scratchpad, as the block pin does reduction_block_for pins a reduction's R0_BLOCK to half a lane's scratchpad shared by the live tiles, and its docstring asks two callers to agree: the block pinned at codegen time and the question whether the reduction may be persistent. They did not. fixed_config_for passed the machine's spad_size from the TOGSim YAML; should_use_persistent_reduction passed nothing, and the default was an environment variable nothing sets, 64 KB. On the 32x32 config (32 KB per lane) a 512-wide layer_norm of mllama was therefore rendered persistent, eight live [XBLOCK, 512] tiles at 16400 bytes per lane against a budget of 16384, and the retry loop shrank XBLOCK four times without moving the usage, since XBLOCK rides the lanes and the depth is the reduction. The default is now the machine's spad_size, so both callers see the target by construction. On the 32x32 config the 512 reduction is looped at R0_BLOCK 256 and test_mllama passes. On the 256x256 config (512 KB per lane) reductions of 768 and 1024 that the 64 KB default had made looped are persistent, within Inductor's own threshold; test_layernorm, test_bert and test_gpt2 pass there. --- PyTorchSimCodegen/launch.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/PyTorchSimCodegen/launch.py b/PyTorchSimCodegen/launch.py index 56916c154..be9b328b7 100644 --- a/PyTorchSimCodegen/launch.py +++ b/PyTorchSimCodegen/launch.py @@ -189,11 +189,16 @@ def triton_max_numel(): def reduction_block_for(extent, elem_bytes=4, lane_bytes=None): """The R0_BLOCK this backend pins for a reduction of `extent`. - Cover the extent and no more, then shrink to the budget. Two callers must + Cover the extent and no more, then shrink to the budget: half of one lane's + scratchpad, shared by the tiles a reduction keeps live. Two callers must agree: this pins the block, inductor_patches asks if persistence matches. + Both see the machine's scratchpad when `lane_bytes` is left to this + function; a persistent reduction rendered for a bigger one than the target + has is a kernel the compiler refuses as over budget, whatever XBLOCK shrinks to. """ if lane_bytes is None: - lane_bytes = int(os.environ.get("TORCHSIM_COMPILE_SPAD_SIZE", str(64 * 1024)), 0) + from . import compiler_bridge + lane_bytes = compiler_bridge.machine()["spad_size"] budget = lane_bytes // 2 // _REDUCTION_LIVE_TILES block = 1 << (int(extent) - 1).bit_length() while block * elem_bytes > budget and block > 1: