diff --git a/extras/pytorch_2_intro.ipynb b/extras/pytorch_2_intro.ipynb index 012f0c9..faff5e3 100644 --- a/extras/pytorch_2_intro.ipynb +++ b/extras/pytorch_2_intro.ipynb @@ -347,13 +347,42 @@ "source": [ "## TK - Get GPU info\n", "\n", + "Time to get GPU info.\n", + "\n", + "It's important to know that many of the speedups PyTorch 2.0 offers are best experienced on newer GPUs.\n", + "\n", + "This is because PyTorch 2.0 takes advantage of the new hardware on newer GPUs.\n", + "\n", + "You can see a list of [NVIDIA GPU compute capability scores](https://developer.nvidia.com/cuda-gpus) on NVIDIA's developer page.\n", + "\n", + "Here are some scores of NVIDIA GPUs released in 2020 or later:\n", + "\n", + "| **NVIDIA GPU** | **Compute capability score** | **GPU Type** | **Release year** | **Architecture** |\n", + "|----- |-----| -----| -----| -----| \n", + "| RTX 4090 | 8.9 | Desktop-class | 2022 | [Ada Lovelace](https://www.nvidia.com/en-au/geforce/ada-lovelace-architecture/) |\n", + "| RTX 4080 | 8.9 | Desktop-class | 2022 | Ada Lovelace |\n", + "| RTX 4070 Ti | 8.9 | Desktop-class | 2022 | Ada Lovelace |\n", + "| RTX 3090 | 8.6 | Desktop-class | 2020 | [Ampere](https://en.wikipedia.org/wiki/Ampere_(microarchitecture)) |\n", + "| RTX 3080 | 8.6 | Desktop-class | 2020 | Ampere| \n", + "| RTX 3070 | 8.6 | Desktop-class | 2020 | Ampere | \n", + "| RTX 3060 Ti | 8.6 | Desktop-class | 2020 | Ampere | \n", + "| A100 | 8.0 | Datacenter-class | 2020 | Ampere |\n", + "| A10 | 8.6 | Datacenter-class | 2021 | Ampere |\n", + "| H100 | 9.0 | Datacenter-class | 2022 | [Hopper](https://developer.nvidia.com/blog/nvidia-hopper-architecture-in-depth/) | \n", + "\n", + "GPUs with a compute capability score of 8.0 or above are likely to see the biggest speedups.\n", + "\n", + "And GPUs which are datacenter-class (e.g. A100, A10, H100) are likely to see more significant speedups than desktop-class GPUs (e.g. RTX 3090, RTX 3080, RTX 3070, RTX 3060 Ti).\n", + "\n", + "We can check the compute capbility score of our GPU using [`torch.cuda.get_device_capability()`](https://pytorch.org/docs/stable/generated/torch.cuda.get_device_capability.html).\n", + "\n", + "This will output a tuple of `(major, minor)` compute capability scores, for example, `(8, 0)` for the A100.\n", + "\n", + "We'll also get some other details about our GPU such as the name and other info using [`nvidia-smi`](https://developer.nvidia.com/nvidia-system-management-interface). \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?" + "> **Resource:** For an in-depth comparison of many different NVIDIA GPUs and their speeds, costs and tradeoffs, I'd recommend reading Tim Dettmers' [*Which GPU for deep learning?*](https://timdettmers.com/2023/01/30/which-gpu-for-deep-learning/) blog post." ] }, { @@ -416,7 +445,6 @@ " 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", @@ -549,10 +577,34 @@ "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", + "Okay, time to measure speed!\n", "\n", - "* CIFAR10\n", - "* ResNet50" + "To keep things simple, we'll run a series of four experiments, all with:\n", + "\n", + "* **Model:** ResNet50 (from [TorchVision](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet50.html))\n", + "* **Data:** CIFAR10 (from [TorchVision](https://pytorch.org/vision/main/generated/torchvision.datasets.CIFAR10.html))\n", + "* **Epochs:** 5 (single run) and 3x5 (multiple runs)\n", + "* **Batch size:** 128\n", + "* **Image size:** 224\n", + "\n", + "However, each experiment will be run with and without `torch.compile()`.\n", + "\n", + "In table form:\n", + "\n", + "| **Experiment** | **Model** | **Data** | **Epochs** | **Batch size** | **Image size** | **`torch.compile()`** | \n", + "|----- |-----| -----| -----| -----| -----| -----|\n", + "| 1 (single run) | ResNet50 | CIFAR10 | 5 | 128 | 224 | No |\n", + "| 2 (single run) | ResNet50 | CIFAR10 | 5 | 128 | 224 | Yes |\n", + "| 3 (multi-run) | ResNet50 | CIFAR10 | 3x5 | 128 | 224 | No |\n", + "| 4 (multi-run) | ResNet50 | CIFAR10 | 3x5 | 128 | 224 | Yes |\n", + "\n", + "Why the single and multiple runs?\n", + "\n", + "Because we can measure speedups via a single run, however, we'll also want to run the tests multiple times to get an average. \n", + "\n", + "> **Note:** Depending on the size of your GPU (the memory available), you may have to lower the batch size or the image size. This tutorial is focused on using an A100 GPU (with 40GB of memory) available on Google Colab Pro. \n", + "\n", + "Let's start by importing `torch` and `torchvision` and setting the target device." ] }, { @@ -571,36 +623,33 @@ ], "source": [ "import torch\n", - "print(f\"PyTorch version: {torch.__version__}\")\n", + "import torchvision\n", "\n", + "print(f\"PyTorch version: {torch.__version__}\")\n", + "print(f\"TorchVision version: {torchvision.__version__}\")\n", + "\n", + "# Set the target device\n", "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", + "\n", "print(f\"Using device: {device}\")" ] }, { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TorchVision version: 0.15.1+cu118\n" - ] - } - ], - "source": [ - "import torchvision\n", - "\n", - "print(f\"TorchVision version: {torchvision.__version__}\")" - ] - }, - { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ - "### Create model and transforms" + "### TK - Create model and transforms\n", + "\n", + "Let's now create our model and transforms.\n", + "\n", + "We'll use the same setup to create the model and transforms we covered in [06. PyTorch Transfer Learning section 2.2](https://www.learnpytorch.io/06_pytorch_transfer_learning/).\n", + "\n", + "In essence, we'll create the model and transforms for the model using the `torchvision.models` API.\n", + "\n", + "We can get the weights and transforms for ResNet50 using the following:\n", + "* `model_weights = torchvision.models.ResNet50_Weights.IMAGENET1K_V2` (this requires `torchvision` 0.14 or later).\n", + "* `transforms = model_weights.transforms()` (once we have the weights, we can get the appropriate transforms for the model)." ] }, { @@ -617,38 +666,35 @@ } ], "source": [ - "model_weights = torchvision.models.ResNet50_Weights.IMAGENET1K_V2\n", + "# Create model weights and transforms\n", + "model_weights = torchvision.models.ResNet50_Weights.IMAGENET1K_V2 # <- use the latest weights (could also use .DEFAULT)\n", "transforms = model_weights.transforms()\n", + "\n", + "# Setup model\n", "model = torchvision.models.resnet50(weights=model_weights)\n", "\n", + "# Count the number of parameters in the model \n", "total_params = sum(\n", - "\tparam.numel() for param in model.parameters()\n", + " param.numel() for param in model.parameters() # <- all params\n", + "\t# param.numel() for param in model.parameters() if param.requires_grad # <- only trainable params\n", ")\n", "\n", - "print(total_params)" + "print(f\"Total parameters of model: {total_params}\")\n", + "print(f\"Model transforms:\\n{transforms}\")" ] }, { - "cell_type": "code", - "execution_count": 11, + "attachments": {}, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ImageClassification(\n", - " crop_size=[224]\n", - " resize_size=[232]\n", - " mean=[0.485, 0.456, 0.406]\n", - " std=[0.229, 0.224, 0.225]\n", - " interpolation=InterpolationMode.BILINEAR\n", - ")\n" - ] - } - ], "source": [ - "print(transforms)" + "UPTOHERE: \n", + "* Check device memory and compute capability \n", + "* If the device doesn't have a high amount of memory, chance the image size\n", + " * Can check the device memory size \n", + " using: https://pytorch.org/docs/stable/generated/torch.cuda.mem_get_info.html#torch.cuda.mem_get_info \n", + "* Set `torch.backends.cuda.matmul.allow_tf32` to `True` for newer GPUs\n", + " * Research to find out what GPUs are compatible with this " ] }, { @@ -805,10 +851,15 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ - "### Create training loops" + "### TK - Create training loops\n", + "\n", + "TK - Training loops source - https://www.learnpytorch.io/05_pytorch_going_modular/ \n", + "\n", + "TK these come from notebook 05 ( or put in the right notebook but have been slightly modified to measure time per epoch)" ] }, {