You are correct tf.function builds a graph, tf.scan and tf.while_loop are native TensorFlow operations that will be included in the graph so you can use them within tf.function. Here’s an internal discussion on TF control flow: Inside TensorFlow: Control Flow - YouTube 1