Inferensys

Glossary

TVM Auto-Scheduler (Ansor)

TVM Auto-Scheduler (Ansor) is a machine learning-based compiler system that automatically generates high-performance tensor programs for deep learning workloads by exploring a vast search space of possible loop transformations and schedules.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
HARDWARE-AWARE MODEL OPTIMIZATION

What is TVM Auto-Scheduler (Ansor)?

A machine learning-based compiler system for automatically generating high-performance tensor programs.

The TVM Auto-Scheduler (Ansor) is a machine learning-based compiler system that automatically generates high-performance tensor programs for deep learning workloads by exploring a vast search space of possible loop transformations, parallelization strategies, and memory layouts. It replaces the need for manual, expert-written schedule templates by using a cost model trained on the target hardware to guide a hierarchical search, discovering novel optimizations tailored to specific NPU or GPU architectures. This process is a core technique in hardware-aware model optimization.

The system operates by first generating a diverse set of initial program sketches that outline high-level structures like loop tiling and fusion. It then uses evolutionary search and a learned performance cost model to iteratively refine these sketches by populating detailed parameters like unrolling factors and thread bindings. This automated graph compilation strategy directly optimizes for hardware-specific metrics like latency, making it a foundational tool for deployment and runtime optimization on diverse accelerators without requiring deep hardware expertise from the model developer.

COMPILER OPTIMIZATION

Key Features of TVM Auto-Scheduler

The TVM Auto-Scheduler (Ansor) is a machine learning-based compiler system that automatically generates high-performance tensor programs by exploring a vast search space of possible loop transformations and schedules.

01

Cost Model-Guided Search

The core of Ansor is a learned cost model that predicts the execution latency of a candidate program without running it on hardware. This model is trained on measured performance data from sampled programs. The search algorithm uses this model to prioritize exploration of the most promising regions of the schedule space, which is astronomically large. This approach is far more efficient than exhaustive search or template-based autotuning.

  • The cost model is typically a gradient-boosted tree (e.g., XGBoost) or a neural network.
  • It evaluates features extracted from the program's loop structure, memory access patterns, and hardware characteristics.
  • This guidance allows Ansor to discover novel, high-performance schedules that human experts might overlook.
02

Hierarchical Task Scheduler

Ansor decomposes the optimization problem using a hierarchical representation of the computational graph. It does not schedule low-level loops directly from the entire model. Instead, it:

  1. Sketches High-Level Structures: Generates possible high-level loop structures (sketches) for each computational task (operator), such as whether to tile, vectorize, or unroll loops.
  2. Explores Low-Level Parameters: For each sketch, it searches for optimal low-level parameters like tile sizes, unroll factors, and parallelization strategies.

This hierarchy makes the search tractable by first deciding on major optimization decisions before fine-tuning the numerical parameters.

03

Automated Program Sampling

To train its cost model, Ansor must generate a diverse set of training programs. It uses automated program sampling to create this dataset.

  • Random Annotation: It starts by randomly applying a set of schedule primitives (like split, reorder, tile) to the initial computational graph.
  • Evolutionary Search: It then uses an evolutionary algorithm to mutate and recombine high-performing sampled programs to generate new, potentially better candidates.
  • This process continuously generates new programs, measures their actual hardware performance, and uses the results to refine the cost model in an iterative feedback loop.
04

Hardware-Aware Optimization

Ansor's search is fundamentally guided by the target hardware's constraints and capabilities. It automatically discovers schedules optimized for specific memory hierarchies, cache sizes, vector units, and parallel compute units.

  • It learns hardware-specific patterns, such as optimal tile sizes for different levels of cache (L1, L2, shared memory).
  • It automatically applies hardware-specific optimizations like vectorization for SIMD units and thread binding for multi-core CPUs or GPU streaming multiprocessors.
  • This eliminates the need for manual, platform-specific tuning, enabling a single model description to achieve near-peak performance across diverse backends (e.g., ARM CPU, NVIDIA GPU, x86).
05

Integration with TVM Stack

Ansor is not a standalone tool; it is deeply integrated into the TVM compiler stack. It operates on TVM's intermediate representation (IR), specifically the Tensor Expression (TE) language.

  • Input: Takes a computational graph defined via TVM's Relay frontend or directly as TE expressions.
  • Output: Produces an optimized schedule object, which is then lowered by TVM to generate hardware-specific code (LLVM, CUDA, OpenCL, etc.).
  • This integration allows Ansor to be used as a drop-in replacement for manual scheduling within existing TVM-based deployment pipelines, providing automatic performance gains.
06

Comparison to AutoTVM

Ansor was developed to address limitations in TVM's earlier autotuning system, AutoTVM. Key differentiators are:

  • Template-Free: AutoTVM required manual definition of schedule templates for each operator, which limited the search space. Ansor requires no templates; it explores the space of all valid schedules.
  • End-to-End Graph Optimization: AutoTVM tuned operators in isolation. Ansor performs cross-operator optimizations, like fusion, across the entire computational graph during its search.
  • Superior Search Efficiency: The learned cost model and hierarchical search in Ansor typically find better-performing schedules faster than AutoTVM's template-guided search and simpler cost models.
COMPARISON

TVM Auto-Scheduler vs. Manual Optimization

A comparison of the automated Ansor scheduler against traditional, expert-led manual schedule optimization for tensor program compilation.

Optimization DimensionTVM Auto-Scheduler (Ansor)Manual Schedule Optimization

Primary Objective

Automatically find a high-performance schedule by exploring a vast search space of possible loop transformations.

Leverage expert knowledge to hand-craft a schedule optimized for a specific model and hardware target.

Required Expertise

Minimal. Requires defining a search space and cost model; the system handles exploration.

Extensive. Requires deep knowledge of target hardware architecture, memory hierarchy, and parallel programming.

Development Time

Hours to days for the search process to converge, largely unattended.

Days to weeks of iterative coding, profiling, and debugging by an expert.

Portability Across Hardware

High. The same search process can target different backends (CPU, GPU, NPU) by adjusting the cost model.

Low. A schedule is typically hand-tuned for a specific hardware generation and microarchitecture.

Performance Ceiling (Theoretical)

Can discover novel, non-intuitive optimizations beyond human design patterns.

Bounded by the optimizer's creativity, experience, and available time.

Performance Consistency

May produce variable results between search runs; final performance depends on search time/budget.

Deterministic and reproducible once the optimal manual schedule is finalized.

Maintenance Overhead

Low. Re-running the search adapts to new model variants or compiler/hardware updates.

High. Manual schedules often require significant rework for new model ops, shapes, or hardware.

Key Enabling Technology

Machine learning-guided search (cost models, reinforcement learning, evolutionary algorithms).

Expert analysis, roofline modeling, and empirical profiling (e.g., using nsight, vtune).

Optimal Use Case

Rapid prototyping, supporting diverse model/hardware combinations, or when compiler expertise is scarce.

Pushing the absolute performance limits for a critical, fixed model on stable, production hardware.

APPLICATION DOMAINS

Where TVM Auto-Scheduler is Used

The TVM Auto-Scheduler (Ansor) is deployed to automate the most challenging performance optimization tasks across diverse hardware targets. Its primary value lies in generating high-performance tensor programs without requiring manual, expert-written schedules.

03

Research & Novel Hardware Prototyping

Researchers use Ansor as a performance evaluation tool for new AI accelerators and exotic architectures. Its ability to automatically explore the schedule space eliminates the need for a mature manual kernel library, allowing for:

  • Rapid architectural exploration by measuring the performance potential of new tensor instructions or memory hierarchies.
  • Benchmarking novel operators (e.g., from cutting-edge research papers) without months of manual optimization effort.
  • Serving as the default optimization backend in ML compilers for academic hardware simulators and FPGA prototypes.
05

Optimizing Emerging Model Architectures

Ansor excels where no pre-optimized kernels exist, making it indispensable for newly published model architectures. Examples include:

  • Vision Transformers (ViTs) and their variants with custom attention mechanisms.
  • Diffusion models with unique sampling schedules and operator compositions.
  • Graph Neural Networks (GNNs) with sparse, irregular computation patterns that are poorly served by standard GEMM libraries.
  • Custom, domain-specific layers developed in-house by AI research teams.
06

Integrating with Higher-Level AutoML

Ansor acts as the low-level optimization engine within larger Neural Architecture Search (NAS) or hyperparameter tuning frameworks. In this role:

  • A Hardware-Aware NAS algorithm proposes a candidate model architecture.
  • Ansor is invoked to rapidly estimate the latency of that model on the target hardware by generating and benchmarking a schedule.
  • This feedback loop allows NAS to directly optimize for inference speed or energy consumption, not just accuracy. Systems like Once-For-All (OFA) can use Ansor to profile the performance of different sub-networks.
TVM AUTO-SCHEDULER (ANSOR)

Frequently Asked Questions

The TVM Auto-Scheduler, also known as Ansor, is a machine learning-based compiler system that automatically generates high-performance tensor programs for deep learning workloads. This FAQ addresses its core mechanisms, relationship to other optimization techniques, and practical applications.

The TVM Auto-Scheduler (Ansor) is a machine learning-based compiler optimization system that automatically generates high-performance tensor programs (schedules) for deep learning workloads on diverse hardware backends. It works by defining a vast, hierarchical search space of possible program transformations—including loop tiling, reordering, parallelization, and unrolling—and then using a cost model guided by reinforcement learning and evolutionary search to iteratively sample, measure, and refine candidate schedules on the actual target hardware. Unlike manual tuning or template-based approaches, Ansor explores novel, hardware-optimal schedules without requiring prior human expertise for each new operator or accelerator.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.