{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Course set-up"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "__author__ = \"Christopher Potts\"\n",
    "__version__ = \"CS224u, Stanford, Fall 2024\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook covers the steps you'll need to take to get set up for [CS224u](http://web.stanford.edu/class/cs224u/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Contents\n",
    "\n",
    "1. [Anaconda](#Anaconda)\n",
    "1. [The course Github repository](#The-course-Github-repository)\n",
    "1. [Services](#Services)\n",
    "1. [Additional installations](#Additional-installations)\n",
    "1. [Jupyter notebooks](#Jupyter-notebooks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Anaconda\n",
    "\n",
    "We recommend installing [the free Anaconda Python distribution](https://www.anaconda.com/products/individual), which includes IPython, Numpy, Scipy, matplotlib, scikit-learn, NLTK, and many other useful packages. This is not required, but it's an easy way to get all these packages installed. Unless you're very comfortable with Python package management and like installing things, this is the option for you!\n",
    "\n",
    "Please be sure that you download the __Python 3__ version, which currently installs Python 3.9. __Our codebase is not compatible with Python 2__.\n",
    "\n",
    "One you have Anaconda installed, create a virtual environment for the course. In a terminal, run\n",
    "\n",
    "```conda create -n nlu python=3.9 anaconda```\n",
    "\n",
    "to create an environment called `nlu`.\n",
    "\n",
    "> NOTE: Run the above command without `anaconda` if you see this error message `PackagesNotFoundError: The following packages are not available from current channels: - anaconda`\n",
    "\n",
    "\n",
    "Then, to enter the environment, run\n",
    "\n",
    "```conda activate nlu```\n",
    "\n",
    "To leave it, you can just close the window, or run\n",
    "\n",
    "```conda deactivate```\n",
    "\n",
    "If your version of Anaconda is older than version 4.4 (see `conda --version`), then replace `conda` with `source` in the above (and consider upgrading your Anaconda!).\n",
    "\n",
    "[This page](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html) has more detailed instructions on managing virtual environments with Anaconda."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The course Github repository\n",
    "\n",
    "The core materials for the course are on Github:\n",
    "\n",
    "https://github.com/cgpotts/cs224u\n",
    "\n",
    "We'll be working in this repository a lot, and it will receive updates throughout the quarter, as we add new materials and correct bugs.\n",
    "\n",
    "If you're new to git and Github, we recommend using [Github's Desktop Apps](https://desktop.github.com). Then you just have to clone our repository and sync your local copy with the official one when there are updates. \n",
    "\n",
    "If you are comfortable with git in the command line, you can type the following command to clone the course's Github repo:\n",
    "\n",
    "```git clone https://github.com/cgpotts/cs224u```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Services\n",
    "\n",
    "There are a variety of services that we recommend signing up for to help you with course work:\n",
    "\n",
    "* [Google Colab](https://colab.research.google.com/signup/pricing): Browser-based system for working with noteoboks. This is free, but for $9.99/month you get a substantial upgrade in performance and reliability. Consider subscribing for three months – this is probably less than your least expensive required textbook (we have no textbook)!\n",
    "\n",
    "* [SageMaker Studio Lab](https://studiolab.sagemaker.aws/): Similiar to Colab but often with better GPU support. This service is currently free for all users.\n",
    "\n",
    "* [OpenAI](https://beta.openai.ai): New accounts get <s>18</s> 5 dollars in free credits, and some of the models may still be free to use."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Additional installations\n",
    "\n",
    "Be sure to do these additional installations from [inside your virtual environment](#Anaconda) for the course! Before you proceed from here, perhaps run\n",
    "\n",
    "```conda activate nlu```\n",
    "\n",
    "to make sure you are in that environment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we would need to define the device used throughout the assignments. If you have a Nvidia GPU the device you would need to set would be of form `cuXXX`. Google Colab uses currently Cuda 12.1 so the device could be set then to `cu121` but can be differently based on the CUDA version installed on your machine. Refer to `nvidia-smi` command to look it up. If you don't have a Nvidia GPU it should be then set to `cpu`. Below we simply default to `cpu` for Windows/Linux systems, while for MacOS this is not required and therefore can be skipped.\n",
    "\n",
    "*NOTE:* uncomment the lines with `torch` in the `requirements.txt` file and then run the following commands to install the required packages.\n",
    "\n",
    "**For Linux**:\n",
    "\n",
    "```\n",
    "export DEVICE=cpu\n",
    "```\n",
    "\n",
    "**For Windows**:\n",
    "\n",
    "```\n",
    "set DEVICE=cpu\n",
    "```\n",
    "\n",
    "After setting the device we need to install the core packages required by the assignments.\n",
    "\n",
    "**For MacOS**:\n",
    "\n",
    "```\n",
    "pip install \\\n",
    "    -f https://download.pytorch.org/whl/torch/ \\\n",
    "    -r requirements.txt\n",
    "```\n",
    "\n",
    "**For Linux**:\n",
    "\n",
    "```\n",
    "pip install \\\n",
    "    -f https://download.pytorch.org/whl/torch/ \\\n",
    "    -r requirements.txt\n",
    "```\n",
    "\n",
    "**For Windows**:\n",
    "\n",
    "```\n",
    "pip install ^\n",
    "    -f https://download.pytorch.org/whl/torch/ ^\n",
    "    -r requirements.txt\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our most important and finicky installations relate to our deep learning code. The following will check that you have the desired versions of the core libraries ([PyTorch](https://pytorch.org/) and [Hugging Face](https://huggingface.co/) `transformers`):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "assert torch.__version__.startswith('2.4.0'),\\\n",
    "    f\"torch version is {torch.__version__}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import transformers\n",
    "from packaging import version\n",
    "\n",
    "assert version.parse(transformers.__version__) > version.parse(\"4.37\"),\\\n",
    "    f\"transformers version is {transformers.__version__}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the above tests didn't pass, you *might* be okay, but it is probably best to change your versions inside the `nlu` virtual environment. These are fast-changing libraries and we can't ensure complete backward compatibility."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Jupyter notebooks\n",
    "\n",
    "The majority of the materials for this course are Jupyter notebooks, which allow you to work in a browser, mixing code and description. It's a powerful form of [literate programming](https://en.wikipedia.org/wiki/Literate_programming), and increasingly a standard for open science.\n",
    "\n",
    "To start a notebook server, navigate to the directory where you want to work and run\n",
    "\n",
    "```jupyter notebook --port 5656```\n",
    "\n",
    "The port specification is optional. \n",
    "\n",
    "This should launch a browser that takes you to a view of the directory you're in. You can then open notebooks for working and create new notebooks.\n",
    "\n",
    "A major advantage of working with Anaconda is that you can switch virtual environments from inside a notebook, via the __Kernel__ menu. If this isn't an option for you, then run this command while inside your virtual environment:\n",
    "\n",
    "```python -m ipykernel install --user --name nlu --display-name \"nlu\"```\n",
    "\n",
    "(If you named your environment something other than `nlu`, then change the `--name` and `--display-name` values.) \n",
    "\n",
    "[Additional discussion of Jupyter and kernels.](https://stackoverflow.com/a/44786736)\n",
    "\n",
    "For some tips on getting started with notebooks, see [our Jupyter tutorial](tutorial_jupyter_notebooks.ipynb).\n",
    "\n",
    "**Alternatively**, if you are *visual studio code* user, you can use the [Jupyter extension](https://code.visualstudio.com/docs/datascience/jupyter-notebooks) to run notebooks directly in the editor."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.19"
  },
  "vscode": {
   "interpreter": {
    "hash": "81ef301a63437b26d6f879bc64646dce7fed7674109198cc71f09ca02787000d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
