docs/cpp/source/api/stable/index.md
The PyTorch Stable C++ API provides a binary-compatible interface for calling tensor operations and utilities that is guaranteed to remain stable across PyTorch versions. This enables ahead-of-time compiled extensions that don't need recompilation when PyTorch is updated.
When to use the Stable API:
Basic usage:
#include <torch/csrc/stable/library.h>
#include <torch/csrc/stable/ops.h>
// Create a tensor using stable API
auto tensor = torch::stable::empty(
{3, 4},
torch::headeronly::ScalarType::Float,
torch::headeronly::Layout::Strided,
torch::stable::Device(torch::headeronly::DeviceType::CPU),
false,
torch::headeronly::MemoryFormat::Contiguous);
// Register operators with stable ABI
STABLE_TORCH_LIBRARY(myops, m) {
m.def("my_op(Tensor input) -> Tensor");
}
STABLE_TORCH_LIBRARY_IMPL(myops, CPU, m) {
m.impl("my_op", TORCH_BOX(&my_cpu_kernel));
}
For more information on the stable ABI, see the Stable ABI notes.
torch/csrc/stable/library.h - Stable library registrationtorch/csrc/stable/ops.h - Stable operator definitionstorch/csrc/stable/tensor_struct.h - Stable tensor structurestorch/csrc/stable/device_struct.h - Stable device structurestorch/csrc/stable/accelerator.h - Accelerator supporttorch/csrc/stable/macros.h - Stable API macros:maxdepth: 1
registration
operators
utilities
../library/index - For standard (non-stable) operator registration