NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

LoopTune: Optimizing Tensor Computations with Reinforcement Learning

Dejan Grubisic, Bram Wasti, Chris Cummins, John M. Mellor-Crummey, Aleksandar ZlateskiarXiv 2023 · DOI

Modern machine learning relies heavily on "tensor contractions"—complex nested loops that handle operations like matrix multiplication and convolutions. To achieve peak performance on modern CPUs, developers typically face a difficult trade-off. Traditional compilers like LLVM often fail to utilize specialized hardware features effectively. Expert-optimized libraries like cuDNN or NumPy are extremely fast but require thousands of manual engineering hours for every new chip architecture. Meanwhile, existing auto-tuners like TVM or Halide search for optimal "schedules" (loop tiling and reordering), but their search spaces are astronomical—often exceeding $10^{720}$ possible configurations—leading to compilation times measured in hours.

LoopTune addresses this challenge by providing the performance of expert libraries with the automation of auto-tuners, reducing tuning time from hours to seconds.

The LoopStack Architecture

To bridge the gap between high-level code and machine-level performance, the researchers introduced LoopStack, a three-layered architecture designed for rapid iteration and optimization.

The LoopStack architecture consists of LoopTune (RL agent), LoopTool (IR manipulation), and LoopNest (backend compiler).

As shown in the figure above, the stack consists of:

  • LoopTune (The Brain): A deep reinforcement learning (RL) agent that decides how to transform the code.
  • LoopTool (The API): An intermediate layer that allows the agent to manipulate loop structures via an intermediate representation.
  • LoopNest (The Muscle): A lightweight, ultra-fast backend compiler that generates executable code and measures its speed in real-time.

Reinforcement Learning for Loop Optimization

Rather than attempting to predict a perfect schedule in a single pass, LoopTune treats optimization as a sequential game. A cursor-based agent "walks" through the loop nest and selects from a discrete set of actions to transform the code structure.

The RL agent iteratively transforms the loop nest using discrete actions, significantly increasing performance (GFLOPS).

Action Space and Navigation

The agent utilizes a manageable action space to navigate and modify the code:

  • Navigation: Moving up or down the loop nest to focus on specific loops.
  • Reordering: Using swap_up or swap_down to change loop nesting order, which is critical for data locality.
  • Tiling: Executing split_n (e.g., split_4, split_16) to break large loops into smaller chunks that fit more efficiently into CPU caches.

State and Reward

To inform its decisions, the agent uses a graph-based state representation that encodes the loop hierarchy and data flow. A key innovation is the inclusion of a histogram of memory access strides, allowing the neural network to learn the relationship between loop order and CPU cache efficiency without a complex hardware simulator. The reward is determined by the actual GFLOPS (speed) achieved on the target hardware.

Evaluation and Key Results

The researchers evaluated LoopTune using a dataset of 2,197 synthesized loop nests for matrix multiplication, with 20% reserved for testing generalization to unseen matrix shapes. The system was trained using the APEX DQN algorithm, which outperformed other RL methods like PPO.

LoopTune demonstrated significant advantages in both execution and compilation speed:

  • Execution Speed: It generated code 3.2x faster than the baseline LoopNest and 10x faster than the base version of TVM.
  • Near-Expert Performance: The system reached within 3% of the performance of NumPy (utilizing Intel’s hand-optimized MKL library) without any manual tuning.
  • Tuning Efficiency: While traditional tools like MetaSchedule and AutoTVM require 33–62 seconds to tune a kernel, LoopTune completes the process in under 1 second.
  • Generalization: The agent successfully optimized matrix sizes it had never encountered during training, proving it learned general optimization principles rather than memorizing specific configurations.

Conclusion and Future Work

LoopTune demonstrates that reinforcement learning can effectively navigate the massive search spaces of compiler optimization. By using discrete actions and a stride-aware state representation, it achieves near-expert performance with unprecedented speed. Future work will focus on extending the framework to handle dynamic tensor shapes, expanding the benchmark to more diverse ML operators like depthwise convolutions, and porting the implementation to GPU architectures.

Summary updated

BibTeX
@misc{grubisic2023looptune,
  author = {Dejan Grubisic and Bram Wasti and Chris Cummins and John M. Mellor-Crummey and Aleksandar Zlateski},
  title = {{LoopTune: Optimizing Tensor Computations with Reinforcement Learning}},
  year = {2023},
  eprint = {2309.01825},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.01825},
}
Figure