Skip to content

Commit 8dc4705

Browse files
authored
Merge pull request #190 from PSAL-POSTECH/perf
Perf
2 parents 92aeeef + fc88d0c commit 8dc4705

8 files changed

Lines changed: 1106 additions & 1058 deletions

File tree

Simulator/simulator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ def get_result_from_file(result_path):
398398
dram_channel_bw = {}
399399
avg_dram_bw = None
400400
simulation_time = None
401+
total_cycle = None
401402

402403
# Read and find total stat position
403404
with open(result_path, "r") as f:
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
}

tutorial/session1/DNNServing.ipynb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## DNN Serving System"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
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)"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"### Scheduler"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"import torch\n",
37+
"from torchvision.models import resnet18\n",
38+
"from Scheduler.scheduler import Scheduler, SchedulerDNNModel, Request\n",
39+
"from PyTorchSimFrontend.extension_config import CONFIG_TORCHSIM_BACKEND_CONFIG\n",
40+
"\n",
41+
"scheduler = Scheduler(num_request_queue=1, engine_select=Scheduler.FIFO_ENGINE, backend_config=CONFIG_TORCHSIM_BACKEND_CONFIG)\n",
42+
"device = scheduler.execution_engine.module.custom_device()\n",
43+
"\n",
44+
"model = resnet18().eval()\n",
45+
"input = torch.randn(1, 3, 224, 224).to(device=device)\n",
46+
"opt_fn = torch.compile(dynamic=False)(model.to(device, memory_format=torch.channels_last))\n",
47+
"\n",
48+
"SchedulerDNNModel.register_model(\"resnet18\", opt_fn)\n",
49+
"request = Request(\"resnet18\", [input], [], request_queue_idx=0)\n",
50+
"scheduler.add_request(request, request_time=0)\n",
51+
"\n",
52+
"# Run scheduler\n",
53+
"while not scheduler.is_finished():\n",
54+
" with torch.no_grad():\n",
55+
" scheduler.schedule()\n",
56+
"\n",
57+
"print(\"ResNet18 Simulation Done\")"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"### Load Generator"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"import os\n",
74+
"import torch\n",
75+
"from torchvision.models import resnet18\n",
76+
"\n",
77+
"from Scheduler.scheduler import Scheduler, SchedulerDNNModel, Request, poisson_request_generator\n",
78+
"CONFIG_TORCHSIM_DIR = os.environ.get('TORCHSIM_DIR', default='/workspace/PyTorchSim')\n",
79+
"\n",
80+
"lambda_requests = 10\n",
81+
"max_time = 30\n",
82+
"\n",
83+
"target_model1 = resnet18().eval()\n",
84+
"\n",
85+
"# Init scheduler\n",
86+
"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",
87+
"# Register compiled model\n",
88+
"opt_model1 = torch.compile(target_model1.to(device=scheduler.execution_engine.module.custom_device(), memory_format=torch.channels_last), dynamic=False)\n",
89+
"SchedulerDNNModel.register_model(\"resnet18\", opt_model1)\n",
90+
"\n",
91+
"# Generate time stamp\n",
92+
"for request_time in poisson_request_generator(lambda_requests, max_time):\n",
93+
" # Init input data\n",
94+
" model_input1 = torch.randn(1, 3, 224, 224)\n",
95+
"\n",
96+
" # Init request\n",
97+
" new_request1 = Request(\"resnet18\", [model_input1], [], request_queue_idx=0)\n",
98+
"\n",
99+
" # Add request to scheduler\n",
100+
" print(\"[Reqest] Resnet18 request time: \", request_time, flush=True)\n",
101+
" scheduler.add_request(new_request1, request_time=request_time)\n",
102+
"\n",
103+
"# Run scheduler\n",
104+
"while not scheduler.is_finished():\n",
105+
" scheduler.schedule()"
106+
]
107+
}
108+
],
109+
"metadata": {
110+
"kernelspec": {
111+
"display_name": "base",
112+
"language": "python",
113+
"name": "python3"
114+
},
115+
"language_info": {
116+
"codemirror_mode": {
117+
"name": "ipython",
118+
"version": 3
119+
},
120+
"file_extension": ".py",
121+
"mimetype": "text/x-python",
122+
"name": "python",
123+
"nbconvert_exporter": "python",
124+
"pygments_lexer": "ipython3",
125+
"version": "3.10.13"
126+
}
127+
},
128+
"nbformat": 4,
129+
"nbformat_minor": 2
130+
}

0 commit comments

Comments
 (0)