docs/cpp/source/api/library/index.md
The Torch Library API provides capabilities for extending PyTorch's core library of operators with user-defined operators and data types. This is the primary mechanism for registering custom C++ operators that can be called from both Python and C++.
When to use the Library API:
Basic usage:
#include <torch/library.h>
// Define a custom operator
torch::Tensor my_add(const torch::Tensor& a, const torch::Tensor& b) {
return a + b;
}
// Register the operator
TORCH_LIBRARY(myops, m) {
m.def("add(Tensor a, Tensor b) -> Tensor", &my_add);
}
// Use from C++
auto result = torch::dispatcher::call("myops::add", tensor_a, tensor_b);
For a tutorial-style introduction to the library API, check out the Extending TorchScript with Custom C++ Operators tutorial.
torch/library.h - Main library API headertorch/custom_class.h - Custom class registration:maxdepth: 1
registration
custom_classes
versioning
../stable/index - For stable ABI operator registration