Repository navigation
[Codegen] inductor_patches, hardware.py folded in, source_rewrite fixes, triton_helpers from torch - #329
Merged
Conversation
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.
- 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.
…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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Renames
PyTorchSimCodegen/inductor_templates.pytoinductor_patches.pyand 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.aten.convolution's lowering was wrapped twice (record groups; transposed -> direct)._wrap_convolution_loweringdoes both in the same order.TORCHSIM_TRITON_TEMPLATES=0(sent mm/conv back toextern_kernels, i.e. CPU or a raise) andTORCHSIM_LOG_SPLIT(debug log): undocumented and set nowhere. Also a duplicatelaunchimport and theosimport.__init__,codecache,test_patch_scope, CLAUDE.md, comments in launch/tests/allowlist.Verified (cleared cache)
triton_slhhg6yvd4e) before and after and stops in the compiler's05-layoutsboth timesSecond commit:
hardware.pyfolded inHardwareInfo.gemm_tile_candidatesbecomes_gemm_tile_candidatesbeside_gemm_tiles, its only caller; the four never-passed parameters and the unreadvlenare gone, and_num_coresreadsconfig.CONFIG_NUM_CORES.hardware.pyis 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 fixesA 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_helpersread as source instead of a hand-kept copytriton_helpers_src.helper_source(used)reads the@triton.jithelpers out oftorch._inductor.runtime.triton_helpers(inspect.getsource), follows their calls to each other, renames them_torchsim_*and binds the used names on atriton_helpersmodule 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 raiseSpecIncomplete.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.pyfolded intosource_rewrite.pyIts 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.pyfolded intoinductor_patchesOnly the
triton_hash_with_backendreplacement still does work (the real one raises "0 active drivers"); it joins_claim_triton_present. Dropped: the pre-2.10triton_keyshim, the latesys.pathfallback (triton must be on PYTHONPATH before torch imports it; the fallback also read$PREFIXliterally from versions.env), andTORCHSIM_COMPILE_TRITON_SRCin 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.