2021-11-02 08:04:29 +00:00
|
|
|
# Copyright The Lightning AI team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2023-01-04 15:57:18 +00:00
|
|
|
"""Here are 4 easy steps to use Fabric in your PyTorch code.
|
2022-11-10 09:16:46 +00:00
|
|
|
|
2023-01-04 15:57:18 +00:00
|
|
|
1. Create the Lightning Fabric object at the beginning of your script.
|
2022-11-10 09:16:46 +00:00
|
|
|
|
2023-01-04 15:57:18 +00:00
|
|
|
2. Remove all ``.to`` and ``.cuda`` calls since Fabric will take care of it.
|
2022-11-10 09:16:46 +00:00
|
|
|
|
|
|
|
3. Apply ``setup`` over each model and optimizers pair, ``setup_dataloaders`` on all your dataloaders,
|
|
|
|
and replace ``loss.backward()`` with ``self.backward(loss)``.
|
|
|
|
|
2024-02-27 16:36:46 +00:00
|
|
|
4. Run the script from the terminal using ``fabric run path/to/train.py``
|
2022-11-10 09:16:46 +00:00
|
|
|
|
|
|
|
Accelerate your training loop by setting the ``--accelerator``, ``--strategy``, ``--devices`` options directly from
|
2024-02-27 16:36:46 +00:00
|
|
|
the command line. See ``fabric run --help`` or learn more from the documentation:
|
2023-03-15 19:19:41 +00:00
|
|
|
https://lightning.ai/docs/fabric.
|
2023-08-09 14:44:20 +00:00
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
"""
|
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
import argparse
|
2022-06-15 12:53:51 +00:00
|
|
|
from os import path
|
2021-11-02 08:04:29 +00:00
|
|
|
|
|
|
|
import torch
|
2023-01-05 14:07:43 +00:00
|
|
|
import torch.nn as nn
|
2021-11-02 08:04:29 +00:00
|
|
|
import torch.nn.functional as F
|
|
|
|
import torch.optim as optim
|
|
|
|
import torchvision.transforms as T
|
ruff: replace isort with ruff +TPU (#17684)
* ruff: replace isort with ruff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing & imports
* lines in warning test
* docs
* fix enum import
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing
* import
* fix lines
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* type ClusterEnvironment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-26 15:54:55 +00:00
|
|
|
from lightning.fabric import Fabric, seed_everything
|
2021-11-02 08:04:29 +00:00
|
|
|
from torch.optim.lr_scheduler import StepLR
|
2022-11-10 09:16:46 +00:00
|
|
|
from torchmetrics.classification import Accuracy
|
|
|
|
from torchvision.datasets import MNIST
|
2021-11-02 08:04:29 +00:00
|
|
|
|
2023-01-05 14:07:43 +00:00
|
|
|
DATASETS_PATH = path.join(path.dirname(__file__), "..", "..", "..", "Datasets")
|
|
|
|
|
|
|
|
|
|
|
|
class Net(nn.Module):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
|
|
|
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
|
|
|
self.dropout1 = nn.Dropout(0.25)
|
|
|
|
self.dropout2 = nn.Dropout(0.5)
|
|
|
|
self.fc1 = nn.Linear(9216, 128)
|
|
|
|
self.fc2 = nn.Linear(128, 10)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.conv1(x)
|
|
|
|
x = F.relu(x)
|
|
|
|
x = self.conv2(x)
|
|
|
|
x = F.relu(x)
|
|
|
|
x = F.max_pool2d(x, 2)
|
|
|
|
x = self.dropout1(x)
|
|
|
|
x = torch.flatten(x, 1)
|
|
|
|
x = self.fc1(x)
|
|
|
|
x = F.relu(x)
|
|
|
|
x = self.dropout2(x)
|
|
|
|
x = self.fc2(x)
|
2023-05-05 09:34:40 +00:00
|
|
|
return F.log_softmax(x, dim=1)
|
2021-11-02 08:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run(hparams):
|
2023-01-04 15:57:18 +00:00
|
|
|
# Create the Lightning Fabric object. The parameters like accelerator, strategy, devices etc. will be proided
|
2024-02-27 16:36:46 +00:00
|
|
|
# by the command line. See all options: `fabric run --help`
|
2023-01-04 15:57:18 +00:00
|
|
|
fabric = Fabric()
|
2021-11-02 08:04:29 +00:00
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
seed_everything(hparams.seed) # instead of torch.manual_seed(...)
|
2021-11-02 08:04:29 +00:00
|
|
|
|
|
|
|
transform = T.Compose([T.ToTensor(), T.Normalize((0.1307,), (0.3081,))])
|
2023-06-12 10:32:32 +00:00
|
|
|
|
|
|
|
# Let rank 0 download the data first, then everyone will load MNIST
|
2023-09-25 19:49:52 +00:00
|
|
|
with fabric.rank_zero_first(local=False): # set `local=True` if your filesystem is not shared between machines
|
2023-06-12 10:32:32 +00:00
|
|
|
train_dataset = MNIST(DATASETS_PATH, download=fabric.is_global_zero, train=True, transform=transform)
|
|
|
|
test_dataset = MNIST(DATASETS_PATH, download=fabric.is_global_zero, train=False, transform=transform)
|
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
train_loader = torch.utils.data.DataLoader(
|
|
|
|
train_dataset,
|
|
|
|
batch_size=hparams.batch_size,
|
|
|
|
)
|
|
|
|
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=hparams.batch_size)
|
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
# don't forget to call `setup_dataloaders` to prepare for dataloaders for distributed training.
|
2023-01-04 15:57:18 +00:00
|
|
|
train_loader, test_loader = fabric.setup_dataloaders(train_loader, test_loader)
|
2022-11-10 09:16:46 +00:00
|
|
|
|
|
|
|
model = Net() # remove call to .to(device)
|
2021-11-02 08:04:29 +00:00
|
|
|
optimizer = optim.Adadelta(model.parameters(), lr=hparams.lr)
|
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
# don't forget to call `setup` to prepare for model / optimizer for distributed training.
|
|
|
|
# the model is moved automatically to the right device.
|
2023-01-04 15:57:18 +00:00
|
|
|
model, optimizer = fabric.setup(model, optimizer)
|
2022-11-10 09:16:46 +00:00
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
scheduler = StepLR(optimizer, step_size=1, gamma=hparams.gamma)
|
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
# use torchmetrics instead of manually computing the accuracy
|
2023-03-03 16:14:31 +00:00
|
|
|
test_acc = Accuracy(task="multiclass", num_classes=10).to(fabric.device)
|
2022-11-10 09:16:46 +00:00
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
# EPOCH LOOP
|
|
|
|
for epoch in range(1, hparams.epochs + 1):
|
|
|
|
# TRAINING LOOP
|
|
|
|
model.train()
|
|
|
|
for batch_idx, (data, target) in enumerate(train_loader):
|
2022-11-10 09:16:46 +00:00
|
|
|
# NOTE: no need to call `.to(device)` on the data, target
|
2021-11-02 08:04:29 +00:00
|
|
|
optimizer.zero_grad()
|
|
|
|
output = model(data)
|
|
|
|
loss = F.nll_loss(output, target)
|
2023-01-04 15:57:18 +00:00
|
|
|
fabric.backward(loss) # instead of loss.backward()
|
2022-11-10 09:16:46 +00:00
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
optimizer.step()
|
|
|
|
if (batch_idx == 0) or ((batch_idx + 1) % hparams.log_interval == 0):
|
|
|
|
print(
|
2024-03-15 19:48:17 +00:00
|
|
|
f"Train Epoch: {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)}"
|
|
|
|
f" ({100.0 * batch_idx / len(train_loader):.0f}%)]\tLoss: {loss.item():.6f}"
|
2021-11-02 08:04:29 +00:00
|
|
|
)
|
|
|
|
if hparams.dry_run:
|
|
|
|
break
|
2022-11-10 09:16:46 +00:00
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
scheduler.step()
|
|
|
|
|
|
|
|
# TESTING LOOP
|
|
|
|
model.eval()
|
|
|
|
test_loss = 0
|
|
|
|
with torch.no_grad():
|
|
|
|
for data, target in test_loader:
|
2022-11-10 09:16:46 +00:00
|
|
|
# NOTE: no need to call `.to(device)` on the data, target
|
2021-11-02 08:04:29 +00:00
|
|
|
output = model(data)
|
2022-11-10 09:16:46 +00:00
|
|
|
test_loss += F.nll_loss(output, target, reduction="sum").item()
|
|
|
|
|
|
|
|
# WITHOUT TorchMetrics
|
|
|
|
# pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
|
|
|
|
# correct += pred.eq(target.view_as(pred)).sum().item()
|
|
|
|
|
|
|
|
# WITH TorchMetrics
|
|
|
|
test_acc(output, target)
|
|
|
|
|
2021-11-02 08:04:29 +00:00
|
|
|
if hparams.dry_run:
|
|
|
|
break
|
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
# all_gather is used to aggregated the value across processes
|
2023-01-04 15:57:18 +00:00
|
|
|
test_loss = fabric.all_gather(test_loss).sum() / len(test_loader.dataset)
|
2021-11-02 08:04:29 +00:00
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
print(f"\nTest set: Average loss: {test_loss:.4f}, Accuracy: ({100 * test_acc.compute():.0f}%)\n")
|
|
|
|
test_acc.reset()
|
2021-11-02 08:04:29 +00:00
|
|
|
|
|
|
|
if hparams.dry_run:
|
|
|
|
break
|
|
|
|
|
2023-01-04 15:57:18 +00:00
|
|
|
# When using distributed training, use `fabric.save`
|
2022-11-10 09:16:46 +00:00
|
|
|
# to ensure the current process is allowed to save a checkpoint
|
2021-11-02 08:04:29 +00:00
|
|
|
if hparams.save_model:
|
2023-01-04 15:57:18 +00:00
|
|
|
fabric.save(model.state_dict(), "mnist_cnn.pt")
|
2021-11-02 08:04:29 +00:00
|
|
|
|
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# Arguments can be passed in through the CLI as normal and will be parsed here
|
|
|
|
# Example:
|
2024-02-27 16:36:46 +00:00
|
|
|
# fabric run image_classifier.py accelerator=cuda --epochs=3
|
2023-01-04 15:57:18 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Fabric MNIST Example")
|
2021-11-02 08:04:29 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--batch-size", type=int, default=64, metavar="N", help="input batch size for training (default: 64)"
|
|
|
|
)
|
|
|
|
parser.add_argument("--epochs", type=int, default=14, metavar="N", help="number of epochs to train (default: 14)")
|
|
|
|
parser.add_argument("--lr", type=float, default=1.0, metavar="LR", help="learning rate (default: 1.0)")
|
|
|
|
parser.add_argument("--gamma", type=float, default=0.7, metavar="M", help="Learning rate step gamma (default: 0.7)")
|
|
|
|
parser.add_argument("--dry-run", action="store_true", default=False, help="quickly check a single pass")
|
|
|
|
parser.add_argument("--seed", type=int, default=1, metavar="S", help="random seed (default: 1)")
|
|
|
|
parser.add_argument(
|
|
|
|
"--log-interval",
|
|
|
|
type=int,
|
|
|
|
default=10,
|
|
|
|
metavar="N",
|
|
|
|
help="how many batches to wait before logging training status",
|
|
|
|
)
|
|
|
|
parser.add_argument("--save-model", action="store_true", default=False, help="For Saving the current Model")
|
|
|
|
hparams = parser.parse_args()
|
|
|
|
|
2022-11-10 09:16:46 +00:00
|
|
|
run(hparams)
|