pytorch/torch
Edward Z. Yang f7365eca90 Add unbacked symints support; item works now (#90624)
The big idea is to add `create_unbacked_symfloat` and `create_unbacked_symint` to ShapeEnv, allowing you to allocate symbolic floats/ints corresponding to data you don't know about at compile time. Then, instead of immediately erroring out when you try to call local_scalar_dense on a FakeTensor, we instead create a fresh symint/symfloat and return that.

There a bunch of odds and ends that need to be handled:

* A number of `numel` calls converted to `sym_numel`
* When we finally return from item(), we need to ensure we actually produce a SymInt/SymFloat when appropriate. The previous binding code assumed that you would have to get a normal Python item. I add a pybind11 binding for Scalar (to PyObject only) and refactor the code to use that. There is some trickiness where you are NOT allowed to go through c10::SymInt if there isn't actually any SymInt involved. See comment.
* One of our unit tests tripped an implicit data dependent access which occurs when you pass a Tensor as an argument to a sizes parameter. This is also converted to support symbolic shapes
* We now support tracking bare SymInt/SymFloat returns in proxy tensor mode (this was already in symbolic-shapes branch)
* Whenever we allocate an unbacked symint, we record the stack trace it was allocated at. These get printed when you attempt data dependent access on the symint (e.g., you try to guard on it)
* Subtlety: unbacked symints are not necessarily > 1. I added a test for this.

These unbacked symints are not very useful right now as you will almost always immediately raise an error later when you try to guard on them. The next logical step is adding an assertion refinement system that lets ShapeEnv learn facts about unbacked symints so it can do a better job eliding guards that are unnecessary.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/90624
Approved by: https://github.com/Skylion007, https://github.com/voznesenskym
2022-12-12 13:33:07 +00:00
..
_C functorch.grad support for autograd.Function (#89860) 2022-12-08 19:31:04 +00:00
_C_flatbuffer
_decomp [inductor][Reland] Use decomposition for _to_copy (#90494) 2022-12-09 16:51:50 +00:00
_dispatch Add crossref debug mode for functionalization, catches stride errors (#89498) 2022-11-23 04:18:25 +00:00
_dynamo [Dynamo] Support access nn.Module keys (#90502) 2022-12-12 09:15:42 +00:00
_functorch Add logging for aot autograd and unified debug flag (#88987) 2022-12-09 17:28:10 +00:00
_inductor inductor(CPU): add Conv+binary+unary fusion filter (#90259) 2022-12-12 06:04:55 +00:00
_lazy
_prims [primTorch] Add prim and ref for as_strided_scatter (#88426) 2022-12-08 00:17:39 +00:00
_prims_common [primTorch] Add prim and ref for as_strided_scatter (#88426) 2022-12-08 00:17:39 +00:00
_refs [primTorch] Add prim and ref for as_strided_scatter (#88426) 2022-12-08 00:17:39 +00:00
_subclasses Add unbacked symints support; item works now (#90624) 2022-12-12 13:33:07 +00:00
amp
ao Fix non-existing parameters in docstrings (#90505) 2022-12-09 21:43:09 +00:00
autograd functorch.grad support for autograd.Function (#89860) 2022-12-08 19:31:04 +00:00
backends Add an env var to skip cudnn version compatibility check (#89184) 2022-11-17 20:10:52 +00:00
contrib
cpu
csrc Add unbacked symints support; item works now (#90624) 2022-12-12 13:33:07 +00:00
cuda Add a way to visualize memory snapshot traces (#90348) 2022-12-10 02:45:11 +00:00
distributed [FSDP] Fix _pre_forward type annotation (#90621) 2022-12-11 06:39:38 +00:00
distributions Add a transform for positive-definite matrices. (#76777) 2022-12-08 09:18:44 +00:00
fft
futures
fx Add unbacked symints support; item works now (#90624) 2022-12-12 13:33:07 +00:00
jit Fix non-existing parameters in docstrings (#90505) 2022-12-09 21:43:09 +00:00
legacy
lib Migrate PyTorch to C++17 (#85969) 2022-12-08 02:27:48 +00:00
linalg update docstring for torch.linalg.lstsq (#89383) 2022-11-25 21:31:53 +00:00
masked Implement correction argument in torch.masked.{std,var} (#87118) 2022-12-08 15:59:09 +00:00
monitor
multiprocessing
nested Fix inaccuracy in nt constructor documentation + broken rendering (#89152) 2022-11-16 22:32:46 +00:00
nn Fix non-existing parameters in docstrings in torch/nn (#90596) 2022-12-10 14:37:31 +00:00
onnx [ONNX] Introduce ONNX reference evaluator for verification (#89808) 2022-12-10 01:29:12 +00:00
optim Fixed a couple of mistakes in type annotations in optim package (#90216) 2022-12-09 03:20:21 +00:00
package [rfc][pkg] check spec for module source before falling back to file in package exporter (#90258) 2022-12-08 20:24:45 +00:00
profiler Fix exception causes all over the codebase (#90271) 2022-12-07 04:29:00 +00:00
quantization Reland "Add heirachical module names to torchFX graph.node" (#90205) 2022-12-09 06:20:31 +00:00
signal Implement old windows in Python (#87082) 2022-11-25 11:09:28 +00:00
sparse
special
testing Skip some unittests (#90609) 2022-12-11 23:53:05 +00:00
utils Fix non-existing parameters in docstrings (#90505) 2022-12-09 21:43:09 +00:00
__config__.py
__future__.py
__init__.py Track torch.compile calls (#90310) 2022-12-08 21:41:15 +00:00
_appdirs.py
_classes.py
_deploy.py
_guards.py Shape guard structure (#90679) 2022-12-12 09:50:00 +00:00
_jit_internal.py
_linalg_utils.py Remove cpu path from lobpcg's basis helper (#88984) 2022-11-14 17:49:30 +00:00
_lobpcg.py
_lowrank.py
_meta_registrations.py [primTorch] Add prim and ref for as_strided_scatter (#88426) 2022-12-08 00:17:39 +00:00
_namedtensor_internals.py
_ops.py Add crossref debug mode for functionalization, catches stride errors (#89498) 2022-11-23 04:18:25 +00:00
_python_dispatcher.py
_six.py
_sources.py
_storage_docs.py
_tensor_docs.py Give std/var correction overloads proper defaults (#56398) 2022-12-07 15:15:00 +00:00
_tensor_str.py
_tensor.py Fix some typed storage is deprecated warnings. (#89867) 2022-12-07 20:09:57 +00:00
_torch_docs.py Give std/var correction overloads proper defaults (#56398) 2022-12-07 15:15:00 +00:00
_utils_internal.py
_utils.py Revert "[follow-up] Python Attr Serialization (#88913)" 2022-12-02 20:14:11 +00:00
_VF.py
_vmap_internals.py
_weights_only_unpickler.py Sparse CSC/BSR/BSC serialization and pickle support (#89553) 2022-11-23 20:56:48 +00:00
abi-check.cpp
CMakeLists.txt
custom_class_detail.h
custom_class.h
extension.h
functional.py Add type hint to torch.norm and Tensor.norm (#89728) 2022-11-29 02:09:51 +00:00
hub.py Revert "Dynamo, FX, Inductor Progress Bars (#88384)" 2022-12-09 16:32:25 +00:00
library.h
library.py
overrides.py Fix non-existing parameters in docstrings (#90505) 2022-12-09 21:43:09 +00:00
py.typed
quasirandom.py
random.py
README.txt
return_types.py
script.h
serialization.py Fix typos in messages under torch (#89049) 2022-11-17 04:18:14 +00:00
storage.py Fix some typed storage is deprecated warnings. (#89867) 2022-12-07 20:09:57 +00:00
torch_version.py
types.py

Note [TH abstraction violation]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TH/THC provide some hpp headers, which are proper C++ headers rather than
C headers.  These headers serve double duty as *internal implementation
detail* headers, whose contents should largely not be used by external
clients.

Ideally, we would not install these headers at all; instead, you should
use public functions (in headers like `THTensor.h`, NOT `THTensor.hpp`)
to manipulate these structs.  However, there are a few places
in torch/csrc where we violate this abstraction.  They are marked with
a pointer to this note.  Each of those sites will have to be refactored
when we refactor the guts of THTensor and related structures.