pytorch/torch/csrc
Edward Yang 50b914aeeb Move CUDAStreamInternals inside detail namespace. (#14109)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14109

Previously it was at the top level, because the author was under
the impression that you could only refer to top-level C++ names
from C, but this is not true; you just need to make a stub struct
conditioned on __cplusplus.

Reviewed By: smessmer

Differential Revision: D13104694

fbshipit-source-id: ecb7ae6dcfa4ab4e062aad7a886937dca15fd1b2
2018-11-19 17:05:46 -08:00
..
api Add SharedDataset (#13800) 2018-11-16 13:07:10 -08:00
autograd Fix Windows build and test in CI (#11716) 2018-11-13 16:35:54 -08:00
cuda Move CUDAStreamInternals inside detail namespace. (#14109) 2018-11-19 17:05:46 -08:00
distributed Split out CUDAMultiStreamGuard from CUDAGuard (#13912) 2018-11-19 08:20:11 -08:00
generic Fix exception catching to catch c10::Error properly (#13665) 2018-11-07 11:22:48 -08:00
jit Store the optimize flag in module (#14166) 2018-11-19 14:34:05 -08:00
nn codemod tensor.type().is_cuda(), tensor.type().is_sparse() (#13590) 2018-11-07 07:27:42 -08:00
onnx Only reference ONNX through onnx_pb.h (#11609) 2018-09-12 18:25:58 -07:00
tensor codemod tensor.type().is_cuda(), tensor.type().is_sparse() (#13590) 2018-11-07 07:27:42 -08:00
utils Fix Windows build and test in CI (#11716) 2018-11-13 16:35:54 -08:00
byte_order.cpp Cleanup byte swapping utilities to generate optimal code on the platforms we care about. (#11394) 2018-09-10 15:40:24 -07:00
byte_order.h Bag of clang tidy fixes for torch/csrc/ and torch/csrc/autograd (#11050) 2018-09-05 19:55:50 -07:00
copy_utils.h NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
DataLoader.cpp Prevent hanging in data loader altogether 2018-10-09 09:54:19 -07:00
DataLoader.h
Device.cpp remove ATen/Error.h and ATen/core/Error.h (#12792) 2018-10-17 17:25:42 -07:00
Device.h
dl.c
Dtype.cpp Make THPDtype_New error instead of truncate (#14103) 2018-11-16 11:35:18 -08:00
Dtype.h
DynamicTypes.cpp use TypeMeta instead of ScalarType in TensorOptions (#13172) 2018-10-29 11:15:37 -07:00
DynamicTypes.h Make TensorOptions contain optional fields, optimize struct size (#12103) 2018-10-05 09:24:53 -07:00
Exceptions.cpp NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
Exceptions.h remove ATen/Error.h and ATen/core/Error.h (#12792) 2018-10-17 17:25:42 -07:00
Generator.cpp Delete a bunch of uses of getType in favor of TensorOptions. 2018-08-30 20:11:24 -07:00
Generator.h
Layout.cpp
Layout.h
Module.cpp add torch-python target (#12742) 2018-11-16 11:43:48 -08:00
Module.h
nvrtc.cpp NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
PtrWrapper.cpp NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
PtrWrapper.h
python_headers.h
PythonTypes.h
README.md NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
serialization.cpp Rewrite serialization to correctly handle partial reads/writes in all cases (#12143) 2018-09-27 19:09:53 -07:00
serialization.h Rewrite serialization to correctly handle partial reads/writes in all cases (#12143) 2018-09-27 19:09:53 -07:00
Size.cpp Add missing HANDLE_TH_ERRORS (#12770) 2018-10-17 13:52:02 -07:00
Size.h Bag of clang tidy fixes for torch/csrc/ and torch/csrc/autograd (#11050) 2018-09-05 19:55:50 -07:00
Storage.cpp Don't call into Python during Storage destruction. (#10407) 2018-08-13 11:20:07 -07:00
Storage.h
stub.cpp add torch-python target (#12742) 2018-11-16 11:43:48 -08:00
THP_API.h
THP_export.h
THP.h
torch.cpp Move Factory functions from Type to TypeExtendedInterface. (#12025) 2018-09-25 09:40:17 -07:00
TypeInfo.cpp Added a default constructor for torch.finfo. 2018-10-23 09:03:24 -07:00
TypeInfo.h torch.finfo and torch.iinfo to mimic the numpy equivalent (#12472) 2018-10-15 13:43:52 -07:00
Types.h Bag of clang tidy fixes for torch/csrc/ and torch/csrc/autograd (#11050) 2018-09-05 19:55:50 -07:00
utils.cpp NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
utils.h Rewrite THPUtils_PySequence_to_CUDAStreamList to return vector<optional<CUDAStream>> (#13125) 2018-10-29 08:27:23 -07:00
variable_tensor_functions.h Restructure torch/torch.h and extension.h (#13482) 2018-11-05 16:46:52 -08:00
WindowsTorchApiMacro.h Additional changes to make GPU builds work (#10507) 2018-08-16 13:25:27 -07: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 AutoGil 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 THStorage 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.

Exceptions defines some useful helpers: 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() {
  HANDLE_TH_ERRORS
  ...
  if (!x) throw python_error();
  ...
  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."

utils/auto_gil.h

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. AutoGIL is a RAII struct which handles taking and releasing the GIL. Use it like this:

void iWantToUsePython() {
  AutoGil 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.)