{
 "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": [
    "# Profiling your PyTorch Module\n",
    "\n",
    "**Author:** [Suraj Subramanian](https://github.com/subramen)\n",
    "\n",
    "PyTorch includes a profiler API that is useful to identify the time and\n",
    "memory costs of various PyTorch operations in your code. Profiler can be\n",
    "easily integrated in your code, and the results can be printed as a\n",
    "table or returned in a JSON trace file.\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>Profiler supports multithreaded models. Profiler runs in thesame thread as the operation but it will also profile child operatorsthat might run in another thread. Concurrently-running profilers will bescoped to their own thread to prevent mixing of results.</p>\n",
    "\n",
    "</div>\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>PyTorch 1.8 introduces the new API that will replace the older profiler APIin the future releases. Check the new API at <a href=\"https://pytorch.org/docs/master/profiler.html\">this page</a>.</p>\n",
    "\n",
    "</div>\n",
    "\n",
    "Head on over to [this\n",
    "recipe](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html)\n",
    "for a quicker walkthrough of Profiler API usage.\n",
    "\n",
    "------------------------------------------------------------------------\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "from torch import nn\n",
    "import torch.autograd.profiler as profiler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Performance debugging using Profiler\n",
    "\n",
    "Profiler can be useful to identify performance bottlenecks in your\n",
    "models. In this example, we build a custom module that performs two\n",
    "sub-tasks:\n",
    "\n",
    "-   a linear transformation on the input, and\n",
    "-   use the transformation result to get indices on a mask tensor.\n",
    "\n",
    "We wrap the code for each sub-task in separate labelled context managers\n",
    "using `profiler.record_function(\"label\")`. In the profiler output, the\n",
    "aggregate performance metrics of all operations in the sub-task will\n",
    "show up under its corresponding label.\n",
    "\n",
    "Note that using Profiler incurs some overhead, and is best used only for\n",
    "investigating code. Remember to remove it if you are benchmarking\n",
    "runtimes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class MyModule(nn.Module):\n",
    "    def __init__(self, in_features: int, out_features: int, bias: bool = True):\n",
    "        super(MyModule, self).__init__()\n",
    "        self.linear = nn.Linear(in_features, out_features, bias)\n",
    "\n",
    "    def forward(self, input, mask):\n",
    "        with profiler.record_function(\"LINEAR PASS\"):\n",
    "            out = self.linear(input)\n",
    "\n",
    "        with profiler.record_function(\"MASK INDICES\"):\n",
    "            threshold = out.sum(axis=1).mean().item()\n",
    "            hi_idx = np.argwhere(mask.cpu().numpy() > threshold)\n",
    "            hi_idx = torch.from_numpy(hi_idx).cuda()\n",
    "\n",
    "        return out, hi_idx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Profile the forward pass\n",
    "\n",
    "We initialize random input and mask tensors, and the model.\n",
    "\n",
    "Before we run the profiler, we warm-up CUDA to ensure accurate\n",
    "performance benchmarking. We wrap the forward pass of our module in the\n",
    "`profiler.profile` context manager. The `with_stack=True` parameter\n",
    "appends the file and line number of the operation in the trace.\n",
    "\n",
    "<div style=\"background-color: #e94f3b; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>WARNING:</strong></div>\n",
    "\n",
    "<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "\n",
    "<p><code>with_stack=True</code> incurs an additional overhead, and is better suited for investigating code.Remember to remove it if you are benchmarking performance.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = MyModule(500, 10).cuda()\n",
    "input = torch.rand(128, 500).cuda()\n",
    "mask = torch.rand((500, 500, 500), dtype=torch.double).cuda()\n",
    "\n",
    "# warm-up\n",
    "model(input, mask)\n",
    "\n",
    "with profiler.profile(with_stack=True, profile_memory=True) as prof:\n",
    "    out, idx = model(input, mask)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Print profiler results\n",
    "\n",
    "Finally, we print the profiler results. `profiler.key_averages`\n",
    "aggregates the results by operator name, and optionally by input shapes\n",
    "and/or stack trace events. Grouping by input shapes is useful to\n",
    "identify which tensor shapes are utilized by the model.\n",
    "\n",
    "Here, we use `group_by_stack_n=5` which aggregates runtimes by the\n",
    "operation and its traceback (truncated to the most recent 5 events), and\n",
    "display the events in the order they are registered. The table can also\n",
    "be sorted by passing a `sort_by` argument (refer to the\n",
    "[docs](https://pytorch.org/docs/stable/autograd.html#profiler) for valid\n",
    "sorting keys).\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>When running profiler in a notebook, you might see entries like <code>&lt;ipython-input-18-193a910735e8&gt;(13): forward</code>instead of filenames in the stacktrace. These correspond to <code>&lt;notebook-cell&gt;(line number): calling-function</code>.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(prof.key_averages(group_by_stack_n=5).table(sort_by='self_cpu_time_total', row_limit=5))\n",
    "\n",
    "\"\"\"\n",
    "(Some columns are omitted)\n",
    "\n",
    "-------------  ------------  ------------  ------------  ---------------------------------\n",
    "         Name    Self CPU %      Self CPU  Self CPU Mem   Source Location\n",
    "-------------  ------------  ------------  ------------  ---------------------------------\n",
    " MASK INDICES        87.88%        5.212s    -953.67 Mb  /mnt/xarfuse/.../torch/au\n",
    "                                                         <ipython-input-...>(10): forward\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "                                                         <ipython-input-...>(9): <module>\n",
    "                                                         /mnt/xarfuse/.../IPython/\n",
    "\n",
    "  aten::copy_        12.07%     715.848ms           0 b  <ipython-input-...>(12): forward\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "                                                         <ipython-input-...>(9): <module>\n",
    "                                                         /mnt/xarfuse/.../IPython/\n",
    "                                                         /mnt/xarfuse/.../IPython/\n",
    "\n",
    "  LINEAR PASS         0.01%     350.151us         -20 b  /mnt/xarfuse/.../torch/au\n",
    "                                                         <ipython-input-...>(7): forward\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "                                                         <ipython-input-...>(9): <module>\n",
    "                                                         /mnt/xarfuse/.../IPython/\n",
    "\n",
    "  aten::addmm         0.00%     293.342us           0 b  /mnt/xarfuse/.../torch/nn\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "                                                         <ipython-input-...>(8): forward\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "\n",
    "   aten::mean         0.00%     235.095us           0 b  <ipython-input-...>(11): forward\n",
    "                                                         /mnt/xarfuse/.../torch/nn\n",
    "                                                         <ipython-input-...>(9): <module>\n",
    "                                                         /mnt/xarfuse/.../IPython/\n",
    "                                                         /mnt/xarfuse/.../IPython/\n",
    "\n",
    "-----------------------------  ------------  ---------- ----------------------------------\n",
    "Self CPU time total: 5.931s\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Improve memory performance\n",
    "\n",
    "Note that the most expensive operations - in terms of memory and time\n",
    "-are at `forward (10)` representing the operations within MASK INDICES.\n",
    "Let's try to tackle the memory consumption first. We can see that the\n",
    "`.to()` operation at line 12 consumes 953.67 Mb. This operation copies\n",
    "`mask` to the CPU. `mask` is initialized with a `torch.double` datatype.\n",
    "Can we reduce the memory footprint by casting it to `torch.float`\n",
    "instead?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = MyModule(500, 10).cuda()\n",
    "input = torch.rand(128, 500).cuda()\n",
    "mask = torch.rand((500, 500, 500), dtype=torch.float).cuda()\n",
    "\n",
    "# warm-up\n",
    "model(input, mask)\n",
    "\n",
    "with profiler.profile(with_stack=True, profile_memory=True) as prof:\n",
    "    out, idx = model(input, mask)\n",
    "\n",
    "print(prof.key_averages(group_by_stack_n=5).table(sort_by='self_cpu_time_total', row_limit=5))\n",
    "\n",
    "\"\"\"\n",
    "(Some columns are omitted)\n",
    "\n",
    "-----------------  ------------  ------------  ------------  --------------------------------\n",
    "             Name    Self CPU %      Self CPU  Self CPU Mem   Source Location\n",
    "-----------------  ------------  ------------  ------------  --------------------------------\n",
    "     MASK INDICES        93.61%        5.006s    -476.84 Mb  /mnt/xarfuse/.../torch/au\n",
    "                                                             <ipython-input-...>(10): forward\n",
    "                                                             /mnt/xarfuse/  /torch/nn\n",
    "                                                             <ipython-input-...>(9): <module>\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "\n",
    "      aten::copy_         6.34%     338.759ms           0 b  <ipython-input-...>(12): forward\n",
    "                                                             /mnt/xarfuse/.../torch/nn\n",
    "                                                             <ipython-input-...>(9): <module>\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "\n",
    " aten::as_strided         0.01%     281.808us           0 b  <ipython-input-...>(11): forward\n",
    "                                                             /mnt/xarfuse/.../torch/nn\n",
    "                                                             <ipython-input-...>(9): <module>\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "\n",
    "      aten::addmm         0.01%     275.721us           0 b  /mnt/xarfuse/.../torch/nn\n",
    "                                                             /mnt/xarfuse/.../torch/nn\n",
    "                                                             /mnt/xarfuse/.../torch/nn\n",
    "                                                             <ipython-input-...>(8): forward\n",
    "                                                             /mnt/xarfuse/.../torch/nn\n",
    "\n",
    "      aten::_local        0.01%     268.650us           0 b  <ipython-input-...>(11): forward\n",
    "      _scalar_dense                                          /mnt/xarfuse/.../torch/nn\n",
    "                                                             <ipython-input-...>(9): <module>\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "                                                             /mnt/xarfuse/.../IPython/\n",
    "\n",
    "-----------------  ------------  ------------  ------------  --------------------------------\n",
    "Self CPU time total: 5.347s\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The CPU memory footprint for this operation has halved.\n",
    "\n",
    "# Improve time performance\n",
    "\n",
    "While the time consumed has also reduced a bit, it's still too high.\n",
    "Turns out copying a matrix from CUDA to CPU is pretty expensive! The\n",
    "`aten::copy_` operator in `forward (12)` copies `mask` to CPU so that it\n",
    "can use the NumPy `argwhere` function. `aten::copy_` at `forward(13)`\n",
    "copies the array back to CUDA as a tensor. We could eliminate both of\n",
    "these if we use a `torch` function `nonzero()` here instead.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class MyModule(nn.Module):\n",
    "    def __init__(self, in_features: int, out_features: int, bias: bool = True):\n",
    "        super(MyModule, self).__init__()\n",
    "        self.linear = nn.Linear(in_features, out_features, bias)\n",
    "\n",
    "    def forward(self, input, mask):\n",
    "        with profiler.record_function(\"LINEAR PASS\"):\n",
    "            out = self.linear(input)\n",
    "\n",
    "        with profiler.record_function(\"MASK INDICES\"):\n",
    "            threshold = out.sum(axis=1).mean()\n",
    "            hi_idx = (mask > threshold).nonzero(as_tuple=True)\n",
    "\n",
    "        return out, hi_idx\n",
    "\n",
    "\n",
    "model = MyModule(500, 10).cuda()\n",
    "input = torch.rand(128, 500).cuda()\n",
    "mask = torch.rand((500, 500, 500), dtype=torch.float).cuda()\n",
    "\n",
    "# warm-up\n",
    "model(input, mask)\n",
    "\n",
    "with profiler.profile(with_stack=True, profile_memory=True) as prof:\n",
    "    out, idx = model(input, mask)\n",
    "\n",
    "print(prof.key_averages(group_by_stack_n=5).table(sort_by='self_cpu_time_total', row_limit=5))\n",
    "\n",
    "\"\"\"\n",
    "(Some columns are omitted)\n",
    "\n",
    "--------------  ------------  ------------  ------------  ---------------------------------\n",
    "          Name    Self CPU %      Self CPU  Self CPU Mem   Source Location\n",
    "--------------  ------------  ------------  ------------  ---------------------------------\n",
    "      aten::gt        57.17%     129.089ms           0 b  <ipython-input-...>(12): forward\n",
    "                                                          /mnt/xarfuse/.../torch/nn\n",
    "                                                          <ipython-input-...>(25): <module>\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "\n",
    " aten::nonzero        37.38%      84.402ms           0 b  <ipython-input-...>(12): forward\n",
    "                                                          /mnt/xarfuse/.../torch/nn\n",
    "                                                          <ipython-input-...>(25): <module>\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "\n",
    "   INDEX SCORE         3.32%       7.491ms    -119.21 Mb  /mnt/xarfuse/.../torch/au\n",
    "                                                          <ipython-input-...>(10): forward\n",
    "                                                          /mnt/xarfuse/.../torch/nn\n",
    "                                                          <ipython-input-...>(25): <module>\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "\n",
    "aten::as_strided         0.20%    441.587us          0 b  <ipython-input-...>(12): forward\n",
    "                                                          /mnt/xarfuse/.../torch/nn\n",
    "                                                          <ipython-input-...>(25): <module>\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "\n",
    " aten::nonzero\n",
    "     _numpy             0.18%     395.602us           0 b  <ipython-input-...>(12): forward\n",
    "                                                          /mnt/xarfuse/.../torch/nn\n",
    "                                                          <ipython-input-...>(25): <module>\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "                                                          /mnt/xarfuse/.../IPython/\n",
    "--------------  ------------  ------------  ------------  ---------------------------------\n",
    "Self CPU time total: 225.801ms\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Further Reading\n",
    "\n",
    "We have seen how Profiler can be used to investigate time and memory\n",
    "bottlenecks in PyTorch models. Read more about Profiler here:\n",
    "\n",
    "-   [Profiler Usage\n",
    "    Recipe](https://docs.pytorch.org/tutorials/recipes/recipes/profiler_recipe.html)\n",
    "-   [Profiler API\n",
    "    Docs](https://pytorch.org/docs/stable/autograd.html?highlight=profiler#profiler)\n",
    "-   [Hugging Face PyTorch Profiler Blog\n",
    "    Post](https://huggingface.co/blog/torch-profiler)\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
}
