# How to add custom attributes to pytorch tensor

**URL:** <https://dev-discuss.pytorch.org/t/how-to-add-custom-attributes-to-pytorch-tensor/3018>\
**Category:** Uncategorized\
**Created:** [May 22, 2025, 12:59am UTC](https://dev-discuss.pytorch.org/t/how-to-add-custom-attributes-to-pytorch-tensor/3018 "2025-05-22T00:59:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bailuan](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/bailuan/32/2742_2.png) [@bailuan](https://dev-discuss.pytorch.org/u/bailuan)\
**Post date:** [May 22, 2025, 12:59am UTC](https://dev-discuss.pytorch.org/t/how-to-add-custom-attributes-to-pytorch-tensor/3018/1 "2025-05-22T00:59:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bailuan](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/bailuan/32/2742_2.png) [@bailuan](https://dev-discuss.pytorch.org/u/bailuan)\
**Post date:** [May 22, 2025, 1:05am UTC](https://dev-discuss.pytorch.org/t/how-to-add-custom-attributes-to-pytorch-tensor/3018/2 "2025-05-22T01:05:41Z")

</div>

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

---

<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:** [May 22, 2025, 10:44pm UTC](https://dev-discuss.pytorch.org/t/how-to-add-custom-attributes-to-pytorch-tensor/3018/3 "2025-05-22T22:44:08Z")

</div>

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](https://docs.pytorch.org/docs/stable/notes/extending.html)

---

<div class="post-metadata">

**Author:** ![bailuan](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/bailuan/32/2742_2.png) [@bailuan](https://dev-discuss.pytorch.org/u/bailuan)\
**Post date:** [May 28, 2025, 2:34am UTC](https://dev-discuss.pytorch.org/t/how-to-add-custom-attributes-to-pytorch-tensor/3018/4 "2025-05-28T02:34:03Z")

</div>

Thanks, I have added the additional attr to myTensorImpl based on tensorImpl, it works.
