-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: Add ModelSummary Callback #9344
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cdb8ebc
feat: Add ModelSummary Callback
kaushikb11 e11d3a2
Address reviews
kaushikb11 391724b
Address reviews
kaushikb11 11c93ba
Add ModelSummaryMode Enum
kaushikb11 4937f9a
Update
kaushikb11 dc135eb
Update ModelSummary callback
kaushikb11 e46ff0c
Update for ModelSummaryMode
kaushikb11 1019d5f
Add test for ModelSummaryMode
kaushikb11 5f2c5f4
Add tests for ModelSummary Callback
kaushikb11 ee41317
Update ModelSummary callback
kaushikb11 d72aa44
Merge branch 'master' into callback/model_summary
kaushikb11 090bff1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2e748c4
Update ModelSummary callback
kaushikb11 9dc0ba2
Merge branch 'callback/model_summary' of https://github.com/PyTorchLi…
kaushikb11 e22adf4
Fix tests
kaushikb11 4baa620
Fix tests
kaushikb11 a0760c8
Update pytorch_lightning/callbacks/model_summary.py
kaushikb11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 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. | ||
|
||
""" | ||
import logging | ||
from typing import List, Optional, Union | ||
|
||
import pytorch_lightning as pl | ||
from pytorch_lightning.callbacks.base import Callback | ||
from pytorch_lightning.utilities.model_summary import _format_summary_table, summarize | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ModelSummary(Callback): | ||
r""" | ||
Generates a summary of all layers in a :class:`~pytorch_lightning.core.lightning.LightningModule`. | ||
|
||
Args: | ||
max_depth: The maximum depth of layer nesting that the summary will include. A value of 0 turns the | ||
layer summary off. | ||
|
||
Example:: | ||
|
||
>>> from pytorch_lightning import Trainer | ||
>>> from pytorch_lightning.callbacks import ModelSummary | ||
>>> trainer = Trainer(callbacks=[ModelSummary(max_depth=1)]) | ||
""" | ||
|
||
def __init__(self, max_depth: Optional[int] = 1): | ||
self._max_depth: int = max_depth | ||
|
||
def on_pretrain_routine_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: | ||
if trainer.is_global_zero and self._max_depth is not None and not trainer.testing: | ||
model_summary = summarize(pl_module, max_depth=self._max_depth) | ||
|
||
summary_data = model_summary._get_summary_data() | ||
total_parameters = model_summary.total_parameters | ||
trainable_parameters = model_summary.trainable_parameters | ||
model_size = model_summary.model_size | ||
|
||
self.summarize(summary_data, total_parameters, trainable_parameters, model_size) | ||
kaushikb11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@staticmethod | ||
def summarize( | ||
summary_data: List[List[Union[str, List[str]]]], | ||
total_parameters: int, | ||
trainable_parameters: int, | ||
model_size: float, | ||
) -> None: | ||
summary_table = _format_summary_table(total_parameters, trainable_parameters, model_size, *summary_data) | ||
|
||
log.info("\n" + summary_table) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.