Download examples/capture_efficientdet_onnx_qnn_batch.cpp from anan19990108/efficientdet-lite0-htp: direct link, hf CLI and curl.
- Browser
- Download file 5.97 kB
-
https://huggingface.co/anan19990108/efficientdet-lite0-htp/resolve/main/examples/capture_efficientdet_onnx_qnn_batch.cpp
- Command line
-
hf download hf://anan19990108/efficientdet-lite0-htp/examples/capture_efficientdet_onnx_qnn_batch.cpp
-
curl -L -o capture_efficientdet_onnx_qnn_batch.cpp https://huggingface.co/anan19990108/efficientdet-lite0-htp/resolve/main/examples/capture_efficientdet_onnx_qnn_batch.cpp
5.97 kB
| namespace fs = std::filesystem; | |
| static std::vector<float> read_input(const fs::path& path) { | |
| std::ifstream file(path, std::ios::binary | std::ios::ate); | |
| if (!file || file.tellg() != 320 * 320 * 3 * sizeof(float)) | |
| throw std::runtime_error("input must be NHWC normalized float32: " + path.string()); | |
| file.seekg(0); | |
| std::vector<float> pixels(320 * 320 * 3); | |
| file.read(reinterpret_cast<char*>(pixels.data()), pixels.size() * sizeof(float)); | |
| if (!file) throw std::runtime_error("cannot read " + path.string()); | |
| return pixels; | |
| } | |
| int main(int argc, char** argv) { | |
| if (argc != 6 && argc != 7) { | |
| std::cerr << "usage: capture_efficientdet_onnx_qnn_batch MODEL.onnx PROVIDER.so " | |
| "libQnnHtp.so INPUT_DIR OUTPUT_DIR [enable_htp_fp16_precision:0|1]\n"; | |
| return 64; | |
| } | |
| try { | |
| const OrtApi& api = Ort::GetApi(); | |
| Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "efficientdet-qnn-capture"); | |
| Ort::ThrowOnError(api.RegisterExecutionProviderLibrary( | |
| env, "QNNExecutionProvider", argv[2])); | |
| const OrtEpDevice* const* devices = nullptr; | |
| size_t device_count = 0; | |
| Ort::ThrowOnError(api.GetEpDevices(env, &devices, &device_count)); | |
| std::vector<const OrtEpDevice*> qnn_devices; | |
| for (size_t i = 0; i < device_count; ++i) { | |
| const char* name = api.EpDevice_EpName(devices[i]); | |
| if (name && std::strcmp(name, "QNNExecutionProvider") == 0) | |
| qnn_devices.push_back(devices[i]); | |
| } | |
| if (qnn_devices.empty()) throw std::runtime_error("QNN EP exposed no device"); | |
| Ort::SessionOptions options; | |
| options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL); | |
| options.AddConfigEntry("session.disable_cpu_ep_fallback", "1"); | |
| const char* keys[] = {"backend_path", "htp_performance_mode", "rpc_control_latency", | |
| "enable_htp_fp16_precision"}; | |
| const char* fp16 = argc == 7 ? argv[6] : "0"; | |
| if (std::strcmp(fp16, "0") != 0 && std::strcmp(fp16, "1") != 0) | |
| throw std::runtime_error("enable_htp_fp16_precision must be 0 or 1"); | |
| const char* values[] = {argv[3], "burst", "100", fp16}; | |
| Ort::ThrowOnError(api.SessionOptionsAppendExecutionProvider_V2( | |
| options, env, qnn_devices.data(), qnn_devices.size(), keys, values, 4)); | |
| Ort::Session session(env, argv[1], options); | |
| if (session.GetInputCount() != 1 || session.GetOutputCount() != 2) | |
| throw std::runtime_error("expected one input and two raw outputs"); | |
| Ort::AllocatorWithDefaultOptions allocator; | |
| auto input_name = session.GetInputNameAllocated(0, allocator); | |
| const char* input_names[] = {input_name.get()}; | |
| std::vector<std::string> names; | |
| std::vector<const char*> output_names; | |
| for (size_t i = 0; i < 2; ++i) | |
| names.emplace_back(session.GetOutputNameAllocated(i, allocator).get()); | |
| for (auto& name : names) output_names.push_back(name.c_str()); | |
| const int64_t shape[] = {1, 320, 320, 3}; | |
| auto memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); | |
| std::vector<fs::path> files; | |
| for (const auto& item : fs::directory_iterator(argv[4])) | |
| if (item.is_regular_file() && item.path().extension() == ".bin") | |
| files.push_back(item.path()); | |
| std::sort(files.begin(), files.end()); | |
| if (files.empty()) throw std::runtime_error("no input files"); | |
| fs::create_directories(argv[5]); | |
| std::vector<double> times; | |
| for (size_t index = 0; index < files.size(); ++index) { | |
| auto pixels = read_input(files[index]); | |
| auto input = Ort::Value::CreateTensor<float>( | |
| memory, pixels.data(), pixels.size(), shape, 4); | |
| if (index == 0) | |
| for (int warm = 0; warm < 10; ++warm) | |
| session.Run(Ort::RunOptions{nullptr}, input_names, &input, 1, | |
| output_names.data(), output_names.size()); | |
| auto begin = std::chrono::steady_clock::now(); | |
| auto outputs = session.Run(Ort::RunOptions{nullptr}, input_names, &input, 1, | |
| output_names.data(), output_names.size()); | |
| auto end = std::chrono::steady_clock::now(); | |
| if (index == 0) { | |
| std::vector<const OrtEpDevice*> owners(2, nullptr); | |
| Ort::ThrowOnError(api.SessionGetEpDeviceForOutputs(session, owners.data(), 2)); | |
| for (const auto* owner : owners) { | |
| const char* name = owner ? api.EpDevice_EpName(owner) : nullptr; | |
| if (!name || std::strcmp(name, "QNNExecutionProvider") != 0) | |
| throw std::runtime_error("output not owned by QNN EP"); | |
| } | |
| } | |
| times.push_back(std::chrono::duration<double, std::milli>(end - begin).count()); | |
| fs::path directory = fs::path(argv[5]) / files[index].stem(); | |
| fs::create_directories(directory); | |
| for (size_t i = 0; i < outputs.size(); ++i) { | |
| auto info = outputs[i].GetTensorTypeAndShapeInfo(); | |
| if (info.GetElementType() != ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) | |
| throw std::runtime_error("output not FP32"); | |
| std::ofstream stream(directory / ("head" + std::to_string(i) + ".bin"), | |
| std::ios::binary); | |
| stream.write(reinterpret_cast<const char*>(outputs[i].GetTensorData<float>()), | |
| info.GetElementCount() * sizeof(float)); | |
| if (!stream) throw std::runtime_error("cannot write output"); | |
| } | |
| std::cout << files[index].stem() << '\n'; | |
| } | |
| std::sort(times.begin(), times.end()); | |
| std::cout << "QNN_EP_NETWORK_MS count=" << times.size() | |
| << " p50=" << times[times.size() / 2] | |
| << " p95=" << times[static_cast<size_t>((times.size() - 1) * 0.95)] | |
| << '\n'; | |
| return 0; | |
| } catch (const std::exception& e) { | |
| std::cerr << "QNN_EP_CAPTURE_ERROR " << e.what() << '\n'; | |
| return 1; | |
| } | |
| } | |