Open-Source Wikis

/

TensorFlow

/

Apps

/

C++ API

tensorflow/tensorflow

C++ API

The C++-native frontend in tensorflow/cc/. Used inside Google for graph-mode programs and by external code that wants to embed TensorFlow without a Python interpreter.

Purpose

  • Build graphs in C++ using a Scope/builder API.
  • Compute gradients (via tensorflow/cc/gradients/).
  • Load and run SavedModel without Python (tensorflow/cc/saved_model/).
  • Train (tensorflow/cc/training/) — small-scale, mostly used in tests.

Directory layout

tensorflow/cc/
├── client/             # Wrapper around tensorflow::Session
├── framework/          # Scope, Operation, Output, GradOps registration
├── gradients/          # Gradient implementations for ops
├── ops/                # Generated op builders (one header per op family)
├── saved_model/        # SavedModelBundle loader
├── tools/              # Misc helpers
└── training/           # Queues, sync_replicas helpers

The tensorflow/cc/ops/ headers (e.g. math_ops.h, array_ops.h, nn_ops.h) are generated from the same OpDef registrations that the Python gen_* files come from.

Key abstractions

Type / class File Role
Scope tensorflow/cc/framework/scope.h Builder context; replaces TF_Graph ergonomics.
Output / Input tensorflow/cc/framework/ops.h Lightweight handles to op outputs.
ClientSession tensorflow/cc/client/client_session.h Convenient runner for graphs.
SavedModelBundle tensorflow/cc/saved_model/loader.h Loads a SavedModel into a Session.
Status / errors::* tensorflow/core/platform/errors.h Standard error type.

How it works

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"

using namespace tensorflow;
using namespace tensorflow::ops;

int main() {
  Scope root = Scope::NewRootScope();
  auto a = Const(root, {{1.0f, 2.0f}, {3.0f, 4.0f}});
  auto b = Const(root, {{1.0f, 0.0f}, {0.0f, 1.0f}});
  auto m = MatMul(root.WithOpName("m"), a, b);

  ClientSession session(root);
  std::vector<Tensor> outputs;
  TF_CHECK_OK(session.Run({m}, &outputs));
  // outputs[0] holds the result
}

The C++ API constructs an in-memory Graph (the same tensorflow::Graph that the runtime executes). ClientSession wraps tensorflow::Session to run it on the local devices. For distributed execution, you would use the Session API directly with a target.

SavedModel in C++

The single most common use of the C++ API in production:

SessionOptions session_options;
RunOptions run_options;
SavedModelBundle bundle;
TF_CHECK_OK(LoadSavedModel(session_options, run_options,
                           model_path, /*tags=*/{kSavedModelTagServe}, &bundle));
bundle.session->Run(...);

Implementation in tensorflow/cc/saved_model/loader.cc. See features/saved-model.

Integration points

  • Generated headers: tensorflow/cc/ops/*.h are produced by Bazel rule tf_gen_op_wrappers_cc. Their generator binary is tensorflow/cc/framework/cc_op_gen.cc.
  • Backed by: tensorflow/core/ runtime types — Tensor, Graph, Session. The C++ API is essentially a builder layer on top.
  • TensorFlow Serving uses this API extensively (separate repo).
  • tfcompile (AOT) consumes graphs you can build through this API to emit static binaries. See compilers/xla.

Entry points for modification

  • Adding a new op — register the OpDef and the C++ kernel as usual; the C++ wrapper header is regenerated automatically.
  • Gradients — implement an entry in tensorflow/cc/gradients/<family>_grad.cc and register with REGISTER_GRADIENT_OP("Op", FooGrad).
  • SavedModel loader changes — tensorflow/cc/saved_model/loader.cc and friends.

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

C++ API – TensorFlow wiki | Factory