|
| 1 | +/* |
| 2 | + * Copyright (c) Qualcomm Innovation Center, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <chrono> |
| 10 | +#include <fstream> |
| 11 | +#include <memory> |
| 12 | +#include <numeric> |
| 13 | + |
| 14 | +#include <executorch/runtime/platform/assert.h> |
| 15 | +#include <gflags/gflags.h> |
| 16 | + |
| 17 | +#include "qnn_executorch.h" |
| 18 | + |
| 19 | +DEFINE_string( |
| 20 | + model_path, |
| 21 | + "model.pte", |
| 22 | + "Model serialized in flatbuffer format."); |
| 23 | +DEFINE_string( |
| 24 | + output_folder_path, |
| 25 | + ".", |
| 26 | + "Executorch inference data output path."); |
| 27 | +DEFINE_string(input_list_path, "input_list.txt", "Model input list path."); |
| 28 | + |
| 29 | +int main(int argc, char** argv) { |
| 30 | + gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 31 | + if (argc != 1) { |
| 32 | + std::string msg = "extra commandline args:"; |
| 33 | + for (int i = 1 /* skip argv[0] (program name) */; i < argc; i++) { |
| 34 | + msg += std::string(" ") + argv[i]; |
| 35 | + } |
| 36 | + ET_LOG(Error, "%s", msg.c_str()); |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + // fastrpc related |
| 41 | + // adsp |
| 42 | + const int adsp_domain_id = 0; |
| 43 | + // signed PD |
| 44 | + const int enable_unsigned_pd = 0; |
| 45 | + // domain uri |
| 46 | + std::string domain_uri(qnn_executorch_URI); |
| 47 | + domain_uri += "&_dom=adsp"; |
| 48 | + // init session |
| 49 | + struct remote_rpc_control_unsigned_module data; |
| 50 | + data.domain = adsp_domain_id; |
| 51 | + data.enable = enable_unsigned_pd; |
| 52 | + int err = AEE_SUCCESS; |
| 53 | + ET_CHECK_MSG( |
| 54 | + AEE_SUCCESS == |
| 55 | + (err = remote_session_control( |
| 56 | + DSPRPC_CONTROL_UNSIGNED_MODULE, (void*)&data, sizeof(data))), |
| 57 | + "remote_session_control failed: 0x%x", |
| 58 | + err); |
| 59 | + // start session |
| 60 | + remote_handle64 handle = -1; |
| 61 | + ET_CHECK_MSG( |
| 62 | + AEE_SUCCESS == (err = qnn_executorch_open(domain_uri.data(), &handle)), |
| 63 | + "qnn_executorch_open failed: 0x%x", |
| 64 | + err); |
| 65 | + // load model |
| 66 | + const char* model_path = FLAGS_model_path.c_str(); |
| 67 | + qnn_executorch_load(handle, model_path); |
| 68 | + |
| 69 | + // prepare io |
| 70 | + std::vector<std::vector<uint8_t>> input_data, output_data; |
| 71 | + std::vector<tensor> input_tensor, output_tensor; |
| 72 | + for (int i = 0;; ++i) { |
| 73 | + int nbytes = 0; |
| 74 | + qnn_executorch_get_input_size(handle, model_path, i, &nbytes); |
| 75 | + if (nbytes == -1) { |
| 76 | + break; |
| 77 | + } |
| 78 | + input_data.emplace_back(std::vector<uint8_t>(nbytes)); |
| 79 | + input_tensor.emplace_back( |
| 80 | + tensor({input_data.back().data(), (int)input_data.back().size()})); |
| 81 | + } |
| 82 | + for (int i = 0;; ++i) { |
| 83 | + int nbytes = 0; |
| 84 | + qnn_executorch_get_output_size(handle, model_path, i, &nbytes); |
| 85 | + if (nbytes == -1) { |
| 86 | + break; |
| 87 | + } |
| 88 | + output_data.emplace_back(std::vector<uint8_t>(nbytes)); |
| 89 | + output_tensor.emplace_back( |
| 90 | + tensor({output_data.back().data(), (int)output_data.back().size()})); |
| 91 | + } |
| 92 | + |
| 93 | + // prepare input data |
| 94 | + std::ifstream input_list(FLAGS_input_list_path); |
| 95 | + // TODO: should check IO info via fastrpc first |
| 96 | + if (input_list.is_open()) { |
| 97 | + auto split = [](std::string s, std::string delimiter) { |
| 98 | + size_t pos_start = 0, pos_end, delim_len = delimiter.length(); |
| 99 | + std::string token; |
| 100 | + std::vector<std::string> res; |
| 101 | + |
| 102 | + while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) { |
| 103 | + token = s.substr(pos_start, pos_end - pos_start); |
| 104 | + pos_start = pos_end + delim_len; |
| 105 | + res.push_back(token); |
| 106 | + } |
| 107 | + res.push_back(s.substr(pos_start)); |
| 108 | + return res; |
| 109 | + }; |
| 110 | + |
| 111 | + std::string file_path; |
| 112 | + int inference_index = 0; |
| 113 | + while (std::getline(input_list, file_path)) { |
| 114 | + auto input_files = split(file_path, " "); |
| 115 | + if (input_files.size() == 0) { |
| 116 | + break; |
| 117 | + } |
| 118 | + size_t num_inputs = input_files.size(); |
| 119 | + for (int i = 0; i < num_inputs; ++i) { |
| 120 | + std::ifstream fin(input_files[i], std::ios::binary); |
| 121 | + fin.seekg(0, fin.end); |
| 122 | + size_t file_size = fin.tellg(); |
| 123 | + fin.seekg(0, fin.beg); |
| 124 | + fin.read((char*)input_data[i].data(), file_size); |
| 125 | + fin.close(); |
| 126 | + } |
| 127 | + qnn_executorch_set_input( |
| 128 | + handle, model_path, input_tensor.data(), input_tensor.size()); |
| 129 | + qnn_executorch_execute(handle, model_path); |
| 130 | + qnn_executorch_get_output( |
| 131 | + handle, model_path, output_tensor.data(), output_tensor.size()); |
| 132 | + for (size_t i = 0; i < output_tensor.size(); i++) { |
| 133 | + auto output_file_name = FLAGS_output_folder_path + "/output_" + |
| 134 | + std::to_string(inference_index) + "_" + std::to_string(i) + ".raw"; |
| 135 | + std::ofstream fout(output_file_name.c_str(), std::ios::binary); |
| 136 | + fout.write( |
| 137 | + (const char*)output_tensor[i].data, output_tensor[i].dataLen); |
| 138 | + fout.close(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // unload model |
| 144 | + qnn_executorch_unload(handle, model_path); |
| 145 | + // tear down |
| 146 | + qnn_executorch_close(handle); |
| 147 | + return 0; |
| 148 | +} |
0 commit comments