Skip to content

[Codegen] inductor_patches, hardware.py folded in, source_rewrite fixes, triton_helpers from torch - #329

Merged
student-Jungmin merged 6 commits into
pytorchsim-triton-backendfrom
inductor-patches
Oct 9, 2026
Merged

student-Jungmin merged 6 commits into
pytorchsim-triton-backendfrom
inductor-patches

Conversation

@student-Jungmin

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

Copy link
Copy Markdown
Collaborator

Renames PyTorchSimCodegen/inductor_templates.py to inductor_patches.py and cleans it up (672 -> 574 lines). The module does more than the mm/conv templates: it makes npu a Triton GPU, pins every kernel's blocks (NPUChoices), sets the reduction policy and skips benchmarking in selection.

  • One convolution wrapper. aten.convolution's lowering was wrapped twice (record groups; transposed -> direct). _wrap_convolution_lowering does both in the same order.
  • Removed TORCHSIM_TRITON_TEMPLATES=0 (sent mm/conv back to extern_kernels, i.e. CPU or a raise) and TORCHSIM_LOG_SPLIT (debug log): undocumented and set nowhere. Also a duplicate launch import and the os import.
  • Comments cut to the three-line docstring rule; what they recorded is in the commit message.
  • References follow: __init__, codecache, test_patch_scope, CLAUDE.md, comments in launch/tests/allowlist.

Verified (cleared cache)

  • conv probe: conv2d, grouped conv2d, conv1d equal CPU (conv1d via the Triton conv template, no extern); a stride-2 ConvTranspose2d generates the same kernel (triton_slhhg6yvd4e) before and after and stops in the compiler's 05-layouts both times
  • test_add, test_matmul, test_gqa, test_conv2d, test_patch_scope, test_eager, test_single_perceptron pass
  • timing: test_add, test_matmul_vector pass with unchanged cycles (100/336/6056/637233/5475, 1559/1570)

Second commit: hardware.py folded in

HardwareInfo.gemm_tile_candidates becomes _gemm_tile_candidates beside _gemm_tiles, its only caller; the four never-passed parameters and the unread vlen are gone, and _num_cores reads config.CONFIG_NUM_CORES. hardware.py is deleted.

Checked: identical candidates and order for all 8788 (M, N, K, extra nodes, bytes) combinations under the tpuv6e and tpuv4 configs; test_matmul, test_gqa, test_addmm_residual, test_patch_scope pass with autotune running; test_matmul_vector timing keeps 1559/1570.


Third commit: source_rewrite.py, three small fixes

A garbled docstring left by the tnpu rename, a redundant second regex match, and mask bookkeeping by set instead of a string search (plus an unused loop variable). Checked: old and new give byte-identical output on 125 real kernel sources, 99 with a % wrap and 38 of those taking the mask path.


Fourth commit: torch's triton_helpers read as source instead of a hand-kept copy

triton_helpers_src.helper_source(used) reads the @triton.jit helpers out of torch._inductor.runtime.triton_helpers (inspect.getsource), follows their calls to each other, renames them _torchsim_* and binds the used names on a triton_helpers module object -- the same shape the hand-kept 18-helper copy (SRC + VENDORED) had, now from the torch that generated the kernel. 32 helpers available instead of 18; unknown names still raise SpecIncomplete.

Checked: AST-identical to the old copy for its 18 helpers (bar private-helper underscores and _log2's alias); all 32 public helpers generate loadable source; test_activation, test_softmax, test_layernorm, test_batchnorm, test_reduce, test_histc, test_topk, test_add pass with max2/maximum/min2/welford/welford_reduce attached from torch.


Fifth commit: triton_helpers_src.py folded into source_rewrite.py

Its only caller is strip_for_compiler, which does the same job. helper_source -> source_rewrite._helper_source, unchanged. Checked: test_activation, test_softmax, test_layernorm, test_reduce, test_add pass with the helpers attached from torch.


Sixth commit: _triton_compat.py folded into inductor_patches

Only the triton_hash_with_backend replacement still does work (the real one raises "0 active drivers"); it joins _claim_triton_present. Dropped: the pre-2.10 triton_key shim, the late sys.path fallback (triton must be on PYTHONPATH before torch imports it; the fallback also read $PREFIX literally from versions.env), and TORCHSIM_COMPILE_TRITON_SRC in Dockerfile.ptb. A missing triton is now an immediate ModuleNotFoundError naming PYTHONPATH. Checked: that error with triton off PYTHONPATH; test_add, test_matmul, test_softmax, test_patch_scope, test_eager pass; test_matmul_vector timing 1559/1570.

The module is more than the mm/conv templates: it makes npu a Triton GPU,
pins every kernel's blocks (NPUChoices), sets the reduction policy and skips
benchmarking in selection. The name now says it patches Inductor.

- aten.convolution's lowering was wrapped twice, once to record the groups
  (_clamp_conv_block_n) and once to lower a transposed conv as a direct one;
  one wrapper (_wrap_convolution_lowering) now does both, in the same order:
  groups set, then the transposed rewrite, then the original lowering.
- TORCHSIM_TRITON_TEMPLATES=0 (sent mm/conv back to extern_kernels, i.e. the
  CPU or a raise) and TORCHSIM_LOG_SPLIT (a debug log) are gone; neither was
  documented or set anywhere.
- launch was imported twice in NPUChoices; os is no longer needed.
- Comments cut to the three-line docstring rule. What they recorded: a block
  the numel divides drops the mask entirely (xmask becomes tl.full), which is
  why blocks are pinned before the kernel is generated; reduction_split_factor
  tests the device argument because _lowering_npu() has no current device yet
  during lowering; a split past the core count is a partial written and read
  for nothing; conv1d_to_conv2d is torch's own, left unregistered, and stops
  RecurrentGemma's Griffin block without it.

Verified on a cleared cache: a conv probe gives conv2d, grouped conv2d and
conv1d equal to CPU; a stride-2 ConvTranspose2d generates the same kernel
(triton_slhhg6yvd4e) before and after and stops in the compiler's 05-layouts
both times. test_add, test_matmul, test_gqa, test_conv2d, test_patch_scope,
test_eager, test_single_perceptron pass; test_add and test_matmul_vector in
timing mode pass with unchanged cycles.
HardwareInfo copied four config values for one method, and its only caller
is inductor_patches._gemm_tiles. gemm_tile_candidates is now the module
function _gemm_tile_candidates beside it, taking only what is passed:
n_prologue_node, n_prologue_extra_read, pad_k and budget_divisor were never
passed and always took their defaults, and vlen was never read.
_num_cores reads config.CONFIG_NUM_CORES directly.

Checked: _gemm_tile_candidates returns the same candidates, in the same
order, as HardwareInfo().gemm_tile_candidates for all 8788 combinations of
M, N, K in {1,7,8,32,100,128,256,300,512,768,1024,2048,4096} and (extra
nodes, bytes) in {(0,4),(0,2),(1,2),(3,1)}, under the tpuv6e 1-core and the
tpuv4 2-core configs. test_matmul, test_gqa, test_addmm_residual and
test_patch_scope pass with autotune running; test_matmul_vector in timing
mode keeps 1559/1570.
@student-Jungmin student-Jungmin changed the title [Codegen] inductor_templates.py -> inductor_patches.py, cleaned up [Codegen] inductor_templates.py -> inductor_patches.py, hardware.py folded in Oct 9, 2026
- strip_for_compiler's docstring read "the torch-free the compiler venv",
  left by the tnpu rename.
- `_HEURISTIC_RE.match(line.strip()) or _HEURISTIC_RE.match(line)`: the
  second match implies the first; one test.
- clamp_instead_of_wrap found which masks it inserted by searching the
  joined inserted lines for the mask's name; it records them in a set as it
  inserts them. The unused loop variable `block` is `_block`.

Checked: old and new strip_for_compiler + clamp_instead_of_wrap give
byte-identical output on 125 kernel sources from four dump paths, 99 of
them holding a `rm % M` / `rn % N` wrap and 38 of those taking the mask path.
@student-Jungmin student-Jungmin changed the title [Codegen] inductor_templates.py -> inductor_patches.py, hardware.py folded in [Codegen] inductor_patches (was inductor_templates), hardware.py folded in, source_rewrite fixes Oct 9, 2026
…copy

The compiler venv has no torch, so a kernel's triton_helpers.X calls were
served by a hand-kept copy of 18 helpers (SRC) and a list of their names
(VENDORED) that had to agree with each other and with torch. The frontend
has torch: helper_source(used) now reads the @triton.jit helpers out of
torch._inductor.runtime.triton_helpers with inspect.getsource, follows each
one's calls to the others, renames them _torchsim_*, and binds the names the
kernel uses on a triton_helpers module object -- the same shape as before,
from the torch that generated the kernel. A name torch does not define as a
jit helper, or a helper needing a torch global the venv cannot supply, is
still a SpecIncomplete.

Checked: for the 18 vendored helpers the generated functions are identical
to the copy by AST, apart from the private helpers' leading underscore and
_log2's alias; every one of torch's 32 public jit helpers generates a source
that loads. On a cleared cache test_activation, test_softmax,
test_layernorm, test_batchnorm, test_reduce, test_histc, test_topk and
test_add pass, their specs carrying max2, maximum, min2, welford and
welford_reduce from torch.
@student-Jungmin student-Jungmin changed the title [Codegen] inductor_patches (was inductor_templates), hardware.py folded in, source_rewrite fixes [Codegen] inductor_patches, hardware.py folded in, source_rewrite fixes, triton_helpers from torch Oct 9, 2026
Without the hand-kept copy, triton_helpers_src is the ~70 lines that read
torch's helpers as source, and its only caller is strip_for_compiler, which
does the same job: making a kernel's source compile in the torch-free venv.
helper_source becomes source_rewrite._helper_source; nothing else changes.

Checked on a cleared cache: test_activation, test_softmax, test_layernorm,
test_reduce and test_add pass, their specs carrying max2, maximum, welford
and welford_reduce from torch.
…ger runs

_triton_compat patched torch so Inductor's Triton codegen runs without a GPU,
the same kind of patch as inductor_patches._claim_triton_present. What still
does work moves there: triton_hash_with_backend becomes the same
triton-version hash, since the real one asks for a GPU driver and raises
"0 active drivers" (checked on torch 2.10).

Dropped:
- the pre-2.10 triton_key shim: torch 2.10, pinned here, reaches triton_key
  through triton_compat itself.
- the sys.path fallback (triton_src_dir, ensure_triton_importable). Triton
  has to be on PYTHONPATH before torch first imports it; adding it later
  surfaced as an AttrsDescriptor NameError further on. triton_src_dir also
  read versions.env's `TRITON_ROOT=$PREFIX/triton-src` literally. A missing
  triton is now a ModuleNotFoundError naming PYTHONPATH.
- TORCHSIM_COMPILE_TRITON_SRC in Dockerfile.ptb, read only by that fallback;
  the image already puts triton on PYTHONPATH.

Checked: with triton off PYTHONPATH, `import torch` stops at once with the
new message; on a cleared cache test_add, test_matmul, test_softmax,
test_patch_scope, test_eager pass and test_matmul_vector in timing mode keeps
1559/1570.
student-Jungmin added a commit that referenced this pull request Oct 9, 2026
…ackend

docker-image.yml triggered on pull requests into master and develop only,
so no PR into the branch PyTorchSim-Triton-Backend pins has ever been
gated: #329, #330 and #331 show no checks. The branch everyone shares
joins the list.
@student-Jungmin
student-Jungmin merged commit 6019420 into pytorchsim-triton-backend Oct 9, 2026
@student-Jungmin
student-Jungmin deleted the inductor-patches branch October 9, 2026 15:22
student-Jungmin added a commit that referenced this pull request Oct 9, 2026
…ackend

docker-image.yml triggered on pull requests into master and develop only,
so no PR into the branch PyTorchSim-Triton-Backend pins has ever been
gated: #329, #330 and #331 show no checks. The branch everyone shares
joins the list.
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