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

fix dtype conversion of example_input_array in model summary #2510

Merged
merged 2 commits into from
Jul 5, 2020
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed using the same DDP python interpreter and actually running ([#2482](https://github.com/PyTorchLightning/pytorch-lightning/pull/2482))

- Fixed model summary input type conversion for models that have input dtype different from model parameters ([#2510](https://github.com/PyTorchLightning/pytorch-lightning/pull/2510))

## [0.8.4] - 2020-07-01

Expand Down
1 change: 0 additions & 1 deletion pytorch_lightning/core/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ def _forward_example_input(self) -> None:

input_ = model.example_input_array
input_ = model.transfer_batch_to_device(input_, model.device)
input_ = apply_to_collection(input_, torch.Tensor, lambda x: x.type(model.dtype))

if trainer is not None and trainer.use_amp:
if NATIVE_AMP_AVALAIBLE:
Expand Down
40 changes: 33 additions & 7 deletions tests/core/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ def forward(self, x, y):
return out


class MixedDtypeModel(LightningModule):
""" The parameters and inputs of this model have different dtypes. """

def __init__(self):
super().__init__()
self.embed = nn.Embedding(10, 20) # expects dtype long as input
self.reduce = nn.Linear(20, 1) # dtype: float
self.example_input_array = torch.tensor([[0, 2, 1], [3, 5, 3]]) # dtype: long

def forward(self, x):
return self.reduce(self.embed(x))


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
Expand All @@ -59,15 +72,15 @@ def test_empty_model_summary_shapes(mode):
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
])
@pytest.mark.parametrize(['device', 'dtype'], [
pytest.param(torch.device('cpu'), torch.double),
pytest.param(torch.device('cuda', 0), torch.float),
pytest.param(torch.device('cuda', 0), torch.float16),
@pytest.mark.parametrize(['device'], [
pytest.param(torch.device('cpu')),
pytest.param(torch.device('cuda', 0)),
pytest.param(torch.device('cuda', 0)),
])
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Test requires GPU.")
def test_linear_model_summary_shapes(device, dtype, mode):
def test_linear_model_summary_shapes(device, mode):
""" Test that the model summary correctly computes the input- and output shapes. """
model = UnorderedModel().type(dtype).to(device)
model = UnorderedModel().to(device)
model.train()
summary = model.summarize(mode=mode)
assert summary.in_sizes == [
Expand All @@ -85,10 +98,23 @@ def test_linear_model_summary_shapes(device, dtype, mode):
UNKNOWN_SIZE,
]
assert model.training
assert model.dtype == dtype
assert model.device == device


def test_mixed_dtype_model_summary():
""" Test that the model summary works with models that have mixed input- and parameter dtypes. """
model = MixedDtypeModel()
summary = model.summarize()
assert summary.in_sizes == [
[2, 3], # embed
[2, 3, 20], # reduce
]
assert summary.out_sizes == [
[2, 3, 20], # embed
[2, 3, 1], # reduce
]


@pytest.mark.parametrize(['mode'], [
pytest.param(ModelSummary.MODE_FULL),
pytest.param(ModelSummary.MODE_TOP),
Expand Down