mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
19da3da3d3
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
1.5 KiB
1.5 KiB
myst
| myst | ||||||
|---|---|---|---|---|---|---|
|
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 APItorch/csrc/autograd/function.h- Custom autograd functionstorch/csrc/autograd/grad_mode.h- Gradient computation modestorch/csrc/api/include/torch/autograd.h- C++ Frontend autograd
Autograd Categories
:maxdepth: 1
gradient
custom_functions
modes