Repository navigation
Conversation
getBroadcastShape used max(size, acc) to fold index broadcast sizes, which maps a size-0 dimension to 1 since the accumulator starts at 1. This diverges from numpy broadcasting semantics and corrupts loop bounds for index_put/scatter lowering when an index tensor has a zero-extent dimension (e.g. from an empty slice). Replace with the correct fold: (size == 1) ? acc : size. Verified: forcing valid strides on a zero-extent y in SliceCopyStartGreaterThanDimSize_Module_basic now succeeds (was OOB load abort before this fix). check-torch_mlir-conversion-torchtotmtensor and check-torch_mlir-conversion-torchtolinalg pass 23/23.
PyTorch→NumPy conversion produces non-canonical strides for zero-element tensors. These fail memref.cast runtime verification against statically-shaped contiguous memrefs at the RefBackend ABI boundary. Add _canonicalize_zero_element_strides() to rewrite zero-element array strides to canonical contiguous form before constructing the memref descriptor in invoke(). Non-empty arrays are unchanged. Supersedes llvm#4680 (self-closed without review, 2026-07-31) — same fix, revived here after independent diagnosis. Combined with the prior TorchToTMTensor broadcast fix, this resolves SliceCopyStartGreaterThanDimSize_Module_basic (previously SIGABRT on memref.cast stride mismatch for a zero-extent slice source). Verified (Colab, torchmlir-pt3): - New lit test test/refbackend/zero_extent_memref.py: PASS - check-torch_mlir-conversion-torchtotmtensor: 1/1 - check-torch_mlir-conversion-torchtolinalg: 22/22 - SliceCopyStartGreaterThanDimSize_Module_basic harness run: PASS (rc=0) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CLerxrBvP29pgpxDJ9D4zJ
Contributor
Author
|
Friendly note: CI shows |
…ests ONNX_CRASHING_SET inherits LINALG_CRASHING_SET, so removing the four zero-extent tests from it makes the onnx config (run by CI on every push) attempt them for the first time: - TraceModule_empty and TraceUnsignedIntModule_empty now pass under onnx, so drop them from ONNX_XFAIL_SET (they were XPASS). - SliceCopyStartGreaterThanDimSize_Module_basic fails under onnx with a torch.onnx export error (inconsistent constraints for the empty slice), so add it to ONNX_XFAIL_SET. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Join the broadcastSizes assignment onto one line; this is what the pre-commit clang-format hook produces for the getBroadcastShape fix. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
Fixes the SIGABRT crash in
SliceCopyStartGreaterThanDimSize_Module_basic(linalg-on-tensors e2e test). The crash happens because
x[100:10:1] = ywith
y = torch.rand(0, 4, 4)(an empty slice source) hits two independentbugs in the lowering/runtime path. Both are fixed here; each is a small,
localized commit.
Commit 1: TorchToTMTensor broadcast fold (numpy semantics bug)
getBroadcastShape(lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp)folds index-tensor broadcast sizes with
max(size, acc), whereaccstarts at 1. This maps a size-0 dimension (from an empty index/slice) to
1 instead of preserving 0 — the numpy broadcasting rule is
(size == 1) ? acc : size, which only differs frommaxat 0. Thiscorrupted the scatter loop bound for
index_putlowering derived fromRecomposeSliceCopy_.Commit 2: RefBackend zero-extent memref stride canonicalization
Separately,
torch.rand(0, 4, 4).numpy()reports strides(0, 0, 0).When RefBackend marshals this into a memref descriptor as-is, the
runtime-verification pass's
memref.castcheck (identity/contiguouslayout, last-dim stride == 1) rejects it and aborts — even though the
array has zero addressable elements, so the stride is semantically
irrelevant.
This is the same fix as #4680, which was self-closed by the author without
review on 2026-07-31. Looking at that author's other PRs closed the same
day, the closures were driven by a TOSA-specific realization (zero-extent
tensors aren't TOSA-conformant) — #4680 targets RefBackend
(linalg-on-tensors), which has no such constraint, so it appears to have
been swept up in that batch close rather than rejected on its own merits.
I independently hit the same bug, arrived at the same fix, and am
reviving it here with a regression test.
Commit 3: keep the ONNX xfail set consistent
ONNX_CRASHING_SETinheritsLINALG_CRASHING_SET, so removing the fourzero-extent tests from the latter makes the
onnxconfig (which CI alwaysruns) attempt them. Running it showed:
TraceModule_emptyandTraceUnsignedIntModule_emptynow pass, so theyare removed from
ONNX_XFAIL_SET(they would otherwise be unexpectedpasses).
SliceCopyStartGreaterThanDimSize_Module_basicfails underonnxintorch.onnx.export(inconsistent constraints for the empty slice), so itis added to
ONNX_XFAIL_SET.ReduceAllDimEmpty_basicpasses underonnxand needs no change.Testing
projects/pt1/python/test/refbackend/zero_extent_memref.pycheck-torch_mlir-conversion-torchtotmtensor: 1/1check-torch_mlir-conversion-torchtolinalg: 22/22 (no regressions)SliceCopyStartGreaterThanDimSize_Module_basicviaprojects.pt1.e2e_testing.main -c linalg: now PASSES (was SIGABRT)onnx,fx_importer,fx_importer_stablehlo,fx_importer_tosa) on this branch: no crashes.The only non-passing results are 22 unexpected passes under
onnxand 3failures under
fx_importer_tosa(ArgminIntModule_basic,ArgminIntModule_multiple_mins,ReduceMinAlongDimSignedInt_basic), andthe same ones occur on the base commit, so they are not caused by this PR.
The four tests above end up PASS/XFAIL as intended in each config (torch
2.15.0.dev20260917).
Scope note
SliceCopyStartGreaterThanDimSize_Module_basicalso appears in xfaillists for other backends (e.g. TOSA/STABLEHLO sections) — those are
not touched here. The stablehlo and tosa sets do not inherit
LINALG_CRASHING_SET, and with this change the tests stay expected-failthere. This PR removes the entries from the linalg-on-tensors
LINALG_CRASHING_SET, where they were directly verified, plus the ONNXadjustment above.
Assisted-by: Claude (Anthropic)