|
| 1 | +#include <chrono> |
| 2 | +#include <iomanip> |
| 3 | +#include <iostream> |
| 4 | +#include <sstream> |
| 5 | +#include <stdexcept> |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include <executorch/extension/module/module.h> |
| 10 | +#include <executorch/extension/tensor/tensor_ptr.h> |
| 11 | +#include <executorch/runtime/core/error.h> |
| 12 | +#include <executorch/runtime/core/evalue.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 14 | +#include <executorch/runtime/core/portable_type/tensor.h> |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +using executorch::aten::ScalarType; |
| 19 | +using executorch::aten::Tensor; |
| 20 | +using executorch::extension::make_tensor_ptr; |
| 21 | +using executorch::extension::TensorPtr; |
| 22 | +using executorch::extension::module::Module; |
| 23 | +using executorch::runtime::Error; |
| 24 | +using executorch::runtime::EValue; |
| 25 | +using executorch::runtime::Result; |
| 26 | +using Clock = std::chrono::steady_clock; |
| 27 | +using DurationMs = std::chrono::duration<double, std::milli>; |
| 28 | + |
| 29 | +std::vector<executorch::aten::SizesType> to_sizes( |
| 30 | + std::initializer_list<int64_t> dims) { |
| 31 | + return std::vector<executorch::aten::SizesType>(dims.begin(), dims.end()); |
| 32 | +} |
| 33 | + |
| 34 | +std::string format_shape(const Tensor& tensor) { |
| 35 | + std::ostringstream oss; |
| 36 | + oss << "["; |
| 37 | + const auto& sizes = tensor.sizes(); |
| 38 | + for (size_t i = 0; i < sizes.size(); ++i) { |
| 39 | + if (i > 0) { |
| 40 | + oss << ", "; |
| 41 | + } |
| 42 | + oss << sizes[i]; |
| 43 | + } |
| 44 | + oss << "]"; |
| 45 | + return oss.str(); |
| 46 | +} |
| 47 | + |
| 48 | +void print_tensor_summary(const std::string& label, const Tensor& tensor) { |
| 49 | + std::cout << " " << label |
| 50 | + << ": dtype=" << executorch::runtime::toString(tensor.scalar_type()) |
| 51 | + << ", shape=" << format_shape(tensor) |
| 52 | + << ", numel=" << tensor.numel() << std::endl; |
| 53 | +} |
| 54 | + |
| 55 | +TensorPtr create_audio_input() { |
| 56 | + const auto sizes = to_sizes({3, 128, 3000}); |
| 57 | + const size_t numel = 3ull * 128ull * 3000ull; |
| 58 | + std::vector<float> data(numel, 0.5f); |
| 59 | + return make_tensor_ptr<float>( |
| 60 | + sizes, std::move(data), {}, {}, ScalarType::BFloat16); |
| 61 | +} |
| 62 | + |
| 63 | +TensorPtr create_token_ids_input() { |
| 64 | + const auto sizes = to_sizes({1, 1138}); |
| 65 | + std::vector<int64_t> data(static_cast<size_t>(1) * 1138, 0); |
| 66 | + return make_tensor_ptr<int64_t>(sizes, std::move(data)); |
| 67 | +} |
| 68 | + |
| 69 | +TensorPtr create_positions_input() { |
| 70 | + const auto sizes = to_sizes({1138}); |
| 71 | + std::vector<int64_t> data(static_cast<size_t>(1138), 0); |
| 72 | + return make_tensor_ptr<int64_t>(sizes, std::move(data)); |
| 73 | +} |
| 74 | + |
| 75 | +TensorPtr create_fallback_text_embedding() { |
| 76 | + const auto sizes = to_sizes({1, 1138, 3072}); |
| 77 | + const size_t numel = 1ull * 1138ull * 3072ull; |
| 78 | + std::vector<float> data(numel, 0.0f); |
| 79 | + return make_tensor_ptr<float>( |
| 80 | + sizes, std::move(data), {}, {}, ScalarType::BFloat16); |
| 81 | +} |
| 82 | + |
| 83 | +struct MethodTiming { |
| 84 | + double load_ms{0.0}; |
| 85 | + double run_ms{0.0}; |
| 86 | +}; |
| 87 | + |
| 88 | +} // namespace |
| 89 | + |
| 90 | +int main(int argc, char** argv) { |
| 91 | + if (argc != 3) { |
| 92 | + std::cerr << "Usage: " << argv[0] |
| 93 | + << " <path/to/model.pte> <path/to/aoti_cuda_blob.ptd>" |
| 94 | + << std::endl; |
| 95 | + return 1; |
| 96 | + } |
| 97 | + |
| 98 | + const std::string program_path = argv[1]; |
| 99 | + const std::string data_map_path = argv[2]; |
| 100 | + |
| 101 | + try { |
| 102 | + Module module(program_path, data_map_path); |
| 103 | + |
| 104 | + const auto program_load_start = Clock::now(); |
| 105 | + const Error program_load_error = module.load(); |
| 106 | + const auto program_load_end = Clock::now(); |
| 107 | + if (program_load_error != Error::Ok) { |
| 108 | + std::cerr << "Failed to load ExecuTorch program: error code " |
| 109 | + << static_cast<int>(program_load_error) << std::endl; |
| 110 | + return 1; |
| 111 | + } |
| 112 | + const DurationMs program_load_latency = |
| 113 | + program_load_end - program_load_start; |
| 114 | + |
| 115 | + MethodTiming audio_timing; |
| 116 | + MethodTiming token_timing; |
| 117 | + MethodTiming text_timing; |
| 118 | + |
| 119 | + auto measure_method_load = |
| 120 | + [&](const std::string& name) -> std::pair<Error, double> { |
| 121 | + const auto start = Clock::now(); |
| 122 | + const Error err = module.load_method(name); |
| 123 | + const auto end = Clock::now(); |
| 124 | + return {err, DurationMs(end - start).count()}; |
| 125 | + }; |
| 126 | + |
| 127 | + // audio_encoder |
| 128 | + { |
| 129 | + const auto [err, load_ms] = measure_method_load("audio_encoder"); |
| 130 | + if (err != Error::Ok) { |
| 131 | + std::cerr << "Failed to load method audio_encoder: error code " |
| 132 | + << static_cast<int>(err) << std::endl; |
| 133 | + return 1; |
| 134 | + } |
| 135 | + audio_timing.load_ms = load_ms; |
| 136 | + |
| 137 | + const TensorPtr audio_input = create_audio_input(); |
| 138 | + std::vector<EValue> inputs; |
| 139 | + std::vector<TensorPtr> owned_inputs; |
| 140 | + owned_inputs.emplace_back(audio_input); |
| 141 | + inputs.emplace_back(*audio_input); |
| 142 | + |
| 143 | + const auto run_start = Clock::now(); |
| 144 | + Result<std::vector<EValue>> output_result = |
| 145 | + module.execute("audio_encoder", inputs); |
| 146 | + const auto run_end = Clock::now(); |
| 147 | + audio_timing.run_ms = DurationMs(run_end - run_start).count(); |
| 148 | + |
| 149 | + if (output_result.error() != Error::Ok) { |
| 150 | + std::cerr << "audio_encoder execution failed: error code " |
| 151 | + << static_cast<int>(output_result.error()) << std::endl; |
| 152 | + return 1; |
| 153 | + } |
| 154 | + |
| 155 | + const auto& outputs = output_result.get(); |
| 156 | + if (!outputs.empty() && outputs[0].isTensor()) { |
| 157 | + print_tensor_summary("audio_encoder output", outputs[0].toTensor()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + EValue token_output; |
| 162 | + bool token_executed = false; |
| 163 | + |
| 164 | + // token_embedding |
| 165 | + { |
| 166 | + const auto [err, load_ms] = measure_method_load("token_embedding"); |
| 167 | + if (err != Error::Ok) { |
| 168 | + std::cerr << "Failed to load method token_embedding: error code " |
| 169 | + << static_cast<int>(err) << std::endl; |
| 170 | + return 1; |
| 171 | + } |
| 172 | + token_timing.load_ms = load_ms; |
| 173 | + |
| 174 | + const TensorPtr token_ids = create_token_ids_input(); |
| 175 | + std::vector<EValue> inputs; |
| 176 | + std::vector<TensorPtr> owned_inputs; |
| 177 | + owned_inputs.emplace_back(token_ids); |
| 178 | + inputs.emplace_back(*token_ids); |
| 179 | + |
| 180 | + const auto run_start = Clock::now(); |
| 181 | + auto token_output_result = module.execute("token_embedding", inputs); |
| 182 | + const auto run_end = Clock::now(); |
| 183 | + token_timing.run_ms = DurationMs(run_end - run_start).count(); |
| 184 | + |
| 185 | + if (token_output_result.error() != Error::Ok) { |
| 186 | + std::cerr << "token_embedding execution failed: error code " |
| 187 | + << static_cast<int>(token_output_result.error()) << std::endl; |
| 188 | + return 1; |
| 189 | + } |
| 190 | + |
| 191 | + token_executed = true; |
| 192 | + const auto& outputs = token_output_result.get(); |
| 193 | + if (!outputs.empty() && outputs[0].isTensor()) { |
| 194 | + print_tensor_summary("token_embedding output", outputs[0].toTensor()); |
| 195 | + token_output = outputs[0]; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // text_decoder |
| 200 | + { |
| 201 | + const auto [err, load_ms] = measure_method_load("text_decoder"); |
| 202 | + if (err != Error::Ok) { |
| 203 | + std::cerr << "Failed to load method text_decoder: error code " |
| 204 | + << static_cast<int>(err) << std::endl; |
| 205 | + return 1; |
| 206 | + } |
| 207 | + text_timing.load_ms = load_ms; |
| 208 | + |
| 209 | + std::vector<EValue> inputs; |
| 210 | + std::vector<TensorPtr> owned_inputs; |
| 211 | + if (token_executed) { |
| 212 | + if (token_output.isTensor()) { |
| 213 | + inputs.emplace_back(token_output); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + if (inputs.empty()) { |
| 218 | + auto fallback_embedding = create_fallback_text_embedding(); |
| 219 | + owned_inputs.emplace_back(fallback_embedding); |
| 220 | + inputs.emplace_back(*fallback_embedding); |
| 221 | + } |
| 222 | + |
| 223 | + auto positions = create_positions_input(); |
| 224 | + owned_inputs.emplace_back(positions); |
| 225 | + inputs.emplace_back(*positions); |
| 226 | + |
| 227 | + const auto run_start = Clock::now(); |
| 228 | + Result<std::vector<EValue>> output_result = |
| 229 | + module.execute("text_decoder", inputs); |
| 230 | + const auto run_end = Clock::now(); |
| 231 | + text_timing.run_ms = DurationMs(run_end - run_start).count(); |
| 232 | + |
| 233 | + if (output_result.error() != Error::Ok) { |
| 234 | + std::cerr << "text_decoder execution failed: error code " |
| 235 | + << static_cast<int>(output_result.error()) << std::endl; |
| 236 | + return 1; |
| 237 | + } |
| 238 | + |
| 239 | + const auto& outputs = output_result.get(); |
| 240 | + if (!outputs.empty() && outputs[0].isTensor()) { |
| 241 | + print_tensor_summary("text_decoder output", outputs[0].toTensor()); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + std::cout << std::fixed << std::setprecision(3); |
| 246 | + std::cout << "Program load latency (ms): " << program_load_latency.count() |
| 247 | + << std::endl; |
| 248 | + |
| 249 | + std::cout << "Method load latency (ms):" << std::endl; |
| 250 | + std::cout << " audio_encoder: " << audio_timing.load_ms << std::endl; |
| 251 | + std::cout << " token_embedding: " << token_timing.load_ms << std::endl; |
| 252 | + std::cout << " text_decoder: " << text_timing.load_ms << std::endl; |
| 253 | + |
| 254 | + std::cout << "Run latency (ms):" << std::endl; |
| 255 | + std::cout << " audio_encoder: " << audio_timing.run_ms << std::endl; |
| 256 | + std::cout << " token_embedding: " << token_timing.run_ms << std::endl; |
| 257 | + std::cout << " text_decoder: " << text_timing.run_ms << std::endl; |
| 258 | + |
| 259 | + return 0; |
| 260 | + } catch (const std::exception& ex) { |
| 261 | + std::cerr << "Unhandled exception: " << ex.what() << std::endl; |
| 262 | + return 1; |
| 263 | + } |
| 264 | +} |
0 commit comments