{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# For tips on running notebooks in Google Colab, see\n",
    "# https://docs.pytorch.org/tutorials/beginner/colab\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Changing default device\n",
    "\n",
    "It is common practice to write PyTorch code in a device-agnostic way,\n",
    "and then switch between CPU and CUDA depending on what hardware is\n",
    "available. Typically, to do this you might have used if-statements and\n",
    "`cuda()` calls to do this:\n",
    "\n",
    "<div style=\"background-color: #54c7ec; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>NOTE:</strong></div>\n",
    "\n",
    "<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "\n",
    "<p>This recipe requires PyTorch 2.0.0 or later.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "USE_CUDA = False\n",
    "\n",
    "mod = torch.nn.Linear(20, 30)\n",
    "if USE_CUDA:\n",
    "    mod.cuda()\n",
    "\n",
    "device = 'cpu'\n",
    "if USE_CUDA:\n",
    "    device = 'cuda'\n",
    "inp = torch.randn(128, 20, device=device)\n",
    "print(mod(inp).device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PyTorch now also has a context manager which can take care of the device\n",
    "transfer automatically. Here is an example:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with torch.device('cuda'):\n",
    "    mod = torch.nn.Linear(20, 30)\n",
    "    print(mod.weight.device)\n",
    "    print(mod(torch.randn(128, 20)).device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also set it globally like this:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "torch.set_default_device('cuda')\n",
    "\n",
    "mod = torch.nn.Linear(20, 30)\n",
    "print(mod.weight.device)\n",
    "print(mod(torch.randn(128, 20)).device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This function imposes a slight performance cost on every Python call to\n",
    "the torch API (not just factory functions). If this is causing problems\n",
    "for you, please comment on [this\n",
    "issue](https://github.com/pytorch/pytorch/issues/92701)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
