Skip to content
Merged

Perf #190

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def get_result_from_file(result_path):
dram_channel_bw = {}
avg_dram_bw = None
simulation_time = None
total_cycle = None

# Read and find total stat position
with open(result_path, "r") as f:
Expand Down
179 changes: 179 additions & 0 deletions tutorial/session1/CompilerOptimization.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compiler Optimization"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import os\n",
"import sys\n",
"base_dir = os.environ.get('TORCHSIM_DIR', default='/workspace/PyTorchSim')\n",
"sys.path.append(base_dir)\n",
"os.environ['TORCHSIM_DUMP_PATH']=base_dir\n",
"os.environ['TORCHSIM_FUNCTIONAL_MODE']=\"0\"\n",
"os.environ['TORCHSIM_TIMING_MODE']=\"1\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GeMM + ReLU fusion"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using /root/.cache/torch_extensions/py310_cu121 as PyTorch extensions root...\n",
"Emitting ninja build file /root/.cache/torch_extensions/py310_cu121/npu/build.ninja...\n",
"Building extension module npu...\n",
"Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)\n",
"Loading extension module npu...\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"ninja: no work to do.\n",
"Wrapper Codegen Path = /tmp/torchinductor_root/3z/c3zx4dfsx2o24goyevxgy4upevdsyxegbadiquz7z33ttsbs22a6.py\n",
"[Gem5] Gem5 is running. \n",
"[TOGSim] TOGSim is running. \n",
"[TOGSim] Simulation of \"/root/workspace/PyTorchSim/tmp/5o2xythi5z3/tile_graph.onnx\" is stored to \"/root/workspace/PyTorchSim/tmp/5o2xythi5z3/togsim_result/0\"\n"
]
}
],
"source": [
"from Scheduler.scheduler import PyTorchSimRunner\n",
"device = PyTorchSimRunner.setup_device().custom_device()\n",
"\n",
"input = torch.randn(1024, 1024).to(device=device)\n",
"weight = torch.randn(1024, 1024).to(device=device)\n",
"\n",
"def gemm_relu(a, b):\n",
" return torch.relu(torch.matmul(a, b))\n",
"\n",
"opt_fn = torch.compile(dynamic=False)(gemm_relu)\n",
"out = opt_fn(input, weight)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2025-11-30 20:03:06.792] [info] Total execution cycle: 50316\n"
]
}
],
"source": [
"!cat /root/workspace/PyTorchSim/tmp/5o2xythi5z3/togsim_result/0 | grep \"Total execution cycle\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Disable fusion"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wrapper Codegen Path = /tmp/torchinductor_root/6s/c6sm56korxlpospvj434xqahpah3qitivib3y66vxn6teip2uh32.py\n",
"[Gem5] Gem5 is running... \n",
"[Gem5] Gem5 is running.. \n",
"[TOGSim] TOGSim is running. \n",
"[TOGSim] Simulation of \"/root/workspace/PyTorchSim/tmp/4q4qv6gbpia/tile_graph.onnx\" is stored to \"/root/workspace/PyTorchSim/tmp/4q4qv6gbpia/togsim_result/4\"\n",
"[TOGSim] TOGSim is running.. \n",
"[TOGSim] Simulation of \"/root/workspace/PyTorchSim/tmp/37dfo4nczcq/tile_graph.onnx\" is stored to \"/root/workspace/PyTorchSim/tmp/37dfo4nczcq/togsim_result/0\"\n"
]
}
],
"source": [
"os.environ['TORCHSIM_COMPILER_OPTIMIZATION']=\"none\"\n",
"\n",
"input = torch.randn(1024, 1024).to(device=device)\n",
"weight = torch.randn(1024, 1024).to(device=device)\n",
"\n",
"def gemm_relu(a, b):\n",
" return torch.relu(torch.matmul(a, b))\n",
"\n",
"opt_fn = torch.compile(dynamic=False)(gemm_relu)\n",
"out = opt_fn(input, weight)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2025-11-30 20:03:30.690] [info] Total execution cycle: 46996\n",
"[2025-11-30 20:03:32.178] [info] Total execution cycle: 58455\n"
]
}
],
"source": [
"!cat /root/workspace/PyTorchSim/tmp/4q4qv6gbpia/togsim_result/4 | grep \"Total execution cycle\"\n",
"!cat /root/workspace/PyTorchSim/tmp/37dfo4nczcq/togsim_result/0 | grep \"Total execution cycle\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
130 changes: 130 additions & 0 deletions tutorial/session1/DNNServing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DNN Serving System"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import os\n",
"import sys\n",
"base_dir = os.environ.get('TORCHSIM_DIR', default='/workspace/PyTorchSim')\n",
"sys.path.append(base_dir)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Scheduler"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from torchvision.models import resnet18\n",
"from Scheduler.scheduler import Scheduler, SchedulerDNNModel, Request\n",
"from PyTorchSimFrontend.extension_config import CONFIG_TORCHSIM_BACKEND_CONFIG\n",
"\n",
"scheduler = Scheduler(num_request_queue=1, engine_select=Scheduler.FIFO_ENGINE, backend_config=CONFIG_TORCHSIM_BACKEND_CONFIG)\n",
"device = scheduler.execution_engine.module.custom_device()\n",
"\n",
"model = resnet18().eval()\n",
"input = torch.randn(1, 3, 224, 224).to(device=device)\n",
"opt_fn = torch.compile(dynamic=False)(model.to(device, memory_format=torch.channels_last))\n",
"\n",
"SchedulerDNNModel.register_model(\"resnet18\", opt_fn)\n",
"request = Request(\"resnet18\", [input], [], request_queue_idx=0)\n",
"scheduler.add_request(request, request_time=0)\n",
"\n",
"# Run scheduler\n",
"while not scheduler.is_finished():\n",
" with torch.no_grad():\n",
" scheduler.schedule()\n",
"\n",
"print(\"ResNet18 Simulation Done\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load Generator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import torch\n",
"from torchvision.models import resnet18\n",
"\n",
"from Scheduler.scheduler import Scheduler, SchedulerDNNModel, Request, poisson_request_generator\n",
"CONFIG_TORCHSIM_DIR = os.environ.get('TORCHSIM_DIR', default='/workspace/PyTorchSim')\n",
"\n",
"lambda_requests = 10\n",
"max_time = 30\n",
"\n",
"target_model1 = resnet18().eval()\n",
"\n",
"# Init scheduler\n",
"scheduler = Scheduler(num_request_queue=1, max_batch=32, engine_select=Scheduler.FIFO_ENGINE, backend_config=f\"{CONFIG_TORCHSIM_DIR}/PyTorchSimBackend/configs/systolic_ws_128x128_c2_simple_noc_tpuv2.json\")\n",
"# Register compiled model\n",
"opt_model1 = torch.compile(target_model1.to(device=scheduler.execution_engine.module.custom_device(), memory_format=torch.channels_last), dynamic=False)\n",
"SchedulerDNNModel.register_model(\"resnet18\", opt_model1)\n",
"\n",
"# Generate time stamp\n",
"for request_time in poisson_request_generator(lambda_requests, max_time):\n",
" # Init input data\n",
" model_input1 = torch.randn(1, 3, 224, 224)\n",
"\n",
" # Init request\n",
" new_request1 = Request(\"resnet18\", [model_input1], [], request_queue_idx=0)\n",
"\n",
" # Add request to scheduler\n",
" print(\"[Reqest] Resnet18 request time: \", request_time, flush=True)\n",
" scheduler.add_request(new_request1, request_time=request_time)\n",
"\n",
"# Run scheduler\n",
"while not scheduler.is_finished():\n",
" scheduler.schedule()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading