|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Compiler Optimization" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 2, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import torch\n", |
| 17 | + "import os\n", |
| 18 | + "import sys\n", |
| 19 | + "base_dir = os.environ.get('TORCHSIM_DIR', default='/workspace/PyTorchSim')\n", |
| 20 | + "sys.path.append(base_dir)\n", |
| 21 | + "os.environ['TORCHSIM_DUMP_PATH']=base_dir\n", |
| 22 | + "os.environ['TORCHSIM_FUNCTIONAL_MODE']=\"0\"\n", |
| 23 | + "os.environ['TORCHSIM_TIMING_MODE']=\"1\"" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "### GeMM + ReLU fusion" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 3, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [ |
| 38 | + { |
| 39 | + "name": "stderr", |
| 40 | + "output_type": "stream", |
| 41 | + "text": [ |
| 42 | + "Using /root/.cache/torch_extensions/py310_cu121 as PyTorch extensions root...\n", |
| 43 | + "Emitting ninja build file /root/.cache/torch_extensions/py310_cu121/npu/build.ninja...\n", |
| 44 | + "Building extension module npu...\n", |
| 45 | + "Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)\n", |
| 46 | + "Loading extension module npu...\n" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "stdout", |
| 51 | + "output_type": "stream", |
| 52 | + "text": [ |
| 53 | + "ninja: no work to do.\n", |
| 54 | + "Wrapper Codegen Path = /tmp/torchinductor_root/3z/c3zx4dfsx2o24goyevxgy4upevdsyxegbadiquz7z33ttsbs22a6.py\n", |
| 55 | + "[Gem5] Gem5 is running. \n", |
| 56 | + "[TOGSim] TOGSim is running. \n", |
| 57 | + "[TOGSim] Simulation of \"/root/workspace/PyTorchSim/tmp/5o2xythi5z3/tile_graph.onnx\" is stored to \"/root/workspace/PyTorchSim/tmp/5o2xythi5z3/togsim_result/0\"\n" |
| 58 | + ] |
| 59 | + } |
| 60 | + ], |
| 61 | + "source": [ |
| 62 | + "from Scheduler.scheduler import PyTorchSimRunner\n", |
| 63 | + "device = PyTorchSimRunner.setup_device().custom_device()\n", |
| 64 | + "\n", |
| 65 | + "input = torch.randn(1024, 1024).to(device=device)\n", |
| 66 | + "weight = torch.randn(1024, 1024).to(device=device)\n", |
| 67 | + "\n", |
| 68 | + "def gemm_relu(a, b):\n", |
| 69 | + " return torch.relu(torch.matmul(a, b))\n", |
| 70 | + "\n", |
| 71 | + "opt_fn = torch.compile(dynamic=False)(gemm_relu)\n", |
| 72 | + "out = opt_fn(input, weight)" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 4, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [ |
| 80 | + { |
| 81 | + "name": "stdout", |
| 82 | + "output_type": "stream", |
| 83 | + "text": [ |
| 84 | + "[2025-11-30 20:03:06.792] [info] Total execution cycle: 50316\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "!cat /root/workspace/PyTorchSim/tmp/5o2xythi5z3/togsim_result/0 | grep \"Total execution cycle\"" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "### Disable fusion" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 5, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "name": "stdout", |
| 106 | + "output_type": "stream", |
| 107 | + "text": [ |
| 108 | + "Wrapper Codegen Path = /tmp/torchinductor_root/6s/c6sm56korxlpospvj434xqahpah3qitivib3y66vxn6teip2uh32.py\n", |
| 109 | + "[Gem5] Gem5 is running... \n", |
| 110 | + "[Gem5] Gem5 is running.. \n", |
| 111 | + "[TOGSim] TOGSim is running. \n", |
| 112 | + "[TOGSim] Simulation of \"/root/workspace/PyTorchSim/tmp/4q4qv6gbpia/tile_graph.onnx\" is stored to \"/root/workspace/PyTorchSim/tmp/4q4qv6gbpia/togsim_result/4\"\n", |
| 113 | + "[TOGSim] TOGSim is running.. \n", |
| 114 | + "[TOGSim] Simulation of \"/root/workspace/PyTorchSim/tmp/37dfo4nczcq/tile_graph.onnx\" is stored to \"/root/workspace/PyTorchSim/tmp/37dfo4nczcq/togsim_result/0\"\n" |
| 115 | + ] |
| 116 | + } |
| 117 | + ], |
| 118 | + "source": [ |
| 119 | + "os.environ['TORCHSIM_COMPILER_OPTIMIZATION']=\"none\"\n", |
| 120 | + "\n", |
| 121 | + "input = torch.randn(1024, 1024).to(device=device)\n", |
| 122 | + "weight = torch.randn(1024, 1024).to(device=device)\n", |
| 123 | + "\n", |
| 124 | + "def gemm_relu(a, b):\n", |
| 125 | + " return torch.relu(torch.matmul(a, b))\n", |
| 126 | + "\n", |
| 127 | + "opt_fn = torch.compile(dynamic=False)(gemm_relu)\n", |
| 128 | + "out = opt_fn(input, weight)" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 6, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "name": "stdout", |
| 138 | + "output_type": "stream", |
| 139 | + "text": [ |
| 140 | + "[2025-11-30 20:03:30.690] [info] Total execution cycle: 46996\n", |
| 141 | + "[2025-11-30 20:03:32.178] [info] Total execution cycle: 58455\n" |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "!cat /root/workspace/PyTorchSim/tmp/4q4qv6gbpia/togsim_result/4 | grep \"Total execution cycle\"\n", |
| 147 | + "!cat /root/workspace/PyTorchSim/tmp/37dfo4nczcq/togsim_result/0 | grep \"Total execution cycle\"" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [] |
| 156 | + } |
| 157 | + ], |
| 158 | + "metadata": { |
| 159 | + "kernelspec": { |
| 160 | + "display_name": "base", |
| 161 | + "language": "python", |
| 162 | + "name": "python3" |
| 163 | + }, |
| 164 | + "language_info": { |
| 165 | + "codemirror_mode": { |
| 166 | + "name": "ipython", |
| 167 | + "version": 3 |
| 168 | + }, |
| 169 | + "file_extension": ".py", |
| 170 | + "mimetype": "text/x-python", |
| 171 | + "name": "python", |
| 172 | + "nbconvert_exporter": "python", |
| 173 | + "pygments_lexer": "ipython3", |
| 174 | + "version": "3.10.13" |
| 175 | + } |
| 176 | + }, |
| 177 | + "nbformat": 4, |
| 178 | + "nbformat_minor": 2 |
| 179 | +} |
0 commit comments