Files
pytorch/docs/cpp/source/api/library/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

63 lines
1.6 KiB
Markdown

---
myst:
html_meta:
description: PyTorch Library API in C++ — operator registration, custom classes, and versioning.
keywords: PyTorch, C++, Library, operator registration, custom class, dispatch
---
# Torch Library API
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:**
- When creating custom operators for PyTorch
- When implementing backend-specific kernels (CPU, CUDA, etc.)
- When registering custom classes for use in TorchScript
- When extending PyTorch with new functionality
**Basic usage:**
```cpp
#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](https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html)
tutorial.
## Header Files
- `torch/library.h` - Main library API header
- `torch/custom_class.h` - Custom class registration
## Library API Categories
```{toctree}
:maxdepth: 1
registration
custom_classes
versioning
```
## See Also
- {doc}`../stable/index` - For stable ABI operator registration