# Tensor type memory usage

**URL:** <https://discuss.pytorch.org/t/tensor-type-memory-usage/150933>\
**Category:** Memory Format\
**Created:** [May 6, 2022, 8:59am UTC](https://discuss.pytorch.org/t/tensor-type-memory-usage/150933 "2022-05-06T08:59:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fst\_Msn](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fst_msn/32/31467_2.png) [@Fst\_Msn](https://discuss.pytorch.org/u/Fst_Msn)\
**Post date:** [May 6, 2022, 8:59am UTC](https://discuss.pytorch.org/t/tensor-type-memory-usage/150933/1 "2022-05-06T08:59:47Z")

</div>

I would like to know how much memory (on GPU) do the different torch  
types allocate, but I was not able to find it anywhere in the docs.

At the following link all the possible Tensor types are specified, but nothing is said about memory usage.  
[https://pytorch.org/docs/stable/tensors.html](https://pytorch.org/docs/stable/tensors.html)

For example, i suppose that the following two tensors have different memory usage, but is there something I can print out ?

```auto
a = torch.ones([10000], dtype=torch.uint8, device='cuda:0')
b = torch.ones([10000], dtype=torch.float32, device='cuda:0')

```

I see that in the protected attributes there is “\_cdata”, what is it ?  
I initially thought that it was the attribute I was looking for, but it can’t be, since:

```auto
a = torch.ones([10000], dtype=torch.uint8, device='cuda:0')
b = torch.ones([10000], dtype=torch.uint8, device='cuda:0')

a._cdata
b._cdata

```

prints out:

> 94394199987296  
> 94392001480192

---

<div class="post-metadata">

**Author:** ![Matias\_Vasquez](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/matias_vasquez/32/47847_2.png) [@Matias\_Vasquez](https://discuss.pytorch.org/u/Matias_Vasquez)\
**Post date:** [May 6, 2022, 9:18am UTC](https://discuss.pytorch.org/t/tensor-type-memory-usage/150933/2 "2022-05-06T09:18:21Z")

</div>

With [Storage](https://pytorch.org/docs/stable/storage.html) you can get some information about what is happening.

For example `data_ptr()` will give you the address of the first element.

```python
a = torch.ones([10000], dtype=torch.uint8, device='cuda:0')
b = torch.ones([10000], dtype=torch.float32, device='cuda:0')

print(a.storage().data_ptr())
print(b.storage().data_ptr())

# 140208108520960
# 140208108531200

```

In the documentation linked above you can see all the information that you can get from there, such as

- get\_device()
- nbytes()
- is\_pinned()
- …

Using `nbytes()` you can see the difference that you wanted to see between `uint8` and `float32`

```python
print(a.storage().nbytes())
print(b.storage().nbytes())

# 10000
# 40000

```

Hope this helps 😄

---

<div class="post-metadata">

**Author:** ![Fst\_Msn](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fst_msn/32/31467_2.png) [@Fst\_Msn](https://discuss.pytorch.org/u/Fst_Msn)\
**Post date:** [May 6, 2022, 9:31am UTC](https://discuss.pytorch.org/t/tensor-type-memory-usage/150933/3 "2022-05-06T09:31:27Z")

</div>

Hi, thank you for your answer.

I tried with your suggestion, but it gives me and error:

```auto
a.storage().nbytes()

```

> AttributeError: ‘torch.cuda.ByteStorage’ object has no attribute ‘nbytes’

But I found out that to get the number of bytes:

```auto
a = torch.ones([10000], dtype=torch.unit8, device='cuda:0')
b = torch.ones([10000], dtype=torch.float32, device='cuda:0')

a.element_size()
b.element_size()

```

> 1  
> 4

Anyway, I could not find any information about what \_cdata() represents ?

---

<div class="post-metadata">

**Author:** ![Matias\_Vasquez](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/matias_vasquez/32/47847_2.png) [@Matias\_Vasquez](https://discuss.pytorch.org/u/Matias_Vasquez)\
**Post date:** [May 6, 2022, 9:36am UTC](https://discuss.pytorch.org/t/tensor-type-memory-usage/150933/4 "2022-05-06T09:36:38Z")

</div>

Oh yeah, `nbytes()` was intoduced on pytorch `1.11` I think. So if you want to use that you need to update your version with

```python
!pip install --update torch
!pip install --update torchvision
# Or however you need to update it (if you want to do it like this, but you also got an alternative)

```

* * *

And if I find anything on `_cdata()` I will post it, but I also have no idea
