TensorFlow starts with a Python API, but the selected execution mode changes what runs next. Eager mode dispatches operations individually. tf.function traces TensorFlow operations into a reusable graph. jit_compile=True additionally asks XLA to compile the function. Device placement then determines CPU or GPU execution; using a graph does not itself mean XLA or guarantee GPU-only work.
Showing eager execution on the CPU: 5 stages, 4 steps.
Your program
Runtime
Framework core
Kernel
Compiler
Intermediate representation
Hardware
The selected path, step by step
Your Python code calls TensorFlow TensorFlow API: The program enters TensorFlow through its Python API.
TensorFlow API dispatches each op Eager execution: Eager execution evaluates operations as Python calls them.
Eager execution selects CPU kernels CPU op kernels: Device placement chooses the implementations for the supported operations shown here.
CPU op kernels executes on CPU: Numerical work reaches the selected physical processor.
Does TensorFlow execute Python on the GPU?
Python supplies the program and launches TensorFlow operations. Numerical kernels execute on the selected device; a Python loop, a print statement and a matrix multiplication are not interchangeable units of GPU work. The device-placement guide explains how to log where operations actually run. A visible GPU is a prerequisite, not proof that the complete program uses it.
The CPU selection here shows CPU numerical work. The GPU selection shows supported NVIDIA device operations while leaving host orchestration implicit. Operations without an applicable GPU implementation may run on the CPU. Inspect placement and data movement before interpreting a low GPU utilization reading as a compiler defect.
What is the difference between eager mode and tf.function?
Eager mode is useful for inspecting values because Python dispatches each operation immediately. Wrapping a compatible function with tf.function produces a callable backed by a traced graph. Repeated compatible calls can reuse that graph instead of rebuilding it operation by operation. The graph introduction describes the difference and why graphs can be exported without the original Python program.
Tracing is not the same as running every Python statement again on every invocation. Python-side effects and changing argument signatures can behave differently from ordinary eager code. Retracing can add startup work; it is separate from the steady-state cost of executing an already constructed graph.
Is tf.function the same as XLA?
No. The graph-only choice in this page uses tf.function with jit_compile=False. The explicit-XLA choice adds compiler optimization through jit_compile=True. The API contract states that an explicitly compiled function must be supported by XLA or an error is raised. Do not assume an unsupported function will silently fall back to eager execution.
XLA can optimize and combine work for supported targets. It is also used by JAX, but sharing a compiler does not make the frameworks identical: their front ends, tracing rules and user APIs differ. See the OpenXLA overview for the supported compiler role.
Where does Keras fit?
A Keras model using the TensorFlow backend enters this stack through TensorFlow operations. Keras 3 can also select another backend, so a Keras import alone does not identify this path. This page explains TensorFlow execution rather than prescribing one framework for every workload.
How should I interpret speed differences?
Compare equivalent computations after separating initialization, tracing or compilation from repeated execution. A graph or XLA can reduce some overhead, but neither is a universal speed guarantee. Tensor sizes, supported operations, device placement and transfers all affect the resulting path. This diagram records mechanisms rather than benchmark rankings.
Every stage
Your Python code
programming language
Python invokes TensorFlow operations or calls a function previously wrapped with tf.function.
TensorFlow 2 numerical computation on one CPU or a configured NVIDIA GPU. The graph-only option explicitly disables XLA; the XLA option assumes successful compilation. TPU, distributed training, TensorFlow Lite, browser TensorFlow.js, custom ops and detailed optimizer/gradient execution are outside this diagram. GPU branches show supported device work; host control and CPU-only ops may coexist.
How this page is checked
Every stage and step above quotes the source that supports it. The quotes were checked against the TensorFlow 2 API, TensorFlow graph and device-placement guides, and the OpenXLA compiler overview on 2026-11-11. The data is a versioned JSON file under the same CC BY 4.0 license as the dataset. Found something wrong or out of date? Open an issue with the better source.