Skip to content

Commit 8ebe39b

Browse files
committed
patch for runner
1 parent 4e38f4a commit 8ebe39b

File tree

7 files changed

+356
-44
lines changed

7 files changed

+356
-44
lines changed

backends/qualcomm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
253253

254254
pybind11_extension(PyQnnManagerAdaptor)
255255
pybind11_extension(PyQnnWrapperAdaptor)
256-
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
256+
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES RelWithDebInfo)
257257
# Strip unnecessary sections of the binary
258258
pybind11_strip(PyQnnManagerAdaptor)
259259
pybind11_strip(PyQnnWrapperAdaptor)

backends/qualcomm/scripts/build.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ CMAKE_X86_64="build-x86"
3030
BUILD_AARCH64="true"
3131
CMAKE_AARCH64="build-android"
3232
CLEAN="true"
33-
BUILD_TYPE="Debug"
33+
BUILD_TYPE="RelWithDebInfo"
3434
BUILD_JOB_NUMBER="16"
3535

3636
if [ -z PYTHON_EXECUTABLE ]; then
@@ -71,7 +71,7 @@ if [ "$BUILD_AARCH64" = true ]; then
7171
rm -rf $BUILD_ROOT && mkdir $BUILD_ROOT
7272
else
7373
# Force rebuild flatccrt for the correct platform
74-
cd $BUILD_ROOT/devtools && make clean
74+
cd $BUILD_ROOT/third-party/flatcc && make clean
7575
fi
7676

7777
cd $BUILD_ROOT
@@ -116,7 +116,7 @@ if [ "$BUILD_X86_64" = true ]; then
116116
rm -rf $BUILD_ROOT && mkdir $BUILD_ROOT
117117
else
118118
# Force rebuild flatccrt for the correct platform
119-
cd $BUILD_ROOT/devtools && make clean
119+
cd $BUILD_ROOT/third-party/flatcc && make clean
120120
fi
121121

122122
cd $BUILD_ROOT

benchmark.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import argparse
2+
import os
3+
import subprocess
4+
5+
qnn_sdk = os.getenv("QNN_SDK_ROOT")
6+
htp_arch = "79"
7+
workspace = "/data/local/tmp/et_ga_benchmark"
8+
memory_script_file = "peak_memory.sh"
9+
perf_file = "statistics.txt"
10+
11+
12+
def get_artifacts(backend, pte_path):
13+
def get_build_dir(backend):
14+
build_dir = {
15+
"qnn": "build-android",
16+
"xnn": "build-xnnpack",
17+
}
18+
return build_dir[backend]
19+
20+
memory_script = """$@ 2> /dev/null &
21+
22+
PROCESS=$1
23+
PEAK_MEM=0
24+
SAMPLES=0
25+
TOTAL=0
26+
while true; do
27+
PID=$(pidof $PROCESS)
28+
if [ "$PID" != "" ]; then
29+
DMA=$(dmabuf_dump $PID | grep "PROCESS TOTAL" | awk '{ print $3 }')
30+
PSS=$(dumpsys meminfo -s $PID | grep "TOTAL PSS" | awk '{ print $3 }')
31+
if [ "$PSS" == "" ]; then
32+
continue
33+
fi
34+
CURRENT=$(($DMA+$PSS))
35+
if [ CURRENT -gt PEAK_MEM ]; then
36+
PEAK_MEM=$CURRENT
37+
fi
38+
SAMPLES=$(($SAMPLES+1))
39+
TOTAL=$(($TOTAL+$CURRENT))
40+
else
41+
break
42+
fi
43+
done
44+
45+
rm -rf memory_usage.txt
46+
echo "peak_mem: $PEAK_MEM" >> statistics.txt
47+
AVG_MEM=$(awk -- 'BEGIN{printf "%.3f", ARGV[1]/ARGV[2]}' "$TOTAL" "$SAMPLES")
48+
echo "avg_mem: $AVG_MEM" >> statistics.txt
49+
"""
50+
with open(memory_script_file, "w") as f:
51+
f.write(memory_script)
52+
53+
runner = {
54+
"qnn": f"{get_build_dir(backend)}/examples/qualcomm/executor_runner/qnn_executor_runner",
55+
"xnn": f"{get_build_dir(backend)}/backends/xnnpack/xnn_executor_runner",
56+
}
57+
artifacts = {
58+
"qnn": [
59+
pte_path,
60+
f"{qnn_sdk}/lib/aarch64-android/libQnnHtp.so",
61+
(
62+
f"{qnn_sdk}/lib/hexagon-v{htp_arch}/"
63+
f"unsigned/libQnnHtpV{htp_arch}Skel.so"
64+
),
65+
(f"{qnn_sdk}/lib/aarch64-android/" f"libQnnHtpV{htp_arch}Stub.so"),
66+
f"{qnn_sdk}/lib/aarch64-android/libQnnHtpPrepare.so",
67+
f"{qnn_sdk}/lib/aarch64-android/libQnnSystem.so",
68+
f"{get_build_dir(backend)}/backends/qualcomm/libqnn_executorch_backend.so",
69+
f"{qnn_sdk}/lib/aarch64-android/libQnnModelDlc.so",
70+
runner[backend],
71+
memory_script_file,
72+
],
73+
"xnn": [
74+
pte_path,
75+
runner[backend],
76+
memory_script_file,
77+
],
78+
}
79+
return artifacts[backend]
80+
81+
82+
def get_cmds(backend, pte_path, iteration):
83+
cmd_args = {
84+
"qnn": (
85+
[
86+
f"--model_path {os.path.basename(pte_path)}",
87+
f"--iteration {iteration}",
88+
"--dump_statistics",
89+
]
90+
),
91+
"xnn": (
92+
[
93+
f"--model_path {os.path.basename(pte_path)}",
94+
f"--num_executions {iteration}",
95+
"--dump_statistics",
96+
]
97+
),
98+
}
99+
cmds_for_inference = {
100+
"qnn": (
101+
" ".join(
102+
[
103+
f"cd {workspace} &&",
104+
"chmod +x ./qnn_executor_runner &&",
105+
f"./qnn_executor_runner {' '.join(cmd_args[backend])}",
106+
]
107+
)
108+
),
109+
"xnn": (
110+
" ".join(
111+
[
112+
f"cd {workspace} &&",
113+
"chmod +x ./xnn_executor_runner &&",
114+
f"./xnn_executor_runner {' '.join(cmd_args[backend])}",
115+
]
116+
)
117+
),
118+
}
119+
# do not dump inference metrics during profiling memory
120+
for _, v in cmd_args.items():
121+
v.pop()
122+
cmds_for_memory = {
123+
"qnn": (
124+
" ".join(
125+
[
126+
f"cd {workspace} &&",
127+
"chmod +x ./qnn_executor_runner &&",
128+
f"chmod +x {memory_script_file} &&",
129+
f"./{memory_script_file} ./qnn_executor_runner {' '.join(cmd_args[backend])}",
130+
]
131+
)
132+
),
133+
"xnn": (
134+
" ".join(
135+
[
136+
f"cd {workspace} &&",
137+
"chmod +x ./xnn_executor_runner &&",
138+
f"chmod +x {memory_script_file} &&",
139+
f"./{memory_script_file} ./xnn_executor_runner {' '.join(cmd_args[backend])}",
140+
]
141+
)
142+
),
143+
}
144+
return [cmds_for_inference[backend], cmds_for_memory[backend]]
145+
146+
147+
def start_benchmark(artifacts, cmds, device, host):
148+
def adb(action):
149+
if not host:
150+
actions = ["adb", "-s", device]
151+
else:
152+
actions = ["adb", "-H", host, "-s", device]
153+
actions.extend(action)
154+
subprocess.run(actions, stdout=subprocess.DEVNULL)
155+
156+
def post_process():
157+
subprocess.run(["rm", "-rf", perf_file], stdout=subprocess.DEVNULL)
158+
for file_name in [perf_file]:
159+
adb(["pull", f"{workspace}/{file_name}", "."])
160+
with open(file_name, "r") as f:
161+
print(f.read())
162+
163+
adb(["shell", "rm", "-rf", workspace])
164+
adb(["shell", "mkdir", "-p", workspace])
165+
for artifact in artifacts:
166+
adb(["push", artifact, workspace])
167+
for cmd in cmds:
168+
adb(["shell", cmd])
169+
post_process()
170+
171+
172+
if __name__ == "__main__":
173+
parser = argparse.ArgumentParser()
174+
parser.add_argument(
175+
"-b",
176+
"--backend",
177+
help="either 'qnn' or 'xnn'",
178+
required=True,
179+
)
180+
parser.add_argument(
181+
"-p",
182+
"--pte",
183+
help="path to generated .pte file",
184+
required=True,
185+
)
186+
parser.add_argument(
187+
"-H",
188+
"--host",
189+
help="hostname for adb gateway",
190+
required=False,
191+
)
192+
parser.add_argument(
193+
"-s",
194+
"--device",
195+
help="serial number for adb device",
196+
required=True,
197+
)
198+
parser.add_argument(
199+
"-i",
200+
"--iteration",
201+
help="total number of inferences",
202+
default=100,
203+
required=False,
204+
)
205+
args = parser.parse_args()
206+
start_benchmark(
207+
artifacts=get_artifacts(args.backend, args.pte),
208+
cmds=get_cmds(args.backend, args.pte, args.iteration),
209+
device=args.device,
210+
host=args.host,
211+
)

build_xnnpack.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
if [[ -z $ANDROID_NDK_ROOT ]]; then
4+
echo "Please export ANDROID_NDK_ROOT=/path/to/ndk"
5+
exit -1
6+
fi
7+
8+
CLEAN_BUILD="false"
9+
BUILD_FOLDER="build-xnnpack"
10+
BUILD_TYPE="release"
11+
12+
while [[ "$#" -gt 0 ]]; do
13+
case "$1" in
14+
-c|--clean_build) CLEAN_BUILD="true"; shift;;
15+
-d|--debug) BUILD_TYPE="Debug"; shift;;
16+
*) echo "unknow arg passed: $1"; exit 1;;
17+
esac
18+
shift
19+
done
20+
21+
if [ "$CLEAN_BUILD" = true ]; then
22+
rm -rf $BUILD_FOLDER
23+
fi
24+
25+
cmake \
26+
-DCMAKE_INSTALL_PREFIX=$BUILD_FOLDER \
27+
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
28+
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake \
29+
-DANDROID_ABI='arm64-v8a' \
30+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
31+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
32+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
33+
-DEXECUTORCH_BUILD_XNNPACK=ON \
34+
-DEXECUTORCH_ENABLE_LOGGING=ON \
35+
-DPYTHON_EXECUTABLE=python \
36+
-B$BUILD_FOLDER .
37+
38+
cmake --build $BUILD_FOLDER -j9 --target install --config $BUILD_TYPE
39+

0 commit comments

Comments
 (0)