LLM-Aided Compilation for Tensor Accelerators

Charles C. Hong, Sahil Bhatia, Altan Haan, Shengjun Kris Dong, Dima Nikiforov, Alvin Cheung, Yakun Sophia Shaov

Abstract

Hardware accelerators, in particular accelerators for tensor processing, have many potential application domains. However, they currently lack the software infrastructure to support the majority of domains outside of deep learning. A compiler that can easily be updated to reflect changes in both application and hardware would provide great benefits to the agile development of hardware accelerators. In this work, we discuss how large language models (LLMs) could be leveraged to build such a compiler. Specifically, we demonstrate the ability of GPT-4 to achieve high pass rates in translating code to the Gemmini accelerator, and prototype a technique for decomposing translation into smaller, more LLM-friendly steps. Additionally, we propose a 2-phase workflow for utilizing LLMs to generate hardware-optimized code.

Introduction

The rapid evolution of hardware accelerators, particularly systolic arrays designed for tensor processing, offers significant potential for gains in energy efficiency and performance. However, these specialized architectures are notoriously difficult to program. While deep learning frameworks like XLA or TVM provide robust compiler support, other domains—such as robotics, graph analytics, and financial modeling—often lack the necessary software infrastructure to target these chips efficiently.

Currently, developers are forced to manually write code in low-level Domain-Specific Languages (DSLs) or undertake the arduous task of building custom compilers for every new hardware iteration. This "software gap" creates a cyclical dependency that stifles hardware innovation. This paper explores the use of Large Language Models (LLMs) as agile compilers to bridge this gap, automating the translation of general-purpose code into hardware-optimized instructions.

Methodology: A Two-Phase Workflow

The researchers propose a two-phase workflow designed to leverage the reasoning capabilities of LLMs for hardware compilation.

  1. Functional Translation: The first phase involves converting source code (e.g., C++) into a functionally correct implementation in the target hardware's DSL.
  2. Performance Optimization: The second phase utilizes a cost-model-driven search. Here, the LLM suggests various optimizations, such as loop tiling, unrolling, or reordering, to maximize performance on the specific hardware target.
The proposed two-phase workflow for LLM-aided compilation, featuring a Code Translator and Hardware Performance Evaluation loop.

To optimize the LLM's performance, several prompting strategies were employed:

  • Structured Prompting: Providing a clear Instruction Set Architecture (ISA) specification alongside the source code.
  • In-Context Learning (ICL): Using one or two examples of successful translations to guide the model's output style.
  • Multi-step Repair: Addressing arithmetic errors by prompting the LLM to identify uncertain constants and then filling them in through targeted reasoning steps.
  • LLM-driven Autoscheduling: Interfacing with libraries like Exo to suggest structural code transformations.

Experimental Evaluation

The approach was evaluated using kernels from diverse real-world applications, including TinyMPC (robotics control), Riccati Recursion (control problems), and PolyBench (linear algebra). The primary target hardware was the Gemmini accelerator, an academic systolic array project.

Key Insights

A critical finding was the importance of the Instruction Set Architecture (ISA) as context. LLMs do not necessarily need prior exposure to a specific hardware DSL if provided with a high-level specification. Furthermore, the transition from "zero-shot" to "one-shot" learning proved transformative for accuracy.

Pass rates for different prompting strategies, highlighting the significant performance jump from zero-shot to one-shot in-context learning.

The study also noted that providing full code implementations of source functions (semantics) was more effective than natural language descriptions alone, as the code provides a structural template for the LLM to follow.

Results

The results demonstrate the viability of LLMs in the compilation pipeline:

  • High Accuracy: GPT-4 achieved a 100% pass rate (at $k=50$) for matrix-vector multiplications when using one-shot examples and semantic descriptions.
  • Robotics Application: The system successfully translated 8 out of 9 complex robotics kernels. The final, most challenging kernel was resolved using the multi-step repair technique.
  • Performance Gains: In autoscheduling experiments on x86 targets, LLM-guided optimization improved a linear algebra kernel's performance from 5.64 GFLOPS to 43.33 GFLOPS—a nearly 8x speedup.

Conclusion and Future Work

While LLMs show immense promise, challenges remain. Models still struggle with precise arithmetic for memory strides in complex nested loops. Future work will focus on integrating hardware-accurate cost models more deeply into the feedback loop and exploring formal verification tools to ensure 100% equivalence between source and generated code. Despite these limitations, LLMs represent a significant step toward agile, automated software support for emerging hardware architectures.

Summary updated

BibTeX
@misc{hong2024llm,
  author = {Charles C. Hong and Sahil Bhatia and Altan Haan and Shengjun Kris Dong and Dima Nikiforov and Alvin Cheung and Yakun Sophia Shaov},
  title = {{LLM-Aided Compilation for Tensor Accelerators}},
  year = {2024},
  eprint = {2408.03408},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.03408},
}
Figure