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

Add typing for utilities/memory.py #11545

Merged
merged 4 commits into from
Feb 3, 2022
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ module = [
"pytorch_lightning.utilities.auto_restart",
"pytorch_lightning.utilities.data",
"pytorch_lightning.utilities.distributed",
"pytorch_lightning.utilities.memory",
"pytorch_lightning.utilities.meta",
]
ignore_errors = "True"
19 changes: 3 additions & 16 deletions pytorch_lightning/utilities/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import shutil
import subprocess
from io import BytesIO
from typing import Any, Dict

import torch
Expand All @@ -25,20 +26,6 @@
from pytorch_lightning.utilities.apply_func import apply_to_collection


class _ByteCounter:
"""Accumulate and stores the total bytes of an object."""

def __init__(self) -> None:
self.nbytes: int = 0

def write(self, data: bytes) -> None:
"""Stores the total bytes of the data."""
self.nbytes += len(data)

def flush(self) -> None:
pass


def recursive_detach(in_dict: Any, to_cpu: bool = False) -> Any:
"""Detach all tensors in `in_dict`.

Expand Down Expand Up @@ -183,7 +170,7 @@ def get_model_size_mb(model: Module) -> float:
Returns:
Number of megabytes in the parameters of the input module.
"""
model_size = _ByteCounter()
model_size = BytesIO()
torch.save(model.state_dict(), model_size)
size_mb = model_size.nbytes / 1e6
size_mb = model_size.getbuffer().nbytes / 1e6
return size_mb