[Paper Review] LazyTensor: combining eager execution with domain-specific compilers
LazyTensor enables full use of a host language's expressivity in define-by-run ML frameworks while compiling tensor operations via domain-specific compilers (e.g., XLA), avoiding the language subset problem. It achieves this by representing operations as lazy, composable IR graphs that are compiled only when needed, enabling high performance on TPUs and GPUs across PyTorch and Swift for TensorFlow with shared implementation across languages and hardware.
Domain-specific optimizing compilers have demonstrated significant performance and portability benefits, but require programs to be represented in their specialized IRs. Existing frontends to these compilers suffer from the "language subset problem" where some host language features are unsupported in the subset of the user's program that interacts with the domain-specific compiler. By contrast, define-by-run ML frameworks-colloquially called "eager" mode-are popular due to their ease of use and expressivity, where the full power of the host programming language can be used. LazyTensor is a technique to target domain specific compilers without sacrificing define-by-run ergonomics. Initially developed to support PyTorch on Cloud TPUs, the technique, along with a substantially shared implementation, has been used by Swift for TensorFlow across CPUs, GPUs, and TPUs, demonstrating the generality of the approach across (1) Tensor implementations, (2) hardware accelerators, and (3) programming languages.
Motivation & Objective
- To address the language subset problem in domain-specific compilers, where user programs must conform to limited IR expressivity.
- To enable define-by-run ML frameworks to leverage high-performance, hardware-optimized compilers without sacrificing debugging or language flexibility.
- To build a reusable, cross-language implementation that supports multiple hardware accelerators (CPUs, GPUs, TPUs) and tensor types.
- To demonstrate that lazy compilation can achieve significant performance gains on production ML workloads, such as HuggingFace Transformers on Cloud TPUs.
Proposed method
- Represent tensor operations as lazy, composable IR graphs that are constructed during eager execution but compiled only when needed.
- Use a shared implementation of the IR graph construction and compilation pipeline across different programming languages (Python, Swift) and frameworks (PyTorch, Swift for TensorFlow).
- Integrate with domain-specific compilers like XLA by translating the lazy IR into the compiler’s intermediate representation (e.g., HLO) just-in-time.
- Leverage caching to avoid recompilation when the same IR graph is encountered again, reducing overhead.
- Support mixed workloads by allowing non-optimized code (e.g., control flow, dynamic shapes) to coexist with optimized tensor operations through explicit barriers like LazyTensorBarrier().
- Enable asynchronous execution by treating Tensor objects as promises that resolve only when their computation is completed.
Experimental results
Research questions
- RQ1Can a define-by-run ML framework retain full host language expressivity while still enabling compilation via a domain-specific compiler?
- RQ2How can the overhead of IR graph construction be minimized to support small-batch or low-latency inference workloads?
- RQ3To what extent can a single implementation of a lazy compilation system be reused across different programming languages and hardware accelerators?
- RQ4Can lazy compilation achieve performance comparable to or better than full tracing or graph-based systems on real-world ML workloads?
Key findings
- LazyTensor successfully enables full use of Python and Swift features in tensor programs while compiling to XLA, avoiding the language subset problem inherent in tracing-based systems.
- The same core implementation was reused across PyTorch and Swift for TensorFlow, demonstrating portability across languages and frameworks.
- Performance evaluations show significant speedups on Cloud TPUs using the HuggingFace Transformers library, demonstrating real-world applicability.
- The system supports mixed workloads with non-optimized code and optimized tensor operations, using LazyTensorBarrier() to manage execution boundaries.
- Graph construction overhead is manageable for most training workloads but can be further reduced via IR representation optimizations and caching.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.