# shashank-mcw/torch-mlir architecture

> Architecture diagram of the GitHub repository shashank-mcw/torch-mlir, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shashank-mcw/torch-mlir
- Repository: https://github.com/shashank-mcw/torch-mlir

## Overview

Torch-MLIR is a PyTorch-to-MLIR compiler bridge. Its user-facing workflow imports models through FX or ONNX into the Torch dialect, then provides transformations and lowerings to backend-oriented dialects such as Linalg, StableHLO, TOSA, and TMTensor. The repository also includes TorchScript/JIT importing and a lazy-tensor backend, plus a reference backend and registration APIs. The README establishes FX and ONNX as entry points; some downstream connections are shown only where the source index or excerpts support them.

Main entry points: [`lib/Dialect/Torch/IR/TorchOps.cpp`](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchOps.cpp) (Torch dialect), [`lib/Dialect/Torch/Transforms`](https://github.com/shashank-mcw/torch-mlir/tree/main/lib/Dialect/Torch/Transforms) (Torch transforms), [`lib/InitAll.cpp`](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/InitAll.cpp) (Dialect and pass registry), [`projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp`](https://github.com/shashank-mcw/torch-mlir/blob/main/projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp) (Lazy backend)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_imports["Model import"]
  node_fx_api["FX API<br/>[fx.py]"]
  node_fx_import["FX importer<br/>[fx_importer.py]"]
  node_onnx_import["ONNX importer<br/>[OnnxImporter.cpp]"]
  node_jit_import["JIT importer"]
end

subgraph group_ir["Torch IR"]
  node_torch_dialect["Torch dialect<br/>[TorchOps.cpp]"]
  node_torch_types["Torch types<br/>[TorchTypes.cpp]"]
  node_torch_passes["Torch transforms"]
end

subgraph group_lower["Lowering"]
  node_onnx_lower["ONNX conversion"]
  node_linalg_lower["Linalg lowering<br/>[Uncategorized.cpp]"]
  node_tmtensor_lower["TMTensor lowering"]
  node_other_lowerings["Other lowerings"]
  node_tmtensor["TMTensor dialect<br/>[TMTensorOps.cpp]"]
end

subgraph group_runtime["Backend integration"]
  node_registry["Dialect and pass registry<br/>[InitAll.cpp]"]
  node_c_api["C API<br/>[Registration.cpp]"]
  node_ref_backend["Reference backend<br/>[RefBackend.cpp]"]
  node_lazy_backend["Lazy backend"]
end

node_pytorch(("PyTorch models"))
node_onnx(("ONNX models"))

node_pytorch -.->|"supplies graph"| node_fx_api
node_fx_api -->|"calls importer"| node_fx_import
node_fx_import -->|"creates operations"| node_torch_dialect
node_onnx -.->|"supplies model"| node_onnx_import
node_onnx_import -.->|"imports ONNX ops"| node_onnx_lower
node_onnx_lower -->|"rewrites to Torch ops"| node_torch_dialect
node_jit_import -->|"imports JIT IR"| node_torch_dialect
node_torch_dialect -->|"uses types"| node_torch_types
node_torch_passes -->|"rewrites operations"| node_torch_dialect
node_torch_dialect -->|"lowers operations"| node_linalg_lower
node_torch_dialect -->|"lowers operations"| node_tmtensor_lower
node_tmtensor_lower -->|"creates operations"| node_tmtensor
node_torch_dialect -.->|"supports conversion"| node_other_lowerings
node_registry -->|"registers dialect"| node_torch_dialect
node_registry -->|"registers passes"| node_other_lowerings
node_c_api -->|"exposes operations"| node_torch_dialect
node_lazy_backend -->|"imports function"| node_jit_import
node_lazy_backend -->|"verifies module"| node_torch_passes
node_ref_backend -->|"uses backend contract"| node_torch_passes

click node_fx_api "https://github.com/shashank-mcw/torch-mlir/blob/main/python/torch_mlir/fx.py"
click node_fx_import "https://github.com/shashank-mcw/torch-mlir/blob/main/python/torch_mlir/extras/fx_importer.py"
click node_onnx_import "https://github.com/shashank-mcw/torch-mlir/blob/main/projects/onnx_c_importer/OnnxImporter.cpp"
click node_jit_import "https://github.com/shashank-mcw/torch-mlir/blob/main/projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp"
click node_torch_dialect "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchOps.cpp"
click node_torch_types "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchTypes.cpp"
click node_torch_passes "https://github.com/shashank-mcw/torch-mlir/tree/main/lib/Dialect/Torch/Transforms"
click node_onnx_lower "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp"
click node_linalg_lower "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Conversion/TorchToLinalg/Uncategorized.cpp"
click node_tmtensor_lower "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp"
click node_other_lowerings "https://github.com/shashank-mcw/torch-mlir/tree/main/lib/Conversion"
click node_tmtensor "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/TMTensor/IR/TMTensorOps.cpp"
click node_registry "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/InitAll.cpp"
click node_c_api "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/CAPI/Registration.cpp"
click node_ref_backend "https://github.com/shashank-mcw/torch-mlir/blob/main/lib/RefBackend/RefBackend.cpp"
click node_lazy_backend "https://github.com/shashank-mcw/torch-mlir/blob/main/projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_fx_api,node_fx_import,node_onnx_import,node_jit_import toneBlue
class node_torch_dialect,node_torch_types,node_torch_passes toneAmber
class node_onnx_lower,node_linalg_lower,node_tmtensor_lower,node_other_lowerings,node_tmtensor toneMint
class node_registry,node_c_api,node_ref_backend,node_lazy_backend toneRose
class node_pytorch,node_onnx toneIndigo
```

## Components

### Model import

- [FX API](https://github.com/shashank-mcw/torch-mlir/blob/main/python/torch_mlir/fx.py): `python/torch_mlir/fx.py`
- [FX importer](https://github.com/shashank-mcw/torch-mlir/blob/main/python/torch_mlir/extras/fx_importer.py): `python/torch_mlir/extras/fx_importer.py`
- [ONNX importer](https://github.com/shashank-mcw/torch-mlir/blob/main/projects/onnx_c_importer/OnnxImporter.cpp): `projects/onnx_c_importer/OnnxImporter.cpp`
- [JIT importer](https://github.com/shashank-mcw/torch-mlir/blob/main/projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp): `projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp`

### Torch IR

- [Torch dialect](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchOps.cpp): `lib/Dialect/Torch/IR/TorchOps.cpp`
- [Torch types](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchTypes.cpp): `lib/Dialect/Torch/IR/TorchTypes.cpp`
- [Torch transforms](https://github.com/shashank-mcw/torch-mlir/tree/main/lib/Dialect/Torch/Transforms): `lib/Dialect/Torch/Transforms`

### Lowering

- [ONNX conversion](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp): `lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp`
- [Linalg lowering](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Conversion/TorchToLinalg/Uncategorized.cpp): `lib/Conversion/TorchToLinalg/Uncategorized.cpp`
- [TMTensor lowering](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp): `lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp`
- [Other lowerings](https://github.com/shashank-mcw/torch-mlir/tree/main/lib/Conversion): `lib/Conversion`
- [TMTensor dialect](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/Dialect/TMTensor/IR/TMTensorOps.cpp): `lib/Dialect/TMTensor/IR/TMTensorOps.cpp`

### Backend integration

- [Dialect and pass registry](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/InitAll.cpp): `lib/InitAll.cpp`
- [C API](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/CAPI/Registration.cpp): `lib/CAPI/Registration.cpp`
- [Reference backend](https://github.com/shashank-mcw/torch-mlir/blob/main/lib/RefBackend/RefBackend.cpp): `lib/RefBackend/RefBackend.cpp`
- [Lazy backend](https://github.com/shashank-mcw/torch-mlir/blob/main/projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp): `projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp`

### Other components

- **PyTorch models**
- **ONNX models**

## Connections

- PyTorch models → FX API: supplies graph (evidence: `README.md`)
- FX API → FX importer: calls importer (evidence: `python/torch_mlir/fx.py`)
- FX importer → Torch dialect: creates operations (evidence: `python/torch_mlir/extras/fx_importer.py`)
- ONNX models → ONNX importer: supplies model (evidence: `README.md`)
- ONNX importer → ONNX conversion: imports ONNX ops
- ONNX conversion → Torch dialect: rewrites to Torch ops
- JIT importer → Torch dialect: imports JIT IR
- Torch dialect → Torch types: uses types
- Torch transforms → Torch dialect: rewrites operations (evidence: `lib/Dialect/Torch/Transforms/ScalarizeShapes.cpp`)
- Torch dialect → Linalg lowering: lowers operations (evidence: `lib/Conversion/TorchToLinalg/Uncategorized.cpp`)
- Torch dialect → TMTensor lowering: lowers operations (evidence: `lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp`)
- TMTensor lowering → TMTensor dialect: creates operations (evidence: `lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp`)
- Torch dialect → Other lowerings: supports conversion
- Dialect and pass registry → Torch dialect: registers dialect (evidence: `lib/InitAll.cpp`)
- Dialect and pass registry → Other lowerings: registers passes (evidence: `lib/InitAll.cpp`)
- C API → Torch dialect: exposes operations
- Lazy backend → JIT importer: imports function (evidence: `projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp`)
- Lazy backend → Torch transforms: verifies module (evidence: `projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp`)
- Reference backend → Torch transforms: uses backend contract (evidence: `lib/RefBackend/RefBackend.cpp`)
