An MLIR-Based Compilation Method for Large Language Models
Summary
Sophgo Inc. researchers present an MLIR-based compilation method for Large Language Models, addressing challenges in model import and efficient autoregressive inference on specialized AI accelerators. This method utilizes two custom dialects: TopOp, a high-level, framework-agnostic graph dialect for model semantics, and TpuOp, a target hardware dialect for chip-specific decisions like quantization and memory layout. A model is initially represented in TopOp, then lowered layer by layer to TpuOp, culminating in a deployable binary. Crucially, each Transformer layer is split into three static compilation stages—prefill, prefill_kv, and decode—to accommodate the distinct computational patterns of prompt-parallel processing and per-token generation. This approach is integrated into the TPU-MLIR compiler and LLM-TPU deployment project, supporting generative models such as Qwen, Llama, InternVL, and MiniCPM-V series, alongside quantization techniques like GPTQ, AWQ, and AutoRound.
Key takeaway
For AI Engineers deploying Large Language Models on specialized AI accelerators, adopting an MLIR-based compilation strategy with a three-stage Transformer layer split can significantly enhance efficiency. You should utilize framework-agnostic intermediate representations like TopOp for model import and target-specific dialects like TpuOp for hardware optimization. This approach enables static compilation for prompt processing (prefill, prefill_kv) and per-token generation (decode), reducing runtime overhead and improving performance on constrained hardware. Consider implementing these stages to optimize memory allocation and operator scheduling.
Key insights
MLIR-based compilation with a three-stage Transformer layer split optimizes LLM deployment on specialized AI accelerators.
Principles
- Compiler infrastructure should be reusable and extensible via custom dialects.
- Static compilation variants reduce dynamic overhead for autoregressive inference.
- High-level fused operators allow target-specific optimal fusion strategies.
Method
A trained LLM checkpoint is converted to framework-agnostic TopOp, then lowered to target-specific TpuOp, incorporating quantization and memory planning. Transformer layers are split into prefill, prefill_kv, and decode stages for static compilation.
In practice
- Use TopOp for hardware-agnostic model representation.
- Implement TpuOp for chip-specific optimizations.
- Split LLM inference into prefill, prefill_kv, decode.
Topics
- MLIR
- Large Language Models
- AI Accelerators
- Compiler Design
- Autoregressive Inference
- KV Cache Optimization
Code references
Best for: NLP Engineer, Computer Vision Engineer, AI Scientist, AI Engineer, Machine Learning Engineer, AI Hardware Engineer
Related on AIssential
See Counsel's argued verdicts on the open AI decisions leaders are weighing →
Editorial summary, takeaway, and curation by AIssential. Original article published by cs.CL updates on arXiv.org.