This document covers TensorFlow's eager execution system in Python, which allows operations to be executed immediately without building a computational graph. It details the EagerTensor Python bindings, NumPy integration, data conversion pipeline, and the mechanisms for executing TensorFlow operations in eager mode.
For information about Go language bindings, see page 4.2. For cross-language data conversion details, see page 4.3. For tf.function graph tracing that builds on eager execution, see page 9.
TensorFlow's eager execution allows operations to execute immediately, returning concrete values instead of building a computational graph. The system consists of Python bindings, C++ execution infrastructure, and data conversion mechanisms.
Eager Execution Data Flow
Sources: tensorflow/python/eager/pywrap_tensor.cc1-100 tensorflow/python/eager/pywrap_tfe_src.cc120-150 tensorflow/core/common_runtime/eager/execute.cc150-200 tensorflow/python/eager/context.py1-100
The EagerTensor type is the Python representation of a tensor during eager execution. It is implemented as a C extension for performance.
The EagerTensor C++ struct provides the Python-facing representation. It bridges the Python PyObject world with the C++ TFE_TensorHandle.
| Field | Type | Purpose |
|---|---|---|
handle | TFE_TensorHandle* | Pointer to the underlying C API tensor handle. |
context | TFE_Context* | The eager context in which this tensor lives. |
id | int64_t | Unique identifier for the tensor, used for caching and tracing. |
Sources: tensorflow/python/eager/pywrap_tensor.h1-50 tensorflow/python/eager/pywrap_tensor.cc1-100
EagerTensor Object Lifecycle
Sources: tensorflow/python/eager/pywrap_tensor.cc1-200 tensorflow/core/common_runtime/eager/context.cc1-100 tensorflow/c/eager/c_api.cc1-100
Executing an operation in eager mode involves the quick_execute function, which dispatches the operation to the C++ runtime.
When a Python op (e.g., tf.add) is called in eager mode:
execute.quick_execute in tensorflow/python/eager/execute.py28-67TFE_Py_Execute in tensorflow/python/eager/pywrap_tfe_src.cc1-100TFE_Execute in tensorflow/core/common_runtime/eager/execute.cc135-170Eager execution supports callbacks for debugging and profiling. execute_with_callbacks allows intercepting op execution. Monitoring gauges, such as those in tensorflow/python/framework/ops.py104-115 track API usage.
Sources: tensorflow/python/eager/execute.py28-67 tensorflow/python/eager/pywrap_tfe_src.cc1-100 tensorflow/core/common_runtime/eager/execute.cc135-170 tensorflow/python/framework/ops.py104-115
TensorFlow provides tight integration with NumPy, allowing EagerTensor to be used in place of NumPy arrays and vice versa.
The conversion pipeline attempts to share memory between NumPy and TensorFlow whenever possible.
NdarrayToTensor in ndarray_tensor.cc handles the buffer sharing logic.EagerTensor object implements the __array__ protocol to allow NumPy to consume it.For non-NumPy types (like Python lists), the system uses PySeqToTFE_TensorHandle in py_seq_tensor.cc to infer shapes and types recursively.
Data Conversion Pipeline
Sources: tensorflow/python/lib/core/ndarray_tensor.cc1-100 tensorflow/python/lib/core/py_seq_tensor.cc1-100 tensorflow/python/eager/pywrap_tensor_conversion.cc1-50
The Context class in tensorflow/python/eager/context.py218-300 manages the global state for eager execution, including device placement policies, executor configuration (sync vs. async), and function libraries.
DEVICE_PLACEMENT_EXPLICIT in tensorflow/python/eager/context.py69-74).SYNC and ASYNC constants tensorflow/python/eager/context.py76-77Sources: tensorflow/python/eager/context.py218-300 tensorflow/python/eager/context.py69-77 tensorflow/python/eager/context.py104-127
While eager execution is the default, tf.function allows for performance optimizations by tracing Python code into a graph.
tf.function is first called, it runs in eager mode but records operations into a FuncGraph.ConcreteFunction tensorflow/python/eager/function.py26Sources: tensorflow/python/eager/function.py21-38 tensorflow/python/framework/ops.py112-115
Refresh this wiki