1C++ 2=================================== 3.. Note:: 4 If you are looking for the PyTorch C++ API docs, directly go `here <https://pytorch.org/cppdocs/>`__. 5 6PyTorch provides several features for working with C++, and it’s best to choose from them based on your needs. At a high level, the following support is available: 7 8TorchScript C++ API 9-------------------- 10`TorchScript <https://pytorch.org/docs/stable/jit.html>`__ allows PyTorch models defined in Python to be serialized and then loaded and run in C++ capturing the model code via compilation or tracing its execution. You can learn more in the `Loading a TorchScript Model in C++ tutorial <https://pytorch.org/tutorials/advanced/cpp_export.html>`__. This means you can define your models in Python as much as possible, but subsequently export them via TorchScript for doing no-Python execution in production or embedded environments. The TorchScript C++ API is used to interact with these models and the TorchScript execution engine, including: 11 12* Loading serialized TorchScript models saved from Python 13* Doing simple model modifications if needed (e.g. pulling out submodules) 14* Constructing the input and doing preprocessing using C++ Tensor API 15 16Extending PyTorch and TorchScript with C++ Extensions 17------------------------------------------------------ 18TorchScript can be augmented with user-supplied code through custom operators and custom classes. 19Once registered with TorchScript, these operators and classes can be invoked in TorchScript code run from 20Python or from C++ as part of a serialized TorchScript model. The `Extending TorchScript with Custom C++ Operators <https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html>`__ tutorial walks through interfacing TorchScript with OpenCV. In addition to wrapping a function call with a custom operator, C++ classes and structs can be bound into TorchScript through a pybind11-like interface which is explained in the `Extending TorchScript with Custom C++ Classes <https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html>`__ tutorial. 21 22Tensor and Autograd in C++ 23--------------------------- 24Most of the tensor and autograd operations in PyTorch Python API are also available in the C++ API. These include: 25 26* ``torch::Tensor`` methods such as ``add`` / ``reshape`` / ``clone``. For the full list of methods available, please see: https://pytorch.org/cppdocs/api/classat_1_1_tensor.html 27* C++ tensor indexing API that looks and behaves the same as the Python API. For details on its usage, please see: https://pytorch.org/cppdocs/notes/tensor_indexing.html 28* The tensor autograd APIs and the ``torch::autograd`` package that are crucial for building dynamic neural networks in C++ frontend. For more details, please see: https://pytorch.org/tutorials/advanced/cpp_autograd.html 29 30Authoring Models in C++ 31------------------------ 32The "author in TorchScript, infer in C++" workflow requires model authoring to be done in TorchScript. 33However, there might be cases where the model has to be authored in C++ (e.g. in workflows where a Python 34component is undesirable). To serve such use cases, we provide the full capability of authoring and training a neural net model purely in C++, with familiar components such as ``torch::nn`` / ``torch::nn::functional`` / ``torch::optim`` that closely resemble the Python API. 35 36* For an overview of the PyTorch C++ model authoring and training API, please see: https://pytorch.org/cppdocs/frontend.html 37* For a detailed tutorial on how to use the API, please see: https://pytorch.org/tutorials/advanced/cpp_frontend.html 38* Docs for components such as ``torch::nn`` / ``torch::nn::functional`` / ``torch::optim`` can be found at: https://pytorch.org/cppdocs/api/library_root.html 39 40 41Packaging for C++ 42------------------ 43For guidance on how to install and link with libtorch (the library that contains all of the above C++ APIs), please see: https://pytorch.org/cppdocs/installing.html. Note that on Linux there are two types of libtorch binaries provided: one compiled with GCC pre-cxx11 ABI and the other with GCC cxx11 ABI, and you should make the selection based on the GCC ABI your system is using. 44