NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Dynamic Control Flow in Large-Scale Machine Learning

Yuan Yu, Martín Abadi, Paul Barham, Eugene Brevdo, Mike Burrows, Andy Davis, Jeff Dean, Sanjay Ghemawat, Tim Harley, Peter Hawkins, Michael Isard, Manjunath Kudlur, Rajat Monga, Derek Gordon Murray, Xiaoqiang Zheng

Abstract

This paper presents the design and implementation of dynamic control flow—including loops and conditionals—within TensorFlow’s distributed dataflow architecture. By embedding control logic "in-graph," the authors enable complex models like Recurrent Neural Networks (RNNs) and Reinforcement Learning agents to execute efficiently across heterogeneous clusters. The system supports automatic differentiation through dynamic constructs, non-strict semantics for increased parallelism, and advanced memory management to handle large-scale workloads.

Introduction

Traditional machine learning frameworks were primarily designed for static computation graphs, where a fixed sequence of operations is optimized for performance. However, modern AI increasingly relies on dynamic control flow. Recurrent Neural Networks (RNNs) require loops that adapt to variable input lengths, while Reinforcement Learning (RL) and Mixture of Experts (MoE) architectures depend on data-driven branching (if/then) decisions.

The challenge lies in supporting these constructs in a distributed, heterogeneous environment (CPUs, GPUs, TPUs). If control logic remains in a host language like Python (an "out-of-graph" approach), the system suffers from significant latency and synchronization bottlenecks. This paper addresses these issues by embedding control decisions directly into the distributed dataflow graph.

Methods: In-Graph Control Flow

The authors implement an "in-graph" approach using a small set of functional primitives inspired by classic dynamic dataflow machines. These primitives allow the representation of complex control logic as part of the execution graph itself.

Core primitives for dynamic control flow: Switch, Merge, Enter, Exit, and NextIteration.

As shown in the figure above, the system utilizes:

  • Switch & Merge: For conditional branching and joining data paths.
  • Enter, NextIteration, & Exit: For defining and managing iterative loop boundaries.

By combining these primitives, the system can construct sophisticated, distributed loops. Instead of a central coordinator, "control-loop state machines" are deployed on each device, allowing partitions of a loop to progress independently and asynchronously.

The graph structure of a distributed while loop using in-graph primitives.

Key Innovations

  1. Automatic Differentiation (Autodiff): The authors developed a method to perform backpropagation through dynamic constructs. The gradient of a loop is itself a loop that runs in reverse, using a stack mechanism to store intermediate values from the forward pass.
  2. Non-strict Semantics: The system allows multiple iterations of a loop to execute in parallel if there are no data dependencies, significantly increasing hardware utilization.
  3. Memory Swapping: To handle sequences that exceed GPU memory, the system automatically swaps tensors to CPU memory and prefetches them just before they are needed in the backward pass, hiding I/O latency behind computation.

Evaluation and Results

The system was evaluated using synthetic microbenchmarks, Neural Machine Translation (NMT) tasks, and Reinforcement Learning (DQN) workloads. Additionally, the authors analyzed over 11.7 million unique TensorFlow graphs from internal Google workloads to validate the prevalence and necessity of control flow in real-world applications.

  • Performance: In-graph loops achieved a 5x speedup in iterations per second compared to out-of-graph loops on an 8-GPU machine. For DQN applications, the approach yielded a 21% speedup.
  • Scalability: The system demonstrated efficient scaling up to 64 machines, with distributed execution overhead remaining acceptable even for very small computations.
  • Memory Efficiency: With memory swapping enabled, the system successfully trained on sequences of length 1000 on a single GPU, whereas it would otherwise fail (Out of Memory) at length 500.

Conclusion

By integrating dynamic control flow directly into the distributed runtime, this work overcomes the limitations of static computation graphs. The "in-graph" approach provides a scalable, efficient foundation for the next generation of complex AI models, ensuring that sophisticated logic like loops and conditionals can be executed with minimal overhead across large-scale heterogeneous clusters.

Summary updated

BibTeX
@inproceedings{yu2018dynamic,
  author = {Yuan Yu and Martín Abadi and Paul Barham and Eugene Brevdo and Mike Burrows and Andy Davis and Jeff Dean and Sanjay Ghemawat and Tim Harley and Peter Hawkins and Michael Isard and Manjunath Kudlur and Rajat Monga and Derek Gordon Murray and Xiaoqiang Zheng},
  title = {{Dynamic Control Flow in Large-Scale Machine Learning}},
  booktitle = {EuroSys},
  year = {2018},
  doi = {10.1145/3190508.3190551},
}
Figure