Torch-MLIR - Bridging PyTorch and MLIR ecosystems

The Torch-MLIR project aims to provide first class compiler support from the PyTorch ecosystem to the MLIR ecosystem.

MLIR The MLIR project is a novel approach to building reusable and extensible compiler infrastructure. MLIR aims to address software fragmentation, improve compilation for heterogeneous hardware, significantly reduce the cost of building domain specific compilers, and aid in connecting existing compilers together.

Torch-MLIR Multiple Vendors use MLIR as the middle layer mapping from platform frameworks like PyTorch, JAX, TensorFlow onto MLIR and then progressively lowering down to their target hardware. We have seen half a dozen custom lowerings from PyTorch to MLIR. Having canonical lowerings from the PyTorch ecosystem to the MLIR ecosystem would provide much needed relief to hardware vendors to focus on their unique value rather than implementing another PyTorch frontend for MLIR. It would be similar to current hardware vendors adding LLVM target support instead of each one also implementing the Clang/C++ frontend.

All the roads from PyTorch to Torch MLIR Dialect

We have a few paths to lower down to the Torch MLIR Dialect.

  • TorchScript
    This is the most tested path from PyTorch to Torch MLIR Dialect.
  • TorchFX
    This provides a path to go from TorchFX down to MLIR. This a functional prototype.
  • Lazy Tensor Core (Based on lazy_tensor_staging branch)
    This path provides the upcoming LTC path of capture. It is based on an unstable devel branch but is the closest way for you to adapt any existing torch_xla derivatives.
  • “ACAP” (Deprecated torch_xla based capture, mentioned here for completeness)

The torch-mlir project includes a few examples of lowering down via each path from PyTorch to MLIR and using the “mlir-cpu-runner” to target a CPU backend. Obviously this is just a starting point and you can import this project into your larger MLIR projects to continue lowering to target GPUs and other Accelerators.

We are looking for specific feedback on two fronts:

Vendors: If you use your custom lowerings to get to MLIR eventually, is there anything else you would like to see. Please open github issues in torch-mlir.

Pytorch Dev community:
Here is a PR to integrate Torch-MLIR with an optional build flag into PyTorch. There are included samples to export a ResNet Model and we plan to add BERT and MASK-RCNN examples next. Please let us know what you think.

Also, any feedback on the various paths down to MLIR (TS, FX, LTC->TS, LTC->MLIR, etc) would be great.

Anush
(For the torch-mlir team, nod.ai team and the others who contributed to this effort.)

19 Likes

As a silicon vendor and contributor to the torch-mlir project, Xilinx is very excited to see this maturing. Having a stable path from pytorch to MLIR that is capable of supporting a wide variety of use cases for inference and training is going to be very helpful!

7 Likes

As a part of the MLIR ecosystem at Google and a contributor to torch-mlir, we’re really supportive of seeing this mature into a great connection point between the PyTorch ecosystem and the MLIR ecosystem broadly. It will help a lot to have this component, done well and aligned with the PyTorch roadmap for interfacing to the existing and new APIs for program extraction.

6 Likes

We at Arm are really excited about this effort. We contributed the TOSA dialect to the MLIR ecosystem, and this offers an interface between the PyTorch and MLIR ecosystems. It will help us develop pathways from PyTorch content to MLIR based code generation targets.

7 Likes

This would be very useful!

5 Likes

As a Life-Ready edge AI provider, GrAI Matter Labs is supportive of this effort.

6 Likes

At Intel we are also supportive of this effort. As we work more on MLIR lowering, it’s really useful to see a path to better connections between the pytorch ecosystem and the broader MLIR ecosystem.

7 Likes

FYI: We will be discussing torch-mlir at the LLVM/MLIR ODM on Oct 7th 9am PT. Details here . Please join us if you are interested and thank you all for your support.

3 Likes

We at SiFive support this effort; having a unified lowering from PyTorch to MLIR allows us to optimize the code-gen on RISC-V in general and SiFive core IP in specific while enabling such an important framework.

4 Likes

We at ByteDance support this effort to further improve PyTorch performance on GPU and other silicons using MLIR backed compiler optimizations.

5 Likes

If you missed the meeting this morning, here are the slides ​and the recording.
This is a very exciting direction, I’m looking forward to it! :slight_smile:

4 Likes

That’s exciting, it would be useful. We had already do some works on Torch to MHLO at Alibaba, such as DISC.

3 Likes

We have pushed a second RFC PR here: Add simple `torch.mlir.export` function and demo notebook. by silvasean · Pull Request #66536 · pytorch/pytorch · GitHub to integrate with PyTorch. It follows the same api format as torch.onnx.export and provides a torch.mlir.export. If there are any suggestions / feedback on this approach from the various interested parties vs the earlier integration RFC (Add torch-mlir MLIR Exporter to PyTorch build by powderluv · Pull Request #65880 · pytorch/pytorch · GitHub) at the C++ level please let us know. The Python integration could potentially work across multiple PyTorch versions.

2 Likes

:tada::tada:This is definitely a good trend. :tada::tada:

However I’m thinking PyTorch frontend need to be reinforced for better usage of MLIR.

2 Likes