Skip to content

Commit

Permalink
Note about MPS in ch06 and ch07 (rasbt#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt authored Aug 19, 2024
1 parent 8ef5022 commit c443035
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 15 additions & 0 deletions ch06/01_main-chapter-code/ch06.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,21 @@
],
"source": [
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"# Note:\n",
"# Uncommenting the following lines will allow the code to run on Apple Silicon chips, if applicable,\n",
"# which is approximately 2x faster than on an Apple CPU (as measured on an M3 MacBook Air).\n",
"# As of this writing, in PyTorch 2.4, the results obtained via CPU and MPS were identical.\n",
"# However, in earlier versions of PyTorch, you may observe different results when using MPS.\n",
"\n",
"#if torch.cuda.is_available():\n",
"# device = torch.device(\"cuda\")\n",
"#elif torch.backends.mps.is_available():\n",
"# device = torch.device(\"mps\")\n",
"#else:\n",
"# device = torch.device(\"cpu\")\n",
"#print(f\"Running on {device} device.\")\n",
"\n",
"model.to(device) # no assignment model = model.to(device) necessary for nn.Module classes\n",
"\n",
"torch.manual_seed(123) # For reproducibility due to the shuffling in the training data loader\n",
Expand Down
20 changes: 12 additions & 8 deletions ch07/01_main-chapter-code/ch07.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1125,13 +1125,17 @@
"source": [
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"# If you have a Mac with Apple Silicon chip, you can uncomment the next lines of code\n",
"# to train the model on the Mac's GPU cores. However, as of this writing, this results in\n",
"# larger numerical deviations from the results shown in this chapter, because Apple Silicon\n",
"# support in PyTorch is still experimental\n",
"\n",
"# if torch.backends.mps.is_available():\n",
"# device = torch.device(\"mps\")\n",
"# Note:\n",
"# Uncommenting the following lines will allow the code to run on Apple Silicon chips, if applicable,\n",
"# which is much faster than on an Apple CPU (as measured on an M3 MacBook Air).\n",
"# However, the resulting loss values may be slightly different.\n",
"\n",
"#if torch.cuda.is_available():\n",
"# device = torch.device(\"cuda\")\n",
"#elif torch.backends.mps.is_available():\n",
"# device = torch.device(\"mps\")\n",
"#else:\n",
"# device = torch.device(\"cpu\")\n",
"\n",
"print(\"Device:\", device)"
]
Expand Down Expand Up @@ -2762,7 +2766,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.11.4"
}
},
"nbformat": 4,
Expand Down

0 comments on commit c443035

Please sign in to comment.