Tensor type memory usage

Fst_Msn: <p>I would like to know how much memory (on GPU) do the different torch<br>
types allocate, but I was not able to find it anywhere in the docs.</p>
<p>At the following link all the possible Tensor types are specified, but nothing is said about memory usage.<br>
<a href="https://pytorch.org/docs/stable/tensors.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/tensors.html</a></p>
<p>For example, i suppose that the following two tensors have different memory usage, but is there something I can print out ?</p>
<pre><code class="lang-auto">a = torch.ones([10000], dtype=torch.uint8, device='cuda:0')
b = torch.ones([10000], dtype=torch.float32, device='cuda:0')
</code></pre>
<p>I see that in the protected attributes there is “_cdata”, what is it ?<br>
I initially thought that it was the attribute I was looking for, but it can’t be, since:</p>
<pre><code class="lang-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
</code></pre>
<p>prints out:</p>
<blockquote>
<p>94394199987296<br>
94392001480192</p>
</blockquote>

Matias_Vasquez: <p>With <a href="https://pytorch.org/docs/stable/storage.html" rel="noopener nofollow ugc">Storage</a> you can get some information about what is happening.</p>
<p>For example <code>data_ptr()</code> will give you the address of the first element.</p>
<pre><code class="lang-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
</code></pre>
<p>In the documentation linked above you can see all the information that you can get from there, such as</p>
<ul>
<li>get_device()</li>
<li>nbytes()</li>
<li>is_pinned()</li>
<li>…</li>
</ul>
<p>Using <code>nbytes()</code> you can see the difference that you wanted to see between <code>uint8</code> and <code>float32</code></p>
<pre><code class="lang-python">print(a.storage().nbytes())
print(b.storage().nbytes())

# 10000
# 40000
</code></pre>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>

Fst_Msn: <p>Hi, thank you for your answer.</p>
<p>I tried with your suggestion, but it gives me and error:</p>
<pre><code class="lang-auto">a.storage().nbytes()
</code></pre>
<blockquote>
<p>AttributeError: ‘torch.cuda.ByteStorage’ object has no attribute ‘nbytes’</p>
</blockquote>
<p>But I found out that to get the number of bytes:</p>
<pre><code class="lang-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()
</code></pre>
<blockquote>
<p>1<br>
4</p>
</blockquote>
<p>Anyway, I could not find any information about what _cdata() represents ?</p>

Matias_Vasquez: <p>Oh yeah, <code>nbytes()</code> was intoduced on pytorch <code>1.11</code> I think. So if you want to use that you need to update your version with</p>
<pre><code class="lang-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)
</code></pre>
<hr>
<p>And if I find anything on <code>_cdata()</code> I will post it, but I also have no idea</p>