1## Autograd 2 3Autograd is a hotspot for PyTorch performance, so most of the heavy lifting is 4implemented in C++. This implies that we have to do some shuffling between 5Python and C++; and in general, we want data to be in a form that is convenient 6to manipulate from C++. 7 8Our general model is that for any key data type that autograd manipulates, 9there are two implementations: a C++ type and a Python object type. For 10example, consider variables in autograd: we have both `Variable` in `variable.h` 11(the C++ type) and `THPVariable` in `python_variable.h` (the Python type.) 12(By the way, THP stands for TorcH Python, not to be confused with THPP, TorcH 13C++). `Variable` contains the payload of a variable, while `THPVariable` just 14contains a `shared_ptr` reference to `Variable`, as well as references to other 15Python objects which the Python runtime needs to know about. A lot of 16data accessor implementations in `python_variable.cpp` simply reach through 17to the underlying `Variable` and return the appropriate value. 18 19The most complicated application of this principle is Function, which also 20supports users implementing custom behavior in Python. We have the following 21classes: 22 23* `Node` in `function.h`, the C++ type. 24* `THPFunction` in `python_function.h`, the Python object type. In 25 `python_function.cpp`, you can see the boilerplate that tells the Python 26 interpreter about this object. 27* `PyNode` in `python_function.h`, a subclass of `Node` which forwards 28 `apply` to a Python `THPFunction`. (NOT a Python object, despite its name!) 29 30Outside of `PyNode`, the C++ objects largely avoid referencing Python 31objects (there are a few exceptions, like `pyobj` in `Variable`, and 32`PyNode`, whose whole point is to let C++ call into Python). And `pyobj` 33in `Node` to ensure uniqueness of the associated python wrapper (if it exists). 34