-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding utility function for calculating model size in bytes #8500
Conversation
Hello @calebrob6! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2021-08-06 09:26:23 UTC |
for more information, see https://pre-commit.ci
Codecov Report
@@ Coverage Diff @@
## master #8500 +/- ##
========================================
- Coverage 93% 89% -4%
========================================
Files 180 169 -11
Lines 15895 14084 -1811
========================================
- Hits 14726 12474 -2252
- Misses 1169 1610 +441 |
@@ -69,3 +71,28 @@ def is_overridden( | |||
parent_code = parent_attr.__code__ | |||
|
|||
return instance_code != parent_code | |||
|
|||
|
|||
def get_model_size(model: nn.Module) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- this could be under https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/utilities/memory.py
get_model_size_bytes
is explicit that the return will be in bytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should utility be part of the training_type_plugin as shared tensors won't provide the right value ?
model = BoringModel() | ||
|
||
size_bytes = get_model_size(model) | ||
|
||
# The BoringModel has a fully connected layer of size 32x2 with a bias resulting in | ||
# 67 weights. Each weight is a float32 -- 4 bytes, therefore we expect a size of | ||
# 264. | ||
assert size_bytes == 264 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer defining a module to test directly here vs importing it. if someone changes the BoringModel changes for any reason, this test will fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BoringModel shouldn't change :) If it does, lot of test will fail :)
class BoringSparseModel(BoringModel): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.layer = nn.Parameter(torch.ones(32).to_sparse()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
@calebrob6 I merged master and added a changelog entry. |
Hey @awaelchli, I just requested a review, although I have not changed anything about the code since the above comments. I think (from #8495) that this code should be merged as-is as an utility function, the model_size() property will be deprecated, then I will implement a new version of this utility function that deals with sparse tensors. |
Yesterday #8495 was labelled as "blocked by 8500". @roshikouhai @ananthsub @calebrob6 can you confirm in which order these two PRs should be reviewed and merged? |
I think this shall be merged before deprecating size attribute |
Co-authored-by: Adrian Wälchli <[email protected]>
for more information, see https://pre-commit.ci
Dear @calebrob6, Thanks for putting the effort with this PR. The Lightning Team decided to go with the ByteCounter approach for v1.5. in this PR: #10123 Closing this PR. Best, |
What does this PR do?
Adding a utility function for computing the size of a model per discussion in #8343
Does your PR introduce any breaking changes ? If yes, please list them.
No breaking changes.
Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:
Did you have fun?
Make sure you had fun coding 🙃