Skip to content

Commit

Permalink
Handle torch.jit scripted modules in layer summary (#6511)
Browse files Browse the repository at this point in the history
(cherry picked from commit 02fa32b)
  • Loading branch information
awaelchli authored and lexierule committed Mar 16, 2021
1 parent 9fc733f commit 50aedad
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed an issue with `Tuner.scale_batch_size` not finding the batch size attribute in the datamodule ([#5968](https://github.com/PyTorchLightning/pytorch-lightning/pull/5968))


- Fixed an exception in the layer summary when the model contains torch.jit scripted submodules ([#6511](https://github.com/PyTorchLightning/pytorch-lightning/pull/6511))


## [1.2.3] - 2021-03-09

### Fixed
Expand Down
12 changes: 8 additions & 4 deletions pytorch_lightning/core/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import shutil
import subprocess
from collections import OrderedDict
from typing import Any, Dict, List, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union

import numpy as np
import torch
Expand Down Expand Up @@ -71,14 +71,15 @@ def __init__(self, module: nn.Module):
def __del__(self):
self.detach_hook()

def _register_hook(self) -> RemovableHandle:
def _register_hook(self) -> Optional[RemovableHandle]:
"""
Registers a hook on the module that computes the input- and output size(s) on the first forward pass.
If the hook is called, it will remove itself from the from the module, meaning that
recursive models will only record their input- and output shapes once.
Registering hooks on :class:`~torch.jit.ScriptModule` is not supported.
Return:
A handle for the installed hook.
A handle for the installed hook, or ``None`` if registering the hook is not possible.
"""

def hook(module, inp, out):
Expand All @@ -88,7 +89,10 @@ def hook(module, inp, out):
self._out_size = parse_batch_shape(out)
self._hook_handle.remove()

return self._module.register_forward_hook(hook)
handle = None
if not isinstance(self._module, torch.jit.ScriptModule):
handle = self._module.register_forward_hook(hook)
return handle

def detach_hook(self):
"""
Expand Down
81 changes: 35 additions & 46 deletions tests/core/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ def forward(self, x):
return self.reduce(self.embed(x))


class PartialScriptModel(LightningModule):
""" A model which contains scripted layers. """

def __init__(self):
super().__init__()
self.layer1 = torch.jit.script(nn.Linear(5, 3))
self.layer2 = nn.Linear(3, 2)
self.example_input_array = torch.rand(2, 5)

def forward(self, x):
return self.layer2(self.layer1(x))


def test_invalid_weights_summmary():
""" Test that invalid value for weights_summary raises an error. """
with pytest.raises(MisconfigurationException, match='`mode` can be None, .* got temp'):
Expand All @@ -97,11 +110,8 @@ def test_invalid_weights_summmary():
Trainer(weights_summary='temp')


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
def test_empty_model_summary_shapes(mode):
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_empty_model_summary_shapes(mode: ModelSummary):
""" Test that the summary works for models that have no submodules. """
model = EmptyModule()
summary = model.summarize(mode=mode)
Expand All @@ -110,10 +120,7 @@ def test_empty_model_summary_shapes(mode):
assert summary.param_nums == []


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
@pytest.mark.parametrize(['device'], [
pytest.param(torch.device('cpu')),
pytest.param(torch.device('cuda', 0)),
Expand Down Expand Up @@ -157,10 +164,7 @@ def test_mixed_dtype_model_summary():
]


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_hooks_removed_after_summarize(mode):
""" Test that all hooks were properly removed after summary, even ones that were not run. """
model = UnorderedModel()
Expand All @@ -171,10 +175,7 @@ def test_hooks_removed_after_summarize(mode):
assert handle.id not in handle.hooks_dict_ref()


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_rnn_summary_shapes(mode):
""" Test that the model summary works for RNNs. """
model = ParityModuleRNN()
Expand All @@ -198,10 +199,7 @@ def test_rnn_summary_shapes(mode):
]


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_summary_parameter_count(mode):
""" Test that the summary counts the number of parameters in every submodule. """
model = UnorderedModel()
Expand All @@ -215,10 +213,7 @@ def test_summary_parameter_count(mode):
]


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_summary_layer_types(mode):
""" Test that the summary displays the layer names correctly. """
model = UnorderedModel()
Expand All @@ -232,10 +227,16 @@ def test_summary_layer_types(mode):
]


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_summary_with_scripted_modules(mode):
model = PartialScriptModel()
summary = model.summarize(mode=mode)
assert summary.layer_types == ["RecursiveScriptModule", "Linear"]
assert summary.in_sizes == [UNKNOWN_SIZE, [2, 3]]
assert summary.out_sizes == [UNKNOWN_SIZE, [2, 2]]


@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
@pytest.mark.parametrize(['example_input', 'expected_size'], [
pytest.param([], UNKNOWN_SIZE),
pytest.param((1, 2, 3), [UNKNOWN_SIZE] * 3),
Expand Down Expand Up @@ -269,21 +270,15 @@ def forward(self, *args, **kwargs):
assert summary.in_sizes == [expected_size]


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_model_size(mode):
""" Test model size is calculated correctly. """
model = PreCalculatedModel()
summary = model.summarize(mode=mode)
assert model.pre_calculated_model_size == summary.model_size


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize('mode', [ModelSummary.MODE_FULL, ModelSummary.MODE_TOP])
def test_empty_model_size(mode):
""" Test empty model size is zero. """
model = EmptyModule()
Expand All @@ -293,23 +288,17 @@ def test_empty_model_size(mode):

@pytest.mark.skipif(not torch.cuda.is_available(), reason="Test requires GPU.")
@pytest.mark.skipif(not _NATIVE_AMP_AVAILABLE, reason="test requires native AMP.")
@pytest.mark.parametrize(
'precision', [
pytest.param(16, marks=pytest.mark.skip(reason="no longer valid, because 16 can mean mixed precision")),
pytest.param(32),
]
)
def test_model_size_precision(monkeypatch, tmpdir, precision):
def test_model_size_precision(tmpdir):
""" Test model size for half and full precision. """
model = PreCalculatedModel(precision)
model = PreCalculatedModel()

# fit model
trainer = Trainer(
default_root_dir=tmpdir,
gpus=1,
max_steps=1,
max_epochs=1,
precision=precision,
precision=32,
)
trainer.fit(model)
summary = model.summarize()
Expand Down

0 comments on commit 50aedad

Please sign in to comment.