Skip to content

The compiler is given the machine the TOGSim YAML describes, whatever the environment says - #335

Merged
student-Jungmin merged 6 commits into
pytorchsim-triton-backendfrom
yaml-target
Oct 11, 2026
Merged

student-Jungmin merged 6 commits into
pytorchsim-triton-backendfrom
yaml-target

Conversation

@student-Jungmin

@student-Jungmin student-Jungmin commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Why

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.

What

  • The target is set unconditionally (PyTorchSimCodegen/compiler_bridge.py).
  • The compiler pin moves to cc80a49: PyTorchSim-Triton-Backend FIXME dram address calculation #44 replaced its one hard-coded 256 (the lane bits of a wrapped linear layout), without which gemm and bmm died in transform on a 32- or 8-lane target.

What this does to the gate

The 32x32 and 8x8 configs now compile for 32 and 8 lanes for the first time. Locally (compiler at cc80a49): test_add, test_batchnorm, test_gqa, test_matmul (11 checks; the mm tiles retry down to the 32 KB-a-lane budget) and test_mlp pass on both, test_gpt2 passes on 8x8, and test_gpt2 fails on 32x32 at 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 compiler's own suite at those widths: 507/539 at 32 lanes and 445/539 at 8 lanes, the rest scratchpad refusals of 512 KB-a-lane tiles, a handful of unsupported forms, and three 8-lane numerical mismatches (mixed_gemm_fp16, ops_bf16_matmul, channel_shuffle) now on record in #44.

So this PR's gate reports the compiler's real coverage of the small machines; some 32x32 / 8x8 jobs that were green only because they ran tpuv6e code will be red. Which ones is the question the run answers; what to do about them (compiler work, or a per-config allowlist) is a separate decision.

Two more commits, from the first gate runs on this PR

  • [Codegen] A bmm template retries the smaller mapped tiles like an mm template -- Mixtral's fused bmm on 8x8 overflowed the 16 KB-a-lane budget and had no retry (_mm_template declined anything named bmm). bmm has the same M/N/K and BLOCK constexprs; _retile now keeps the grid's batch axes.
  • [Codegen] A retried mm tile keeps BLOCK_K dividing K when the body was rendered without K masks -- the gate's 32x32 config showed BERT, Llama, Llama3 and Gemma with wrong numbers (max abs diff 1.4 to 8.3). Inductor's mm template masks the K loads only when rendered with EVEN_K False; a retry from BLOCK_K=64 (EVEN_K) to BLOCK_K=1024 over K=768 read 256 columns of garbage. _template_tiles now offers such a body only tiles whose BLOCK_K divides K. Latent on every config; the 256-lane ones had not retried across that boundary. Verified with the per-kernel CPU verify on BERT 32x32 (max 1.7e-6), Llama 32x32 (7.7e-7, was 6.2), matmul 32x32 11/11, gpt2 and mistral 8x8, BERT 256x256.

Left for the compiler at 32 lanes so far: Llama4 (a window copy no XLU op carries).

…, 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.
…t 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.
…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.
…s 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.
…ine 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.
…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.
@student-Jungmin
student-Jungmin merged commit 3984351 into pytorchsim-triton-backend Oct 11, 2026
4 checks passed
@student-Jungmin
student-Jungmin deleted the yaml-target branch October 12, 2026 02:06
student-Jungmin added a commit that referenced this pull request Oct 24, 2026
…onic network per row through tl.gather

#447's sort (aten.sort.stable and aten.sort, torchsim::sort) in the dispatcher's one form. A program
sorts one row of the last axis as an [L, D] tile, padded to a power of two n, on L = min(lanes, n)
lanes; the key is one int64 (a float's bits made ordered, NaN last, -0 as +0, flipped when
descending) carried as two int32 words with the int32 index, so every sort is stable. Keys int32,
int64 and float32. plan declines another dtype, another axis, an empty or 0-d input and a row past
the scratchpad budget; a gradient declines as every CustomFusedKernel's does; the fallback runs the
same network in aten ops.

It waits on PSAL-POSTECH/PyTorchSim-Triton-Backend#335: with the compiler this branch pins (PTB
70221f4), the kernel stops at transform with tt.gather left unlowered, so test_sort fails here. The
fallback matches torch.sort(stable=True) exactly on 34 cases (float32 with NaN, -0, +-inf; int32;
int64; every dim, both orders); test_topk, test_add, test_eager and test_patch_scope pass. #447's
measurements (sim/v6e 1.13-1.15 at T 128-512 on PTB #335) are in that PR.
student-Jungmin added a commit that referenced this pull request Oct 24, 2026
…onic network per row through tl.gather

#447's sort (aten.sort.stable and aten.sort, torchsim::sort) in the dispatcher's one form. A program
sorts one row of the last axis as an [L, D] tile, padded to a power of two n, on L = min(lanes, n)
lanes; the key is one int64 (a float's bits made ordered, NaN last, -0 as +0, flipped when
descending) carried as two int32 words with the int32 index, so every sort is stable. Keys int32,
int64 and float32. plan declines another dtype, another axis, an empty or 0-d input and a row past
the scratchpad budget; a gradient declines as every CustomFusedKernel's does; the fallback runs the
same network in aten ops.

It waits on PSAL-POSTECH/PyTorchSim-Triton-Backend#335: with the compiler this branch pins (PTB
70221f4), the kernel stops at transform with tt.gather left unlowered, so test_sort fails here. The
fallback matches torch.sort(stable=True) exactly on 34 cases (float32 with NaN, -0, +-inf; int32;
int64; every dim, both orders); test_topk, test_add, test_eager and test_patch_scope pass. #447's
measurements (sim/v6e 1.13-1.15 at T 128-512 on PTB #335) are in that PR.
student-Jungmin added a commit that referenced this pull request Oct 24, 2026
…onic network per row through tl.gather

#447's sort (aten.sort.stable and aten.sort, torchsim::sort) in the dispatcher's one form. A program
sorts one row of the last axis as an [L, D] tile, padded to a power of two n, on L = min(lanes, n)
lanes; the key is one int64 (a float's bits made ordered, NaN last, -0 as +0, flipped when
descending) carried as two int32 words with the int32 index, so every sort is stable. Keys int32,
int64 and float32. plan declines another dtype, another axis, an empty or 0-d input and a row past
the scratchpad budget; a gradient declines as every CustomFusedKernel's does; the fallback runs the
same network in aten ops.

The compiler pin moves 70221f4 -> 2abb4a3, PSAL-POSTECH/PyTorchSim-Triton-Backend main with #335
(tt.gather lowered), which this kernel needs; TOGSim stays 5791272. On that pin test_sort passes
24/24 in functional and in timing mode, and test_topk, test_add, test_matmul, test_grouped_mm,
test_custom_fused_kernel, test_eager, test_patch_scope, test_togsim_result and test_cache_identity
pass. test_conv_decomposition's depthwise_grad still fails at lower ("index tile ... no one entry
per position of its gather") until PSAL-POSTECH/PyTorchSim-Triton-Backend#351. #447's
measurements (sim/v6e 1.13-1.15 at T 128-512 on PTB #335) are in that PR.
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