Repository navigation
Commit 42d0740
authored
Qualcomm AI Engine Direct - Enabling On-host profiling estimation for Executorch QNN HTP (pytorch#22143)
### **User description**
## Summary
Introduce two officially supported HTP profiling workflows — on-device
**OpTrace** and host-only **Hextimate** — behind a small public Python
API returning `QnnHtpProfileArtifacts`. Split the demo into three
workflow-named example scripts and rewrite the README around the three
routes.
## Motivation
Profiling QNN-delegated `.pte`s used to be a single demo with a hidden
`--online_prepare` switch that silently picked between `.dlc` vs `.bin`,
host-side vs on-device tools, and set (or didn't set) `profile_level=3`
behind the user's back. This PR replaces that with **three named routes,
one public API, and a self-contained `.pte`**.
## What each topic changes
### Public HTP profiling API (`backends/qualcomm/debugger/utils.py`)
Two workflow-specific entry points instead of one mode-flag function.
Each returns a `QnnHtpProfileArtifacts` per compiled binary in the
`.pte`, carrying `binary_path`, `profile_mode`, `prepare_mode`,
`qhas_html`, `qhas_json`, `chrometrace_json`, and HTP graph JSONs.
- `generate_htp_profile_result()` — on-device OpTrace (both prepare
modes).
- Validates offline-prepare inputs by decoding the `.pte`'s compile spec
and rejecting anything without `profile_level=3` at the Python boundary,
rather than producing an unusable output later.
- For online-prepare, no `profile_level` is required — the QNN CLI
attaches instrumentation at host-side context-binary-generation time.
- `estimate_htp_profile_result()` — host-only Hextimate.
- Enforces `online_prepare=True` (Hextimate requires a `.dlc`, not a
`.bin`).
- Enforces supported-SoC allowlist (`SA8540`, `SA8255`, `QCS9100`,
`SA8797`) fail with a legible error.
- Enforces minimum QNN SDK version support for Hextimate.
- **Design rationale**: two functions, not one function with a mode
enum. Each route has genuinely different constraints (device vs host,
prepare-mode requirements, SoC allowlist) — a shared entry point would
push all four validation matrices into the caller. Two functions with
clear names is cheaper than one function with four flags.
### Compile-spec plumbing
(`backends/qualcomm/serialization/qc_compiler_spec.fbs`, `qc_schema.py`,
`utils/utils.py`)
- Adds `SA8540 = 62` to `QcomChipset` and the host-side
`get_soc_to_chipset_map()`. The other three Hextimate-supported SoCs
(`SA8255`, `QCS9100`, `SA8797`) already existed.
- Wire value `62` matches the QNN SDK's internal SoC ID for SA8540 — the
enum wire values are load-bearing, so the addition is at a specific
numeric slot, not the tail of the enum.
- Purely additive; no existing entries are renumbered.
### Example scripts (`examples/qualcomm/util_scripts/`)
One script per route, with the route encoded in the filename so a reader
picks the right file without reading code:
- `htp_profiling_on_device_op_trace_online.py` — on-device OpTrace,
`online_prepare=True`, `.dlc` path.
- Asserts `qnn_config.online_prepare` at entry.
- No `profile_level` handling — QNN CLI attaches instrumentation
host-side.
- `htp_profiling_on_device_op_trace_offline.py` — on-device OpTrace,
`online_prepare=False` + `profile_level=3`, `.bin` path.
- Asserts `qnn_config.profile_level == 3 and not
qnn_config.online_prepare` at entry.
- Exercises the new self-contained schematic packaging.
- `htp_profiling_on_host_hextimate.py` — host-only Hextimate (renamed
from the old merged demo, not net-new).
- Requires `--enable_x86_64`; no adb involved.
**`qairt-visualizer` is optional in all three scripts**:
- If installed: reports open automatically after generation.
- If not installed: `qhas_html` paths are printed to stdout so users can
view them however they want.
- Removes a soft dependency without losing the happy path.
**Design rationale**: three short files (~90 lines each) beat one file
with `if args.mode == "..."` branches. The old merged demo had a bug
where `--profile_level 3` was passed under `--online_prepare` even
though online-prepare doesn't need it — the kind of bug you get when one
file quietly handles multiple pipelines. Naming a file after its
pipeline eliminates the class.
### Documentation (`backends/qualcomm/debugger/README.md` +
`examples/qualcomm/util_scripts/README.md`)
- README rewritten around the **five user tasks** rather than around the
toolchain:
1. Select a `.pte` prepare mode (decision table: online-prepare needs
Hextimate/graph view; offline-prepare when you already have a
`profile_level=3` `.pte`).
2. Run on-device OpTrace generation.
3. Run host-only Hextimate estimation.
4. Inspect the returned `QnnHtpProfileArtifacts`.
5. View QHAS reports with QAIRT Visualizer.
- Hextimate limitations and the supported-SoC list are documented in
place, next to the code path that enforces them.
- README quick-start snippets link directly to the three renamed example
scripts.
- `examples/qualcomm/util_scripts/README.md` indexes each of the three
profiling example scripts by filename.
### Tests (`backends/qualcomm/tests/test_qnn_delegate.py`)
- New public-API test covering **unsupported-SoC rejection for Hextimate
before reading `.pte` inputs** — this is a defensive-order check
(validate first, then touch inputs), not just an error-message check.
Catches regressions where validation gets reordered behind expensive
`.pte` parsing.
- `test_qnn_backend_generate_optrace` refactored in both
`TestQNNFloatingPointUtils` and `TestQNNQuantizedUtils`:
- Exercises both prepare modes in one test (online-prepare, then
offline-prepare with `profile_level=3`).
- Consumes the new `QnnHtpProfileArtifacts` return shape (`.qhas_json`,
`.chrometrace_json`).
- Skip when `enable_x86_64` (OpTrace requires on-device execution) or
LPAI backend (unsupported).
- `test_qnn_backend_generate_hextimate` refactored in
`TestQNNQuantizedUtils`:
- Skip when `not enable_x86_64` (Hextimate is host-only; needs
`--enable_x86_64`).
- Asserts `qhas_json is None` — Hextimate QHAS JSON is truncated by an
upstream SDK bug (division by zero on `time_us=0`); we surface this by
returning `None` from the API rather than papering over it. The HTML
report and chrometrace remain usable.
- Skip logic is **complementary**: OpTrace ⇒ on-device (`enable_x86_64`
skips), Hextimate ⇒ host-only (`not enable_x86_64` skips). Users run
them in two separate test invocations with different `--build_folder` /
`--enable_x86_64` combinations.
## Verification
### Script syntax
```bash
python -m py_compile \
examples/qualcomm/util_scripts/htp_profiling_on_device_op_trace_online.py \
examples/qualcomm/util_scripts/htp_profiling_on_device_op_trace_offline.py \
examples/qualcomm/util_scripts/htp_profiling_on_host_hextimate.py
```
### End-to-end example scripts (three routes, matched flags)
```bash
python -m examples.qualcomm.util_scripts.htp_profiling_on_device_op_trace_online \
--host <HOST> --device <DEVICE_SERIAL> --soc_model <SOC_MODEL> \
--build_folder build-android --online_prepare -a <ARTIFACT_DIR>
python -m examples.qualcomm.util_scripts.htp_profiling_on_device_op_trace_offline \
--host <HOST> --device <DEVICE_SERIAL> --soc_model <SOC_MODEL> \
--build_folder build-android --profile_level 3 -a <ARTIFACT_DIR>
python -m examples.qualcomm.util_scripts.htp_profiling_on_host_hextimate \
--soc_model SA8797 --build_folder build-x86 --enable_x86_64 --online_prepare -a <ARTIFACT_DIR>
```
### Targeted profiling tests (existing framework, no CLI shape change)
```bash
python -m backends.qualcomm.tests.test_qnn_delegate \
TestQNNQuantizedUtils.test_qnn_backend_generate_optrace \
--host <HOST> --device <DEVICE_SERIAL> --soc_model <SOC_MODEL> --build_folder build-android
python -m backends.qualcomm.tests.test_qnn_delegate \
TestQNNFloatingPointUtils.test_qnn_backend_generate_optrace \
--host <HOST> --device <DEVICE_SERIAL> --soc_model <SOC_MODEL> --build_folder build-android
python -m backends.qualcomm.tests.test_qnn_delegate \
TestQNNQuantizedUtils.test_qnn_backend_generate_hextimate \
--soc_model SA8797 --build_folder build-x86 --enable_x86_64
```1 parent 8734b06 commit 42d0740
10 files changed
Lines changed: 1099 additions & 358 deletions
File tree
- backends/qualcomm
- debugger
- serialization
- tests
- utils
- examples/qualcomm/util_scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
9 | 70 | | |
10 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
11 | 74 | | |
12 | 75 | | |
13 | | - | |
14 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
15 | 86 | | |
16 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
17 | 94 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
35 | 98 | | |
36 | 99 | | |
37 | 100 | | |
38 | 101 | | |
39 | | - | |
| 102 | + | |
40 | 103 | | |
41 | 104 | | |
42 | 105 | | |
43 | | - | |
44 | | - | |
45 | 106 | | |
46 | 107 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
52 | 118 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 119 | + | |
| 120 | + | |
57 | 121 | | |
58 | 122 | | |
59 | | - | |
60 | 123 | | |
61 | | - | |
62 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
63 | 127 | | |
64 | | - | |
| 128 | + | |
| 129 | + | |
65 | 130 | | |
66 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
67 | 136 | | |
68 | 137 | | |
69 | | - | |
70 | | - | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
71 | 181 | | |
72 | | - | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
73 | 187 | | |
74 | 188 | | |
75 | | - | |
76 | | - | |
77 | 189 | | |
78 | | - | |
79 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
80 | 196 | | |
81 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
82 | 200 | | |
83 | | - | |
84 | 201 | | |
85 | 202 | | |
86 | | - | |
| 203 | + | |
87 | 204 | | |
88 | 205 | | |
89 | 206 | | |
90 | | - | |
| 207 | + | |
91 | 208 | | |
92 | 209 | | |
93 | 210 | | |
| |||
0 commit comments