Skip to content

Commit 7eb43bb

Browse files
tgonzalezorlandoarmTomás González
andauthored
Arm Backend: add .pte file visualization in Model Explorer (#16290)
### Summary Modify `examples/arm/run.sh` and `examples/arm/visualize.py` to be able to trigger visualization (pte or tosa) of the model. For the Ethos-U delegate, we need to pass down intermediate .tosa artifacts. Invoke the PTE adapter API to do that. ### Test plan I tested with 1. `./examples/arm/run.sh --model_name=mv2 --model_explorer --visualize_pte` 2. `./examples/arm/run.sh --model_name=mv2 --target="vgf" --model_explorer --visualize_pte` 3. `./examples/arm/run.sh --model_name=mv2 --model_explorer --visualize_tosa` Signed-off-by: Tomás González <[email protected]> Co-authored-by: Tomás González <[email protected]>
1 parent abe3a71 commit 7eb43bb

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

backends/arm/requirements-arm-ethos-u.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
55

6-
ethos-u-vela == 4.4.1
6+
ethos-u-vela == 4.4.1
7+
pte-adapter-model-explorer == 0.0.2

examples/arm/run.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ select_ops_list="aten::_softmax.out"
4343
qdq_fusion_op=false
4444
model_explorer=false
4545
perf_overlay=false
46+
visualize_tosa=false
47+
visualize_pte=false
4648

4749
function help() {
4850
echo "Usage: $(basename $0) [options]"
@@ -73,8 +75,11 @@ function help() {
7375
echo " --et_build_root=<FOLDER> Executorch build output root folder to use, defaults to ${et_build_root}"
7476
echo " --scratch-dir=<FOLDER> Path to your Arm scrach dir if you not using default ${arm_scratch_dir}"
7577
echo " --qdq_fusion_op Enable QDQ fusion op"
76-
echo " --model_explorer Enable model explorer to visualize TOSA graph."
77-
echo " --perf_overlay With --model_explorer, include performance data from FVP PMU trace."
78+
echo " --model_explorer Enable model explorer to visualize a TOSA or PTE model graph."
79+
echo " --visualize_pte With --model_explorer, visualize PTE flatbuffer model and delegates. Cannot be used with --visualize_tosa"
80+
echo " NOTE: If PTE contains an Ethos-U delegate, the Ethos-U subgraph will be visualized if aot_arm_compiler_flags is set with the -i flag to include intermediate tosa files."
81+
echo " --visualize_tosa With --model_explorer, visualize TOSA flatbuffer model. Cannot be used with --visualize_pte"
82+
echo " --perf_overlay With --model_explorer and --visualize_tosa, include performance data from FVP PMU trace."
7883
exit 0
7984
}
8085

@@ -105,6 +110,8 @@ for arg in "$@"; do
105110
--qdq_fusion_op) qdq_fusion_op=true;;
106111
--model_explorer) model_explorer=true ;;
107112
--perf_overlay) perf_overlay=true ;;
113+
--visualize_tosa) visualize_tosa=true ;;
114+
--visualize_pte) visualize_pte=true ;;
108115
*)
109116
;;
110117
esac
@@ -351,12 +358,21 @@ for i in "${!test_model[@]}"; do
351358
fi
352359

353360
if [ "$model_explorer" = true ]; then
354-
tosa_flatbuffer_path=$(find ${output_folder} -name "*TOSA*.tosa" | head -n 1)
355361
perf_flags=""
356362
if [ "$perf_overlay" = true ]; then
357363
perf_flags+="--trace ${output_folder}/pmu_trace.gz --tables ${output_folder}/output/out_debug.xml"
358364
fi
359-
python3 ${script_dir}/visualize.py --model_path ${tosa_flatbuffer_path} ${perf_flags}
365+
366+
visualization_file=""
367+
if [ "$visualize_tosa" = true ]; then
368+
visualization_file+=" --tosa"
369+
fi
370+
if [ "$visualize_pte" = true ]; then
371+
visualization_file+=" --pte"
372+
fi
373+
374+
me_flags="${visualization_file} ${perf_flags}"
375+
python3 ${script_dir}/visualize.py --model_dir ${output_folder} ${me_flags}
360376
fi
361377
done
362378

examples/arm/visualize.py

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
visualize_model_explorer,
1919
)
2020
from model_explorer import config as model_explorer_config, node_data_builder as ndb
21+
from model_explorer.config import ModelSource
2122

2223
COMPILER_OP_ID = "scheduled_id"
2324

@@ -252,17 +253,53 @@ def validate_perf_mode_args(trace: str, tables: str) -> None:
252253
)
253254

254255

256+
def set_pte_model_explorer_config(model_file, tosa_files, config):
257+
from pte_adapter_model_explorer.main import PTEAdapter
258+
259+
pte_adapter = PTEAdapter()
260+
261+
settings = {"delegate_file_paths": [str(path) for path in tosa_files]}
262+
me_graphs = pte_adapter.convert(model_path=str(model_file), settings=settings)
263+
264+
# Convert the given model to model explorer graphs.
265+
266+
graphs_index = len(config.graphs_list)
267+
config.graphs_list.append(me_graphs)
268+
269+
# Construct model source.
270+
#
271+
# The model source has a special format, in the form of:
272+
# graphs://{name}/{graphs_index}
273+
model_name = model_file.stem
274+
model_source: ModelSource = {"url": f"graphs://{model_name}/{graphs_index}"}
275+
config.model_sources.append(model_source)
276+
277+
278+
def set_tosa_model_explorer_config(model_file, config):
279+
config.add_model_from_path(str(model_file))
280+
281+
255282
def main() -> None:
256283
parser = argparse.ArgumentParser(
257284
description="Visualize a model using model explorer."
258285
)
259286
parser.add_argument(
260-
"--model_path", required=True, type=str, help="Path to the model file"
287+
"--model_dir", required=True, type=str, help="Path to the model directory"
288+
)
289+
parser.add_argument(
290+
"--pte",
291+
action="store_true",
292+
help="Visualize PTE flatbuffer model and delegates. Cannot be used with --tosa",
293+
)
294+
parser.add_argument(
295+
"--tosa",
296+
action="store_true",
297+
help="Visualize TOSA flatbuffer model. Cannot be used with --pte",
261298
)
262299
parser.add_argument(
263300
"--trace",
264301
required=False,
265-
help="(perf mode) PMU trace JSON.gz file with performance data",
302+
help="(perf mode) PMU trace JSON.gz file with performance data. Can only be used together with --tosa",
266303
)
267304
parser.add_argument(
268305
"--tables",
@@ -271,10 +308,37 @@ def main() -> None:
271308
)
272309

273310
args = parser.parse_args()
274-
model_file = Path(args.model_path).resolve()
311+
if args.pte and args.tosa:
312+
raise ValueError("Cannot use both --pte and --tosa options together")
313+
314+
model_dir = Path(args.model_dir).resolve()
315+
316+
tosa_files = list(model_dir.glob("*TOSA*.tosa"))
317+
318+
model_files = None
319+
if args.pte:
320+
model_files = list(model_dir.glob("*.pte"))
321+
elif args.tosa:
322+
model_files = tosa_files
323+
324+
if not model_files:
325+
raise FileNotFoundError(
326+
f"No model files found in {model_dir} for the specified format."
327+
)
328+
329+
model_file = model_files[0]
330+
275331
validate_file_exists(model_file)
276332

277-
config = model_explorer_config().add_model_from_path(str(model_file))
333+
config = model_explorer_config()
334+
335+
extensions = []
336+
337+
if args.pte:
338+
set_pte_model_explorer_config(model_file, tosa_files, config)
339+
elif args.tosa:
340+
set_tosa_model_explorer_config(model_file, config)
341+
extensions.append("tosa_adapter_model_explorer")
278342

279343
if args.trace or args.tables:
280344
validate_perf_mode_args(args.trace, args.tables)
@@ -287,7 +351,7 @@ def main() -> None:
287351
"Duration (Cycles)", build_overlay_data(trace_file, tables_file)
288352
)
289353

290-
visualize_model_explorer(config=config, extensions=["tosa_adapter_model_explorer"])
354+
visualize_model_explorer(config=config, extensions=extensions)
291355

292356

293357
if __name__ == "__main__":

0 commit comments

Comments
 (0)