|
| 1 | +# Copyright The PyTorch Lightning team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import pytest |
| 15 | + |
| 16 | +import torch |
| 17 | +from torch.utils.data import DataLoader, Dataset |
| 18 | + |
| 19 | +from pytorch_lightning import Trainer |
| 20 | +from tests.helpers.boring_model import BoringModel, RandomDataset |
| 21 | + |
| 22 | + |
| 23 | +class RandomFloatIntDataset(Dataset): |
| 24 | + |
| 25 | + def __init__(self, size, length): |
| 26 | + self.len = length |
| 27 | + self.float_data = torch.randn(length, size) |
| 28 | + self.int_data = torch.randint(10, (length, 1)) |
| 29 | + |
| 30 | + def __getitem__(self, index): |
| 31 | + return self.float_data[index], self.int_data[index] |
| 32 | + |
| 33 | + def __len__(self): |
| 34 | + return self.len |
| 35 | + |
| 36 | + |
| 37 | +class DoublePrecisionBoringModel(BoringModel): |
| 38 | + |
| 39 | + def training_step(self, batch, batch_idx): |
| 40 | + float_data, int_data = batch |
| 41 | + assert float_data.dtype == torch.float64 |
| 42 | + output = self(float_data) |
| 43 | + loss = self.loss(batch, output) |
| 44 | + return {"loss": loss} |
| 45 | + |
| 46 | + def validation_step(self, batch, batch_idx): |
| 47 | + assert batch.dtype == torch.float64 |
| 48 | + output = self(batch) |
| 49 | + loss = self.loss(batch, output) |
| 50 | + return {"x": loss} |
| 51 | + |
| 52 | + def test_step(self, batch, batch_idx): |
| 53 | + assert batch.dtype == torch.float64 |
| 54 | + output = self(batch) |
| 55 | + loss = self.loss(batch, output) |
| 56 | + return {"y": loss} |
| 57 | + |
| 58 | + def predict_step(self, batch, batch_idx, dataloader_idx=None): |
| 59 | + assert batch.dtype == torch.float64 |
| 60 | + return self(batch) |
| 61 | + |
| 62 | + def on_fit_start(self): |
| 63 | + assert self.layer.weight.dtype == torch.float64 |
| 64 | + |
| 65 | + def on_after_backward(self): |
| 66 | + assert self.layer.weight.grad.dtype == torch.float64 |
| 67 | + |
| 68 | + def train_dataloader(self): |
| 69 | + dataset = RandomFloatIntDataset(32, 64) |
| 70 | + assert dataset.float_data.dtype == torch.float32 # Don't start with double data |
| 71 | + return DataLoader(dataset) |
| 72 | + |
| 73 | + def predict_dataloader(self): |
| 74 | + return DataLoader(RandomDataset(32, 64)) |
| 75 | + |
| 76 | + |
| 77 | +class DoublePrecisionBoringModelNoForward(BoringModel): |
| 78 | + |
| 79 | + def training_step(self, batch, batch_idx): |
| 80 | + assert batch.dtype == torch.float64 |
| 81 | + output = self.layer(batch) |
| 82 | + assert output.dtype == torch.float64 |
| 83 | + loss = self.loss(batch, output) |
| 84 | + return {"loss": loss} |
| 85 | + |
| 86 | + def validation_step(self, batch, batch_idx): |
| 87 | + assert batch.dtype == torch.float64 |
| 88 | + output = self.layer(batch) |
| 89 | + assert output.dtype == torch.float64 |
| 90 | + loss = self.loss(batch, output) |
| 91 | + return {"x": loss} |
| 92 | + |
| 93 | + def test_step(self, batch, batch_idx): |
| 94 | + assert batch.dtype == torch.float64 |
| 95 | + output = self.layer(batch) |
| 96 | + assert output.dtype == torch.float64 |
| 97 | + loss = self.loss(batch, output) |
| 98 | + return {"y": loss} |
| 99 | + |
| 100 | + def predict_step(self, batch, batch_idx, dataloader_idx=None): |
| 101 | + assert batch.dtype == torch.float64 |
| 102 | + output = self.layer(batch) |
| 103 | + assert output.dtype == torch.float64 |
| 104 | + return output |
| 105 | + |
| 106 | + def predict_dataloader(self): |
| 107 | + return DataLoader(RandomDataset(32, 64)) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + 'boring_model', |
| 112 | + (DoublePrecisionBoringModel, DoublePrecisionBoringModelNoForward) |
| 113 | +) |
| 114 | +def test_double_precision(tmpdir, boring_model): |
| 115 | + model = boring_model() |
| 116 | + original_training_step = model.training_step |
| 117 | + |
| 118 | + trainer = Trainer( |
| 119 | + max_epochs=2, |
| 120 | + default_root_dir=tmpdir, |
| 121 | + fast_dev_run=2, |
| 122 | + precision=64, |
| 123 | + log_every_n_steps=1, |
| 124 | + ) |
| 125 | + trainer.fit(model) |
| 126 | + trainer.test(model) |
| 127 | + trainer.predict(model) |
| 128 | + |
| 129 | + assert model.training_step == original_training_step |
0 commit comments