{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 0
   },
   "source": [
    "# Using Amazon SageMaker\n",
    ":label:`sec_sagemaker`\n",
    "\n",
    "Many deep learning applications require a significant amount of computation. Your local machine might be too slow to solve these problems in a reasonable amount of time. Cloud computing services give you access to more powerful computers to run the GPU-intensive portions of this book. This tutorial will guide you through Amazon SageMaker: a service that allows you to run this book easily.\n",
    "\n",
    "\n",
    "## Registering and Logging In\n",
    "\n",
    "First, we need to register an account at https://aws.amazon.com/. We encourage you to use two-factor authentication for additional security. It is also a good idea to set up detailed billing and spending alerts to avoid any unexpected surprises in case you forget to stop any running instance.\n",
    "Note that you will need a credit card.\n",
    "After logging into your AWS account, go to your [console](http://console.aws.amazon.com/) and search for \"SageMaker\" (see :numref:`fig_sagemaker`) then click to open the SageMaker panel.\n",
    "\n",
    "![Open the SageMaker panel.](../img/sagemaker.png)\n",
    ":width:`300px`\n",
    ":label:`fig_sagemaker`\n",
    "\n",
    "\n",
    "\n",
    "## Creating a SageMaker Instance\n",
    "\n",
    "Next, let us create a notebook instance as described in :numref:`fig_sagemaker-create`.\n",
    "\n",
    "![Create a SageMaker instance.](../img/sagemaker-create.png)\n",
    ":width:`400px`\n",
    ":label:`fig_sagemaker-create`\n",
    "\n",
    "SageMaker provides multiple [instance types](https://aws.amazon.com/sagemaker/pricing/instance-types/) of different computational power and prices.\n",
    "When creating an instance, we can specify the instance name and choose its type.\n",
    "In :numref:`fig_sagemaker-create-2`, we choose `ml.p3.2xlarge`. With one Tesla V100 GPU and an 8-core CPU, this instance is powerful enough for most chapters.\n",
    "\n",
    "![Choose the instance type.](../img/sagemaker-create-2.png)\n",
    ":width:`400px`\n",
    ":label:`fig_sagemaker-create-2`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 1,
    "tab": [
     "mxnet"
    ]
   },
   "source": [
    "A Jupyter notebook version of this book for fitting SageMaker is available at https://github.com/d2l-ai/d2l-en-sagemaker. We can specify this GitHub repository URL to let SageMaker clone this repository during instance creation, as shown in :numref:`fig_sagemaker-create-3`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 4
   },
   "source": [
    "![Specify the GitHub repository.](../img/sagemaker-create-3.png)\n",
    ":width:`400px`\n",
    ":label:`fig_sagemaker-create-3`\n",
    "\n",
    "\n",
    "\n",
    "## Running and Stopping an Instance\n",
    "\n",
    "It may take a few minutes before the instance is ready.\n",
    "When it is ready, you can click on the \"Open Jupyter\" link as shown in :numref:`fig_sagemaker-open`.\n",
    "\n",
    "![Open Jupyter on the created SageMaker instance.](../img/sagemaker-open.png)\n",
    ":width:`400px`\n",
    ":label:`fig_sagemaker-open`\n",
    "\n",
    "Then, as shown in :numref:`fig_sagemaker-jupyter`, you may navigate through the Jupyter server running on this instance.\n",
    "\n",
    "![The Jupyter server running on the SageMaker instance.](../img/sagemaker-jupyter.png)\n",
    ":width:`400px`\n",
    ":label:`fig_sagemaker-jupyter`\n",
    "\n",
    "Running and editing Jupyter notebooks on the SageMaker instance is similar to what we have discussed in :numref:`sec_jupyter`.\n",
    "After finishing your work, do not forget to stop the instance to avoid further charging, as shown in :numref:`fig_sagemaker-stop`.\n",
    "\n",
    "![Stop a SageMaker instance.](../img/sagemaker-stop.png)\n",
    ":width:`300px`\n",
    ":label:`fig_sagemaker-stop`\n",
    "\n",
    "\n",
    "## Updating Notebooks\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 5,
    "tab": [
     "mxnet"
    ]
   },
   "source": [
    "We will regularly update the notebooks in the [d2l-ai/d2l-en-sagemaker](https://github.com/d2l-ai/d2l-en-sagemaker) GitHub repository. You can simply use the `git pull` command to update to the latest version.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 8
   },
   "source": [
    "First, you need to open a terminal as shown in :numref:`fig_sagemaker-terminal`.\n",
    "\n",
    "![Open a terminal on the SageMaker instance.](../img/sagemaker-terminal.png)\n",
    ":width:`300px`\n",
    ":label:`fig_sagemaker-terminal`\n",
    "\n",
    "You may want to commit your local changes before pulling the updates. Alternatively, you can simply ignore all your local changes with the following commands in the terminal.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 9,
    "tab": [
     "mxnet"
    ]
   },
   "source": [
    "```bash\n",
    "cd SageMaker/d2l-en-sagemaker/\n",
    "git reset --hard\n",
    "git pull\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 12
   },
   "source": [
    "## Summary\n",
    "\n",
    "* We can launch and stop a Jupyter server through Amazon SageMaker to run this book.\n",
    "* We can update notebooks via the terminal on the Amazon SageMaker instance.\n",
    "\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. Try to edit and run the code in this book using Amazon SageMaker.\n",
    "1. Access the source code directory via the terminal.\n",
    "\n",
    "\n",
    "[Discussions](https://discuss.d2l.ai/t/422)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}