DEV Community

Cover image for torch.compile vs torch.export
제민욱
제민욱

Posted on • Edited on

torch.compile vs torch.export

based on pytorch 2.6

torch.compile() and torch.export both leverage the PT2 stack, but serve different purposes.

Image description

torch.compile

torch.compile() is a JIT compiler that falls back to the Python runtime for untraceable parts, offering flexibility.

torch.export

In contrast, torch.export is an AOT compiler that captures the full graph, errors on untraceable code, and produces portable, low-level (ATen) graphs ideal for deployment.

Internally

  • torch.compile(): Uses TorchDynamo for tracing and AOT Autograd for optimization, with torch.fx as the graph backbone, enabling runtime flexibility.
  • torch.export(): Relies on TorchDynamo for broad bytecode tracing, AOT Autograd for ATen lowering, and torch.fx for graph representation, ensuring a standalone graph.

Comparison Table

Feature torch.compile() torch.export()
Compilation JIT AOT
Graph Capture Partial (w/ breaks) Full (errors if untraceable)
Untraceable Handling Python runtime fallback Requires rewrite/info
Graph Output Flexible, runtime-tied Portable, ATen-level
Python Support High (via fallback) Broad (bytecode tracing)

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay