What’s the most Pytorch-ich way to add an attribute to a Tensor? Is it necessary to implement a new class that inherits from the Tensor class? I would appreciate an example.
In the end, the goal is to allow access as follows:
t: Tensor
attr_to_set: Any
t.attr = attr_to_set
attr: Any = t.attr


So far, I’ve only found these two posts.

A suggestion 2 to directly inherit from the Tensor class. The problem is that it seems like such a new subclass should implement methods like __new__, clone, and to, rather than only __init__. Since there’s no official answer in that post, I’m not sure that there aren’t additional functions that require handling. I don’t want to create a new Tensor subclass that breaks the Tensor interface.
A suggestion 2 to use the tensor.as_subclass method. The problem is that I’m not sure how to use it to set a new attribute at the initialization or afterward.  Therefore, I think that it doesn’t solve our case here.


Thanks