|
| 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 | + ) |
0 commit comments