Skip to content

Bring micro2026's log, autotune/env, megablox and paged-KV work onto pytorchsim-triton-backend - #526

Merged
student-Jungmin merged 5 commits into
pytorchsim-triton-backendfrom
pr/pts-0-prereq
Nov 10, 2026
Merged

student-Jungmin merged 5 commits into
pytorchsim-triton-backendfrom
pr/pts-0-prereq

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Summary

Brings the non-tutorial work that landed on micro2026 onto pytorchsim-triton-backend; the DRAM-trace PRs stacked after this one build on it.

  • Each log line prints once, and a rerun says its gem5 tile cycles are reused.
  • Inductor's autotune tables print only in debug mode; the model tests find the repository through TORCHSIM_DIR (not PYTORCHSIM_ROOT_PATH).
  • grouped_mm can run as megablox's structure (torch_xla's Pallas gmm on v6e, tiled as torch_xla 2.9 picks the tiles).
  • The decode's KV cache can be laid out and read as ragged paged attention (tokenwise pages, one program a sequence).
  • Both are options named for what they choose: TORCHSIM_GROUPED_MM_STRUCTURE (pergroup | megablox) and TORCHSIM_KVCACHE_LAYOUT (headwise | paged); the defaults are unchanged.

The autotune/env commit conflicted with this branch's newer model tests; the resolution keeps this branch's tests and applies only the rename and the .claude/CLAUDE.md table edits (same 15 files, +85/-20, as on micro2026).

Verification

On this branch: test_autotune_tables, test_grouped_mm (its megablox case included), test_sdpa, test_pointwise, test_fused_sdpa (30/30, paged case included) pass.

… cycles are reused

setup_logger gave every module logger its own handler but left it propagating, so a logger whose
parent also has one (timing.togsim, timing.gem5 under timing) printed each line twice. It stops
propagating once it has its own handler.

timing.run measures the tiles on gem5 only when the workdir has no cycle table; a rerun of the same
kernel reused the table silently, so the [Gem5] line was missing. It now names the table it reuses.
…ames cleaned up

Inductor's autotune tables (the choices displayed and the choice stats) printed on every template
selection; they are now shown only with TORCHSIM_DEBUG_MODE, and Inductor's own variables for them,
TORCHINDUCTOR_AUTOTUNE_NUM_CHOICES_DISPLAYED and TORCHINDUCTOR_MAX_AUTOTUNE_REPORT_CHOICES_STATS,
which torch 2.14 reads at import, still win. tests/system/test_autotune_tables.py pins both.

An audit of the variables the tree reads: the model tests found the repository root through
PYTORCHSIM_ROOT_PATH, a second name for TORCHSIM_DIR, and now use TORCHSIM_DIR; test_sdpa and
test_pointwise set TORCHINDUCTOR_CACHE, which torch does not read (FxGraphCache.clear() beside it does
the clearing), and no longer do. The CLAUDE.md table gives TORCHSIM_DUMP_PATH's real default
($TORCHSIM_DIR/outputs) and lists the variables it lacked: the two mapping settings, the per-kernel
check's tolerances and trace, TORCHSIM_TRITON_REPLAY.
…ATION_XLA_STRUCTURE=megablox

To show by measurement that the MoE gap to v6e is XLA's structure and not the simulator: with the flag, the
dispatcher installs a grouped matmul shaped as megablox's (the Pallas kernel torch_xla's gmm runs, seen in the
10-30 HLO as tpu_custom_call with group_ids / m_tile_ids s32[tiles_m + G - 1]): a program per (row tile, group)
visit in make_group_metadata's order, each reading its group's weight again over K and computing the whole row
tile, only the group's rows stored.

It tiles as torch_xla 2.9 picks the tiles when gmm is called with no tiling (both Mixtral's registered experts path
and Llama 4's stock one): custom_kernel._get_tiling_size_for_gmm_kernel, TM from twice a group's mean rows rounded
to 128 within 512, TK and TN from K and N within 2048 -- what the 10-30 HLO shows (Llama 4 TM 128 at S128..S512,
Mixtral 512 at S1024). Triton blocks are powers of two: TK and TN take the largest one within torch_xla's (Llama 4's
TK 1280 -> 1024, Qwen3-30B's TN 1536 -> 1024; a K loop's step does not set the time, measured on grouped_mm), TM is
exact or the mode declines, as the visits are counted in it.

The default path is untouched: install() picks one class or the other, and grouped_mm.py has no branch.
s32[V] is the table's length, an upper bound: the visits are the (tile, group) pairs the routing makes, between
G and tiles_m + G - 1 (10 of 11 for the Mixtral S1024-like routing the case pins).

Pinned by test_grouped_mm's xla_structure_megablox, run in a process with the flag: the visit table, the tilings
and the reference's values (err 0.00239 on uneven groups with empty ones).
…ind TORCHSIM_VALIDATION_KVCACHE_LAYOUT=paged

The TPU decode row runs torch_xla's ragged_paged_attention over kv_pages [pages, 128 tokens, K and V of every
head, D]: tokenwise, one work item a sequence walking its KV in steps, every head in it. Ours reads [B, H, S, D]
headwise, a program a KV head. With TORCHSIM_VALIDATION_KVCACHE_LAYOUT=paged (default headwise, unchanged):

- the harness (one_layer._reads_given) hands each layer its filled K and V as views of one tokenwise page tensor
  [B, S, 2H, D], each head's K then its V, pages in order as the TPU row's page_indices are; neither side writes
  the step's token into the cache, as before;
- dispatcher.install registers PagedSDPA in place of FusedSDPA: its decode is one program a sequence, each KV
  head's query heads in turn over its keys, K and V read in place by their (batch, head, token) strides through
  the same _attend; prefill and the plan are FusedSDPA's. It takes the padding masks the plan carries (KEYS, DW,
  VW) as _decode_kernel does: forwarded to _attend with QROWS 0 and no mask, the rope half of q loaded under
  DA + rb < DW where DW is set, the output stored under rv < VW where VW is.

Pinned by test_fused_sdpa's paged_kvcache, in a process with the flag: one _paged_decode_kernel launch and no
copy over the page views (head stride 2D, token stride 2HD), and the reference's values (err 2.6e-3).
…at they choose, not validation flags

TORCHSIM_VALIDATION_KVCACHE_LAYOUT becomes TORCHSIM_KVCACHE_LAYOUT (headwise | paged): the decode's KV cache layout
and the kernel that reads it. TORCHSIM_VALIDATION_XLA_STRUCTURE becomes TORCHSIM_GROUPED_MM_STRUCTURE (pergroup |
megablox): how grouped_mm runs a MoE's experts, megablox's module grouped_mm_megablox (the op
torchsim::grouped_mm_megablox) and test_grouped_mm's case megablox. headwise and pergroup stay the defaults and
unchanged (qwen3_30b@decode1024 189,484 cycles, per kernel as grid-1031); any other value is refused at import.

Measured with paged (decode, TPU 10-30 ratio headwise -> paged): qwen3_30b S1024 0.93 -> 0.93, S4096 0.90 -> 0.91,
llama3_8b S1024 1.00 -> 1.00, S4096 0.99 -> 1.01; the attention kernel alone -10%, +7%, -7%, +50% (one program a
sequence walks the KV heads in turn, so a long S overlaps less than a program a head).
@student-Jungmin
student-Jungmin merged commit 44a9c92 into pytorchsim-triton-backend Nov 10, 2026
8 of 10 checks passed
@YWHyuk
YWHyuk deleted the pr/pts-0-prereq branch November 19, 2026 05:29
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