TensorFlow
SavedModel
Machine Learning
Operations
Tutorial

How to list all used operations in Tensorflow SavedModel?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Listing operations inside a TensorFlow SavedModel is useful for deployment checks, converter compatibility, and regression debugging. Many runtimes support only a subset of TensorFlow ops, so knowing exact op usage early prevents late-stage failures. You can inspect ops from command line tools or programmatically with TensorFlow APIs.

Quick Inspection with saved_model_cli

For a fast structural view, use the built-in CLI.

bash
saved_model_cli show --dir ./my_saved_model --all

This prints signatures, input-output tensors, and graph details. It is a good first diagnostic step in CI and release reviews.

Programmatic Operation Listing in Python

For automation, load the model and inspect operations from each concrete signature graph.

python
1import tensorflow as tf
2
3model = tf.saved_model.load("./my_saved_model")
4
5for sig_name, fn in model.signatures.items():
6    ops = fn.graph.get_operations()
7    print("signature:", sig_name)
8    print("op_count:", len(ops))
9    print("sample:", [op.type for op in ops[:10]])

This gives signature-specific operation usage instead of one flattened list.

Build a Unique Operation Inventory

For compatibility auditing, you usually need distinct operation types across all signatures.

python
1import json
2import tensorflow as tf
3
4model = tf.saved_model.load("./my_saved_model")
5unique_ops = set()
6
7for fn in model.signatures.values():
8    unique_ops.update(op.type for op in fn.graph.get_operations())
9
10sorted_ops = sorted(unique_ops)
11print("unique operation count:", len(sorted_ops))
12
13with open("savedmodel_ops.json", "w", encoding="utf-8") as f:
14    json.dump(sorted_ops, f, indent=2)

Persisting this list makes model-to-model diffs easy.

Compare Operation Sets Across Versions

To catch unexpected graph changes after retraining or exporter updates, compare op inventories between two model versions.

python
1import tensorflow as tf
2
3
4def collect_ops(path: str) -> set[str]:
5    m = tf.saved_model.load(path)
6    ops = set()
7    for fn in m.signatures.values():
8        ops.update(op.type for op in fn.graph.get_operations())
9    return ops
10
11ops_v1 = collect_ops("./model_v1")
12ops_v2 = collect_ops("./model_v2")
13
14print("added:", sorted(ops_v2 - ops_v1))
15print("removed:", sorted(ops_v1 - ops_v2))

This is especially useful as a pre-deploy guard.

Inspect Signature Contracts Alongside Ops

Operation inventory alone is not enough. A model can keep the same ops while changing serving interface.

python
1for name, fn in model.signatures.items():
2    print("signature:", name)
3    print("inputs:", fn.structured_input_signature)
4    print("outputs:", fn.structured_outputs)

Include contract checks to avoid interface drift between model and service layers.

GraphDef-Level Analysis for Advanced Tooling

For deeper analysis tools, inspect GraphDef node definitions directly.

python
for sig_name, fn in model.signatures.items():
    graph_def = fn.graph.as_graph_def()
    print(sig_name, "nodes:", len(graph_def.node))

Node-level metadata can support custom linting, op whitelists, or converter preprocessing.

CI Integration Pattern

A practical CI gate:

  1. export op inventory from candidate model.
  2. compare against approved op allowlist.
  3. fail build on unsupported operations.
  4. attach diff artifact for review.

This catches incompatibilities before runtime rollout.

Version and Runtime Considerations

TensorFlow internal graph rewrites can vary by version, so op inventories may differ even when model semantics are equivalent. Compare models under consistent exporter/runtime versions whenever possible.

When comparing across versions, treat differences as signals for review, not automatic failures, unless strict runtime constraints require exact matching.

Common Pitfalls

  • Inspecting only one signature and assuming all call paths are covered.
  • Confusing tensor names with operation types during compatibility checks.
  • Comparing inventories from different TensorFlow versions without context.
  • Skipping signature interface checks while focusing only on ops.
  • Running manual checks once and missing later regression drift.

Summary

  • Use saved_model_cli for quick structural inspection.
  • Use Python APIs for scriptable, repeatable op extraction.
  • Build unique op inventories for compatibility and compliance checks.
  • Diff operation sets between model versions before deployment.
  • Validate signatures and ops together for safer serving integration. Keep TensorFlow exporter versions consistent when running automated comparison gates.

Course illustration
Course illustration

All Rights Reserved.