tutorial_pipe.py 800 Bytes
Newer Older
1
2
3
4
import torch
import torch.optim as optim

import fairscale
5
from helpers import getModel, getData, getLossFun
6

7
8
9
10
11
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

model = getModel()
data, target = getData()[0]
loss_fn = getLossFun()
12
13
14
15
16
17
18
19
20
21

model = fairscale.nn.Pipe(model, balance=[2, 1])

# define optimizer and loss function
optimizer = optim.SGD(model.parameters(), lr=0.001)


# zero the parameter gradients
optimizer.zero_grad()

22
device = torch.device("cuda", rank) if DEVICE == "cuda" else torch.device("cpu")
23
24
25

# outputs and target need to be on the same device
# forward step
26
outputs = model(data.to(device).requires_grad_())
27
28
29
30
31
32
33
34
35
36
# compute loss
loss = loss_fn(outputs.to(device), target.to(device))

# backward + optimize
loss.backward()
optimizer.step()

print("Finished Training Step")

del model