mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-07 12:01:39 +00:00
[booster] implemented the torch ddd + resnet example (#3232)
* [booster] implemented the torch ddd + resnet example * polish code
This commit is contained in:
5
examples/tutorial/new_api/README.md
Normal file
5
examples/tutorial/new_api/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# New API Features
|
||||
|
||||
**The New API is not officially released yet.**
|
||||
|
||||
This folder contains some of the demonstrations of the new API. The new API is still under intensive development and will be released soon.
|
2
examples/tutorial/new_api/test_ci.sh
Normal file
2
examples/tutorial/new_api/test_ci.sh
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env
|
||||
echo "The CI integration will be completed when the API is stable"
|
4
examples/tutorial/new_api/torch_ddp/.gitignore
vendored
Normal file
4
examples/tutorial/new_api/torch_ddp/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
data
|
||||
checkpoint
|
||||
ckpt-fp16
|
||||
ckpt-fp32
|
44
examples/tutorial/new_api/torch_ddp/README.md
Normal file
44
examples/tutorial/new_api/torch_ddp/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Distributed Data Parallel
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
This example provides a training script and and evaluation script. The training script provides a an example of training ResNet on CIFAR10 dataset from scratch.
|
||||
|
||||
- Training Arguments
|
||||
- `-r, `--resume`: resume from checkpoint file path
|
||||
- `-c`, `--checkpoint`: the folder to save checkpoints
|
||||
- `-i`, `--interval`: epoch interval to save checkpoints
|
||||
- `-f`, `--fp16`: use fp16
|
||||
|
||||
- Eval Arguments
|
||||
- `-e`, `--epoch`: select the epoch to evaluate
|
||||
- `-c`, `--checkpoint`: the folder where checkpoints are found
|
||||
|
||||
|
||||
### Train
|
||||
|
||||
```bash
|
||||
# train with torch DDP with fp32
|
||||
colossalai run --nproc_per_node 2 train.py -c ./ckpt-fp32
|
||||
|
||||
# train with torch DDP with mixed precision training
|
||||
colossalai run --nproc_per_node 2 train.py -c ./ckpt-fp16 --fp16
|
||||
```
|
||||
|
||||
### Eval
|
||||
|
||||
```bash
|
||||
# evaluate fp32 training
|
||||
python eval.py -c ./ckpt-fp32 -e 80
|
||||
|
||||
# evaluate fp16 mixed precision training
|
||||
python eval.py -c ./ckpt-fp16 -e 80
|
||||
```
|
||||
|
||||
Expected accuracy performance will be:
|
||||
|
||||
| Model | Single-GPU Baseline FP32 | Booster DDP with FP32 | Booster DDP with FP16 |
|
||||
| --------- | ------------------------ | --------------------- | --------------------- |
|
||||
| ResNet-18 | 85.85% | 85.03% | 85.12% |
|
||||
|
||||
**Note: the baseline is a adapted from the [script](https://pytorch-tutorial.readthedocs.io/en/latest/tutorial/chapter03_intermediate/3_2_2_cnn_resnet_cifar10/) to use `torchvision.models.resnet18`**
|
48
examples/tutorial/new_api/torch_ddp/eval.py
Normal file
48
examples/tutorial/new_api/torch_ddp/eval.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import argparse
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torchvision
|
||||
import torchvision.transforms as transforms
|
||||
|
||||
# ==============================
|
||||
# Parse Arguments
|
||||
# ==============================
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-e', '--epoch', type=int, default=80, help="resume from the epoch's checkpoint")
|
||||
parser.add_argument('-c', '--checkpoint', type=str, default='./checkpoint', help="checkpoint directory")
|
||||
args = parser.parse_args()
|
||||
|
||||
# ==============================
|
||||
# Prepare Test Dataset
|
||||
# ==============================
|
||||
# CIFAR-10 dataset
|
||||
test_dataset = torchvision.datasets.CIFAR10(root='./data/', train=False, transform=transforms.ToTensor())
|
||||
|
||||
# Data loader
|
||||
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=128, shuffle=False)
|
||||
|
||||
# ==============================
|
||||
# Load Model
|
||||
# ==============================
|
||||
model = torchvision.models.resnet18(num_classes=10).cuda()
|
||||
state_dict = torch.load(f'{args.checkpoint}/model_{args.epoch}.pth')
|
||||
model.load_state_dict(state_dict)
|
||||
|
||||
# ==============================
|
||||
# Run Evaluation
|
||||
# ==============================
|
||||
model.eval()
|
||||
|
||||
with torch.no_grad():
|
||||
correct = 0
|
||||
total = 0
|
||||
for images, labels in test_loader:
|
||||
images = images.cuda()
|
||||
labels = labels.cuda()
|
||||
outputs = model(images)
|
||||
_, predicted = torch.max(outputs.data, 1)
|
||||
total += labels.size(0)
|
||||
correct += (predicted == labels).sum().item()
|
||||
|
||||
print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))
|
128
examples/tutorial/new_api/torch_ddp/train.py
Normal file
128
examples/tutorial/new_api/torch_ddp/train.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torchvision
|
||||
import torchvision.transforms as transforms
|
||||
from torch.optim.lr_scheduler import MultiStepLR
|
||||
|
||||
import colossalai
|
||||
from colossalai.booster import Booster
|
||||
from colossalai.booster.plugin import TorchDDPPlugin
|
||||
from colossalai.cluster import DistCoordinator
|
||||
|
||||
# ==============================
|
||||
# Parse Arguments
|
||||
# ==============================
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-r', '--resume', type=int, default=-1, help="resume from the epoch's checkpoint")
|
||||
parser.add_argument('-c', '--checkpoint', type=str, default='./checkpoint', help="checkpoint directory")
|
||||
parser.add_argument('-i', '--interval', type=int, default=5, help="interval of saving checkpoint")
|
||||
parser.add_argument('-f', '--fp16', action='store_true', help="use fp16")
|
||||
args = parser.parse_args()
|
||||
|
||||
# ==============================
|
||||
# Prepare Checkpoint Directory
|
||||
# ==============================
|
||||
Path(args.checkpoint).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# ==============================
|
||||
# Prepare Hyperparameters
|
||||
# ==============================
|
||||
NUM_EPOCHS = 80
|
||||
LEARNING_RATE = 1e-3
|
||||
START_EPOCH = args.resume if args.resume >= 0 else 0
|
||||
|
||||
# ==============================
|
||||
# Launch Distributed Environment
|
||||
# ==============================
|
||||
colossalai.launch_from_torch(config={})
|
||||
coordinator = DistCoordinator()
|
||||
|
||||
# update the learning rate with linear scaling
|
||||
# old_gpu_num / old_lr = new_gpu_num / new_lr
|
||||
LEARNING_RATE *= coordinator.world_size
|
||||
|
||||
# ==============================
|
||||
# Prepare Booster
|
||||
# ==============================
|
||||
plugin = TorchDDPPlugin()
|
||||
if args.fp16:
|
||||
booster = Booster(mixed_precision='fp16', plugin=plugin)
|
||||
else:
|
||||
booster = Booster(plugin=plugin)
|
||||
|
||||
# ==============================
|
||||
# Prepare Train Dataset
|
||||
# ==============================
|
||||
transform = transforms.Compose(
|
||||
[transforms.Pad(4),
|
||||
transforms.RandomHorizontalFlip(),
|
||||
transforms.RandomCrop(32),
|
||||
transforms.ToTensor()])
|
||||
|
||||
# CIFAR-10 dataset
|
||||
with coordinator.priority_execution():
|
||||
train_dataset = torchvision.datasets.CIFAR10(root='./data/', train=True, transform=transform, download=True)
|
||||
|
||||
# ====================================
|
||||
# Prepare model, optimizer, criterion
|
||||
# ====================================
|
||||
# resent50
|
||||
model = torchvision.models.resnet18(num_classes=10).cuda()
|
||||
|
||||
# Loss and optimizer
|
||||
criterion = nn.CrossEntropyLoss()
|
||||
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
|
||||
|
||||
# lr scheduler
|
||||
lr_scheduler = MultiStepLR(optimizer, milestones=[20, 40, 60, 80], gamma=1 / 3)
|
||||
|
||||
# prepare dataloader with torch ddp plugin
|
||||
train_dataloader = plugin.prepare_train_dataloader(train_dataset, batch_size=100, shuffle=True)
|
||||
|
||||
# ==============================
|
||||
# Resume from checkpoint
|
||||
# ==============================
|
||||
if args.resume >= 0:
|
||||
booster.load_model(model, f'{args.checkpoint}/model_{args.resume}.pth')
|
||||
booster.load_optimizer(optimizer, f'{args.checkpoint}/optimizer_{args.resume}.pth')
|
||||
booster.load_lr_scheduler(lr_scheduler, f'{args.checkpoint}/lr_scheduler_{args.resume}.pth')
|
||||
|
||||
# ==============================
|
||||
# Boost with ColossalAI
|
||||
# ==============================
|
||||
model, optimizer, criterion, train_dataloader, lr_scheduler = booster.boost(model, optimizer, criterion,
|
||||
train_dataloader, lr_scheduler)
|
||||
|
||||
# ==============================
|
||||
# Train model
|
||||
# ==============================
|
||||
total_step = len(train_dataloader)
|
||||
|
||||
for epoch in range(START_EPOCH, NUM_EPOCHS):
|
||||
for i, (images, labels) in enumerate(train_dataloader):
|
||||
images = images.cuda()
|
||||
labels = labels.cuda()
|
||||
|
||||
# Forward pass
|
||||
outputs = model(images)
|
||||
loss = criterion(outputs, labels)
|
||||
|
||||
# Backward and optimize
|
||||
optimizer.zero_grad()
|
||||
booster.backward(loss, optimizer)
|
||||
optimizer.step()
|
||||
|
||||
if (i + 1) % 100 == 0:
|
||||
print("Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}".format(epoch + 1, NUM_EPOCHS, i + 1, total_step,
|
||||
loss.item()))
|
||||
|
||||
lr_scheduler.step()
|
||||
|
||||
# save checkpoint every 5 epoch
|
||||
if (epoch + 1) % args.interval == 0:
|
||||
booster.save_model(model, f'{args.checkpoint}/model_{epoch + 1}.pth')
|
||||
booster.save_optimizer(optimizer, f'{args.checkpoint}/optimizer_{epoch + 1}.pth')
|
||||
booster.save_lr_scheduler(lr_scheduler, f'{args.checkpoint}/lr_scheduler_{epoch + 1}.pth')
|
Reference in New Issue
Block a user