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 and pytorch/torch/_subclasses/fake_tensor.py at main · pytorch/pytorch · GitHub
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 and examples GitHub - albanD/subclass_zoo, but I can’t find any document about subclassing torch.Tensor in detail. The document in Extending PyTorch — PyTorch 2.1 documentation is not qualified for users to define subclasses of torch.Tensor. Here are some questions about subclassing torch.Tensor:
-
In the
__new__method, Sometimes_make_wrapper_subclassis used, sometimes_make_subclassis used, and sometimes neither is used, which confused me a lot. -
How should the
__new__and__init__methods be defined? In other words, what are the responsibilities of__new__and__init__? -
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__?