Finding model size

edshkim98: <p>Hi, I am curious about calculating model size (MB) for NN in pytorch.<br>
Is it equivalent to the size of the file from torch.save(model.state_dict(),‘example.pth’)?<br>
Please refer to the image below from PointNet++<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/9/29a65a47a5cb536d66d834c1c1d1e4c05cb6d639.png" data-download-href="https://discuss.pytorch.org/uploads/default/29a65a47a5cb536d66d834c1c1d1e4c05cb6d639" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/9/29a65a47a5cb536d66d834c1c1d1e4c05cb6d639_2_690x124.png" alt="image" data-base62-sha1="5WrZ592uX9Rm9TSbHeykBHhpSxX" width="690" height="124" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/9/29a65a47a5cb536d66d834c1c1d1e4c05cb6d639_2_690x124.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/9/29a65a47a5cb536d66d834c1c1d1e4c05cb6d639_2_1035x186.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/9/29a65a47a5cb536d66d834c1c1d1e4c05cb6d639.png 2x" data-dominant-color="ECEBEC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1222×220 30.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>I wouldn’t depend on the stored size, as the file might be compressed.<br>
Instead you could calculate the number of parameters and buffers, multiply them with the element size and accumulate these numbers as seen here:</p>
<pre><code class="lang-python">model = models.resnet18()
param_size = 0
for param in model.parameters():
    param_size += param.nelement() * param.element_size()
buffer_size = 0
for buffer in model.buffers():
    buffer_size += buffer.nelement() * buffer.element_size()

size_all_mb = (param_size + buffer_size) / 1024**2
print('model size: {:.3f}MB'.format(size_all_mb))
&gt; model size: 44.629MB
</code></pre>

edshkim98: <p>Thank you so much!!! <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

christophezei: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a> I have a question concerning activation’s, do they affect model size? because I am working on model quantization and when i represent activation in lower precision exp 4bits my accuracy drops a lot and model dosen’t learn so what i did is quantize weights in 4 bits and kept activation’s in 8bits so it works now but my question is how does this affect model size.</p>
<p>Thank you in advance</p>

ptrblck: <p>I think it depends on what you would consider counts as the “model size”. I would probably not count the activations to the model size as they usually depend on the input shape as well as the model architecture. Thus, I would only count the internal model states (parameters, buffers, etc.) to the model size.</p>

christophezei: <p>Thank you for your reply. So if I understand correctly if we quantize activations this will reduce model size in training and not in inference ?</p>

ptrblck: <p>Activations are used in both use cases. However, since they do not need to be stored during inference (as there won’t be any gradient calculation) the peak memory usage could still decrease but the expected savings wouldn’t be as large as during training.</p>

christophezei: <p>I understand, is there still a way to calculate the capacity of activations storage to see how does it affect my model size even though it might be not a lot in inference but I want to deploy my model on embedded system which is critical for me to do a deep analysis since I am also a Phd student and I am new to this topic.</p>
<p>Again thank you for your reply I tried to post on a more relevant section and no one replied. So I posted here since I see you a lot in these blogs and you are so helpful <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=12" title=":grinning:" class="emoji" alt=":grinning:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>Yes, you could use e.g. forward hooks to record the output shapes of each module. However, depending on the model architecture, registering forward hooks to each module might be a bit tricky as you could easily track the same output multiple times if your modules are nested.<br>
Maybe <code>torch.fx</code> would be helpful here which might allow you to analyze the actual computation graph (and then the output activation shapes).</p>

lara: <p>How can I make this work on the torchvision quantized <a href="https://pytorch.org/vision/main/models/generated/torchvision.models.quantization.resnet18.html#torchvision.models.quantization.resnet18" rel="noopener nofollow ugc">ResNet models</a>? The above code works for the standard ResNet18 but for this quantized version it gives a size of 0.000MB.</p>

Laser_Cho: <p>thank you ptrblck.</p>
<p>I’m newbie in pytorch, so I google all problems I met, and everytime your answer save me.</p>