Skip to content

Commit e5bea03

Browse files
luis-realAlannaBurkesvekars
authored
Add XPU support to torchvision_tutorial.py (#3536)
## Motivation - Refine the torchvision tutorial to make it device-agnostic by using the Accelerator API. ## Description - Integrate the Accelerator API into the torchvision tutorial to support multiple accelerators. Co-authored-by: Alanna Burke <burkealanna@meta.com> Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
1 parent 4355164 commit e5bea03

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

intermediate_source/torchvision_tutorial.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@ def get_transform(train):
406406

407407

408408
######################################################################
409-
# Let’s now write the main function which performs the training and the
410-
# validation:
409+
# We want to be able to train our model on an `accelerator <https://pytorch.org/docs/stable/torch.html#accelerators>`_
410+
# such as CUDA, MPS, MTIA, or XPU. Let’s now write the main function which performs the training and the validation:
411411

412412

413413
from engine import train_one_epoch, evaluate
414414

415-
# train on the GPU or on the CPU, if a GPU is not available
416-
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
415+
# train on the accelerator or on the CPU, if an accelerator is not available
416+
device = torch.accelerator.current_accelerator() if torch.accelerator.is_available() else torch.device('cpu')
417417

418418
# our dataset has two classes only - background and person
419419
num_classes = 2

0 commit comments

Comments
 (0)