Skip to content

Fixes from reading the refactor: a queued TOGSimulator launch, npu RNG state, grouped_mm and sort fallbacks, stale stage directories - #464

Merged
student-Jungmin merged 5 commits into
pytorchsim-triton-backendfrom
fix/code-map-bugs
Oct 24, 2026
Merged

student-Jungmin merged 5 commits into
pytorchsim-triton-backendfrom
fix/code-map-bugs

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Draft: waiting for verification (CI) before review.

Bugs found reading the merged refactor (pytorchsim-triton-backend 338b42d), each reproduced first. One commit per bug; no commit depends on another, and no default changes. Each fix comes with a test that fails on the old code.

What

  1. A launch queued on a TOGSimulator ([Simulator])
    • Inside with torch.npu.TOGSimulator() with timing on, the first launch raised TypeError: 'NoneType' object is not subscriptable. The cause: torch.npu.launch_kernel queues the launch and returns None, and the launcher indexed that None as a standalone result.
    • run_togsim now returns None for a queued launch (after its copy nodes, as [GEMM Timing Validation][Frontend] An npu mm the compiler's copy_node.plan holds runs the resident mm, as v6e XLA runs it #457 queues them), and the launcher logs it as queued.
    • Each queued launch's copy of the runtime attributes was never removed. The session now removes the copies at shutdown, after the Simulator exits.
  2. torch.npu's RNG state ([Device])
    • get_rng_state and set_rng_state defaulted to device="openreg", a device torch no longer knows once PrivateUse1 is renamed npu. Called with no device, as torch.utils.checkpoint calls them, or with an index, they raised.
  3. grouped_mm's fallback ([Dispatcher])
    • With offs[-1] < S it returned offs[-1] rows; aten and transformers return S. Rows past offs[-1] are now zero, as transformers gives them.
    • It added a bias to every row. aten refuses a bias ("Bias not supported yet"), and so does the fallback now.
    • plan indexed the first GEMM tile candidate unchecked; with no candidate it now declines. The kernel is unchanged.
  4. clear_stale on a stage directory ([Backend], a regression from [Refactor 7/9] One place for each fact: tool paths in config, the runtime attribute directory, build identity, launch hooks, template records #462)
    • PTB 2abb4a3 and later leave 08-passes/ and 09-passes/ directories. os.remove raised IsADirectoryError, which failed the first rebuild of a workdir under another identity.
    • Directories are now removed with shutil.rmtree.
  5. sort's fallback on float64 ([Dispatcher])
    • float64 always runs the fallback (plan takes int32, int64 and float32), and the fallback keyed it through float32, so values float32 rounds together kept their input order.
    • float64 is now keyed by its own 64 bits. Every other dtype keeps the 32-bit key, so the kernel and the fallback still agree.

Checked locally

Setup: this branch on PTB 6f88e2b (this branch's pin), TOGSim 5791272, target tpuv6e.

Mode Test Result
functional test_add 5/5
functional test_matmul 11/11
functional test_grouped_mm (7 cases, tail and bias new) pass
functional test_topk, test_custom_fused_kernel, test_histc, test_eager pass
functional test_patch_scope 8/8
functional test_device_interface (RNG case new) pass
functional test_togsim_result (2 cases new) pass
functional test_togsim_exit, test_cache_identity (directory case new), test_reduction_tiling, test_module_form pass
functional test_copy_node 9/9
functional test_sort (float64 case new) 26/26
timing test_select, test_matmul 11/11 pass
timing, inside a TOGSimulator a 256x256 add pass, 2741 cycles; TypeError before the fix

Known gap, not from this PR: test_conv_decomposition fails on conv1d at lower ("3 access(es) leave their scratchpad buffer's slot"). It fails the same way on the base 338b42d. Bisected to PSAL-POSTECH/PyTorchSim-Triton-Backend ef0d13df (fit-to-numel cuts a dot's K below the block the matmul lowering reads); its parent f6ddd80f compiles the kernel. The fix goes to the compiler.

…its held attribute copy goes at shutdown

Inside `with torch.npu.TOGSimulator()` with timing on, the first launch failed: torch.npu.launch_kernel
queues the launch on the stream and returns None, and TritonNPULauncher took a non-int as a standalone
result and indexed result["total_cycles"] (TypeError: 'NoneType' object is not subscriptable).
run_togsim now returns None for a queued launch, and the launcher logs it as queued.

Each queued launch of a kernel with runtime attributes copied them into launch-<pid>/runtime_attribute-*
for the session to read later, and nothing removed the copies. The session now keeps them in
TOGSimProcess.held and removes them in shutdown, after the Simulator has exited.

test_togsim_result: a held copy is gone after shutdown and the original stays; a queued launch returns
None. A 256x256 add compiled and timed inside a TOGSimulator now passes (2741 cycles).
…again

openreg/random.py kept the openreg example's device name: get_rng_state and set_rng_state defaulted
to device="openreg" and turned an index into torch.device("openreg", i), a device type torch no longer
knows once PrivateUse1 is renamed npu. Called with no device (torch.utils.checkpoint's preserve_rng_state
does) or with an index, both raised "Expected one of cpu, cuda, ... device string: openreg". They now
name npu.

test_device_interface: the state reads and writes back for the default device, 0, "npu" and "npu:0";
it fails with the old default.
… and a plan with no tile declines

The fallback (eager calls, and compiled ones plan declines) concatenated the groups' products, so with
offs ending before A's last row it returned offs[-1] rows where aten._grouped_mm and transformers'
grouped_mm_fallback return A's S rows; the kernel already allocates [S, N]. It now writes each group
into an [S, N] output whose rows past offs[-1] are zero, as transformers' fallback gives them (aten
leaves them unspecified). A bias was added to every row; aten refuses one ("Bias not supported yet"),
and so does the fallback now. plan took the first GEMM tile candidate unchecked, an IndexError on a
machine whose scratchpad holds none; it declines instead. The kernel is unchanged.

test_grouped_mm: tail (offs[-1] < S, eager rows past it zero, compiled rows up to it match) and bias
(refused); all 7 cases pass.
…ot an IsADirectoryError

clear_stale removes what another identity built, matching "[0-9][0-9]-*" among others, with
os.remove, and since #462 raises any removal error but a file already gone. The compiler at PTB 2abb4a3
leaves each stage's per-pass dumps as a directory (08-passes, 09-passes), so the first rebuild of such
a workdir under another identity failed the compile with IsADirectoryError. A directory is now removed
with shutil.rmtree; a symlink or file still with os.remove.

test_cache_identity: a workdir with 08-passes/, kernel.json and a stage file keeps only the spec; on
the old code it raises IsADirectoryError.
…t32's

plan takes int32, int64 and float32 only, so a float64 sort always runs the fallback, and the fallback
keyed every float through t.float(): float64 values one float32 holds as equal (1 + 2^-30, 1, 1 + 2^-40)
kept their input order. A float64 is now keyed by its 64 bits (NaN canonical and last, -0 as +0, as the
32-bit key); every other float still by float32's, so the kernel's keys and the fallback's still agree.

test_sort: a float64 row of such values, -0, 0, NaN and -1, both orders, gives torch.sort's indices;
the old fallback gave [6, 3, 4, 0, 1, 2, 5] for [6, 3, 4, 1, 2, 0, 5]. All 26 cases pass.
@student-Jungmin
student-Jungmin marked this pull request as ready for review October 24, 2026 15:09
@student-Jungmin
student-Jungmin merged commit e1d8aa3 into pytorchsim-triton-backend Oct 24, 2026
2 of 3 checks passed
student-Jungmin added a commit that referenced this pull request Oct 24, 2026
device/torch_openreg/lib is no longer tracked: a local build symlink from #464 broke the device build in CI
@YWHyuk
YWHyuk deleted the fix/code-map-bugs branch October 27, 2026 07:46
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