Open-Source Wikis

/

TensorFlow

/

Features

/

SavedModel and Checkpoint

tensorflow/tensorflow

SavedModel and Checkpoint

The two persistence formats. SavedModel captures graphs + variables + signatures; Checkpoint captures only variable state. Implementation lives in tensorflow/python/saved_model/, tensorflow/python/checkpoint/, tensorflow/python/training/, and the C++ loaders in tensorflow/cc/saved_model/.

SavedModel

What it is

A directory containing:

my_model/
├── saved_model.pb           # MetaGraphDef(s)
├── variables/
│   ├── variables.index
│   └── variables.data-00000-of-00001
├── assets/                  # Vocab files, images, etc.
└── fingerprint.pb           # Hash for cache busting

The saved_model.pb is a serialised SavedModel proto wrapping one or more MetaGraphDefs, each carrying the actual graph (GraphDef), the function library, the signatures (named inputs/outputs), and asset references.

Saving

tf.saved_model.save(model, "path", signatures={"serving_default": model.serve_fn})

The save path is in tensorflow/python/saved_model/save.py. It:

  1. Walks the model as a Trackable graph (variables, layers, sub-modules).
  2. Traces every tf.function in the model into ConcreteFunctions.
  3. Writes the resulting MetaGraphDef and serialised function library.
  4. Writes variable values via the same machinery that powers tf.train.Checkpoint.
  5. Writes assets and fingerprint.

Loading

In Python:

loaded = tf.saved_model.load("path")
loaded.serving_default(x)

Implementation in tensorflow/python/saved_model/load.py. It reconstructs Python objects (tf.Modules with __call__ methods backed by the saved ConcreteFunctions).

In C++:

SavedModelBundle bundle;
TF_CHECK_OK(LoadSavedModel(session_options, run_options,
                           "path", {"serve"}, &bundle));
bundle.session->Run(...);

Implementation in tensorflow/cc/saved_model/loader.cc.

Key files

File Role
tensorflow/python/saved_model/save.py Top-level save.
tensorflow/python/saved_model/load.py Top-level load.
tensorflow/python/saved_model/builder_impl.py Older v1 builder (still used internally).
tensorflow/python/saved_model/signature_def_utils.py Signature helpers.
tensorflow/python/saved_model/saved_model.py Public API surface.
tensorflow/cc/saved_model/loader.cc C++ loader.
tensorflow/cc/saved_model/reader.cc Just reads the proto without instantiating.
tensorflow/cc/saved_model/fingerprinting.cc Computes the on-disk fingerprint.

Checkpoint

What it is

A pair of files:

ckpt-12345.index
ckpt-12345.data-00000-of-00001

The index is a small file describing every saved tensor's name, shape, dtype, and offset; the data file holds the raw bytes. The format is implemented in tensorflow/core/util/tensor_bundle/.

Saving and restoring

ckpt = tf.train.Checkpoint(model=model, optimizer=opt)
ckpt.save("ckpt")           # Writes ckpt-1.{index,data-00000-of-00001}
ckpt.restore("ckpt-1").assert_consumed()

Implementation in tensorflow/python/checkpoint/. Key concepts:

  • Trackable (tensorflow/python/trackable/base.py) — base class. Anything trackable exposes _trackable_children().
  • Object-based checkpoints — saves the trackable graph rather than flat name→tensor maps. Restore is robust against renaming.
  • Async checkpointingtensorflow/python/checkpoint/async_checkpoint_helper.py.

Key files

File Role
tensorflow/python/checkpoint/checkpoint.py tf.train.Checkpoint, CheckpointManager.
tensorflow/python/trackable/base.py Trackable base class.
tensorflow/python/training/saving/saveable_object.py Lower-level "saveable object" interface.
tensorflow/python/training/saver.py The legacy v1 tf.compat.v1.train.Saver.
tensorflow/core/util/tensor_bundle/tensor_bundle.{h,cc} On-disk format implementation.

Trackable graph

Every Python object that can be saved subclasses Trackable. The graph of trackable parents/children is what defines the checkpoint contents: when you save tf.train.Checkpoint(root=root), TF traverses root._trackable_children(), then their children, recursively.

tf.Module, tf.keras.layers.Layer, tf.keras.Model, tf.Variable, tf.lookup.StaticHashTable, and tf.data.Iterator are all trackable.

SignatureDef

A SavedModel's signatures describe how to call it from production. Each SignatureDef is {inputs: name→TensorInfo, outputs: name→TensorInfo, method_name: 'predict'/'classify'/...}. TF Serving and other servers read these to dispatch RPC calls.

Integration points

  • Keras (tensorflow/python/keras/saving/) calls tf.saved_model.save under the hood for model.save("path"). Keras 3 uses a slightly different format but the v2 tf.keras here uses SavedModel.
  • TFLite converter typically takes a SavedModel as input.
  • TF Serving loads SavedModels via the C++ loader and exposes their signatures over gRPC/REST.
  • tf.distribute participates in checkpointing — variables that live on multiple devices serialise their primary copy.

Entry points for modification

  • New trackable subclass — subclass Trackable, implement _trackable_children and _serialize_to_tensors.
  • New SavedModel pass — tensorflow/python/saved_model/save.py is the orchestrator.
  • Checkpoint format changes — tensorflow/core/util/tensor_bundle/ (rare; the format is stable).

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

SavedModel and Checkpoint – TensorFlow wiki | Factory