|
| 1 | +.. _custom-ops-landing-page: |
| 2 | + |
| 3 | +PyTorch Custom Operators Landing Page |
| 4 | +===================================== |
| 5 | + |
| 6 | +PyTorch offers a large library of operators that work on Tensors (e.g. :func:`torch.add`, |
| 7 | +:func:`torch.sum`, etc). However, you may wish to bring a new custom operation to PyTorch |
| 8 | +and have it behave like PyTorch's built-in operators. In order to do so, you must |
| 9 | +register the custom operation with PyTorch via the Python :ref:`torch-library-docs` or C++ TORCH_LIBRARY |
| 10 | +APIs. |
| 11 | + |
| 12 | +TL;DR |
| 13 | +----- |
| 14 | + |
| 15 | +How do I author a custom op from Python? |
| 16 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 17 | + |
| 18 | +.. |
| 19 | + [comment] TODO(rzou): The following will be a link to a tutorial on the PyTorch tutorials site in 2.4 |
| 20 | +
|
| 21 | +Please see the `Python Custom Operators tutorial <https://colab.research.google.com/drive/1xCh5BNHxGnutqGLMHaHwm47cbDL9CB1g#scrollTo=gg6WorNtKzeh>`_ |
| 22 | + |
| 23 | +How do I black-box a Python function for use with torch.compile? |
| 24 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 25 | + |
| 26 | +.. |
| 27 | + [comment] TODO(rzou): The following will be a link to a tutorial on the PyTorch tutorials site in 2.4 |
| 28 | +
|
| 29 | +Please see the `Python Custom Operators tutorial <https://colab.research.google.com/drive/1xCh5BNHxGnutqGLMHaHwm47cbDL9CB1g#scrollTo=gg6WorNtKzeh>`_ |
| 30 | + |
| 31 | +How do I integrate custom C++ and/or CUDA code with PyTorch? |
| 32 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 33 | + |
| 34 | +.. |
| 35 | + [comment] TODO(rzou): The following will be a link to a tutorial on the PyTorch tutorials site in 2.4 |
| 36 | +
|
| 37 | +Please see the `Custom C++ and CUDA Operators tutorial <https://docs.google.com/document/d/1-LdJZBzlxiF0Tm-8NfbyFvRJaofdwRgLcycXGmlIpS0/edit>`_ |
| 38 | + |
| 39 | + |
| 40 | +For more details |
| 41 | +^^^^^^^^^^^^^^^^ |
| 42 | + |
| 43 | +Please see `The Custom Operators Manual (gdoc) <https://docs.google.com/document/d/1-LdJZBzlxiF0Tm-8NfbyFvRJaofdwRgLcycXGmlIpS0/edit>`_ |
| 44 | +(we're working on moving the information to our docs site). We recommend that you |
| 45 | +first read one of the tutorials above and then use the Custom Operators Manual as a reference; |
| 46 | +it is not meant to be read head to toe. |
| 47 | + |
| 48 | +When should I create a Custom Operator? |
| 49 | +--------------------------------------- |
| 50 | +If your operation is expressible as a composition of built-in PyTorch operators |
| 51 | +then please write it as a Python function and call it instead of creating a |
| 52 | +custom operator. Use the operator registration APIs to create a custom op if you |
| 53 | +are calling into some library that PyTorch doesn't understand (e.g. custom C/C++ code, |
| 54 | +a custom CUDA kernel, or Python bindings to C/C++/CUDA extensions). |
| 55 | + |
| 56 | +Why should I create a Custom Operator? |
| 57 | +-------------------------------------- |
| 58 | + |
| 59 | +It is possible to use a C/C++/CUDA kernel by grabbing a Tensor's data pointer |
| 60 | +and passing it to a pybind'ed kernel. However, this approach doesn't compose with |
| 61 | +PyTorch subsystems like autograd, torch.compile, vmap, and more. In order |
| 62 | +for an operation to compose with PyTorch subsystems, it must be registered |
| 63 | +via the operator registration APIs. |
0 commit comments