pytorch/torch/csrc/autograd
PyTorch MergeBot 75b8295868 Revert "Warn if AccumulateGrad stream does not match producer node stream (#165065)"
This reverts commit 12f742941d.

Reverted https://github.com/pytorch/pytorch/pull/165065 on behalf of https://github.com/clee2000 due to broke internal builds D85273204 usages of TORCH_API void add need to be updated? ([comment](https://github.com/pytorch/pytorch/pull/165065#issuecomment-3438061854))
2025-10-23 17:02:49 +00:00
..
functions Revert "[RELAND] Always build USE_DISTRIBUTED (#160449) and Make distributed modules importable even when backend not built (#159889) (#162594)" 2025-09-25 13:47:46 +00:00
utils [2/N] Fix clang-tidy readability checks (#164652) 2025-10-06 01:06:01 +00:00
anomaly_mode.cpp
anomaly_mode.h
autograd_meta.cpp [reland] Allow setting grad_dtype on leaf tensors (#164751) 2025-10-08 20:23:13 +00:00
autograd_not_implemented_fallback.cpp [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
autograd_not_implemented_fallback.h
autograd.cpp
autograd.h
cpp_hook.cpp [Code Clean] Replace std::runtime_error with TORCH_CHECK (#163264) 2025-09-25 11:28:51 +00:00
cpp_hook.h
custom_function.cpp Add pure view support in autograd Function (#164736) 2025-10-06 18:21:05 +00:00
custom_function.h Add pure view support in autograd Function (#164736) 2025-10-06 18:21:05 +00:00
edge.h
engine.cpp Revert "Warn if AccumulateGrad stream does not match producer node stream (#165065)" 2025-10-23 17:02:49 +00:00
engine.h
forward_grad.cpp
forward_grad.h
function_hook.h [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
function.cpp
function.h [reland] Allow setting grad_dtype on leaf tensors (#164751) 2025-10-08 20:23:13 +00:00
FunctionsManual.cpp [1/N] Fix clang-tidy readability checks (#164561) 2025-10-04 09:40:38 +00:00
FunctionsManual.h [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
grad_mode.h
graph_task.h [2/N] Fix clang-tidy readability checks (#164652) 2025-10-06 01:06:01 +00:00
InferenceMode.h
init.cpp Make custom op alias check consistent (#164576) 2025-10-07 02:05:09 +00:00
input_buffer.cpp Revert "Warn if AccumulateGrad stream does not match producer node stream (#165065)" 2025-10-23 17:02:49 +00:00
input_buffer.h Revert "Warn if AccumulateGrad stream does not match producer node stream (#165065)" 2025-10-23 17:02:49 +00:00
input_metadata.cpp [reland] Allow setting grad_dtype on leaf tensors (#164751) 2025-10-08 20:23:13 +00:00
input_metadata.h [reland] Allow setting grad_dtype on leaf tensors (#164751) 2025-10-08 20:23:13 +00:00
jit_decomp_interface.cpp
jit_decomp_interface.h
profiler_kineto.cpp [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
profiler_kineto.h [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
profiler_legacy.cpp
profiler_legacy.h [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
profiler_python.cpp [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
profiler_python.h
profiler.h
python_anomaly_mode.cpp [Code Clean] Replace std::runtime_error with TORCH_CHECK (#163264) 2025-09-25 11:28:51 +00:00
python_anomaly_mode.h
python_autograd.h
python_cpp_function.cpp [Code Clean] Replace std::runtime_error with TORCH_CHECK (#163264) 2025-09-25 11:28:51 +00:00
python_cpp_function.h
python_engine.cpp refactor: replace runtime_error with TORCH_CHECK for better error handling (#163628) 2025-10-16 11:09:48 +00:00
python_engine.h
python_enum_tag.h
python_fft_functions.h
python_function.cpp Add pure view support in autograd Function (#164736) 2025-10-06 18:21:05 +00:00
python_function.h Add pure view support in autograd Function (#164736) 2025-10-06 18:21:05 +00:00
python_hook.cpp [Code Clean] Replace std::runtime_error with TORCH_CHECK (#163264) 2025-09-25 11:28:51 +00:00
python_hook.h
python_legacy_variable.cpp
python_legacy_variable.h
python_linalg_functions.h
python_nested_functions_manual.cpp
python_nested_functions.h
python_nn_functions.h
python_saved_variable_hooks.cpp
python_saved_variable_hooks.h
python_sparse_functions.h
python_special_functions.h
python_torch_functions_manual.cpp Reapply "Make functionalization ViewMeta serializable with pickle. (#143712)" (#163769) 2025-09-25 10:27:37 +00:00
python_torch_functions.h
python_variable_indexing.cpp Fix performance regression when indexing by Numpy arrays (#163280) 2025-09-19 05:02:58 +00:00
python_variable_indexing.h
python_variable.cpp [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
python_variable.h
README.md
record_function_ops.cpp
record_function_ops.h
saved_variable_hooks.h [Code Clean] Replace std::runtime_error with TORCH_CHECK (#163264) 2025-09-25 11:28:51 +00:00
saved_variable.cpp [BE] Improve torch.inference_mode docs and error message (#161164) 2025-08-26 20:58:56 +00:00
saved_variable.h Add methods to access data and unpack_hook on SavedVariable (#164358) 2025-10-02 13:05:16 +00:00
symbolic.h
TraceTypeManual.cpp [Code Clean] Replace std::runtime_error with TORCH_CHECK (#163264) 2025-09-25 11:28:51 +00:00
variable_info.cpp
variable_info.h
variable.cpp [reland] Allow setting grad_dtype on leaf tensors (#164751) 2025-10-08 20:23:13 +00:00
variable.h Various C++ code fixes in LSAN integration (#165818) 2025-10-18 17:59:23 +00:00
VariableTypeManual.cpp Fix double dispatch to Python for detach (#163671) 2025-10-15 17:24:50 +00:00
VariableTypeUtils.h

Autograd

Autograd is a hotspot for PyTorch performance, so most of the heavy lifting is implemented in C++. This implies that we have to do some shuffling between Python and C++; and in general, we want data to be in a form that is convenient to manipulate from C++.

Our general model is that for any key data type that autograd manipulates, there are two implementations: a C++ type and a Python object type. For example, consider variables in autograd: we have both Variable in variable.h (the C++ type) and THPVariable in python_variable.h (the Python type.) (By the way, THP stands for TorcH Python, not to be confused with THPP, TorcH C++). Variable contains the payload of a variable, while THPVariable just contains a shared_ptr reference to Variable, as well as references to other Python objects which the Python runtime needs to know about. A lot of data accessor implementations in python_variable.cpp simply reach through to the underlying Variable and return the appropriate value.

The most complicated application of this principle is Function, which also supports users implementing custom behavior in Python. We have the following classes:

  • Node in function.h, the C++ type.
  • THPFunction in python_function.h, the Python object type. In python_function.cpp, you can see the boilerplate that tells the Python interpreter about this object.
  • PyNode in python_function.h, a subclass of Node which forwards apply to a Python THPFunction. (NOT a Python object, despite its name!)

Outside of PyNode, the C++ objects largely avoid referencing Python objects (there are a few exceptions, like pyobj in Variable, and PyNode, whose whole point is to let C++ call into Python). And pyobj in Node to ensure uniqueness of the associated python wrapper (if it exists).