Skip to content
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

Closed
wants to merge 14 commits into from

Conversation

calebrob6
Copy link
Contributor

@calebrob6 calebrob6 commented Jul 21, 2021

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

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)
  • Did you list all the breaking changes introduced by this pull request?

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:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Jul 21, 2021

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

@codecov
Copy link

codecov bot commented Jul 21, 2021

Codecov Report

Merging #8500 (4ca4579) into master (6701526) will decrease coverage by 4%.
The diff coverage is 100%.

❗ Current head 4ca4579 differs from pull request most recent head 415c0bd. Consider uploading reports for the commit 415c0bd to get more accurate results

@@           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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. this could be under https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/utilities/memory.py
  2. get_model_size_bytes is explicit that the return will be in bytes

Copy link
Contributor

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 ?

Comment on lines +100 to +107
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
Copy link
Contributor

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

Copy link
Contributor

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 :)

Comment on lines 112 to 116
class BoringSparseModel(BoringModel):

def __init__(self):
super().__init__()
self.layer = nn.Parameter(torch.ones(32).to_sparse())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@awaelchli awaelchli added this to the v1.5 milestone Jul 27, 2021
@awaelchli awaelchli added the feature Is an improvement or enhancement label Jul 27, 2021
@awaelchli
Copy link
Contributor

@calebrob6 I merged master and added a changelog entry.
Ready for review?
This will then unblock #8495

@calebrob6 calebrob6 marked this pull request as ready for review July 28, 2021 00:21
@calebrob6
Copy link
Contributor Author

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.

@awaelchli
Copy link
Contributor

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?

@Borda
Copy link
Member

Borda commented Jul 28, 2021

can you confirm in which order these two PRs should be reviewed and merged?

I think this shall be merged before deprecating size attribute

@mergify mergify bot removed the has conflicts label Jul 29, 2021
tests/utilities/test_model_helpers.py Outdated Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Aug 6, 2021
Co-authored-by: Adrian Wälchli <[email protected]>
@Borda Borda requested a review from rohitgr7 as a code owner October 20, 2021 13:22
@tchaton
Copy link
Contributor

tchaton commented Nov 1, 2021

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,
T.C

@tchaton tchaton closed this Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement has conflicts ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants