|
| 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 | + |
| 15 | +import torch |
| 16 | +from torch.nn import functional as F |
| 17 | + |
| 18 | +import pytorch_lightning as pl |
| 19 | +from pl_examples.basic_examples.mnist_datamodule import MNISTDataModule |
| 20 | + |
| 21 | + |
| 22 | +class LitClassifier(pl.LightningModule): |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + hidden_dim: int = 128, |
| 27 | + learning_rate: float = 0.0001, |
| 28 | + ): |
| 29 | + super().__init__() |
| 30 | + self.save_hyperparameters() |
| 31 | + |
| 32 | + self.l1 = torch.nn.Linear(28 * 28, self.hparams.hidden_dim) |
| 33 | + self.l2 = torch.nn.Linear(self.hparams.hidden_dim, 10) |
| 34 | + |
| 35 | + def forward(self, x): |
| 36 | + x = x.view(x.size(0), -1) |
| 37 | + x = torch.relu(self.l1(x)) |
| 38 | + x = torch.relu(self.l2(x)) |
| 39 | + return x |
| 40 | + |
| 41 | + def training_step(self, batch, batch_idx): |
| 42 | + x, y = batch |
| 43 | + y_hat = self(x) |
| 44 | + loss = F.cross_entropy(y_hat, y) |
| 45 | + return loss |
| 46 | + |
| 47 | + def validation_step(self, batch, batch_idx): |
| 48 | + x, y = batch |
| 49 | + probs = self(x) |
| 50 | + # we currently return the accuracy as the validation_step/test_step is run on the IPU devices. |
| 51 | + # Outputs from the step functions are sent to the host device, where we calculate the metrics in |
| 52 | + # validation_epoch_end and test_epoch_end for the test_step. |
| 53 | + acc = self.accuracy(probs, y) |
| 54 | + return acc |
| 55 | + |
| 56 | + def test_step(self, batch, batch_idx): |
| 57 | + x, y = batch |
| 58 | + logits = self(x) |
| 59 | + acc = self.accuracy(logits, y) |
| 60 | + return acc |
| 61 | + |
| 62 | + def accuracy(self, logits, y): |
| 63 | + # currently IPU poptorch doesn't implicit convert bools to tensor |
| 64 | + # hence we use an explicit calculation for accuracy here. Once fixed in poptorch |
| 65 | + # we can use the accuracy metric. |
| 66 | + acc = torch.sum(torch.eq(torch.argmax(logits, -1), y).to(torch.float32)) / len(y) |
| 67 | + return acc |
| 68 | + |
| 69 | + def validation_epoch_end(self, outputs) -> None: |
| 70 | + # since the training step/validation step and test step are run on the IPU device |
| 71 | + # we must log the average loss outside the step functions. |
| 72 | + self.log('val_acc', torch.stack(outputs).mean(), prog_bar=True) |
| 73 | + |
| 74 | + def test_epoch_end(self, outputs) -> None: |
| 75 | + self.log('test_acc', torch.stack(outputs).mean()) |
| 76 | + |
| 77 | + def configure_optimizers(self): |
| 78 | + return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + dm = MNISTDataModule(batch_size=32) |
| 83 | + |
| 84 | + model = LitClassifier() |
| 85 | + |
| 86 | + trainer = pl.Trainer(max_epochs=2, ipus=8) |
| 87 | + |
| 88 | + trainer.fit(model, datamodule=dm) |
| 89 | + trainer.test(model, datamodule=dm) |
0 commit comments