Skip to main content
QUICK REVIEW

[Paper Review] AutoGraph: Imperative-style Coding with Graph-based Performance

Moldovan, Dan, Decker, James M|arXiv (Cornell University)|Oct 16, 2018
DNA and Biological ComputingBiochemistry, Genetics and Molecular Biology14 references10 citations
TL;DR

AutoGraph enables imperative-style Python coding with graph-based performance by using source code transformation and staged programming to convert idiomatic Python into optimized, deployable IRs like TensorFlow Graph or Lantern. It achieves native TensorFlow performance with no runtime overhead while supporting complex control flow and backend-agnostic compilation.

ABSTRACT

There is a perceived trade-off between machine learning code that is easy to write, and machine learning code that is scalable or fast to execute. In machine learning, imperative style libraries like Autograd and PyTorch are easy to write, but suffer from high interpretive overhead and are not easily deployable in production or mobile settings. Graph-based libraries like TensorFlow and Theano benefit from whole-program optimization and can be deployed broadly, but make expressing complex models more cumbersome. We describe how the use of staged programming in Python, via source code transformation, offers a midpoint between these two library design patterns, capturing the benefits of both. A key insight is to delay all type-dependent decisions until runtime, via dynamic dispatch. We instantiate these principles in AutoGraph, a software system that improves the programming experience of the TensorFlow library, and demonstrate usability improvements with no loss in performance compared to native TensorFlow graphs. We also show that our system is backend agnostic, and demonstrate targeting an alternate IR with characteristics not found in TensorFlow graphs.

Motivation & Objective

  • To bridge the gap between imperative ML frameworks (easy to code) and graph-based systems (high performance, portable).
  • To enable developers to write idiomatic Python with control flow (e.g., loops, conditionals) while compiling to optimized, statically-typed IRs.
  • To eliminate the performance and portability trade-offs inherent in existing imperative and graph-based ML frameworks.
  • To support backend-agnostic compilation by targeting multiple intermediate representations (IRs), including TensorFlow Graph and the more expressive Lantern IR.
  • To maintain semantic correctness during code transformation while minimizing developer burden through automatic type dispatch and minimal annotations.

Proposed method

  • Uses source code transformation (SCT) in Python to analyze and rewrite imperative code into a lower-level intermediate representation (IR).
  • Employs staged programming with dynamic dispatch on runtime type information to delay type-dependent decisions until execution time.
  • Applies static analysis to detect control flow constructs (e.g., loops, conditionals) and rewrites them into equivalent graph-based operations in the target IR.
  • Supports backend-agnostic compilation by generating code for different IRs, such as TensorFlow Graph and the more expressive Lantern IR.
  • Relies on Python introspection and reflection (e.g., inspect, imp) to analyze function structure and perform transformation at runtime or compile time.
  • Minimizes required annotations by inferring types and control flow, only requiring explicit hints when the IR’s type system demands it.

Experimental results

Research questions

  • RQ1Can imperative-style Python code with data-dependent control flow be automatically transformed into an optimized, statically-typed IR without performance loss?
  • RQ2To what extent can source code transformation and staged programming preserve semantics while enabling high-performance execution in ML frameworks?
  • RQ3Can a single system target multiple IRs, including those with richer features (e.g., re-entrant function calls) than TensorFlow Graph?
  • RQ4How does the system handle type mismatches and dynamic dispatch in a way that avoids silent performance degradation?
  • RQ5Can the transformation pipeline be made robust and correct without requiring extensive manual annotations?

Key findings

  • AutoGraph successfully converts idiomatic Python code with complex control flow into optimized TensorFlow Graph IR with no performance degradation compared to hand-written graphs.
  • The system achieves native TensorFlow performance, demonstrating that imperative coding style does not incur runtime overhead when compiled via AutoGraph.
  • AutoGraph generalizes beyond TensorFlow by targeting the Lantern IR, which supports advanced features like re-entrant function calls not expressible in TensorFlow Graph.
  • The transformation process is largely automatic, requiring no annotations in most cases, and only minimal hints where the target IR’s type system demands them.
  • AutoGraph is production-ready and integrated into TensorFlow 2.0 via tf.function, benefiting from the full test suite of the TensorFlow codebase.
  • Despite its robustness, the system may fail silently if users pass Python booleans instead of TensorFlow booleans to conditionals, highlighting a need for improved debugging support.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.