-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from train_resnet_base import TrainResNetBase | ||
|
||
import itertools | ||
|
||
import torch_xla.distributed.xla_multiprocessing as xmp | ||
import torch_xla.core.xla_model as xm | ||
from torch_xla.amp import autocast | ||
|
||
|
||
# For more details check https://github.com/pytorch/xla/blob/master/docs/amp.md | ||
class TrainResNetXLAAMP(TrainResNetBase): | ||
|
||
def train_loop_fn(self, loader, epoch): | ||
tracker = xm.RateTracker() | ||
self.model.train() | ||
loader = itertools.islice(loader, self.num_steps) | ||
for step, (data, target) in enumerate(loader): | ||
self.optimizer.zero_grad() | ||
# Enables autocasting for the forward pass | ||
with autocast(xm.xla_device()): | ||
output = self.model(data) | ||
loss = self.loss_fn(output, target) | ||
# TPU amp uses bf16 hence gradient scaling is not necessary. If runnign with XLA:GPU | ||
# check https://github.com/pytorch/xla/blob/master/docs/amp.md#amp-for-xlagpu. | ||
loss.backward() | ||
self.run_optimizer() | ||
tracker.add(self.batch_size) | ||
if step % 10 == 0: | ||
xm.add_step_closure( | ||
self._train_update, args=(step, loss, tracker, epoch)) | ||
|
||
|
||
if __name__ == '__main__': | ||
xla_amp = TrainResNetXLAAMP() | ||
xla_amp.start_training() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters