Title: Torch Library API — PyTorch main documentation
Description: PyTorch Library API in C++ — operator registration, custom classes, and versioning.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Torch Library API",
"headline": "Torch Library API",
"description": "PyTorch Library API in C++ \u2014 operator registration, custom classes, and versioning.",
"url": "/api/library/index.html",
"articleBody": "Torch Library API# The Torch Library API provides capabilities for extending PyTorch\u2019s 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: #include \u003ctorch/library.h\u003e // Define a custom operator torch::Tensor my_add(const torch::Tensor\u0026 a, const torch::Tensor\u0026 b) { return a + b; } // Register the operator TORCH_LIBRARY(myops, m) { m.def(\"add(Tensor a, Tensor b) -\u003e Tensor\", \u0026my_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. Header Files# torch/library.h - Main library API header torch/custom_class.h - Custom class registration Library API Categories# Operator Registration Custom Classes Library Versioning See Also# Torch Stable API - For stable ABI operator registration",
"author": {
"@type": "Organization",
"name": "PyTorch Contributors",
"url": "https://pytorch.org"
},
"image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "/api/library/index.html"
},
"datePublished": "2023-01-01T00:00:00Z",
"dateModified": "2023-01-01T00:00:00Z"
}
| docsearch:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | PyTorch Library API in C++ — operator registration, custom classes, and versioning. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 2 |
Links:
Viewport: width=device-width, initial-scale=1