Terra is a "best of both worlds" execution system for deep learning that combines the flexibility of imperative programming (like PyTorch) with the high performance of symbolic graphs (like TensorFlow 1.x). By running a "skeleton" of the original Python code alongside an optimized symbolic graph, Terra achieves significant speedups while supporting complex Python features that break existing state-of-the-art systems.
The Dilemma: Imperative vs. Symbolic Execution
In the world of deep learning (DL), developers usually choose between two execution models:
- Imperative (Define-by-Run): This model is easy to debug and allows full use of Python (e.g., PyTorch). However, it is slower because the Python interpreter must manage every operation individually, missing out on global graph optimizations.
- Symbolic (Define-and-Run): This model is extremely fast because the entire computation is pre-built into a graph that can be optimized through techniques like operator fusion. However, it is restrictive; it often fails when the code uses "non-symbolic" Python features like third-party libraries, complex object mutations, or certain dynamic control flows.
Existing bridge systems, such as TensorFlow’s AutoGraph or TorchScript, attempt to convert Python code into symbolic graphs. Unfortunately, they often hit a wall: if your code contains a feature they don't recognize (like a call to a custom C++ library or a try-except block), they either crash or produce incorrect results.
Terra's Solution: Co-Execution
To overcome these limitations, the authors propose a novel approach called Imperative-Symbolic Co-Execution. Instead of trying to replace the Python code entirely with a graph, Terra runs them simultaneously.
Terra's execution pipeline involves several key phases:
- Tracing Phase: Terra initially runs the program imperatively. As it runs, it records the sequence of DL operations. It merges these sequences into a TraceGraph—a directed acyclic graph that captures different possible execution paths.
- Skeleton Program: Terra generates a "skeleton" version of the user's Python code. This skeleton handles all the control logic (loops, conditionals, third-party calls) but delegates the heavy DL math to the symbolic graph.
- Co-Execution Phase: The Python interpreter runs the skeleton while a separate graph engine runs the optimized DL operations in parallel.
- Communication Points: Terra inserts "feed" and "fetch" points. If the Python logic needs a value from a DL operation (like a loss value to print), it fetches it from the graph. If the graph needs a value from Python (like a hyperparameter), Python feeds it in.
- Dynamic Fallback: If the program takes a path not yet recorded in the TraceGraph, Terra detects the "new trace," stops the graph, falls back to imperative mode to learn the new path, and then re-generates the graph.
Key Insights
The core "aha moment" of Terra is realizing that you don't need to turn everything into a graph. By keeping the Python interpreter in the loop to handle the "glue" logic, you maintain 100% compatibility with the Python ecosystem. Furthermore, by running the Python skeleton and the symbolic graph in parallel, Terra successfully hides the overhead of the Python interpreter behind the much longer execution time of the GPU kernels. Unlike single-path tracers that break if a condition changes, Terra’s TraceGraph acts as an evolving map of the program's behavior, getting smarter with each new iteration.
Evaluation and Results
The paper evaluates Terra using ten diverse DL programs representing various architectures and complexities, including Vision models (ResNet50, YOLOv3, DCGAN, FasterRCNN), NLP models (BERT-Q&A, BERT-CLS, GPT2), and specialized layers (DropBlock, SDPoint, MusicTransformer). These models were chosen specifically because they utilize features that typically challenge symbolic compilers.
- Performance: Terra consistently outperformed standard imperative execution, reaching speedups of up to 1.73x.
- Robustness: Terra successfully executed all 10 test programs. In contrast, AutoGraph failed on 5 of them due to unsupported Python features like object mutation and third-party library calls.
- Parity with Symbolic: For the programs that AutoGraph could handle, Terra’s performance was nearly identical, proving that the "co-execution" overhead is negligible compared to the gains from graph optimization.
Limitations and Future Work
While highly effective, Terra does have a few limitations. Because it needs to "trace" the program for a few iterations to build the TraceGraph, there is a slight initial warm-up delay before the speedup kicks in. Additionally, while Terra can use XLA (a linear algebra compiler) for further speedups, it inherits XLA's limitations regarding dynamic input shapes.
Future directions for this research include extending the co-execution model to other frameworks beyond TensorFlow and exploring more advanced ways to optimize the communication between the Python runner and the graph executor.
Summary updated