Skip to content

Conversation

sahas3
Copy link
Member

@sahas3 sahas3 commented May 9, 2025

  1. Disabled createVerifyTosaLinalgBackendContractPass pass to avoid erroring out early. This will leave unsupported ops in the IR which we can then catch by "unregistered dialect" in our cgir-mlir-opt pass.
  2. Added custom type conversion passes that keeps uint8 as uint8 similar to the StableHloBackend.
  3. Added custom FinalizingBackendTypeConversionForTosaLinalg pass to do greedy pattern rewriting instead of full to avoid failures when some patterns cannot be replaced. torch.constant.int is one such op that will cause the pass to fail in full rewrite pass. Instead we only replace as much as we can and then report the unlowered ops from cgir-mlir-opt.
  4. Refactored the import_exported_program call to be able to dump raw torch IR similar to how we dump tfl IR.

@sahas3 sahas3 requested a review from Hanumanth04 May 13, 2025 19:43
@Hanumanth04
Copy link

Hanumanth04 commented May 14, 2025

Could you please republish the PR with the updated changes whenever the changes are ready? I will take another quick look and approve the PR.

@sahas3 sahas3 merged commit fe21276 into mw_main May 14, 2025
1 check passed
sahas3 added a commit that referenced this pull request Jul 10, 2025
…#1)

* [Task] : Fix LIT test + disable pass failure for unlowered torch ops.
sahas3 added a commit that referenced this pull request Jul 18, 2025
…#1)

* [Task] : Fix LIT test + disable pass failure for unlowered torch ops.
sahas3 added a commit that referenced this pull request Jul 18, 2025
…#1)

* [Task] : Fix LIT test + disable pass failure for unlowered torch ops.
sahas3 pushed a commit that referenced this pull request Aug 11, 2025
…eration during folding (llvm#4274)

This fixes a SEGFAULT in the GreedyPatternRewriteDriver and adds a
missing size check to the `torch.aten._assert_tensor_metadata`
operation.

Erasing an operation during folding is not allowed. Folding the
operation may eithermodify it in place or return a set of replacements,
but may not erase the operation. (see
https://github.com/llvm/llvm-project/blob/e56384ff540e68f9d0500fa27a95354c0730e37b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp#L492-L508)

Doing this causes a SEGFAULT (witnessed on macOS Sequoia 15.5, Apple
M4):
```
Stack dump:
0.	Program arguments: build/bin/torch-mlir-opt -canonicalize --split-input-file -verify-diagnostics test/Dialect/Torch/invalid_canonicalize.mlir
 #0 0x0000000104091524 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (build/bin/torch-mlir-opt+0x10140d524)
 #1 0x000000010408fa5c llvm::sys::RunSignalHandlers() (build/bin/torch-mlir-opt+0x10140ba5c)
 #2 0x0000000104091bc8 SignalHandler(int, __siginfo*, void*) (build/bin/torch-mlir-opt+0x10140dbc8)
 #3 0x0000000181e10624 (/usr/lib/system/libsystem_platform.dylib+0x1804ac624)
 #4 0x0000000103c1f7a8 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() (build/bin/torch-mlir-opt+0x100f9b7a8)
 #5 0x0000000103c1cf4c mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (build/bin/torch-mlir-opt+0x100f98f4c)
 llvm#6 0x0000000102c8f62c (anonymous namespace)::Canonicalizer::runOnOperation() (build/bin/torch-mlir-opt+0x10000b62c)
 llvm#7 0x0000000103c72fa4 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (build/bin/torch-mlir-opt+0x100feefa4)
 llvm#8 0x0000000103c750d4 mlir::PassManager::run(mlir::Operation*) (build/bin/torch-mlir-opt+0x100ff10d4)
 llvm#9 0x0000000102c8d774 performActions(llvm::raw_ostream&, std::__1::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) (build/bin/torch-mlir-opt+0x100009774)
llvm#10 0x0000000102c8d35c llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::$_0>(long, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) (build/bin/torch-mlir-opt+0x10000935c)
llvm#11 0x000000010403194c mlir::splitAndProcessBuffer(std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef)::$_0::operator()(llvm::StringRef) const (build/bin/torch-mlir-opt+0x1013ad94c)
llvm#12 0x00000001040316a4 mlir::splitAndProcessBuffer(std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (build/bin/torch-mlir-opt+0x1013ad6a4)
llvm#13 0x0000000102c87078 mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (build/bin/torch-mlir-opt+0x100003078)
llvm#14 0x0000000102c8731c mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (build/bin/torch-mlir-opt+0x10000331c)
llvm#15 0x0000000102c87538 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (build/bin/torch-mlir-opt+0x100003538)
llvm#16 0x0000000102c85cd0 main (build/bin/torch-mlir-opt+0x100001cd0)
llvm#17 0x0000000181a36b98 
build/tools/torch-mlir/test/Dialect/Torch/Output/invalid_canonicalize.mlir.script: line 1: 72586 Segmentation fault: 11  build/bin/torch-mlir-opt -canonicalize --split-input-file -verify-diagnostics test/Dialect/Torch/invalid_canonicalize.mlir
```

Since the `torch.aten._assert_tensor_metadata` operation is only used
for static assertion during compile time the folding can be replaced by
a canonicalization that checks the assert and then uses a rewriter to
erase the operation.

The second commit deals with a missing size check in the assert
operation before using a zip operation. Without the explicit checkout of
the size, the would assert not fail in case the size of the dimensions
were the same, but there are either less or more dimensions in the input
than specified in the assert.

---------

Signed-off-by: Florian Walbroel <walbroel@roofline.ai>
Co-authored-by: Florian Walbroel <walbroel@roofline.ai>
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.

2 participants