pytorch/torch/csrc
Edward Yang 9b0c53ac22 Deduplicate THTensor and THCTensor. (#9495)
Summary:
This is enabled by the allocator patch; previously we could not
deduplicate THStorage_free/THCStorage_free; now we can.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/9495

Reviewed By: SsnL

Differential Revision: D8875497

Pulled By: ezyang

fbshipit-source-id: 387198dff446eb9f84d2d6187066fae1d595dea7
2018-07-17 15:41:15 -07:00
..
api Add OptimizerBase::add_parameters (#9472) 2018-07-17 14:10:22 -07:00
autograd Implement tensor weak references (#9363) 2018-07-16 13:10:29 -07:00
cuda Deduplicate THTensor and THCTensor. (#9495) 2018-07-17 15:41:15 -07:00
distributed Remove legacy redundant codes (#9252) 2018-07-09 16:55:28 -07:00
generic Eliminate storage views. (#9466) 2018-07-16 15:40:24 -07:00
jit Add a tagged union type that replaces tensor in the interpreter. (#9368) 2018-07-16 15:40:22 -07:00
nn Remove NO_* and WITH_* across codebase, except in setup.py (#8555) 2018-06-15 12:29:48 -04:00
onnx Update from Facebook (#8887) 2018-06-26 14:55:48 -07:00
tensor [C++ API] Bag of fixes (#8843) 2018-06-25 21:11:49 -07:00
utils Add a tagged union type that replaces tensor in the interpreter. (#9368) 2018-07-16 15:40:22 -07:00
assertions.cpp Move jit/assert.h to csrc/assertions.h (#3442) 2017-11-02 13:26:51 -04:00
assertions.h Remove virtual+override anti-pattern (#9335) 2018-07-13 11:25:01 -07:00
byte_order.cpp Fix decodeUInt64BE 2017-05-26 11:21:31 -07:00
byte_order.h Expose torch.HalfTensor 2017-02-27 19:35:47 -05:00
copy_utils.h Have a single THStorage and THCStorage type. (#8030) 2018-06-02 11:05:02 -04:00
DataLoader.cpp Improve DataLoader worker fail error message (#9007) 2018-06-29 11:09:55 -07:00
DataLoader.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
Device.cpp Create ATen tensors via TensorOptions (#7869) 2018-06-16 00:40:35 -07:00
Device.h Create ATen tensors via TensorOptions (#7869) 2018-06-16 00:40:35 -07:00
dl.c Improve Windows Compatibility (for csrc/scripts) (#2941) 2017-11-08 19:51:35 +01:00
Dtype.cpp Implement __reduce__ for torch.dtype (#7699) 2018-05-20 14:59:02 +02:00
Dtype.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
DynamicTypes.cpp Port THCS to ATen. (#8689) 2018-06-24 15:14:09 -04:00
DynamicTypes.h Create ATen tensors via TensorOptions (#7869) 2018-06-16 00:40:35 -07:00
Exceptions.cpp Remove NO_PYTHON macros from Exceptions.h/cpp (#8007) 2018-06-01 22:37:18 -07:00
Exceptions.h Remove NO_PYTHON macros from Exceptions.h/cpp (#8007) 2018-06-01 22:37:18 -07:00
finalizer.cpp Add weak pointer and finalizer support directly to THStorage. (#9148) 2018-07-10 06:25:33 -07:00
finalizer.h Add weak pointer and finalizer support directly to THStorage. (#9148) 2018-07-10 06:25:33 -07:00
Generator.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Generator.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
Layout.cpp Create ATen tensors via TensorOptions (#7869) 2018-06-16 00:40:35 -07:00
Layout.h Create ATen tensors via TensorOptions (#7869) 2018-06-16 00:40:35 -07:00
Module.cpp Call deleter when destroying unconsumed DLPack PyCapsules (#9297) 2018-07-10 07:56:59 -07:00
Module.h Delete dead Tensor code paths (#5417) 2018-02-27 17:58:09 -05:00
nvrtc.cpp Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
PtrWrapper.cpp Pass THDRequest as void* pointer to THDRequest_free (#9398) 2018-07-16 19:25:22 -07:00
PtrWrapper.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
python_headers.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
PythonTypes.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
README.md Comment that data of THStorage may be NULL. 2017-07-20 10:55:35 -04:00
serialization.cpp Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
serialization.h torch.load() / torch.save() support arbitrary file-like object (#5466) 2018-03-08 22:18:55 -05:00
Size.cpp Handling of scalars in torch.Size (#5676) 2018-05-30 17:50:32 -04:00
Size.h Trace size-dependent expressions correctly (#6554) 2018-05-04 10:55:39 +02:00
Storage.cpp Introduce SupervisedPtr, delete THAllocator and THCDeviceAllocator (#9358) 2018-07-15 15:11:18 -07:00
Storage.h Expose torch.HalfTensor 2017-02-27 19:35:47 -05:00
THP_API.h Remove NO_* and WITH_* across codebase, except in setup.py (#8555) 2018-06-15 12:29:48 -04:00
THP_export.h Fix extension test on Windows (#5548) 2018-04-02 13:53:25 -04:00
THP.h Port THCS to ATen. (#8689) 2018-06-24 15:14:09 -04:00
torch.cpp [jit] Change interpreter/fuser to work on Variables only (#7489) 2018-05-11 13:33:47 -07:00
torch.h [jit] Change interpreter/fuser to work on Variables only (#7489) 2018-05-11 13:33:47 -07:00
Types.h Make THStorage / THCStorage have void* data ptr. (#7964) 2018-05-31 13:10:08 -04:00
utils.cpp Deduplicate THTensor and THCTensor. (#9495) 2018-07-17 15:41:15 -07:00
utils.h Remove NO_* and WITH_* across codebase, except in setup.py (#8555) 2018-06-15 12:29:48 -04:00
variable_tensor_functions.h [jit] Change interpreter/fuser to work on Variables only (#7489) 2018-05-11 13:33:47 -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 NULL]

Historically, Torch supported NULL 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 NULL.

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 NULL, we lose this information.

Although storage is never NULL, the data field of THStorage may be NULL. 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.)