{
 "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": [
    "# (beta) Using TORCH_LOGS python API with torch.compile\n",
    "\n",
    "**Author:** [Michael Lazos](https://github.com/mlazos)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import logging"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial introduces the `TORCH_LOGS` environment variable, as well\n",
    "as the Python API, and demonstrates how to apply it to observe the\n",
    "phases of `torch.compile`.\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 tutorial requires PyTorch 2.2.0 or later.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup\n",
    "\n",
    "In this example, we\\'ll set up a simple Python function which performs\n",
    "an elementwise add and observe the compilation process with `TORCH_LOGS`\n",
    "Python API.\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>There is also an environment variable <code>TORCH_LOGS</code>, which can be used tochange logging settings at the command line. The equivalent environmentvariable setting is shown for each example.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "# exit cleanly if we are on a device that doesn't support torch.compile\n",
    "if torch.cuda.get_device_capability() < (7, 0):\n",
    "    print(\"Skipping because torch.compile is not supported on this device.\")\n",
    "else:\n",
    "    @torch.compile()\n",
    "    def fn(x, y):\n",
    "        z = x + y\n",
    "        return z + 2\n",
    "\n",
    "\n",
    "    inputs = (torch.ones(2, 2, device=\"cuda\"), torch.zeros(2, 2, device=\"cuda\"))\n",
    "\n",
    "\n",
    "# print separator and reset dynamo\n",
    "# between each example\n",
    "    def separator(name):\n",
    "        print(f\"==================={name}=========================\")\n",
    "        torch._dynamo.reset()\n",
    "\n",
    "\n",
    "    separator(\"Dynamo Tracing\")\n",
    "# View dynamo tracing\n",
    "# TORCH_LOGS=\"+dynamo\"\n",
    "    torch._logging.set_logs(dynamo=logging.DEBUG)\n",
    "    fn(*inputs)\n",
    "\n",
    "    separator(\"Traced Graph\")\n",
    "# View traced graph\n",
    "# TORCH_LOGS=\"graph\"\n",
    "    torch._logging.set_logs(graph=True)\n",
    "    fn(*inputs)\n",
    "\n",
    "    separator(\"Fusion Decisions\")\n",
    "# View fusion decisions\n",
    "# TORCH_LOGS=\"fusion\"\n",
    "    torch._logging.set_logs(fusion=True)\n",
    "    fn(*inputs)\n",
    "\n",
    "    separator(\"Output Code\")\n",
    "# View output code generated by inductor\n",
    "# TORCH_LOGS=\"output_code\"\n",
    "    torch._logging.set_logs(output_code=True)\n",
    "    fn(*inputs)\n",
    "\n",
    "    separator(\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conclusion\n",
    "\n",
    "In this tutorial we introduced the TORCH_LOGS environment variable and\n",
    "python API by experimenting with a small number of the available logging\n",
    "options. To view descriptions of all available options, run any python\n",
    "script which imports torch and set TORCH_LOGS to \\\"help\\\".\n",
    "\n",
    "Alternatively, you can view the [torch.\\_logging\n",
    "documentation](https://pytorch.org/docs/main/logging.html) to see\n",
    "descriptions of all available logging options.\n",
    "\n",
    "For more information on torch.compile, see the [torch.compile\n",
    "tutorial](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html).\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
}
