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

feat: Add ModelSummary Callback #9344

Merged
merged 17 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions pytorch_lightning/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pytorch_lightning.callbacks.lambda_function import LambdaCallback
from pytorch_lightning.callbacks.lr_monitor import LearningRateMonitor
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint
from pytorch_lightning.callbacks.model_summary import ModelSummary
from pytorch_lightning.callbacks.prediction_writer import BasePredictionWriter
from pytorch_lightning.callbacks.progress import ProgressBar, ProgressBarBase, RichProgressBar
from pytorch_lightning.callbacks.pruning import ModelPruning
Expand All @@ -38,6 +39,7 @@
"LambdaCallback",
"LearningRateMonitor",
"ModelCheckpoint",
"ModelSummary",
kaushikb11 marked this conversation as resolved.
Show resolved Hide resolved
"ModelPruning",
"BasePredictionWriter",
"ProgressBar",
Expand Down
32 changes: 32 additions & 0 deletions pytorch_lightning/callbacks/model_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.
"""
Model Summary
=============

Generates a summary of all layers in a :class:`~pytorch_lightning.core.lightning.LightningModule`.

The string representation of this summary prints a table with columns containing
the name, type and number of parameters for each layer.

"""
from typing import Optional

from pytorch_lightning.callbacks.base import Callback


class ModelSummary(Callback):
kaushikb11 marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, mode: Optional[str] = None, max_depth: Optional[int] = 1):
kaushikb11 marked this conversation as resolved.
Show resolved Hide resolved
self._mode = mode
self._max_depth = max_depth
11 changes: 10 additions & 1 deletion pytorch_lightning/trainer/connectors/callback_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from datetime import timedelta
from typing import Dict, List, Optional, Union

from pytorch_lightning.callbacks import Callback, ModelCheckpoint, ProgressBar, ProgressBarBase
from pytorch_lightning.callbacks import Callback, ModelCheckpoint, ModelSummary, ProgressBar, ProgressBarBase
from pytorch_lightning.callbacks.timer import Timer
from pytorch_lightning.utilities import rank_zero_info
from pytorch_lightning.utilities.exceptions import MisconfigurationException
Expand All @@ -34,6 +34,7 @@ def on_trainer_init(
process_position: int,
default_root_dir: Optional[str],
weights_save_path: Optional[str],
weights_summary: Optional[str],
stochastic_weight_avg: bool,
max_time: Optional[Union[str, timedelta, Dict[str, int]]] = None,
):
Expand All @@ -58,6 +59,8 @@ def on_trainer_init(
# responsible to stop the training when max_time is reached.
self._configure_timer_callback(max_time)

self._configure_model_summary_callback(weights_summary)

# init progress bar
if process_position != 0:
rank_zero_deprecation(
Expand Down Expand Up @@ -89,6 +92,12 @@ def _configure_checkpoint_callbacks(self, checkpoint_callback: bool) -> None:
if not self._trainer_has_checkpoint_callbacks() and checkpoint_callback is True:
self.trainer.callbacks.append(ModelCheckpoint())

def _configure_model_summary_callback(self, weights_summary: Optional[str] = None) -> None:
if any(isinstance(cb, ModelSummary) for cb in self.trainer.callbacks):
return
model_summary = ModelSummary(mode=weights_summary)
self.trainer.callbacks.append(model_summary)

def _configure_swa_callbacks(self):
if not self.trainer._stochastic_weight_avg:
return
Expand Down