# What's the difference between torch.Tensor.\_make\_subclass and torch.Tensor.\_make\_wrapper\_subclass

**URL:** <https://dev-discuss.pytorch.org/t/whats-the-difference-between-torch-tensor-make-subclass-and-torch-tensor-make-wrapper-subclass/1839>\
**Category:** Uncategorized\
**Created:** [January 26, 2024, 10:44am UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-torch-tensor-make-subclass-and-torch-tensor-make-wrapper-subclass/1839 "2024-01-26T10:44:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuokay](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/shuokay/32/594_2.png) [@shuokay](https://dev-discuss.pytorch.org/u/shuokay)\
**Post date:** [January 26, 2024, 10:44am UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-torch-tensor-make-subclass-and-torch-tensor-make-wrapper-subclass/1839/1 "2024-01-26T10:44:21Z")

</div>

Both of `torch.Tensor._make_subclass` and `torch.Tensor._make_wrapper_subclass` are used in Subclassing `torch.Tensor` such as [subclass\_zoo/quantized\_tensor.py at main · albanD/subclass\_zoo · GitHub](https://github.com/albanD/subclass_zoo/blob/main/quantized_tensor.py#L39) and [pytorch/torch/\_subclasses/fake\_tensor.py at main · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/blob/main/torch/_subclasses/fake_tensor.py#L1203)  
What’s the difference between them?  
What’s more, is there any document illustrating how to subclassing torch.Tensor. I find there are many discussions such as [Subclassing torch.Tensor - PyTorch Forums](https://discuss.pytorch.org/t/subclassing-torch-tensor/23754) and examples [GitHub - albanD/subclass\_zoo](https://github.com/albanD/subclass_zoo/tree/main), but I can’t find any document about subclassing `torch.Tensor` in detail. The document in [Extending PyTorch — PyTorch 2.1 documentation](https://pytorch.org/docs/stable/notes/extending.html#extending-torch-python-api) is not qualified for users to define subclasses of torch.Tensor. Here are some questions about subclassing torch.Tensor:

1. In the ` __new__ ` method, Sometimes `_make_wrapper_subclass` is used, sometimes `_make_subclass` is used, and sometimes neither is used, which confused me a lot.

2. How should the ` __new__ ` and ` __init__ ` methods be defined? In other words, what are the responsibilities of ` __new__ ` and ` __init__ `?

3. When should we use ` __torch_function__ `, and when should we use ` __torch_dispatch__ `? Should we prioritize using ` __torch_dispatch__ ` according to [What (and Why) is \_\_torch\_dispatch\_\_?](https://dev-discuss.pytorch.org/t/what-and-why-is-torch-dispatch/557)

---

<div class="post-metadata">

**Author:** ![albanD](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/alband/32/9_2.png) [@albanD](https://dev-discuss.pytorch.org/u/albanD)\
**Post date:** [January 26, 2024, 9:10pm UTC](https://dev-discuss.pytorch.org/t/whats-the-difference-between-torch-tensor-make-subclass-and-torch-tensor-make-wrapper-subclass/1839/2 "2024-01-26T21:10:40Z")

</div>

Hey!

The Extending PyTorch doc does talk about subclasses of Tensor a bit below from where you showed: [Extending PyTorch — PyTorch main documentation](https://pytorch.org/docs/main/notes/extending.html#subclassing-torch-tensor)

1. These two functions do quite different things. The main difference is that when you do `_make_subclass()`, the current object is a honest to goodness Tensor with data in its storage and everything. When you do `_make_wrapper_subclass()`, the current object has no data and it is expected that some field on the Tensor will be another Tensor (hence the outer one being called wrapper) that contains real data.
2. This is the same as any Python class: new creates a new instance while init initializes it. It makes very little difference unless you’re considering serialization of objects. General Python doc does cover that in details though.
3. It depends on what you want to do. They are different tools for different jobs. The Extending doc tries to give details, this poster from the PyTorch conference tries to answer that question as well: [BackToPython PTC 2022 Poster - Google Slides](https://docs.google.com/presentation/d/1piuv9nBzyoqdH49D1SoE5OZUPSMpOOFqfSKOhr-ab2c/edit#slide=id.p1)

Hope this helps
