Introduction
The rapid evolution of AI hardware and software frameworks has created a significant bottleneck for developers. Migrating legacy sequential code in C++ or Python to specialized accelerators like Intel Gaudi or Apple MLX typically requires manual, expert-level rewriting. This process is time-consuming, prone to bugs, and difficult to scale. Tenspiler addresses this challenge by automatically translating standard sequential code into highly optimized tensor operations, achieving an average kernel speedup of 105x while formally guaranteeing mathematical equivalence to the original source.
System Architecture
Tenspiler employs a three-stage workflow based on the principle of "verified lifting," which treats compilation as a search and verification problem rather than a set of rigid, hand-written rules.
1. Synthesis (The Search Phase)
Instead of traditional pattern matching, Tenspiler uses a program synthesizer (Rosette) to explore a space of possible programs written in Tensir—a simple, unified intermediate representation (IR) designed specifically for tensor algebra. To prevent an exponential search space explosion, the system uses Expression Tree Leveraging, a static analysis technique that prunes the search space by over 99% by focusing on operations that match the structure of the original computation.
2. Verification
Once a candidate Tensir program is found, Tenspiler utilizes the CVC5 SMT solver to formally prove that the synthesized program is functionally equivalent to the input source code for all possible inputs. This provides a mathematical guarantee of correctness that modern LLMs cannot offer.
3. Backend Code Translation
Because Tensir is high-level and clean, translating it into specific target languages—such as PyTorch, NumPy, or Gaudi’s TPC-C—is a straightforward process. This modularity makes the compiler highly extensible; for instance, adding support for Apple’s MLX framework required fewer than 200 lines of code.
Evaluation and Key Results
The authors evaluated Tenspiler using 69 functions across 10 real-world benchmark suites, including image processing kernels (blend) and essential kernels from the Llama2 LLM architecture. The compiler was tested against six different backends, including TensorFlow, Apple MLX, and the Intel Gaudi 2 processor.
- Performance: Tenspiler achieved an average 105.1x speedup for kernels and a 9.65x end-to-end speedup over fully optimized sequential C++ code. On Intel Gaudi 2, some kernels saw speedups as high as 241x.
- Comparison to Industry Tools: Tenspiler-generated code was 1.87x faster on average than Numba, as it could automatically leverage higher-level optimized library calls.
- Robustness: While top-tier LLMs like Claude 3 Opus often failed to generate working code for specialized DSLs like Gaudi or MLX, Tenspiler handled them reliably due to its formal verification phase.
Limitations and Future Work
Current limitations include a restricted subset of supported C++ and Python features (excluding complex pointers or object-oriented structures) and challenges in SMT reasoning for complex floating-point operations. Future work will explore machine-learning-guided search and bottom-up synthesis to handle even deeper nested loops and more complex data structures.
Summary updated