|
| 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 | +This script will generate 2 traces: one for `training_step` and one for `validation_step`. |
| 16 | +The traces can be visualized in 2 ways: |
| 17 | +* With Chrome: |
| 18 | + 1. Open Chrome and copy/paste this url: `chrome://tracing/`. |
| 19 | + 2. Once tracing opens, click on `Load` at the top-right and load one of the generated traces. |
| 20 | +* With PyTorch Tensorboard Profiler (Instructions are here: https://github.com/pytorch/kineto/tree/master/tb_plugin) |
| 21 | + 1. pip install tensorboard torch-tb-profiler |
| 22 | + 2. tensorboard --logdir={FOLDER} |
| 23 | +""" |
| 24 | + |
| 25 | +import sys |
| 26 | +from argparse import ArgumentParser |
| 27 | + |
| 28 | +import torch |
| 29 | +import torchvision |
| 30 | +import torchvision.models as models |
| 31 | +import torchvision.transforms as T |
| 32 | + |
| 33 | +from pl_examples import cli_lightning_logo |
| 34 | +from pytorch_lightning import LightningDataModule, LightningModule, Trainer |
| 35 | + |
| 36 | +DEFAULT_CMD_LINE = ( |
| 37 | + "--max_epochs", |
| 38 | + "1", |
| 39 | + "--limit_train_batches", |
| 40 | + "15", |
| 41 | + "--limit_val_batches", |
| 42 | + "15", |
| 43 | + "--profiler", |
| 44 | + "pytorch", |
| 45 | + "--gpus", |
| 46 | + f"{int(torch.cuda.is_available())}", |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +class ModelToProfile(LightningModule): |
| 51 | + |
| 52 | + def __init__(self, model): |
| 53 | + super().__init__() |
| 54 | + self.model = model |
| 55 | + self.criterion = torch.nn.CrossEntropyLoss() |
| 56 | + |
| 57 | + def training_step(self, batch, batch_idx): |
| 58 | + inputs, labels = batch |
| 59 | + outputs = self.model(inputs) |
| 60 | + loss = self.criterion(outputs, labels) |
| 61 | + self.log("train_loss", loss) |
| 62 | + return loss |
| 63 | + |
| 64 | + def validation_step(self, batch, batch_idx): |
| 65 | + inputs, labels = batch |
| 66 | + outputs = self.model(inputs) |
| 67 | + loss = self.criterion(outputs, labels) |
| 68 | + self.log("val_loss", loss) |
| 69 | + |
| 70 | + def configure_optimizers(self): |
| 71 | + return torch.optim.SGD(self.parameters(), lr=0.001, momentum=0.9) |
| 72 | + |
| 73 | + |
| 74 | +class CIFAR10DataModule(LightningDataModule): |
| 75 | + |
| 76 | + transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()]) |
| 77 | + |
| 78 | + def train_dataloader(self, *args, **kwargs): |
| 79 | + trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=self.transform) |
| 80 | + return torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True, num_workers=0) |
| 81 | + |
| 82 | + def val_dataloader(self, *args, **kwargs): |
| 83 | + valset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=self.transform) |
| 84 | + return torch.utils.data.DataLoader(valset, batch_size=32, shuffle=True, num_workers=0) |
| 85 | + |
| 86 | + |
| 87 | +def cli_main(): |
| 88 | + |
| 89 | + parser = ArgumentParser() |
| 90 | + parser = Trainer.add_argparse_args(parser) |
| 91 | + cmd_line = None if len(sys.argv) != 1 else DEFAULT_CMD_LINE |
| 92 | + args = parser.parse_args(args=cmd_line) |
| 93 | + |
| 94 | + model = ModelToProfile(models.resnet50(pretrained=True)) |
| 95 | + datamodule = CIFAR10DataModule() |
| 96 | + trainer = Trainer(**vars(args)) |
| 97 | + trainer.fit(model, datamodule=datamodule) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + cli_lightning_logo() |
| 102 | + cli_main() |
0 commit comments