How onnx/onnx-mlir works
Below are the guidelines and key points to include, tailored to this project:
Main Components
• Input – the ONNX model supplied to the compiler.
• Frontend / Parser – the module that reads the ONNX model and establishes an initial representation in an ONNX Dialect.
• Intermediate Representation Layer – where the ONNX model is converted to an MLIR-based representation. This includes the ONNX Dialect defined in the project (visible in the src/Dialect/ONNX directories) and the representation that evolves through various transformation passes.
• Transformation & Conversion Passes – a rich pipeline of conversion stages:
– ONNX-to-MLIR lowering.
– Further conversion passes through the MLIR “Dialect” layer (e.g., ONNX, Krnl, ZHigh, ZLow, and LLVM).
– Specific conversion modules (see src/Conversion/* and src/Transform/*) that implement various optimizations and passes (numerical optimizations, device placement for accelerators, etc.).
• Code Generation / Backend – components that generate the final target code. This includes:
– Emitting LLVM IR.
– Building object files, shared libraries, or JNI libraries (as indicated by the various --Emit flags in the onnx-mlir driver and infrastructure in src/Compiler & src/Runtime).
• Runtime Environment – the provided runtime support (C/C++ runtime, Python wrappers, Java interfaces) that allow the resulting code to execute, including specialized runtime support found in src/Runtime and even the Accelerator wrappers in src/Accelerators.
• Build & Tooling Components – not part of the runtime architecture per se, but crucial to the project:
– CMake files (CMakeLists.txt at various levels).
– Build system configurations and CI/CD pipelines (e.g. .azure-pipelines, .buildbot, .circleci, and .github/workflows directories).
– Docker container files (docker/, docs/docker-example) emphasizing that development and testing often occur in containerized environments.
– Testing and validation infrastructure (test/, utils/, third_party).
Relationships and Data Flow
• The onnx-mlir driver parses the model by invoking the ONNX dialect front-end, which then feeds into the series of conversion passes.
Important Architectural Patterns or Design Principles
• Layered design: The project is clearly modular with separate layers for frontend parsing (ONNX dialect and model import), IR transformation (MLIR and conversion passes), and backend code generation.
• Plugin-like extensibility: Different conversion passes and dialect definitions (for onnx, krnl, zhigh, zlow) show that the system is designed to be extended for various hardware accelerators and target architectures.
• Separation of concerns: The tool separates compile-time phases from runtime support – with dedicated directories for the compiler (src/Compiler), runtime (src/Runtime), and DSL/IR definitions (src/Dialect, src/Conversion, src/Transform).
Relevant Technologies, Frameworks, and Libraries
• LLVM/MLIR – as the core technology for the lowering and intermediate representation.
• ONNX – as the primary input specification.
• CMake – as the build system.
• Docker and various CI/CD systems (Azure Pipelines, Jenkins, GitHub Actions, CircleCI) – for building, testing, and deployment.
• Language runtimes (C/C++, Python, Java) – for the generated outputs and runtime libraries.