pytorch/torch/csrc
Gregory Chanan 705d80b51e Remove some Type.tensor usages and remove native_tensor without size. (#12355)
Summary:
This is to move us along the path to removing Type from the public API.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/12355

Reviewed By: ezyang

Differential Revision: D10212616

Pulled By: gchanan

fbshipit-source-id: c9cd128d1111ab219cb0b2f3bf5b632502ab97c0
2018-10-05 11:12:07 -07:00
..
api Make TensorOptions contain optional fields, optimize struct size (#12103) 2018-10-05 09:24:53 -07:00
autograd Enable clang-tidy in CI (#12213) 2018-10-03 17:25:06 -07:00
cuda Get rid of most usages of Type.tensor. (#12002) 2018-09-24 10:16:18 -07:00
distributed Add message tag parameter to send/recv 2018-09-14 10:55:37 -07:00
generic Rewrite serialization to correctly handle partial reads/writes in all cases (#12143) 2018-09-27 19:09:53 -07:00
jit Remove some Type.tensor usages and remove native_tensor without size. (#12355) 2018-10-05 11:12:07 -07:00
nn Merge THTensor into TensorImpl 2018-08-16 08:10:06 -07:00
onnx Only reference ONNX through onnx_pb.h (#11609) 2018-09-12 18:25:58 -07:00
tensor Replace 'struct Tensor' with 'class Tensor'. (#12034) 2018-09-25 09:54:35 -07:00
utils Remove some Type.tensor usages and remove native_tensor without size. (#12355) 2018-10-05 11:12:07 -07: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 NULL -> nullptr (#11047) 2018-08-30 16:25:42 -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 NULL -> nullptr (#11047) 2018-08-30 16:25:42 -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 NULL -> nullptr (#11047) 2018-08-30 16:25:42 -07:00
Dtype.h Add workaround to fix include warnings in Python 2 builds. (#6716) 2018-04-24 12:30:19 -07:00
DynamicTypes.cpp Fix #11430 (CPU only builds raise opaque error message when calling .… (#11533) 2018-09-14 09:10:08 -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 NULL -> nullptr (#11047) 2018-08-30 16: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 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 Improve autograd profiler performance (#11773) 2018-09-19 09:25:43 -07:00
Module.h Delete dead Tensor code paths (#5417) 2018-02-27 17:58:09 -05:00
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 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 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 Sparse tensor printing; add NotImplemented autograd fn (#10181) 2018-09-05 19:41:22 -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
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 Move Factory functions from Type to TypeExtendedInterface. (#12025) 2018-09-25 09:40:17 -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 use at::Half in THC (#11322) 2018-09-12 17:39:37 -07:00
variable_tensor_functions.h Move Factory functions from Type to TypeExtendedInterface. (#12025) 2018-09-25 09:40:17 -07: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.)