TensorFlow’s Python API serves as the primary interface for model development, bridging high-level Python logic with the high-performance C++ core. This layer handles the conversion of Python code into computational graphs, manages eager execution, and provides a comprehensive suite of mathematical and structural operations.
The Python API operates in two primary modes: Eager Execution and Graph Execution. The transition between these modes is managed by tf.function, which uses a process called "tracing" to generate static graphs from Python functions.
When a Python function is decorated with @tf.function, TensorFlow creates a PolymorphicFunction tensorflow/python/eager/polymorphic_function/polymorphic_function.py24 This object manages a cache of specialized ConcreteFunction instances, each tailored to specific input types and shapes (signatures).
TraceType system to uniquely identify input signatures tensorflow/core/function/trace_type/trace_type_builder.py22FuncGraph, a specialized ops.Graph subclass that tracks captures from the outer scope tensorflow/python/framework/func_graph.py134-161FunctionDef and ready for execution by the runtime tensorflow/python/eager/polymorphic_function/atomic_function.py42For details, see tf.function and Tracing.
TensorFlow provides a vast library of operations (ops) implemented in Python that wrap underlying C++ kernels. These ops are organized into modules like math_ops, array_ops, and nn_ops.
TensorFlow employs a dispatch system to handle various input types (e.g., Tensor, RaggedTensor, SparseTensor). The dispatch decorator allows ops to be overloaded based on the type of their arguments, enabling a unified API for disparate data structures tensorflow/python/framework/ops.py60
Modern TensorFlow uses "V2" control flow, which represents loops and conditionals as functional ops (e.g., While, If) rather than the low-level Switch/Merge nodes used in V1.
cond_v2: Implements conditional logic by tracing "true" and "false" branches into separate FuncGraph objects tensorflow/python/ops/cond_v2.py30while_v2: Implements loops by tracing the condition and body, handling gradient accumulation via functional backpropagation tensorflow/python/ops/while_v2.py58-67For details, see Core Python Ops.
The following diagram maps high-level Python constructs to their corresponding internal code entities.
Sources: tensorflow/python/eager/polymorphic_function/polymorphic_function.py24 tensorflow/python/framework/func_graph.py134 tensorflow/python/eager/pywrap_tfe_src.cc128 tensorflow/python/framework/type_spec.py50
The execution of a tf.function involves several layers of abstraction to move from Python logic to the C++ runtime.
Sources: tensorflow/python/eager/polymorphic_function/tracing_compilation.py147 tensorflow/python/eager/polymorphic_function/atomic_function.py42 tensorflow/python/eager/pywrap_tfe_src.cc143
This section documents the mechanics of polymorphic_function, the trace_type system, and how FuncGraph captures Python state to build a FunctionDef. It also covers the lifecycle of ConcreteFunction and its specialization for different input shapes and types.
This section documents the implementation of standard operations in Python. It covers the dispatch system, the conversion of Python scalars to EagerTensor, and the implementation of functional control flow (V2) ops like while_v2 and cond_v2.
Refresh this wiki