How to add custom attributes to pytorch tensor

How can I add custom attributes like device_local or host_local to a PyTorch tensor without affecting TensorImpl or StorageImpl? I have a use case where I need to convert an external tensor into a PyTorch tensor while preserving such properties.

My ultimate goal is to record the memory type stored in IREE’s buffer view (a custom memory view defined by the IREE compiler) when converting it into a PyTorch tensor. This information is crucial for my use case, because I need to reconstruct the buffer view later based on it

Hey!
You can add any attribute to the python object of the Tensor!
If you need this attribute to be preserved when operations are done on this Tensor, you can create a subclass that will preserve that attribute as operations happen: Extending PyTorch — PyTorch 2.7 documentation