TL;DR
RISCompiler is a new compilation framework that treats hardware optimization as a language generation task. By fine-tuning a Llama-3.2 model with a specialized "Instruction Language Model" (ILM) and a novel Mixture-of-Experts (MoE) adapter strategy, it automatically generates high-performance vectorized code for RISC-V processors, outperforming traditional compilers like LLVM and GCC by up to 1.65x.
What Problem Does This Paper Address?
In the world of high-performance computing and AI, vectorization is the secret sauce for speed. It allows a processor to perform the same operation on multiple data points simultaneously (SIMD). However, the open-source RISC-V architecture’s vector extension (RVV) is notoriously difficult to program.
Traditional compilers (like GCC or LLVM) often fail to vectorize code because they are too "conservative"—they struggle with complex data dependencies and memory patterns. On the other hand, "search-based" compilers try to find the best code by brute-forcing thousands of combinations, which is slow and relies on imperfect performance models. RISCompiler addresses this "vectorization gap" by using Large Language Models (LLMs) to predict the best optimization strategies directly.
Methods
The authors introduce RISCompiler, which transforms the optimization process into a sequence-to-sequence generation task. The core components include:
- Instruction Sentences: Instead of feeding raw assembly to the LLM, the authors designed a "language-model-friendly" text representation. These sentences describe the operator (e.g., a matrix multiplication), its dimensions, and specific hardware-level "knobs" like vector width, register count, and instruction combinations.
- Instruction Language Model (ILM): Based on the Llama-3.2-3B architecture, this model is trained to "read" a scalar program and "write" the optimized instruction sentences.
- Asymmetric LoRA-MoE: This is the technical highlight. Standard fine-tuning (LoRA) uses a single set of trainable weights. RISCompiler uses an asymmetric structure: a shared "A" matrix for general knowledge and multiple "B" matrices acting as "experts" for different types of vectorized instructions. A "router" mechanism decides which expert to use for a given piece of code, making the fine-tuning both efficient and highly specialized.
- Three-Stage Pipeline:
- Offline Learning: Sampling a massive space of possible optimizations to create a training dataset.
- Efficient Fine-Tuning: Training the ILM using the MoE strategy.
- Code Generation: Using the trained model to generate optimized RISC-V programs for new, unseen operators.
Datasets
The researchers built a comprehensive offline dataset to train the ILM:
- Workloads: 86 distinct workloads and roughly 1,800 operators.
- Scale: 0.5 million instruction sentences encompassing various vectorized optimizations.
- Sources: Operators were derived from popular deep learning benchmarks like TenSet, timm (PyTorch Image Models), and HuggingFace Transformers, covering both Computer Vision and NLP tasks.
- Evaluation: The model was tested on end-to-end networks including ResNet-50, VGG-16, MobileNet-v2, DenseNet, and BERT.
Key Insights and Ideas
- Optimization as Translation: The biggest "aha moment" is treating compiler optimization not as a mathematical search, but as a translation task from "what the code does" to "how the hardware should do it."
- Domain-Specific PEFT: The authors realized that a standard LLM is too general. By using the Asymmetric LoRA-MoE, they allow the model to develop "muscle memory" for specific RISC-V instructions (like
vfmaccorvle32) without needing to retrain the entire 3-billion-parameter model. - Iterative Refinement: The ILM doesn't just guess the whole program at once. It iteratively predicts optimizations along specific axes (like vector width first, then register count), which helps filter out invalid configurations early.
Key Results
RISCompiler consistently outperformed both traditional compilers and general-purpose LLMs:
- Single Operators: Achieved speedups of up to 1.65x over LLVM-Vectorizer and GCC-Loops across common operators like Conv2D and GEMM.
- End-to-End Networks: Improved performance by up to 1.38x on full models like ResNet-50 and BERT.
- Vs. General LLMs: It significantly outperformed Claude-3-Haiku. While Claude could achieve a 1.34x improvement over scalar code, RISCompiler reached 2.99x on the same operators because it was specifically fine-tuned for the RVV domain.
Limitations and Future Work
- Fine-Tuning Overhead: Training the ILM takes about 35 hours on a high-end server. The authors note that if you only need to compile a single model once, this might not be the most cost-effective route.
- Sampling Quality: The model's performance is heavily dependent on the quality of the "high-performance" samples it sees during the offline learning stage.
- Future Directions: The authors suggest expanding the framework to support more complex operator fusion (combining multiple layers into one) and potentially adapting the ILM approach to other emerging hardware architectures beyond RISC-V.
Summary updated