pytorch/torch/csrc
Will Feng a54416d208 [C++ API] Remove deprecated torch::nn::BatchNorm / FeatureDropout / modules_ordered_dict and torch::nn::init::Nonlinearity / FanMode (#34508)
Summary:
This PR is BC-breaking in the following way:
- The deprecated `torch::nn::BatchNorm` is removed in favor of `torch::nn::BatchNorm{1,2,3}d`
- The deprecated `torch::nn::FeatureDropout` is removed in favor of `torch::nn::Dropout{2,3}d`
- The deprecated `torch::nn::modules_ordered_dict` is removed. User should do `Sequential sequential({{"m1", MyModule(1)}, {"m2", MyModule(2)}})` instead.
- The deprecated `torch::nn::init::Nonlinearity` is removed, in favor of the following enums:
    - `torch::kLinear`
    - `torch::kConv1D`
    - `torch::kConv2D`
    - `torch::kConv3D`
    - `torch::kConvTranspose1D`
    - `torch::kConvTranspose2D`
    - `torch::kConvTranspose3D`
    - `torch::kSigmoid`
    - `torch::kTanh`
    - `torch::kReLU`
    - `torch::kLeakyReLU`
- The deprecated `torch::nn::init::FanMode` is removed, in favor of the following enums:
    - `torch::kFanIn`
    - `torch::kFanOut`
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34508

Differential Revision: D20351601

Pulled By: yf225

fbshipit-source-id: cca0cd112f29a31bb023e348ca8f82780e42bea3
2020-03-12 10:09:58 -07:00
..
api [C++ API] Remove deprecated torch::nn::BatchNorm / FeatureDropout / modules_ordered_dict and torch::nn::init::Nonlinearity / FanMode (#34508) 2020-03-12 10:09:58 -07:00
autograd Fix version check for grad_fn for views (#34145) 2020-03-12 09:47:56 -07:00
cuda Stop using ctypes to interface with CUDA libraries. (#33678) 2020-03-11 07:22:46 -07:00
distributed [jit] kill script namespace (#34515) 2020-03-11 23:32:48 -07:00
generic Fix some bugs with zipfile serialization (#32244) 2020-02-05 15:32:14 -08:00
jit [JIT] remove builtin interpolate functions (#34514) 2020-03-12 09:21:33 -07:00
multiprocessing
onnx Upgrade exported ONNX IR version to 6 (#31025) 2019-12-16 23:18:22 -08:00
tensor Fix typos, via a Levenshtein-type corrector (#31523) 2020-01-17 16:03:19 -08:00
utils Replace THPLayout with at::Layout in Python Argument Parser (#34543) (#34584) 2020-03-12 07:19:00 -07:00
copy_utils.h
CudaIPCTypes.cpp
CudaIPCTypes.h
DataLoader.cpp Enable -Werror=format compile errors on torch exception types (#34019) 2020-03-02 13:25:39 -08:00
DataLoader.h
Device.cpp
Device.h
dl.c
Dtype.cpp Added tensor.is_complex(), is_complex and dtype.is_complex py binding, tensor printing, and dixed the scalar type returned for complex float (#33268) 2020-02-20 13:38:01 -08:00
Dtype.h
DynamicTypes.cpp Rename TensorTypeId to DispatchKey (#32154) 2020-01-15 11:16:08 -08:00
DynamicTypes.h
empty.c Don't use RTLD_GLOBAL to load _C. (#31162) 2020-01-09 07:28:15 -08:00
Exceptions.cpp Add pybind11 exception translator (#30588) 2020-02-18 11:33:29 -08:00
Exceptions.h Enable -Werror=format compile errors on torch exception types (#34019) 2020-03-02 13:25:39 -08:00
Generator.cpp Deprecate tensor.type() (#30281) 2019-12-05 10:55:34 -08:00
Generator.h
Layout.cpp
Layout.h
MemoryFormat.cpp
MemoryFormat.h
Module.cpp Stop using ctypes to interface with CUDA libraries. (#33678) 2020-03-11 07:22:46 -07:00
Module.h
PtrWrapper.cpp
PtrWrapper.h
python_dimname.cpp Remove unnecessary ATen/core/EnableNamedTensor.h (#31117) 2019-12-12 09:53:07 -08:00
python_dimname.h Remove unnecessary ATen/core/EnableNamedTensor.h (#31117) 2019-12-12 09:53:07 -08:00
python_headers.h
PythonTypes.h
QScheme.cpp
QScheme.h
README.md Use pybind11::gil_scoped_* functions instead of AutoGIL/AutoNoGIL (#30274) 2019-12-02 12:19:58 -08:00
serialization.cpp
serialization.h
Size.cpp [jit] do the code reorg (#33851) 2020-02-27 13:02:51 -08:00
Size.h
Storage.cpp
Storage.h
StorageDefs.h
stub.cpp
THP_export.h
THP.h
TypeInfo.cpp
TypeInfo.h
Types.h
utils.cpp
utils.h
WindowsTorchApiMacro.h

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 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.

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.)