How to save and restore partitioned variable in Tensorflow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Partitioned variables are a way to split one logical TensorFlow variable into multiple smaller shards. They are common in older large-model and distributed-training setups, especially for embeddings. Saving and restoring them works reliably, but only if the checkpoint naming and variable construction stay consistent between the save side and the restore side.
What a Partitioned Variable Actually Is
In TensorFlow 1 style code, a partitioned variable is usually created through tf.compat.v1.get_variable with a partitioner. TensorFlow then creates several underlying shard variables while exposing one logical grouped object to your graph-building code.
A simple example:
The important point is that the checkpoint will store shards such as embedding/part_0 and embedding/part_1, not one monolithic tensor entry.
Saving in TensorFlow 1 Style
If you are using graph mode and Saver, the usual pattern is straightforward:
Saver knows how to write the partitioned shards, so you do not need custom save logic for the common case.
Restoring Requires the Same Variable Structure
The restore side must build variables with the same scope names, base variable names, and partitioning scheme. If you change those, TensorFlow no longer knows how the checkpoint shards map back into the graph.
The initializer can differ, because restore overwrites the variable values. The structure cannot differ.
The Most Common Restore Failure
The usual failure is not a broken checkpoint. It is a mismatch such as:
- different variable scope
- different base variable name
- different number of shards
- different shape
For example, saving with 2 shards and restoring with 4 shards is usually not a safe drop-in change. The checkpoint keys no longer line up with the graph in the way Saver expects.
Inspect Checkpoint Contents When Debugging
If restore fails, inspect the checkpoint names directly:
This is one of the fastest ways to confirm what TensorFlow actually saved. You can then compare those names against the graph variables you are creating during restore.
TensorFlow 2 Perspective
In TensorFlow 2, the usual recommendation is to use object-based checkpointing through tf.train.Checkpoint or Keras saving APIs. Partitioned-variable workflows are much more tied to TensorFlow 1 style graph code and large older models.
That means if you are maintaining legacy code, tf.compat.v1 compatibility mode is often the practical answer. If you are starting fresh, design around modern checkpointing rather than older partitioned-variable patterns unless you have a strong reason not to.
A Good Rule for Stable Checkpoints
Treat the save and restore graph definitions as part of the checkpoint contract. If you need to refactor:
- inspect the existing checkpoint names
- preserve scope and variable naming where possible
- only change partitioning layout if you are also planning a controlled checkpoint conversion
That mindset prevents a lot of avoidable restore failures.
Common Pitfalls
- Changing the variable scope or variable name between save and restore.
- Rebuilding the model with a different partitioning scheme and expecting the checkpoint to restore automatically.
- Debugging restore failures without listing checkpoint variable names first.
- Mixing TensorFlow 1 style
Saverassumptions with TensorFlow 2 object-based checkpoint expectations. - Assuming the logical grouped variable name is the only name that matters, while the checkpoint stores shard names too.
Summary
- Partitioned variables are saved as multiple shards under one logical variable concept.
- '
tf.compat.v1.train.Savercan handle them automatically when the graph structure matches.' - Restore succeeds only if scope, variable names, shape, and partitioning layout stay compatible.
- '
tf.train.list_variablesis the quickest debugging tool for checkpoint mismatches.' - For new code, prefer modern TensorFlow 2 checkpointing unless legacy partitioned-variable workflows are required.
Related reading
- How to save final model using keras?
- How To Save Keras Regressor Model?
- How to save TextVectorization to disk in tensorflow?
- How to save the model for text-classification in tensorflow?
- How to save estimator in Tensorflow for later use?
- How to save Keras model as frozen graph?
- How to save load xgboost model?
- How to save obtained model in terms of SSDMetaArch object into .pbtxt or .ckpt
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.