Files
pytorch/docs/cpp/source/api/autograd/index.md
T
Svetlana Karslioglu 19da3da3d3 Restructure C++ docs (#174096)
The fix-cpp-build branch is a major restructuring that:

- Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives
- Reorganizes notes into a hierarchical API reference with proper categories
- Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs
- Always runs Doxygen instead of gating on an env var

Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096
Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
2026-04-07 17:24:38 +00:00

1.5 KiB

myst
myst
html_meta
description keywords
PyTorch C++ Autograd API — automatic differentiation for tensor computations. PyTorch, C++, autograd, automatic differentiation, gradient

Autograd: Automatic Differentiation

PyTorch's autograd system provides automatic differentiation for all operations on tensors. It records operations on tensors to build a computational graph, then computes gradients automatically via backpropagation.

When to use Autograd:

  • When training neural networks (gradients are computed automatically)
  • When implementing custom backward passes for specialized operations
  • When you need fine-grained control over gradient computation

Basic usage:

#include <torch/torch.h>

// Create tensor with gradient tracking
auto x = torch::randn({2, 2}, torch::requires_grad());
auto y = x * x;
auto z = y.sum();

// Compute gradients via backpropagation
z.backward();
std::cout << x.grad() << std::endl;  // dz/dx = 2x

// Disable gradient tracking for inference
{
    torch::NoGradGuard no_grad;
    auto result = model->forward(input);  // No gradients computed
}

Header Files

  • torch/csrc/autograd/autograd.h - High-level autograd API
  • torch/csrc/autograd/function.h - Custom autograd functions
  • torch/csrc/autograd/grad_mode.h - Gradient computation modes
  • torch/csrc/api/include/torch/autograd.h - C++ Frontend autograd

Autograd Categories

:maxdepth: 1

gradient
custom_functions
modes