Skip to content

Commit a12bfb6

Browse files
ajassaniCopilot
authored andcommitted
Feat/replay from report (#168)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent afd6a5d commit a12bfb6

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

TraceLens/EventReplay/batched_replay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import argparse
44
import warnings
55
import torch
6-
from TraceLens.EventReplay.utils import TensorCfg, build_tensor, benchmark_func, summarize_tensor, dict_profile2torchdtype
6+
from .utils import TensorCfg, build_tensor, benchmark_func, summarize_tensor, dict_profile2torchdtype
77

88
def _get_args_kwargs_from_ir(event_replay_IR: dict[str, any], device: str = 'cuda') -> tuple[list[any], dict[str, any]]:
99
# (Copy the implementation of _get_args_kwargs_from_ir from Step 1 here)

examples/event_replayer_example.ipynb

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"outputs": [],
2121
"source": [
2222
"# replace by your profile path, it can be a single rank profile from a multi gpu run as well\n",
23+
"# If you are interested in replaying from perf report then skip down \n",
2324
"path =\"/home/buffer/resnet_trace.json\"\n",
2425
"perf_analyzer = TreePerfAnalyzer.from_file(path)"
2526
]
@@ -32,8 +33,7 @@
3233
"outputs": [],
3334
"source": [
3435
"# Replay works for any op, taking gemm as example \n",
35-
"# gemm_events = [event for event in perf_analyzer.tree.events if event['name'] in ['aten::addmm', 'aten::mm', 'aten::_scaled_mm']]\n",
36-
"gemm_events = [event for event in perf_analyzer.tree.events if event['name'] in ['aten::convolution']]\n",
36+
"gemm_events = [event for event in perf_analyzer.tree.events if event['name'] in ['aten::addmm', 'aten::mm', 'aten::_scaled_mm']]\n",
3737
"df_gemm_ops = perf_analyzer.build_df_perf_metrics(gemm_events)\n",
3838
"df_gemm_summary = perf_analyzer.summarize_df_perf_metrics(df_gemm_ops, ['mean'])\n",
3939
"df_gemm_summary"
@@ -218,14 +218,98 @@
218218
"print(f\"Created zip file: {zip_file_path}\")"
219219
]
220220
},
221+
{
222+
"cell_type": "markdown",
223+
"id": "1722250f",
224+
"metadata": {},
225+
"source": [
226+
"Replay ops from report"
227+
]
228+
},
221229
{
222230
"cell_type": "code",
223231
"execution_count": null,
224232
"id": "144ce15b-5f0a-4f8e-b7a2-f287154a5d18",
225233
"metadata": {},
226234
"outputs": [],
227235
"source": [
228-
"batched_replay.__file__"
236+
"# we can replay events from the perf reports as well - without the full profile too!\n",
237+
"# This is because we essentially require the args and the op name to replay\n",
238+
"# excel -> df -> for each row (row -> event -> replayer -> replayer IR -> append to replayer IR list) -> save replayer IR list as json\n",
239+
"import pandas as pd\n",
240+
"import ast\n",
241+
"# read sheet from excel\n",
242+
"\n",
243+
"df_unique_ops = pd.read_excel('/path/to/your/perf_report.xlsx', sheet_name='sheet_name')\n",
244+
"\n",
245+
"def row_to_evt(row):\n",
246+
" event = {\n",
247+
" 'name': row['name'],\n",
248+
" 'args': {\n",
249+
" 'Input Dims': ast.literal_eval(row['Input Dims']),\n",
250+
" 'Input Strides': ast.literal_eval(row['Input Strides']),\n",
251+
" 'Input type': ast.literal_eval(row['Input type']),\n",
252+
" 'Concrete Inputs': ast.literal_eval(row['Concrete Inputs']),\n",
253+
" }\n",
254+
" }\n",
255+
" return event\n"
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": null,
261+
"id": "eb91e0c0",
262+
"metadata": {},
263+
"outputs": [],
264+
"source": [
265+
"repro_data_list = []\n",
266+
"processed_count = 0\n",
267+
"# lets say we are interested in the following ops\n",
268+
"ops_interest = ['aten::miopen_convolution',\n",
269+
" 'aten::convolution_backward', \n",
270+
" 'aten::miopen_batch_norm',\n",
271+
" 'aten::miopen_batch_norm_backward'] \n",
272+
"\n",
273+
"df_ops_interest = df_unique_ops[df_unique_ops['name'].isin(ops_interest)].copy()\n",
274+
"\n",
275+
"for index, row in df_ops_interest.iterrows():\n",
276+
" event = row_to_evt(row)\n",
277+
" # Initialize EventReplayer similar to above\n",
278+
" replayer = EventReplayer(event, lazy=True, verbose=False)\n",
279+
" # Extract the serializable info\n",
280+
" repro_info = replayer.get_repro_info()\n",
281+
" repro_data_list.append(repro_info)\n",
282+
" processed_count += 1\n",
283+
"print(f\"Processed {processed_count} events.\")\n",
284+
"# --- Save the Extracted Data ---\n",
285+
"OUTPUT_REPRO_FILE = '/path/to/your/output/replay_ir.json'\n",
286+
"if repro_data_list:\n",
287+
" print(f\"\\nSaving {len(repro_data_list)} extracted operator infos to '{OUTPUT_REPRO_FILE}'...\")\n",
288+
" with open(OUTPUT_REPRO_FILE, 'w') as f:\n",
289+
" json.dump(repro_data_list, f, indent=4)\n",
290+
" print(\"Save complete.\")"
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": null,
296+
"id": "a0245187",
297+
"metadata": {},
298+
"outputs": [],
299+
"source": [
300+
"# STANDALONE ARTIFACTS FOR REPRO - independent of model code or tracelens code\n",
301+
"# artifacts include (a)replay_ir.json, (b) utils.py, (c) batched_replay.py\n",
302+
"files = [\n",
303+
" OUTPUT_REPRO_FILE,\n",
304+
" tl_utils.__file__, # Path to utils.py\n",
305+
" batched_replay.__file__, # Path to batched_replay.py\n",
306+
" batched_replay.__file__.replace('batched_replay.py', 'batched_replay_readme.md') # path to the readme\n",
307+
"]\n",
308+
"zip_file_path = '/path/to/your/directory/replay_code.zip' # Specify your desired zip file path\n",
309+
"with zipfile.ZipFile(zip_file_path, 'w') as zipf:\n",
310+
" for file in files:\n",
311+
" zipf.write(file, arcname=os.path.basename(file)) # ← use file.name\n",
312+
"print(f\"Created zip file: {zip_file_path}\")"
229313
]
230314
}
231315
],

0 commit comments

Comments
 (0)