Open-Source Wikis

/

PyTorch

/

API

/

C++ API (libtorch)

pytorch/pytorch

C++ API (libtorch)

What it is

libtorch is the C++ frontend to PyTorch — what you link against when embedding PyTorch in a C++ application or shipping a TorchScript model in production. The supported C++ surface is the torch:: namespace declared under torch/csrc/api/.

Layout

Path Contents
torch/csrc/api/include/torch/ Public headers
torch/csrc/api/include/torch/torch.h The umbrella header
torch/csrc/api/include/torch/nn/ C++ torch::nn modules
torch/csrc/api/include/torch/optim/ C++ torch::optim optimizers
torch/csrc/api/include/torch/data/ C++ torch::data (datasets / dataloaders)
torch/csrc/api/src/ Implementation
torch/script.h TorchScript module loader for inference

Capabilities

The C++ frontend covers:

  • Tensor operationsat::Tensor and the full ATen op set (also reachable directly via at::add(a, b), at::matmul, etc.).
  • Modulestorch::nn::Linear, torch::nn::Sequential, torch::nn::Conv2d, … mirror the Python nn.Module catalogue.
  • Optimizerstorch::optim::SGD, Adam, AdamW, …
  • DataLoaderstorch::data::DataLoader with the same multi-worker pattern.
  • TorchScript loadingtorch::jit::load("model.pt") returns a torch::jit::Module that can be called like a function.
  • Autograd — the engine, torch::autograd::Function, hooks.
  • Distributedtorch::distributed::* collectives are usable from C++.

Building a C++ project

The recommended path is to download a libtorch zip from https://pytorch.org/get-started/locally/ and link via CMake:

find_package(Torch REQUIRED)
target_link_libraries(my_app "${TORCH_LIBRARIES}")

Torch_DIR should point at the unpacked libtorch/share/cmake/Torch directory.

For Python-extension authors, torch.utils.cpp_extension.CMakeExtension and BuildExtension integrate libtorch detection with setup.py. See Custom ops and extensions.

TorchScript loading example

#include <torch/script.h>
int main() {
  torch::jit::script::Module m = torch::jit::load("model.pt");
  m.eval();
  std::vector<torch::jit::IValue> inputs;
  inputs.emplace_back(torch::randn({1, 3, 224, 224}));
  auto out = m.forward(inputs).toTensor();
  std::cout << out.sizes() << std::endl;
}

AOTInductor as an alternative

For non-Python deployment, the modern path is AOTInductor-compiled .so plus the small C runtime in torch/csrc/inductor/aoti_runtime/. See Features / torch.export and AOTInductor. AOTInductor links a much smaller subset of libtorch.

ABI

PyTorch's pre-built libtorch builds with the new C++ ABI (_GLIBCXX_USE_CXX11_ABI=1). If your application uses the old ABI, recompile from source or use the prebuilt CXX11-ABI=0 archive. Mixing ABIs across the boundary will cause link-time or runtime failures.

Versioning

C++ symbols can change between minor versions. For long-lived deployments, prefer:

  • AOTInductor-compiled .so files (no PyTorch link).
  • Or pin to a specific PyTorch version end-to-end.

For ABI-stable extensions, see C-API stable.

Where to look

File Purpose
torch/csrc/api/include/torch/torch.h Umbrella header
torch/csrc/api/include/torch/nn/ nn modules
torch/csrc/api/include/torch/optim/ optimizers
torch/csrc/api/include/torch/data/ data loading
torch/script.h TorchScript loading
torch/extension.h Convenience header for extension authors

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

C++ API (libtorch) – PyTorch wiki | Factory