|
| 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 | +/** |
| 10 | + * @file |
| 11 | + * |
| 12 | + * This tool can run t5 with Qualcomm AI Engine Direct. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#include <executorch/backends/qualcomm/runtime/QnnExecuTorch.h> |
| 17 | +#include <executorch/examples/qualcomm/oss_scripts/t5/runner/runner.h> |
| 18 | +#include <executorch/runtime/platform/log.h> |
| 19 | +#include <gflags/gflags.h> |
| 20 | +#include <fstream> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +DEFINE_string( |
| 24 | + model_path, |
| 25 | + "t5_qnn.pte", |
| 26 | + "t5 model serialized in flatbuffer format."); |
| 27 | + |
| 28 | +DEFINE_string( |
| 29 | + tokenizer_model_path, |
| 30 | + "tokenizer.model", |
| 31 | + "The tokenizer is saved from T5Tokenize.save_pretrained for tokenizer."); |
| 32 | +DEFINE_string( |
| 33 | + input_list_path, |
| 34 | + "input_list.txt", |
| 35 | + "Input list storing file name of encoded results."); |
| 36 | +DEFINE_int32( |
| 37 | + seq_len, |
| 38 | + 128, |
| 39 | + "Maximum sequence length for the generated output. Defaults to use the model's `max_cache_size` attribute. Will be truncated to maximal cache size if larger than `max_cache_size`."); |
| 40 | + |
| 41 | +DEFINE_string( |
| 42 | + output_folder_path, |
| 43 | + "outputs", |
| 44 | + "Executorch inference data output path."); |
| 45 | + |
| 46 | +std::vector<std::vector<std::vector<int64_t>>> parse_input_list_file( |
| 47 | + const std::string& input_list_path) { |
| 48 | + std::vector<std::vector<std::vector<int64_t>>> bufs; |
| 49 | + std::ifstream input_list(input_list_path); |
| 50 | + |
| 51 | + auto split = [](std::string s, std::string delimiter) { |
| 52 | + size_t pos_start = 0, pos_end, delim_len = delimiter.length(); |
| 53 | + std::string token; |
| 54 | + std::vector<std::string> res; |
| 55 | + |
| 56 | + while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) { |
| 57 | + token = s.substr(pos_start, pos_end - pos_start); |
| 58 | + pos_start = pos_end + delim_len; |
| 59 | + res.push_back(token); |
| 60 | + } |
| 61 | + res.push_back(s.substr(pos_start)); |
| 62 | + return res; |
| 63 | + }; |
| 64 | + |
| 65 | + if (!input_list.is_open()) { |
| 66 | + ET_LOG(Error, "Unable to open file"); |
| 67 | + return bufs; |
| 68 | + } |
| 69 | + |
| 70 | + std::string file_path; |
| 71 | + while (std::getline(input_list, file_path)) { |
| 72 | + auto input_files = split(file_path, " "); |
| 73 | + int num_inputs = input_files.size(); |
| 74 | + if (num_inputs == 0) { |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + bufs.emplace_back(); |
| 79 | + bufs.back().resize(num_inputs); |
| 80 | + for (int input_index = 0; input_index < num_inputs; ++input_index) { |
| 81 | + std::ifstream fin(input_files[input_index], std::ios::binary); |
| 82 | + if (!fin.is_open()) { |
| 83 | + ET_LOG( |
| 84 | + Error, "Could not open file %s", input_files[input_index].c_str()); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + fin.seekg(0, std::ios::end); |
| 89 | + size_t file_size = fin.tellg(); |
| 90 | + fin.seekg(0, std::ios::beg); |
| 91 | + |
| 92 | + size_t num_tokens = file_size / sizeof(int64_t); |
| 93 | + bufs.back()[input_index].resize(num_tokens); |
| 94 | + |
| 95 | + if (!fin.read( |
| 96 | + reinterpret_cast<char*>(bufs.back()[input_index].data()), |
| 97 | + file_size)) { |
| 98 | + ET_LOG( |
| 99 | + Error, "Could not read file %s", input_files[input_index].c_str()); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + fin.close(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + input_list.close(); |
| 108 | + return bufs; |
| 109 | +} |
| 110 | + |
| 111 | +int main(int argc, char** argv) { |
| 112 | + gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 113 | + |
| 114 | + std::vector<std::vector<std::vector<int64_t>>> multi_turns_input_buffers = |
| 115 | + parse_input_list_file(FLAGS_input_list_path); |
| 116 | + |
| 117 | + for (int iter = 0; iter < multi_turns_input_buffers.size(); ++iter) { |
| 118 | + std::vector<char> bufs; |
| 119 | + bufs.reserve(5 * FLAGS_seq_len); // assume each token is around 5 char |
| 120 | + auto callback = [&](const std::string& piece) { |
| 121 | + for (const char c : piece) { |
| 122 | + bufs.push_back(c); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + example::Runner runner(FLAGS_model_path, FLAGS_tokenizer_model_path); |
| 127 | + // generate tokens |
| 128 | + runner.generate(FLAGS_seq_len, multi_turns_input_buffers[iter], callback); |
| 129 | + auto output_file_name = |
| 130 | + FLAGS_output_folder_path + "/output_" + std::to_string(iter) + ".txt"; |
| 131 | + std::ofstream fout(output_file_name); |
| 132 | + fout.write(bufs.data(), bufs.size()); |
| 133 | + fout.close(); |
| 134 | + } |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments