Skip to content

Commit

Permalink
Merge pull request #756 from ZhouLong0/Modified
Browse files Browse the repository at this point in the history
Added 'getting Pytorch to run on Apple Arm GPU' guide
  • Loading branch information
mrdbourke committed Jan 11, 2024
2 parents c60e58f + 1c585d1 commit 1f43ea8
Showing 1 changed file with 83 additions and 3 deletions.
86 changes: 83 additions & 3 deletions 00_pytorch_fundamentals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3501,6 +3501,86 @@
"Knowing the number of GPUs PyTorch has access to is helpful incase you wanted to run a specific process on one GPU and another process on another (PyTorch also has features to let you run a process across *all* GPUs)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"### 2.1 Getting PyTorch to run on the ARM GPUs\n",
"\n",
"In order to run PyTorch on the Apple's M1/M2 GPUs you can use the [`torch.backends.mps`](https://pytorch.org/docs/stable/notes/mps.html) package.\n",
"\n",
"Be sure that the versions of the MacOS and Pytorch are updated\n",
"\n",
"You can test if PyTorch has access to a GPU using `torch.backends.mps.is_available()`\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check for ARM GPU\n",
"import torch\n",
"torch.backends.mps.is_available()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'mps'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Set device type\n",
"device = \"mps\" if torch.backends.mps.is_available() else \"cpu\"\n",
"device"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As before, if the above output `\"mps\"` it means we can set all of our PyTorch code to use the available Apple Arm GPU"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"if torch.cuda.is_available():\n",
" device = 'cuda'\n",
"elif torch.backends.mps.is_available():\n",
" device = 'mps'\n",
"else:\n",
" device = 'cpu'"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -3524,7 +3604,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -3543,10 +3623,10 @@
{
"data": {
"text/plain": [
"tensor([1, 2, 3], device='cuda:0')"
"tensor([1, 2, 3], device='mps:0')"
]
},
"execution_count": 74,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand Down

0 comments on commit 1f43ea8

Please sign in to comment.