pytorch/torch
Edward Yang 4d72538f80 Give Tensor a trivial (for now) metaclass _TensorMeta (#56147)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56147

This is support of #55686, you can see the broader context of the metaclass in
a more complete PR #56017.  The short story is that in the future I want to
give Tensor a non-trivial metaclass, so to derisk the change first I give it a
trivial metaclass to shake out any bugs that might be caused by it.  The
metaclass shouldn't have any performance impact on Tensor as it only gets
invoked upon subclass creation.

By the way, it was totally not documented how to create metaclasses in the Python
C API, and it took a good bit of trial error to figure it out (and the answer is
now immortalized in https://stackoverflow.com/q/67077317/23845 -- the things
that I got wrong in earlier versions of the PR included setting tp_basicsize
incorrectly, incorrectly setting Py_TPFLAGS_HAVE_GC on the metaclass--you want
to leave it unset so that it inherits, and determining that tp_init is what
actually gets called when you construct a class, not tp_call as another
not-to-be-named StackOverflow question suggests).

Aside: Ordinarily, adding a metaclass to a class is a user visible change, as
it means that it is no longer valid to mixin another class with a different
metaclass. However, because _C._TensorBase is a C extension object, it will
typically conflict with most other metaclasses, so this is not BC breaking.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Test Plan: Imported from OSS

Reviewed By: H-Huang

Differential Revision: D28028747

Pulled By: ezyang

fbshipit-source-id: c1e35a986aeb3db540c73d188f53dce951eeed33
2021-04-28 09:24:21 -07:00
..
_C Give Tensor a trivial (for now) metaclass _TensorMeta (#56147) 2021-04-28 09:24:21 -07:00
ao [sparsity] Moving the sparsity python files to OSS (#56617) 2021-04-22 14:07:31 -07:00
autograd [profiler] Support for trace metadata (#56575) 2021-04-28 05:12:34 -07:00
backends Add type annotations to nnapi (#48142) 2021-04-26 19:08:07 -07:00
contrib
csrc Give Tensor a trivial (for now) metaclass _TensorMeta (#56147) 2021-04-28 09:24:21 -07:00
cuda Make old_gpu warning dynamic (#56621) 2021-04-23 17:52:07 -07:00
distributed Torchelastic: add support for the new error file format (#57084) 2021-04-28 00:04:45 -07:00
distributions Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
fft torch.fft: Document out argument (#56732) 2021-04-24 17:14:00 -07:00
for_onnx Un-ignore F403 in .flake8 (#55838) 2021-04-13 09:24:07 -07:00
futures Add CUDA support to a user-created torch.futures.Future (#56517) 2021-04-23 08:13:56 -07:00
fx [FX] fix normalization issues with lists of tensors (#57004) 2021-04-27 20:02:00 -07:00
jit Infer types (#56832) 2021-04-25 13:01:55 -07:00
legacy
lib Fix ProcessGroupMPITest.cpp Gather, Scatter and SendRecv. Enable ProcessGroupMPITest (#56709) 2021-04-28 08:39:08 -07:00
linalg Improved docs for torch.linalg (#56265) 2021-04-27 11:16:09 -07:00
multiprocessing Fix mp serialization for integer nn.Parameter on CUDA (#56529) 2021-04-22 16:21:04 -07:00
nn change ddpLoggingData struct to map or dict (#56641) 2021-04-28 06:43:25 -07:00
onnx Revert "Revert [ONNX] Redesign inplace conversion" (#56675) 2021-04-22 22:22:16 -07:00
optim [optim] take kw-only argument for functional optim APIs (#56185) 2021-04-15 20:08:04 -07:00
package [torch.package] is_from_package check (#56729) 2021-04-22 22:28:07 -07:00
profiler [profiler] Support for trace metadata (#56575) 2021-04-28 05:12:34 -07:00
quantization ns for fx: additional bugfix for user defined functions (#57028) 2021-04-27 16:29:03 -07:00
sparse
special Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
testing change ddpLoggingData struct to map or dict (#56641) 2021-04-28 06:43:25 -07:00
utils model_dump tool for model inspection (#56868) 2021-04-28 07:33:10 -07:00
__config__.py
__future__.py
__init__.py add deterministic path for index_copy_cpu (#56900) 2021-04-26 16:57:47 -07:00
_appdirs.py
_autograd_functions.py
_classes.py
_deploy.py
_jit_internal.py Support custom Python classes in CUDAFuture (#56516) 2021-04-24 07:06:28 -07:00
_linalg_utils.py
_lobpcg.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
_lowrank.py
_namedtensor_internals.py
_ops.py
_python_dispatcher.py
_six.py
_storage_docs.py
_tensor_docs.py Support __pos__ and positive (#55891) 2021-04-27 13:23:59 -07:00
_tensor_str.py Add CSR (compressed sparse row) layout for sparse tensors (#50937) 2021-04-12 10:09:12 -07:00
_tensor.py Support __pos__ and positive (#55891) 2021-04-27 13:23:59 -07:00
_torch_docs.py Support __pos__ and positive (#55891) 2021-04-27 13:23:59 -07:00
_utils_internal.py
_utils.py Merge CUDA Streams and Events (#53902) 2021-04-05 08:19:55 -07:00
_VF.py
_vmap_internals.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
abi-check.cpp
CMakeLists.txt Add minidump collection via breakpad (#55647) 2021-04-16 13:05:01 -07:00
custom_class_detail.h
custom_class.h [PyTorch][easy] Move more strings in torch::class_ (#54547) 2021-03-29 12:27:11 -07:00
deploy.h Lint trailing newlines (#54737) 2021-03-30 13:09:52 -07:00
extension.h
functional.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
hub.py Add lint for unqualified type: ignore (#56290) 2021-04-21 08:07:23 -07:00
library.h
overrides.py Support __pos__ and positive (#55891) 2021-04-27 13:23:59 -07:00
py.typed
quasirandom.py Port NumPy typing testing style to PyTorch (#54234) 2021-04-15 01:25:16 -07:00
random.py Port NumPy typing testing style to PyTorch (#54234) 2021-04-15 01:25:16 -07:00
README.txt
script.h Implement public API InferenceMode and its error handling (#55008) 2021-03-31 10:48:00 -07:00
serialization.py Hide module paths leaking in the documentation. (#54585) 2021-04-27 10:58:01 -07:00
storage.py
types.py Add type annotations to nnapi (#48142) 2021-04-26 19:08:07 -07:00

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.