<p dir="auto">I'm getting this unexpected error on pytorch nightly (1.8.0.dev20201025) when I subclass Tensor and call <code class="notranslate">new_ones</code> on an instance of my subclass.</p>
<h2 dir="auto">To Reproduce</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from torch import Tensor

class MyTensor(Tensor):
    pass

a = MyTensor([1,2,3])
a.new_ones(320)"><pre class="notranslate"><code class="notranslate">from torch import Tensor

class MyTensor(Tensor):
    pass

a = MyTensor([1,2,3])
a.new_ones(320)
</code></pre></div>
<h2 dir="auto">Expected result</h2>
<p dir="auto">A tensor of 320 ones.</p>
<h2 dir="auto">Actual result</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-3-75c7d48cef15&gt; in &lt;module&gt;
      5 
      6 a = MyTensor([1,2,3])
----&gt; 7 a.new_ones(320)

~/anaconda3/envs/sped3090/lib/python3.7/site-packages/torch/tensor.py in __torch_function__(cls, func, types, args, kwargs)
   1022 
   1023         with _C.DisableTorchFunction():
-&gt; 1024             ret = func(*args, **kwargs)
   1025             return _convert(ret, cls)
   1026 

TypeError: new_ones() missing 1 required positional arguments: &quot;size&quot;"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-3-75c7d48cef15&gt; in &lt;module&gt;
      5 
      6 a = MyTensor([1,2,3])
----&gt; 7 a.new_ones(320)

~/anaconda3/envs/sped3090/lib/python3.7/site-packages/torch/tensor.py in __torch_function__(cls, func, types, args, kwargs)
   1022 
   1023         with _C.DisableTorchFunction():
-&gt; 1024             ret = func(*args, **kwargs)
   1025             return _convert(ret, cls)
   1026 

TypeError: new_ones() missing 1 required positional arguments: "size"
</code></pre></div>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PyTorch version: 1.8.0.dev20201025
Is debug build: True
CUDA used to build PyTorch: 11.0
ROCM used to build PyTorch: N/A

OS: Linux Mint 20 (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.16.3

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 3090
Nvidia driver version: 455.23.04
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.1
[pip3] torch==1.8.0.dev20201025
[pip3] torchvision==0.8.0.dev20201025
[conda] blas                      1.0                         mkl    conda-forge
[conda] cudatoolkit               11.0.221             h6bb024c_0
[conda] mkl                       2020.2                      256
[conda] mkl-service               2.3.0            py37he904b0f_0
[conda] mkl_fft                   1.2.0            py37h23d657b_0
[conda] mkl_random                1.1.1            py37h0573a6f_0
[conda] numpy                     1.19.1           py37hbc911f0_0
[conda] numpy-base                1.19.1           py37hfa32c7d_0
[conda] pytorch                   1.8.0.dev20201025 py3.7_cuda11.0.221_cudnn8.0.3_0    pytorch-nightly
[conda] torchvision               0.8.0.dev20201025      py37_cu110    pytorch-nightly"><pre class="notranslate"><code class="notranslate">PyTorch version: 1.8.0.dev20201025
Is debug build: True
CUDA used to build PyTorch: 11.0
ROCM used to build PyTorch: N/A

OS: Linux Mint 20 (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.16.3

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 3090
Nvidia driver version: 455.23.04
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.1
[pip3] torch==1.8.0.dev20201025
[pip3] torchvision==0.8.0.dev20201025
[conda] blas                      1.0                         mkl    conda-forge
[conda] cudatoolkit               11.0.221             h6bb024c_0
[conda] mkl                       2020.2                      256
[conda] mkl-service               2.3.0            py37he904b0f_0
[conda] mkl_fft                   1.2.0            py37h23d657b_0
[conda] mkl_random                1.1.1            py37h0573a6f_0
[conda] numpy                     1.19.1           py37hbc911f0_0
[conda] numpy-base                1.19.1           py37hfa32c7d_0
[conda] pytorch                   1.8.0.dev20201025 py3.7_cuda11.0.221_cudnn8.0.3_0    pytorch-nightly
[conda] torchvision               0.8.0.dev20201025      py37_cu110    pytorch-nightly
</code></pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/hameerabbasi/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/hameerabbasi">@hameerabbasi</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/rgommers/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/rgommers">@rgommers</a></p>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">Pickling and then unpickling a subclass of <code class="notranslate">Tensor</code> should result in an object of the same class as what was pickled. However, it doesn't - instead it always creates something of class <code class="notranslate">Tensor</code>.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">The following prints <code class="notranslate">torch.Tensor</code>, however it should print <code class="notranslate">_T</code>.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class _T(torch.Tensor): ...
t = tensor([1]).as_subclass(_T)
print(type(pickle.loads(pickle.dumps(t))))"><pre class="notranslate"><code class="notranslate">class _T(torch.Tensor): ...
t = tensor([1]).as_subclass(_T)
print(type(pickle.loads(pickle.dumps(t))))
</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">It should print <code class="notranslate">_T</code>.</p>
<h2 dir="auto">Environment</h2>
<p dir="auto"><code class="notranslate">torch.__version__</code> == 1.7.0, running on Ubuntu.</p>
<h2 dir="auto">Additional context</h2>
<p dir="auto">Placing the following method in the base class resolves the problem:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    def __reduce_ex__(self,proto):
        torch.utils.hooks.warn_if_has_hooks(self)
        args = (type(self), self.storage(), self.storage_offset(), tuple(self.size()), self.stride())
        if self.is_quantized: args = args + (self.q_scale(), self.q_zero_point())
        f = _fa_rebuild_qtensor if self.is_quantized else  _fa_rebuild_tensor
        return (f, args + (self.requires_grad, OrderedDict()))"><pre class="notranslate"><code class="notranslate">    def __reduce_ex__(self,proto):
        torch.utils.hooks.warn_if_has_hooks(self)
        args = (type(self), self.storage(), self.storage_offset(), tuple(self.size()), self.stride())
        if self.is_quantized: args = args + (self.q_scale(), self.q_zero_point())
        f = _fa_rebuild_qtensor if self.is_quantized else  _fa_rebuild_tensor
        return (f, args + (self.requires_grad, OrderedDict()))
</code></pre></div>
<p dir="auto">Perhaps something like this could be used to fix this issue.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/hameerabbasi/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/hameerabbasi">@hameerabbasi</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/rgommers/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/rgommers">@rgommers</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/mruberry/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/mruberry">@mruberry</a></p>