Skip to content

[Frontend] has_triton, the conv heuristic and the SDPA flags are upstream's (PTB rule 16) - #435

Merged
YWHyuk merged 3 commits into
pytorchsim-triton-backendfrom
item11/public-extension-points
Oct 23, 2026
Merged

YWHyuk merged 3 commits into
pytorchsim-triton-backendfrom
item11/public-extension-points

Conversation

@YWHyuk

@YWHyuk YWHyuk commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #427

Scope trimmed (2026-10-23). The post-grad pass hookup (was b0cb85d) and the npu-only histc/polar decompositions (was 4c3009d) are dropped from this PR: #433 rewrites the same code as post_grad_pass registered through device_custom_pass. A probe on #433 (cpu-in/cpu-out graph using npu inside: topk(a.to(npu)*2, 4)) shows aten.topk does not reach lowering and the values match, with a cpu-only control where it does — so the coverage concern that made this PR keep the list form does not apply to #433. This PR keeps the three changes #433 does not make.

Why

PTB rule 16: the backend attaches to PyTorch/Inductor only through public extension points. Three places in PTS replaced or wrapped upstream behaviour globally (for every device), or duplicated what upstream already does once npu's device interface is registered. Each one is removed or moved onto an npu-scoped extension point.

What changed (before -> after)

commit before after
456b5a6 _claim_triton_present (frontend/inductor_patches.py) forced has_triton() / triton_hash_with_backend() Deleted. Upstream has_triton() (torch/utils/_triton.py:208-239) already answers True via npu's registered ExtensionDeviceInterface; the hash goes through the active torchsim CPUDriver.
c03d207 Global patch of InductorChoices.get_config_heuristics (all devices) NPUChoices.get_config_heuristics returns the lane-wide conv table for "npu"; Inductor reaches it only via V.choices (kernel/conv.py:655,728,1087). Shim imports retargeted to torch._inductor.heuristics.template.*.
c79957a device/torch_openreg/openreg/__init__.py:88-90 called three torch._C._set_sdp_use_* on import Deleted; the process keeps upstream's SDPA flags (Extra.cpp:15-48 reads only userEnabledOverrideableSDP).

Tests (each fails on its parent for the stated reason)

test parent this PR
tests/system/test_patch_scope.py::test_triton_presence_is_upstream FAIL [True,False,False] PASS [True,True,True]
...::test_conv_heuristics_are_upstream_outside_npu_choices FAIL [False,True] PASS
...::test_sdpa_flags_are_upstream (vs. subprocess with TORCH_DEVICE_BACKEND_AUTOLOAD=0) FAIL [0,1,1,1,0] vs [1,1,1,1,1] PASS (equal)

tests/ops/misc/test_complex_to_real.py now traces with npu inputs (the graph the pass actually runs on); with CPU inputs it only saw polar decomposed because of the leak 4c3009d closes. It passes on the parent too.

Verification

Container on image topk-ptb:pts-base-92028c8; each pin checked on its parent by overlaying the parent's files. Independent review re-ran every pin and the smoke in a separate container.

Smoke on HEAD 4c3009d, all rc=0: test_patch_scope 10/10, test_topk 5/5, test_grouped_mm 1/1, test_row_matmul 4/4, test_complex_to_real 2/2, test_histc 3/3, test_polar 2/2, test_group_conv 1/1; on earlier commits of this branch also test_sdpa 16/16 (route lines identical to base), test_fused_sdpa 11/11, test_conv2d 14/14, test_eager_to_compile_recursion, test_reduction_tiling.

Post-grad position: a probe recording each GraphLowering's call_function targets and externs for npu topk, row mm, row addmm and RoPE gives identical output on base and on this branch; the five kernels' 07-regularized.mlir are byte-identical.

Stacking / dependencies

Not stacked. Based on pytorchsim-triton-backend @ 069878a; #421 and #423 are already merged there and touch none of these files. git merge-tree against the current base (5fb15ee) is clean. Open #430 edits NPUChoices.uuid in inductor_patches.py in a different hunk. No PTB change; PTB origin/main references none of the removed symbols.

Open issues

  • test_fused_sdpa requires_grad grad error moved 0.000e+00 -> 5.535e-07 (CPU reference backward now uses upstream flash SDPA instead of math); well under the 1e-4 tolerance.
  • _claim_triton_present used to raise a clear error when triton was not importable; now a bad PYTHONPATH makes has_triton() return False (cached).
  • _npu_config_heuristic caches a class in a module global; it could be a module-level class.
  • rewrite_grouped_mm (target transformers.grouped_mm_fallback) has no test; test_grouped_mm covers aten._grouped_mm.
  • NpuPostGradPasses.uuid() is a fixed string, so editing a pass body does not invalidate the FX cache (same as the old _Chain).
  • Pins use private torch probes (torch._C._get_overrideable_sdp_enabled, torch._fused_sdp_choice, mock.patch.object(GraphLowering, "run")), in tests only.
  • Verification ran on the PTB 92028c8 build in the image, not the current 1b35878 pin.
  • Out of scope, untouched: AlgorithmSelectorCache patches, convolution and degenerate-gemm lowering wraps, conv grid functions, the GPU_TYPES append.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UnJWSTNtd6944XAzy4bhk5

@YWHyuk YWHyuk self-assigned this Oct 23, 2026
…face and the torchsim driver answer them

_claim_triton_present replaced torch.utils._triton.has_triton, scheduler.has_triton and
triton_hash_with_backend with constants. On torch 2.14 with the PTB torchsim driver none of
that is needed: has_triton() walks the registered device interfaces, and npu's
ExtensionDeviceInterface says it is available and Triton capable; triton_hash_with_backend()
hashes the active driver's backend, which is torchsim's CPUDriver.

Pin: tests/system/test_patch_scope.py test_triton_presence_is_upstream
  parent: [True, False, False]  (has_triton a lambda, hash = our constant)
  this:   [True, True, True]

Refs #427
…n every choices class

_size_conv_blocks_from_the_machine replaced InductorChoices.get_config_heuristics for
every subclass and every caller. Inductor reaches it only through V.choices
(kernel/conv.py, choices.py get_conv_configs/get_depthwise_conv_configs/flex), and
V.choices is NPUChoices, so the override now lives there and upstream's class is left
alone. The deprecated torch._inductor.template_heuristics shim imports
(inductor_patches.py, codegen/tiling.py) now name torch._inductor.heuristics.template.

Pin: tests/system/test_patch_scope.py test_conv_heuristics_are_upstream_outside_npu_choices
  [InductorChoices("npu") BLOCK_N == upstream BaseConfigHeuristic's,
   NPUChoices("npu") BLOCK_N == [vector_lanes]]
  parent: [False, True]   this: [True, True]

Refs #427
… them

openreg._lazy_init turned flash SDPA off and set overrideable/math on for the whole
process. npu's _fused_sdp_choice (device/csrc/aten/native/Extra.cpp) reads only the
overrideable flag, which is on by default, so the npu route is unchanged; the flash flag
only ever reached cpu, whose fused choice fell from FLASH (1) to MATH (0).

Pin: tests/system/test_patch_scope.py test_sdpa_flags_are_upstream compares
[flash, mem_efficient, math, overrideable, cpu _fused_sdp_choice] with a process
started with TORCH_DEVICE_BACKEND_AUTOLOAD=0
  parent: [0, 1, 1, 1, 0] vs [1, 1, 1, 1, 1]   this: equal
npu: tests/ops/attention/test_sdpa.py 16/16 and test_fused_sdpa.py 11/11, route lines
identical to the parent's.

Refs #427
@YWHyuk
YWHyuk force-pushed the item11/public-extension-points branch from 4c3009d to 5db43bf Compare October 23, 2026 09:51
@YWHyuk YWHyuk changed the title [Frontend] Attach to Inductor through public extension points only (PTB rule 16) [Frontend] has_triton, the conv heuristic and the SDPA flags are upstream's (PTB rule 16) Oct 23, 2026
@YWHyuk
YWHyuk merged commit 47d7490 into pytorchsim-triton-backend Oct 23, 2026
@YWHyuk
YWHyuk deleted the item11/public-extension-points branch October 26, 2026 05:43
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