pytorch/torch/csrc
Priya Ramani ac97e953b4 Add dynamic shape support to AOT driver & compiler (#72995)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72995

Add ability to specify input dimensions that need to be dynamic.
Example: if dim 115 can be dynamic in input sizes "1,115;1", then specify dynamic_dims as "115"

Also recompile and update CI models and some asm code as the old ones don't compile with compiler changes in context.cpp

Test Plan: - Compiles and runs BI Bytedoc model with and without dynamic inputs.

Reviewed By: ZolotukhinM

Differential Revision: D34233121

fbshipit-source-id: 35095e549ebd6d3bec98b9abb3f0764366a0ff6f
(cherry picked from commit 33166a9f9ac9194b5df0a35280b57708df255ebd)
2022-02-24 04:30:48 +00:00
..
api Remove duplicate call to objective function in strong wolfe line search in L-BFGS optimizer. (#72773) 2022-02-15 15:33:13 +00:00
autograd [Profiler] Optimize reportMemoryUsage (#71538) 2022-02-20 23:29:13 +00:00
cuda Use irange in PyTorch (#72836) 2022-02-18 19:29:07 +00:00
deploy Use irange in PyTorch (#72836) 2022-02-18 19:29:07 +00:00
distributed Make "server socket not listening" warning logs less noisy (#73149) 2022-02-24 02:33:05 +00:00
generic Rename Typed/UntypedStorage to _Typed/_UntypedStorage (#72540) 2022-02-15 23:53:01 +00:00
jit Add dynamic shape support to AOT driver & compiler (#72995) 2022-02-24 04:30:48 +00:00
lazy Squeeze p2: hook up Squeeze to LazyView (#73067) 2022-02-24 04:30:48 +00:00
monitor torch/monitor: merge Interval and FixedCount stats (#72009) 2022-01-30 23:21:59 +00:00
multiprocessing
onnx Integrate full ONNX check into ONNX export API (#71125) 2022-02-18 18:40:09 +00:00
profiler [Profiler] Split observer implementations based on ProfilerState (#71135) 2022-01-26 18:33:24 +00:00
tensor Check if the iterator is valid before dereferencing it (#72405) 2022-02-23 18:33:46 +00:00
utils Check if the iterator is valid before dereferencing it (#72405) 2022-02-23 18:33:46 +00:00
copy_utils.h
CudaIPCTypes.cpp
CudaIPCTypes.h
DataLoader.cpp
DataLoader.h
Device.cpp
Device.h
dl.c
Dtype.cpp
Dtype.h Codegen: python_torch_functions only include relevant operators (#68693) 2022-01-21 15:37:06 +00:00
DynamicTypes.cpp Rename Typed/UntypedStorage to _Typed/_UntypedStorage (#72540) 2022-02-15 23:53:01 +00:00
DynamicTypes.h Enable Python bindings for UntypedStorage (#68945) 2022-01-20 02:11:34 +00:00
empty.c
Exceptions.cpp torch.linalg routines return torch.linalg.LinAlgError when a numerical error in the computation is found. (#68571) 2021-12-23 10:53:26 -08:00
Exceptions.h Refactor TORCH_DISTRIBUTED_DEBUG implementation (#73166) 2022-02-24 02:33:05 +00:00
Export.h
Generator.cpp [pytorch][aten][cuda] move CUDAGeneratorImpl.h to ATen/cuda (#70650) 2022-01-10 22:27:04 -08:00
Generator.h Codegen: python_torch_functions only include relevant operators (#68693) 2022-01-21 15:37:06 +00:00
Layout.cpp
Layout.h
MemoryFormat.cpp
MemoryFormat.h
Module.cpp Fix error handling TestSetDefaultMobileCPUAllocator 2022-02-22 19:45:49 +00:00
Module.h
python_dimname.cpp
python_dimname.h
python_headers.h
PythonTypes.h Codegen: python_torch_functions only include relevant operators (#68693) 2022-01-21 15:37:06 +00:00
QScheme.cpp
QScheme.h
README.md
serialization.cpp Fix for windows builds with python 3.10 , getting rid of ssize_t (ssize_t is not a C++ defined type) (#71390) 2022-01-18 22:12:41 +00:00
serialization.h
Size.cpp
Size.h
Storage.cpp
Storage.h
StorageDefs.h
Stream.cpp
Stream.h
stub.c
THCGenerateByteType.h
THConcat.h
THGenerateByteType.h
THP.h
TypeInfo.cpp Port amax to structured kernel (#72124) 2022-02-16 06:33:09 +00:00
TypeInfo.h
Types.h
utils.cpp
utils.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 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.)