pytorch/torch/csrc
gchanan ae0c04c773
Add torch.empty, torch.full and new_ size Tensor factory methods. (#5668)
* Add torch.empty, torch.full and new_ size Tensor factory methods.

This adds torch.full, torch.empty equivalents of np.full, np.empty.
In addition, this adds size-based Tensor factory methods new_empty, new_ones, new_full, new_zeros,
which is meant to complete the separation of the legacy "new" method into data-based and size-based
functions.

This also fixes an issue in sparse zeros_like when the dtype didn't match the argument dtype.

* Get rid of unnecessary zero in sparse tensor zeros_like.

* Fix test if only 1 cuda device.
2018-03-09 15:29:29 -05:00
..
autograd Support native namespace functions with type dispatch. (#5576) 2018-03-09 10:52:53 -05:00
cuda add gpu guard for broadcast_coalesce (#5655) 2018-03-08 21:59:19 -05:00
distributed Delete unused files (#5500) 2018-03-01 14:28:06 -05:00
generic torch.load() / torch.save() support arbitrary file-like object (#5466) 2018-03-08 22:18:55 -05:00
jit Defer shape analysis failures until runtime (#5574) 2018-03-09 18:43:03 +01:00
nn Delete dead Tensor code paths (#5417) 2018-02-27 17:58:09 -05:00
onnx PyTorch now uses operator versioning. 2017-11-30 23:09:45 -05:00
tensor Add dtype to torch.Tensor constructors and accept them in set_default_tensor_type (#5444) 2018-03-01 14:06:55 -05:00
utils Add torch.empty, torch.full and new_ size Tensor factory methods. (#5668) 2018-03-09 15:29:29 -05:00
allocators.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
allocators.h Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
assertions.cpp Move jit/assert.h to csrc/assertions.h (#3442) 2017-11-02 13:26:51 -04:00
assertions.h Improve Windows Compatibility (for csrc/scripts) (#2941) 2017-11-08 19:51:35 +01: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 Delete dead Tensor code paths (#5417) 2018-02-27 17:58:09 -05:00
DataLoader.cpp Prefix DataLoaderIter with underscore to discourage subclassing (#5619) 2018-03-08 11:09:51 +01:00
DataLoader.h Compile DataLoader.cpp separately (#5507) 2018-03-02 05:54:33 -05:00
dl.c Improve Windows Compatibility (for csrc/scripts) (#2941) 2017-11-08 19:51:35 +01:00
Dtype.cpp Add numpy-style dtypes to Variable factories. (#5245) 2018-02-20 11:04:14 -05:00
Dtype.h Add numpy-style dtypes to Variable factories. (#5245) 2018-02-20 11:04:14 -05:00
DynamicTypes.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
DynamicTypes.h Add dtype to torch.Tensor constructors and accept them in set_default_tensor_type (#5444) 2018-03-01 14:06:55 -05:00
Exceptions.cpp Python-free build of autograd + jit (#5356) 2018-03-08 15:13:10 -05:00
Exceptions.h Python-free build of autograd + jit (#5356) 2018-03-08 15:13:10 -05:00
Generator.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Generator.h Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Module.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Module.h Delete dead Tensor code paths (#5417) 2018-02-27 17:58:09 -05:00
nvrtc.cpp lazy-load nvrtc and libcuda (#3408) 2017-11-01 06:07:03 -04:00
PtrWrapper.cpp DataChannel tests rewrite (#42); DataChannel isend and irecv implementation (#44) 2017-01-31 01:58:09 +01:00
PtrWrapper.h Fixes for testing on FB infra (#1009) 2017-03-15 18:37:11 -04:00
PythonTypes.h Implement Variable.storage() (#3765) 2017-11-20 14:18:07 -05:00
README.md Comment that data of THStorage may be NULL. 2017-07-20 10:55:35 -04:00
serialization.cpp torch.load() / torch.save() support arbitrary file-like object (#5466) 2018-03-08 22:18:55 -05:00
serialization.h torch.load() / torch.save() support arbitrary file-like object (#5466) 2018-03-08 22:18:55 -05:00
Size.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Size.h Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Storage.cpp Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Storage.h Expose torch.HalfTensor 2017-02-27 19:35:47 -05:00
THP_API.h Refactor _C extension to export some utilities 2016-09-21 08:36:54 -07:00
THP_export.h Improve Windows Compatibility (for csrc/scripts) (#2941) 2017-11-08 19:51:35 +01:00
THP.h Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05:00
Types.h Improve Windows Compatibility (for csrc/scripts) (#2941) 2017-11-08 19:51:35 +01:00
utils.cpp Use ATen infer_size implementation rather than TH. (#4781) 2018-01-22 15:34:31 -05:00
utils.h Some additional clean-ups (#5505) 2018-03-05 17:45:02 -05: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.)