From 3122205dff376552692138f825ce696582214837 Mon Sep 17 00:00:00 2001 From: Alvaro Perez Date: Thu, 30 Mar 2023 18:17:29 +0200 Subject: [PATCH 1/4] added mps code and comment --- beginner_source/basics/buildmodel_tutorial.py | 9 ++++----- beginner_source/basics/quickstart_tutorial.py | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/beginner_source/basics/buildmodel_tutorial.py b/beginner_source/basics/buildmodel_tutorial.py index d2c0572d1ff..1e4261f345e 100644 --- a/beginner_source/basics/buildmodel_tutorial.py +++ b/beginner_source/basics/buildmodel_tutorial.py @@ -32,12 +32,11 @@ ############################################# # Get Device for Training # ----------------------- -# We want to be able to train our model on a hardware accelerator like the GPU, -# if it is available. Let's check to see if -# `torch.cuda `_ is available, else we -# continue to use the CPU. +# We want to be able to train our model on a hardware accelerator like the GPU or MPS, +# if available. Let's check to see if `torch.cuda `_ +# or `torch.backends.mps are available, otherwise we use the CPU. -device = "cuda" if torch.cuda.is_available() else "cpu" +device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" print(f"Using {device} device") ############################################## diff --git a/beginner_source/basics/quickstart_tutorial.py b/beginner_source/basics/quickstart_tutorial.py index 366a4193d0b..233adb6aa23 100644 --- a/beginner_source/basics/quickstart_tutorial.py +++ b/beginner_source/basics/quickstart_tutorial.py @@ -84,9 +84,9 @@ # To define a neural network in PyTorch, we create a class that inherits # from `nn.Module `_. We define the layers of the network # in the ``__init__`` function and specify how data will pass through the network in the ``forward`` function. To accelerate -# operations in the neural network, we move it to the GPU if available. +# operations in the neural network, we move it to the GPU or MPS if available. -# Get cpu or gpu device for training. +# Get cpu, gpu or mps device for training. device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" print(f"Using {device} device") From fd984ebed7a1adf97583cb5f4290d9c6185e5b99 Mon Sep 17 00:00:00 2001 From: Alvaro Perez Date: Thu, 30 Mar 2023 19:15:33 +0200 Subject: [PATCH 2/4] fixed link --- beginner_source/basics/buildmodel_tutorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beginner_source/basics/buildmodel_tutorial.py b/beginner_source/basics/buildmodel_tutorial.py index 1e4261f345e..484beabf2db 100644 --- a/beginner_source/basics/buildmodel_tutorial.py +++ b/beginner_source/basics/buildmodel_tutorial.py @@ -34,7 +34,7 @@ # ----------------------- # We want to be able to train our model on a hardware accelerator like the GPU or MPS, # if available. Let's check to see if `torch.cuda `_ -# or `torch.backends.mps are available, otherwise we use the CPU. +# or `torch.backends.mps ` are available, otherwise we use the CPU. device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" print(f"Using {device} device") From 2b25c19826af2fdd12cd01de771ea4b46d44d8b5 Mon Sep 17 00:00:00 2001 From: Alvaro Perez Date: Thu, 30 Mar 2023 19:52:39 +0200 Subject: [PATCH 3/4] better formatting --- beginner_source/basics/buildmodel_tutorial.py | 8 +++++++- beginner_source/basics/quickstart_tutorial.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/beginner_source/basics/buildmodel_tutorial.py b/beginner_source/basics/buildmodel_tutorial.py index 484beabf2db..6e78f42eb27 100644 --- a/beginner_source/basics/buildmodel_tutorial.py +++ b/beginner_source/basics/buildmodel_tutorial.py @@ -36,7 +36,13 @@ # if available. Let's check to see if `torch.cuda `_ # or `torch.backends.mps ` are available, otherwise we use the CPU. -device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" +device = ( + "cuda" + if torch.cuda.is_available() + else "mps" + if torch.backends.mps.is_available() + else "cpu" +) print(f"Using {device} device") ############################################## diff --git a/beginner_source/basics/quickstart_tutorial.py b/beginner_source/basics/quickstart_tutorial.py index 233adb6aa23..de4a8b45437 100644 --- a/beginner_source/basics/quickstart_tutorial.py +++ b/beginner_source/basics/quickstart_tutorial.py @@ -87,7 +87,13 @@ # operations in the neural network, we move it to the GPU or MPS if available. # Get cpu, gpu or mps device for training. -device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" +device = ( + "cuda" + if torch.cuda.is_available() + else "mps" + if torch.backends.mps.is_available() + else "cpu" +) print(f"Using {device} device") # Define model From 87da1cbffae54d468f5afd90c669813c0000cde1 Mon Sep 17 00:00:00 2001 From: Alvaro Perez Date: Thu, 30 Mar 2023 21:28:31 +0200 Subject: [PATCH 4/4] finally fixed link --- beginner_source/basics/buildmodel_tutorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beginner_source/basics/buildmodel_tutorial.py b/beginner_source/basics/buildmodel_tutorial.py index 6e78f42eb27..cae5c99134a 100644 --- a/beginner_source/basics/buildmodel_tutorial.py +++ b/beginner_source/basics/buildmodel_tutorial.py @@ -34,7 +34,7 @@ # ----------------------- # We want to be able to train our model on a hardware accelerator like the GPU or MPS, # if available. Let's check to see if `torch.cuda `_ -# or `torch.backends.mps ` are available, otherwise we use the CPU. +# or `torch.backends.mps `_ are available, otherwise we use the CPU. device = ( "cuda"