mirror of
https://github.com/mrdbourke/pytorch-deep-learning.git
synced 2026-11-27 06:07:09 +00:00
update pytorch 2 tutorial
This commit is contained in:
1 parent
9a933f53fc
commit
6db24449d9
1 file changed
+199
-82
+199
-82
@@ -53,7 +53,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code examples"
|
||||
"## Quick code examples"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -124,7 +124,7 @@
|
||||
"\n",
|
||||
"<img src=\"https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/extras-pytorch-2-speedups.png\" alt=\"speedups for PyTorch 2.0 across various model resources\" width=650/>\n",
|
||||
"\n",
|
||||
"*PyTorch 2.0 speedups across various models from different locations. **Source:** [PyTorch 2.0 announcement post](https://pytorch.org/get-started/pytorch-2.0/).*"
|
||||
"*PyTorch 2.0 speedups across various models from different locations. *Source:* [PyTorch 2.0 announcement post](https://pytorch.org/get-started/pytorch-2.0/).*"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -243,8 +243,9 @@
|
||||
"\n",
|
||||
"One of the main ones being with exporting models.\n",
|
||||
"\n",
|
||||
"<img src=\"https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/extras-pytorch-2-limitations.png\" alt=\"PyTorch 2 limitations\" width=650/>\n",
|
||||
"\n",
|
||||
"TK - image of where it doesn't work\n",
|
||||
"*There are a few caveats when using the PyTorch 2.0 features, such as not being about to export to mobile devices when using the `torch.compile()` default options. However, there are work arounds to this and improved exporting is on the PyTorch 2.x roadmap. *Source:* [PyTorch 2.0 announcement post](https://pytorch.org/get-started/pytorch-2.0/).*\n",
|
||||
"\n",
|
||||
"However, these will likely be fixed in future releases.\n",
|
||||
"\n",
|
||||
@@ -256,14 +257,33 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TK - Resources to learn more\n",
|
||||
"## TK - What we're going to cover\n",
|
||||
"\n",
|
||||
"The following resources are helpful \n",
|
||||
"* PyTorch 2.0 launch blog post - https://pytorch.org/get-started/pytorch-2.0/ \n",
|
||||
"* PyTorch 2.0 release notes - https://pytorch.org/blog/pytorch-2.0-release/ \n",
|
||||
" * GitHub release notes - https://github.com/pytorch/pytorch/releases/tag/v2.0.0 (lots of info here!)\n",
|
||||
"* PyTorch default device context manager - https://github.com/pytorch/tutorials/pull/2220/files \n",
|
||||
"* PyTorch 2.0 video introduction - https://youtu.be/WqLKfta5Ijw"
|
||||
"Since many of the upgrades in PyTorch 2.0 are under the hood, in this notebook we're going to run a compartive speed test.\n",
|
||||
"\n",
|
||||
"Namely we'll make two of the same models, one using the default PyTorch setup and the other using the new `torch.compile()` setup.\n",
|
||||
"\n",
|
||||
"1. Model 1 - no `torch.compile()`.\n",
|
||||
"2. Model 2 - `torch.compile()`.\n",
|
||||
"\n",
|
||||
"We'll then compare the training/testing times of both models for single run and multiple runs.\n",
|
||||
"\n",
|
||||
"| **Experiment** | **Model 1* | **Model 2** | \n",
|
||||
"|----- |-----|-----|\n",
|
||||
"| Single run | no `torch.compile()` | with `torch.compile()` |\n",
|
||||
"| Multiple runs (3 x single runs) | no `torch.compile()` | with `torch.compile()` |\n",
|
||||
"\n",
|
||||
"To keep things straight forward, we'll use the same modelling and data setup for all experiments:\n",
|
||||
"\n",
|
||||
"| **Model** | **Data** | **Epochs** | **Batch size** | **Image size** |\n",
|
||||
"|----- |-----|-----|-----|-----|\n",
|
||||
"| [ResNet50](https://pytorch.org/vision/stable/models/generated/torchvision.models.resnet50.html#torchvision.models.resnet50) (from TorchVision) | [CIFAR10](https://pytorch.org/vision/main/generated/torchvision.datasets.CIFAR10.html) (from TorchVision) | 5 (single run) | 128 | 224 |\n",
|
||||
"\n",
|
||||
"We've chosen ResNet50 here for ease of access or use, however, you could substitute any model you like.\n",
|
||||
"\n",
|
||||
"And the biggest speedups I've noticed are when the GPU computes on as much data as possible (e.g. larger batch size/image size/data size/model size). \n",
|
||||
"\n",
|
||||
"> **Note:** Depending on the size of your GPU, you may have to lower the batch size (or image size) to fit the model on your GPU. If you're using a GPU with 8GB of memory or less, you may have to lower the batch size to 64 or 32."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -271,12 +291,16 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TK - 0. Getting setup"
|
||||
"## TK - 0. Getting setup\n",
|
||||
"\n",
|
||||
"To get setup we'll first check for PyTorch 2.x+ and install it if it's not available. \n",
|
||||
"\n",
|
||||
"You can see how to install PyTorch 2.x on your own system in the [documentation](https://pytorch.org/get-started/locally/)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -311,65 +335,41 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### TK - New feature: globally set devices"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"cuda:0\n",
|
||||
"cuda:0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# See here: https://github.com/pytorch/tutorials/pull/2220/files \n",
|
||||
"import torch\n",
|
||||
"with torch.device('cuda'):\n",
|
||||
" mod = torch.nn.Linear(20, 30)\n",
|
||||
" print(mod.weight.device)\n",
|
||||
" print(mod(torch.randn(128, 20)).device)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"TODO: \n",
|
||||
"* add in info about PyTorch 2.0\n",
|
||||
"* a quick upgrade for speed ups\n",
|
||||
"* a quick note on which GPU will be needed (works best on NVIDIA GPUs, not macOS)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TK - Check GPU\n",
|
||||
"Wonderful!\n",
|
||||
"\n",
|
||||
"* **Note:** If you're running on Google Colab, you'll need to setup a GPU: runtime -> change runtime type -> hardware accelerator\n",
|
||||
"* Best speedups are on newer NVIDIA/AMD GPUs (this is because PyTorch 2.0 leverages new GPU hardware)"
|
||||
"Now PyTorch 2.x is installed, let's check out a new feature!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TK - Get GPU info\n",
|
||||
"\n",
|
||||
"> **Note:** If you're running on Google Colab, you'll need to setup a GPU: runtime -> change runtime type -> hardware accelerator. The best speedups are on newer NVIDIA/AMD GPUs (this is because PyTorch 2.0 leverages new GPU hardware). This tutorial focuses on NVIDIA GPUs.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UPTOHERE: \n",
|
||||
"* add info about GPU scores\n",
|
||||
"* add info about PyTorch 2.x features being on newer GPUs (e.g. score of (8, 0) and above)\n",
|
||||
"* potentially a table of the GPU availability?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"GPU name: NVIDIA_GeForce_RTX_4080\n",
|
||||
"GPU capability score: (8, 9)\n",
|
||||
"GPU score higher than (8, 0), PyTorch 2.x speedup features available.\n",
|
||||
"GPU information:\n",
|
||||
"Thu Mar 16 16:19:41 2023 \n",
|
||||
"Thu Mar 23 13:43:20 2023 \n",
|
||||
"+-----------------------------------------------------------------------------+\n",
|
||||
"| NVIDIA-SMI 525.89.02 Driver Version: 525.89.02 CUDA Version: 12.0 |\n",
|
||||
"|-------------------------------+----------------------+----------------------+\n",
|
||||
@@ -378,7 +378,7 @@
|
||||
"| | | MIG M. |\n",
|
||||
"|===============================+======================+======================|\n",
|
||||
"| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 Off | N/A |\n",
|
||||
"| 0% 45C P2 35W / 320W | 371MiB / 16376MiB | 0% Default |\n",
|
||||
"| 0% 46C P8 11W / 320W | 371MiB / 16376MiB | 0% Default |\n",
|
||||
"| | | N/A |\n",
|
||||
"+-------------------------------+----------------------+----------------------+\n",
|
||||
" \n",
|
||||
@@ -389,10 +389,8 @@
|
||||
"|=============================================================================|\n",
|
||||
"| 0 N/A N/A 1001 G /usr/lib/xorg/Xorg 86MiB |\n",
|
||||
"| 0 N/A N/A 1223 G /usr/bin/gnome-shell 10MiB |\n",
|
||||
"| 0 N/A N/A 73068 C ...ch/env-nightly/bin/python 270MiB |\n",
|
||||
"+-----------------------------------------------------------------------------+\n",
|
||||
"GPU name: NVIDIA_GeForce_RTX_4080\n",
|
||||
"GPU capability score: (8, 9)\n"
|
||||
"| 0 N/A N/A 2195975 C ...ch/env-nightly/bin/python 270MiB |\n",
|
||||
"+-----------------------------------------------------------------------------+\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -401,30 +399,147 @@
|
||||
"if torch.cuda.is_available():\n",
|
||||
" gpu_info = !nvidia-smi\n",
|
||||
" gpu_info = '\\n'.join(gpu_info)\n",
|
||||
" if gpu_info.find('failed') >= 0:\n",
|
||||
" print('Not connected to a GPU')\n",
|
||||
" else:\n",
|
||||
" print(f\"GPU information:\\n{gpu_info}\")\n",
|
||||
" if gpu_info.find(\"failed\") >= 0:\n",
|
||||
" print(\"Not connected to a GPU, to leverage the best of PyTorch 2.0, you should connect to a GPU.\")\n",
|
||||
"\n",
|
||||
" # Get GPU name\n",
|
||||
" gpu_name = !nvidia-smi --query-gpu=gpu_name --format=csv\n",
|
||||
" gpu_name = gpu_name[1]\n",
|
||||
" # Replace spaces with \"_\" (for naming files later on)\n",
|
||||
" gpu_name = gpu_name.replace(\" \", \"_\")\n",
|
||||
" print(f'GPU name: {gpu_name}')\n",
|
||||
"\n",
|
||||
" # Get GPU capability score\n",
|
||||
" gpu_score = torch.cuda.get_device_capability()\n",
|
||||
" print(f\"GPU capability score: {gpu_score}\")\n",
|
||||
" if gpu_score > (8, 0):\n",
|
||||
" print(f\"GPU score higher than (8, 0), PyTorch 2.x speedup features available.\")\n",
|
||||
" else:\n",
|
||||
" print(f\"GPU score lower than (8, 0), PyTorch 2.x speedup features will be limited (PyTorch 2.x speedups happen most on newer GPUs).\")\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # Print GPU info\n",
|
||||
" print(f\"GPU information:\\n{gpu_info}\")\n",
|
||||
"\n",
|
||||
" # TK - print out if GPU score is capable of getting the best speedups "
|
||||
"else:\n",
|
||||
" print(\"PyTorch couldn't find a GPU, to leverage the best of PyTorch 2.0, you should connect to a GPU.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"* TK - add a table for NVIDIA GPUs and architectures etc and which lead to speedups"
|
||||
"### TK - New feature: globally set devices\n",
|
||||
"\n",
|
||||
"One of my favourite new features in PyTorch 2.x is being able to set the [default device type](https://pytorch.org/tutorials/recipes/recipes/changing_default_device.html ) via:\n",
|
||||
"* Context manager\n",
|
||||
"* Globally\n",
|
||||
"\n",
|
||||
"Previously, you could only set the default device type via:\n",
|
||||
"* `tensor.to(device)`\n",
|
||||
"\n",
|
||||
"Let's see these two new device settings in action."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Layer weights are on device: cuda:0\n",
|
||||
"Layer creating data on device: cuda:0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"# Set the device\n",
|
||||
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
||||
"\n",
|
||||
"# Set the device with context manager (requires PyTorch 2.x+)\n",
|
||||
"with torch.device(device):\n",
|
||||
" # All tensors created in this block will be on device\n",
|
||||
" layer = torch.nn.Linear(20, 30)\n",
|
||||
" print(f\"Layer weights are on device: {layer.weight.device}\")\n",
|
||||
" print(f\"Layer creating data on device: {layer(torch.randn(128, 20)).device}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now how about setting the global device?\n",
|
||||
"\n",
|
||||
"This will mean that any tensors created without an explicit device will be created on the device you set by default."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Layer weights are on device: cuda:0\n",
|
||||
"Layer creating data on device: cuda:0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"# Set the device\n",
|
||||
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
||||
"\n",
|
||||
"# Set the device globally\n",
|
||||
"torch.set_default_device(device)\n",
|
||||
"\n",
|
||||
"# All tensors created will be on the global device by default\n",
|
||||
"layer = torch.nn.Linear(20, 30)\n",
|
||||
"print(f\"Layer weights are on device: {layer.weight.device}\")\n",
|
||||
"print(f\"Layer creating data on device: {layer(torch.randn(128, 20)).device}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"And now back to CPU."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Layer weights are on device: cpu\n",
|
||||
"Layer creating data on device: cpu\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import torch \n",
|
||||
"\n",
|
||||
"# Set the device globally\n",
|
||||
"torch.set_default_device(\"cpu\")\n",
|
||||
"\n",
|
||||
"# All tensors created will be on \"cpu\"\n",
|
||||
"layer = torch.nn.Linear(20, 30)\n",
|
||||
"print(f\"Layer weights are on device: {layer.weight.device}\")\n",
|
||||
"print(f\"Layer creating data on device: {layer(torch.randn(128, 20)).device}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -434,6 +549,8 @@
|
||||
"source": [
|
||||
"## TK - Simple training example \n",
|
||||
"\n",
|
||||
"TK - start experiments here (the experiment table is what we want to be going through all with the same model/dataset just with `torch.compile()` and without `torch.compile()` )\n",
|
||||
"\n",
|
||||
"* CIFAR10\n",
|
||||
"* ResNet50"
|
||||
]
|
||||
@@ -2556,17 +2673,17 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
"source": [
|
||||
"## TK - Resources to learn more\n",
|
||||
"\n",
|
||||
"I've found the following resources to be helpful learning about PyTorch 2.0 and it's upcoming features.\n",
|
||||
"\n",
|
||||
"* [PyTorch 2.0 launch blog post](https://pytorch.org/get-started/pytorch-2.0/). \n",
|
||||
"* [PyTorch 2.0 release notes](https://pytorch.org/blog/pytorch-2.0-release/) (blog post).\n",
|
||||
" * As well as the [GitHub release notes](https://github.com/pytorch/pytorch/releases/tag/v2.0.0) (lots of info here!).\n",
|
||||
"* [PyTorch default device context manager docs](https://github.com/pytorch/tutorials/pull/2220/files).\n",
|
||||
"* [PyTorch 2.0 video introduction on YouTube](https://youtu.be/WqLKfta5Ijw) (created by yours truly)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in new issue
Block a user