pytorch/torch/csrc
Tristan Rice 2673a440d0 [distributed] add PG APIs and general doc cleanups (#140853)
Doc updates:

* This adds documentation for the object oriented ProcessGroup APIs that are being used in torchft as well as https://github.com/pytorch/rfcs/pull/71 .
* It also does some general cleanups to simplify the distributed.rst by using `:methods`.
* It adds `__init__` definitions for the Stores
* I've reordered things so the collective APIs are before the Store/PG apis

Test plan:

```
lintrunner -a
cd docs && sphinx-autobuild source build/ -j auto -WT --keep-going
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/140853
Approved by: https://github.com/kwen2501
2024-11-19 02:06:32 +00:00
..
api Make Context to be Device-agnostic Step by Step (2/N) (#136526) 2024-11-18 18:21:17 +00:00
autograd Revert "[Environment Variable][7/N] Use thread-safe getenv functions (#140211)" 2024-11-13 14:05:01 +00:00
cpu [Inductor][CPP] Add oneDNN BRGEMM config for Half cpp gemm template (#136255) 2024-11-05 05:33:29 +00:00
cuda [codemod] Remove unused-variable in pytorch (#140569) 2024-11-13 20:38:03 +00:00
deploy
distributed [distributed] add PG APIs and general doc cleanups (#140853) 2024-11-19 02:06:32 +00:00
dynamo [dynamo] Identify pre-existing captured cells by cell id rather than content id (#140436) 2024-11-15 17:17:30 +00:00
export [export] Implement cpp deserializer. (#136398) 2024-11-14 16:34:59 +00:00
functorch Remove c10::optional usages in PyTorch (#139525) 2024-11-04 15:35:23 +00:00
fx
inductor [AOTI][refactor] Add sizes and strides util functions (#140449) 2024-11-14 16:48:43 +00:00
instruction_counter
jit Fix misuse of offset param in seek (#140633) 2024-11-15 19:07:52 +00:00
lazy [Reland][Environment Variable][5/N] Use thread-safe getenv functions (#140594) 2024-11-18 21:45:35 +00:00
monitor Enable cppcoreguidelines-special-member-functions (#139132) 2024-11-06 13:42:20 +00:00
mps Make Context to be Device-agnostic Step by Step (2/N) (#136526) 2024-11-18 18:21:17 +00:00
mtia Make Context to be Device-agnostic Step by Step (1/N) (#136519) (#138155) 2024-10-17 20:58:56 +00:00
multiprocessing [1/N] Remove usage of C array (#139567) 2024-11-04 04:52:46 +00:00
onnx [8/N] Fix extra warnings brought by clang-tidy-17 (#139151) 2024-10-30 14:20:08 +00:00
profiler Revert "[Environment Variable][6/N] Use thread-safe getenv functions (#140200)" 2024-11-13 15:33:23 +00:00
tensor
utils Move complex<Half> from Half.h to complex.h (#140565) 2024-11-18 15:56:21 +00:00
xpu Refine XPU device prop and fix typo (#140661) 2024-11-14 11:18:01 +00:00
copy_utils.h
CudaIPCTypes.cpp
CudaIPCTypes.h
DataLoader.cpp Use Wextra-semi (#140236) 2024-11-13 02:15:16 +00:00
DataLoader.h
Device.cpp [2/N] Remove usage of C array (#139589) 2024-11-05 01:58:12 +00:00
Device.h
DeviceAccelerator.cpp Introduce a device-agnostic runtime API design (#132204) 2024-10-27 10:37:09 +00:00
DeviceAccelerator.h Introduce a device-agnostic runtime API design (#132204) 2024-10-27 10:37:09 +00:00
Dtype.cpp [2/N] Remove usage of C array (#139589) 2024-11-05 01:58:12 +00:00
Dtype.h
DynamicTypes.cpp
DynamicTypes.h
empty.c
Event.cpp [9/N] Fix extra warnings brought by clang-tidy-17 (#139286) 2024-10-31 05:20:31 +00:00
Event.h
Exceptions.cpp [3/N] Replace c10::sv with std::sv (#139861) 2024-11-07 20:03:57 +00:00
Exceptions.h Use Wextra-semi (#140236) 2024-11-13 02:15:16 +00:00
Export.h
Generator.cpp Make Context to be Device-agnostic Step by Step (2/N) (#136526) 2024-11-18 18:21:17 +00:00
Generator.h
itt_wrapper.cpp
itt_wrapper.h
itt.cpp
Layout.cpp Turn some variables and functions into static (#136847) 2024-10-29 17:01:56 +00:00
Layout.h
MemoryFormat.cpp
MemoryFormat.h
Module.cpp [export] Implement cpp deserializer. (#136398) 2024-11-14 16:34:59 +00:00
Module.h
PyInterpreter.cpp Use Wextra-semi (#140236) 2024-11-13 02:15:16 +00:00
PyInterpreter.h
python_dimname.cpp
python_dimname.h
python_headers.h
QScheme.cpp
QScheme.h
README.md
serialization.cpp Turn static inline into static function (#139843) 2024-11-07 23:58:18 +00:00
serialization.h
Size.cpp
Size.h
Storage.cpp Turn some variables and functions into static (#136847) 2024-10-29 17:01:56 +00:00
Storage.h Use Wextra-semi (#140236) 2024-11-13 02:15:16 +00:00
StorageMethods.cpp move c10::overflows to its own header (#140564) 2024-11-18 15:56:21 +00:00
StorageMethods.h
StorageSharing.cpp Turn some variables and functions into static (#136847) 2024-10-29 17:01:56 +00:00
StorageSharing.h
Stream.cpp [2/N] Remove usage of C array (#139589) 2024-11-05 01:58:12 +00:00
Stream.h
stub.c
THConcat.h
THP.h
TypeInfo.cpp [2/N] Remove usage of C array (#139589) 2024-11-05 01:58:12 +00:00
TypeInfo.h
Types.h
utils.cpp [1/N] Remove usage of C array (#139567) 2024-11-04 04:52:46 +00:00
utils.h [1/N] Remove usage of C array (#139567) 2024-11-04 04:52:46 +00:00

csrc

The csrc directory contains all of the code concerned with integration with Python. This is in contrast to lib, which contains the Torch libraries that are Python agnostic. csrc depends on lib, but not vice versa.

There are a number of utilities for easing integration with Python which are worth knowing about, which we briefly describe here. But the most important gotchas:

  • DO NOT forget to take out the GIL with pybind11::gil_scoped_acquire before calling Python API or bringing a THPObjectPtr into scope.

  • Make sure you include Python.h first in your header files, before any system headers; otherwise, you will get error: "_XOPEN_SOURCE" redefined error. If you pay attention to warnings, you will see where you need to do this.

Notes

Note [Storage is not nullptr]

Historically, Torch supported nullptr storage, as a minor optimization to avoid having to allocate a storage object when it would be empty. However, this is actually a confusing special case to deal with, so by-in-large, PyTorch assumes that, in fact, storage is never nullptr.

One important case where this assumption is important is when tracking the CUDA device a tensor is stored in: this information is stored solely in the storage, so if a storage is nullptr, we lose this information.

Although storage is never nullptr, the data field of c10::StorageImpl may be nullptr. This mostly occurs when we want to pre-allocate an output tensor struct, but then have it be resized and filled with data by some operator: there's no point in allocating data for it in this case!

Files

Exceptions.h

Frequently when working with the Python API, you may call a function which returns an error. In this case, we want to return directly to the Python interpreter, so that this exception can be propagated accordingly; however, because the Python API is C-based, what actually will happen is it will return control to whatever C++ code called it. Similarly, if we raise a C++ exception, prior to returning to the Python interpreter, we must set the Python error flags, so it turns into a C++ exception.

Moreover, when using the following macros, the generated warnings will be converted into python warnings that can be caught by the user.

Exceptions define helpers for two main cases:

  • For code where you write the python binding by hand, HANDLE_TH_ERRORS, END_HANDLE_TH_ERRORS and an exception class python_error. You call them like this:
// Entry point from Python interpreter
PyObject* run(PyObject* arg) {
  HANDLE_TH_ERRORS
  ...
  if (!x) throw python_error();
  // From c10/Exception.h
  TORCH_CHECK(cond, "cond was false here");
  TORCH_WARN("Warning message");
  ...
  END_HANDLE_TH_ERRORS
}

The HANDLE_TH_ERRORS macro will catch all exceptions and convert them into an appropriate Python signal. python_error is a special exception which doesn't contain any info, instead it says, "An error occurred in the Python API; if you return to the interpreter, Python will raise that exception, nothing else needs to be done."

  • For code that you bind using pybind, HANDLE_TH_ERRORS and END_HANDLE_TH_ERRORS_PYBIND can be used. They will work jointly with pybind error handling to raise pytorch errors and warnings natively and let pybind handle other errors. It can be used as:
// Function given to the pybind binding
at::Tensor foo(at::Tensor x) {
  HANDLE_TH_ERRORS
  ...
  if (!x) throw python_error();
  // pybind native error
  if (!x) throw py::value_error();
  // From c10/Exception.h
  TORCH_CHECK(cond, "cond was false here");
  TORCH_WARN("Warning message");
  ...
  END_HANDLE_TH_ERRORS_PYBIND
}

GIL

Whenever you make any calls to the Python API, you must have taken out the Python GIL, as none of these calls are thread safe. pybind11::gil_scoped_acquire is a RAII struct which handles taking and releasing the GIL. Use it like this:

void iWantToUsePython() {
  pybind11::gil_scoped_acquire gil;
  ...
}

In general, the compiler will NOT warn you if you use Python functionality without taking out the GIL, so DO NOT FORGET this call.

utils/object_ptr.h

THPPointer is a smart pointer class analogous to std::shared_ptr, but which is overloaded to handle reference counting scheme of various objects which are not based on shared_ptr. The most important overloads are:

  • PyObject (so important we've aliased it as THPObjectPtr), which hooks into Python reference counting. (By the way, that means you MUST take out the GIL before bringing one of these into scope!)

  • The various TH tensor and storage types (e.g., THTensor), which hook into TH's reference counting. (TH's reference counting IS thread safe, no locks necessary.)