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

Deprecate LightningModule.model_size #8495

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Deprecated

-
- Deprecated `LightningModule.model_size` ([#8343](https://github.com/PyTorchLightning/pytorch-lightning/pull/8343))


-
Expand Down
18 changes: 7 additions & 11 deletions pytorch_lightning/core/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import numbers
import os
import tempfile
import uuid
from abc import ABC
from contextlib import contextmanager
from pathlib import Path
Expand All @@ -44,6 +43,7 @@
from pytorch_lightning.utilities.cloud_io import get_filesystem
from pytorch_lightning.utilities.distributed import distributed_available, sync_ddp
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.memory import get_model_size_mb
from pytorch_lightning.utilities.parsing import collect_init_args
from pytorch_lightning.utilities.signature_utils import is_param_in_hook_signature
from pytorch_lightning.utilities.types import _METRIC_COLLECTION, EPOCH_OUTPUT, STEP_OUTPUT
Expand Down Expand Up @@ -1980,16 +1980,12 @@ def to_torchscript(

@property
def model_size(self) -> float:
"""
The model's size in megabytes. The computation includes everything in the
:meth:`~torch.nn.Module.state_dict`, i.e., by default the parameteters and buffers.
"""
# todo: think about better way without need to dump model to drive
tmp_name = f"{uuid.uuid4().hex}.pt"
torch.save(self.state_dict(), tmp_name)
size_mb = os.path.getsize(tmp_name) / 1e6
os.remove(tmp_name)
return size_mb
rank_zero_deprecation(
"The `LightningModule.model_size` property was deprecated in v1.5 and will be removed in v1.7."
" Please use the `pytorch_lightning.utilities.memory.get_model_size_mb`.",
stacklevel=5,
)
return get_model_size_mb(self)

def add_to_queue(self, queue: torch.multiprocessing.SimpleQueue) -> None:
"""
Expand Down
21 changes: 21 additions & 0 deletions pytorch_lightning/utilities/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# limitations under the License.

import gc
import os
import uuid

import torch
from torch.nn import Module


def recursive_detach(in_dict: dict, to_cpu: bool = False) -> dict:
Expand Down Expand Up @@ -87,3 +90,21 @@ def garbage_collection_cuda():
if not is_oom_error(exception):
# Only handle OOM errors
raise


def get_model_size_mb(model: Module) -> float:
"""
Calculates the size of a Module in megabytes by saving the model to a temporary file and reading its size.

The computation includes everything in the :meth:`~torch.nn.Module.state_dict`,
i.e., by default the parameteters and buffers.

Returns:
Number of megabytes in the parameters of the input module.
"""
# TODO: Implement a method without needing to download the model
tmp_name = f"{uuid.uuid4().hex}.pt"
torch.save(model.state_dict(), tmp_name)
size_mb = os.path.getsize(tmp_name) / 1e6
os.remove(tmp_name)
return size_mb
5 changes: 3 additions & 2 deletions tests/callbacks/test_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pytorch_lightning import seed_everything, Trainer
from pytorch_lightning.callbacks import QuantizationAwareTraining
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.memory import get_model_size_mb
from tests.helpers.datamodules import RegressDataModule
from tests.helpers.runif import RunIf
from tests.helpers.simple_models import RegressionModel
Expand All @@ -40,7 +41,7 @@ def test_quantization(tmpdir, observe: str, fuse: bool, convert: bool):

trainer = Trainer(**trainer_args)
trainer.fit(model, datamodule=dm)
org_size = model.model_size
org_size = get_model_size_mb(model)
org_score = torch.mean(torch.tensor([mean_relative_error(model(x), y) for x, y in dm.test_dataloader()]))

fusing_layers = [(f"layer_{i}", f"layer_{i}a") for i in range(3)] if fuse else None
Expand All @@ -62,7 +63,7 @@ def test_quantization(tmpdir, observe: str, fuse: bool, convert: bool):
qmodel.eval()
torch.quantization.convert(qmodel, inplace=True)

quant_size = qmodel.model_size
quant_size = get_model_size_mb(qmodel)
# test that the trained model is smaller then initial
size_ratio = quant_size / org_size
assert size_ratio < 0.65
Expand Down
25 changes: 25 additions & 0 deletions tests/deprecated_api/test_remove_1-7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Test deprecated functionality which will be removed in v1.7.0 """
import pytest

from tests.helpers import BoringModel


def test_v1_7_0_deprecated_model_size():
model = BoringModel()
with pytest.deprecated_call(
match="LightningModule.model_size` property was deprecated in v1.5 and will be removed in v1.7"
):
_ = model.model_size
27 changes: 26 additions & 1 deletion tests/utilities/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math

import torch
import torch.nn as nn

from pytorch_lightning.utilities.memory import recursive_detach
from pytorch_lightning.utilities.memory import get_model_size_mb, recursive_detach
from tests.helpers import BoringModel


def test_recursive_detach():
Expand All @@ -28,3 +32,24 @@ def test_recursive_detach():
assert y["foo"].device.type == "cpu"
assert y["bar"]["baz"].device.type == "cpu"
assert not y["bar"]["baz"].requires_grad


def test_get_model_size_mb():
model = BoringModel()

size_bytes = get_model_size_mb(model)

# Size will be python version dependent.
assert math.isclose(size_bytes, 0.001319, rel_tol=0.1)


def test_get_sparse_model_size_mb():
class BoringSparseModel(BoringModel):
def __init__(self):
super().__init__()
self.layer = nn.Parameter(torch.ones(32).to_sparse())

model = BoringSparseModel()
size_bytes = get_model_size_mb(model)

assert math.isclose(size_bytes, 0.001511, rel_tol=0.1)