|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 <fstream> |
| 10 | +#include <memory> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include <gflags/gflags.h> |
| 15 | + |
| 16 | +#include <executorch/examples/models/whisper/runner.h> |
| 17 | +#include <executorch/extension/llm/runner/util.h> |
| 18 | +#include <executorch/extension/llm/runner/wav_loader.h> |
| 19 | +#include <executorch/extension/module/module.h> |
| 20 | +#include <executorch/extension/tensor/tensor_ptr_maker.h> |
| 21 | +#include <executorch/runtime/core/evalue.h> |
| 22 | +#include <executorch/runtime/platform/log.h> |
| 23 | + |
| 24 | +DEFINE_string(model_path, "model.pte", "Path to Whisper model (.pte)."); |
| 25 | +DEFINE_string(data_path, "", "Optional path to Whisper weights (.ptd)."); |
| 26 | +DEFINE_string( |
| 27 | + tokenizer_path, |
| 28 | + ".", |
| 29 | + "Path to tokenizer directory containing tokenizer.json, tokenizer_config.json, and special_tokens_map.json."); |
| 30 | +DEFINE_string( |
| 31 | + preprocessor_path, |
| 32 | + "", |
| 33 | + "Path to preprocessor .pte for converting raw audio."); |
| 34 | +DEFINE_string( |
| 35 | + audio_path, |
| 36 | + "", |
| 37 | + "Path to input audio file. Accepts .wav or raw float .bin."); |
| 38 | +DEFINE_string( |
| 39 | + preprocessed_audio_path, |
| 40 | + "", |
| 41 | + "Path to preprocessed audio features file (.bin). If provided, skips preprocessing."); |
| 42 | +DEFINE_double( |
| 43 | + temperature, |
| 44 | + 0.0, |
| 45 | + "Sampling temperature. 0.0 performs greedy decoding."); |
| 46 | +DEFINE_int32(max_new_tokens, 128, "Maximum number of tokens to generate."); |
| 47 | + |
| 48 | +namespace { |
| 49 | + |
| 50 | +using ::executorch::extension::from_blob; |
| 51 | +using ::executorch::extension::Module; |
| 52 | + |
| 53 | +bool ends_with(const std::string& value, const std::string& suffix) { |
| 54 | + return value.size() >= suffix.size() && |
| 55 | + value.compare(value.size() - suffix.size(), suffix.size(), suffix) == 0; |
| 56 | +} |
| 57 | + |
| 58 | +std::vector<float> load_preprocessed_audio( |
| 59 | + const std::string& preprocessed_audio_path) { |
| 60 | + ET_LOG( |
| 61 | + Info, |
| 62 | + "Loading preprocessed audio from: %s", |
| 63 | + preprocessed_audio_path.c_str()); |
| 64 | + |
| 65 | + std::ifstream stream( |
| 66 | + preprocessed_audio_path, std::ios::binary | std::ios::ate); |
| 67 | + if (!stream.is_open()) { |
| 68 | + ET_LOG( |
| 69 | + Error, |
| 70 | + "Failed to open preprocessed audio file: %s", |
| 71 | + preprocessed_audio_path.c_str()); |
| 72 | + throw std::runtime_error("Failed to open preprocessed audio file"); |
| 73 | + } |
| 74 | + |
| 75 | + std::size_t byte_size = static_cast<std::size_t>(stream.tellg()); |
| 76 | + stream.seekg(0, std::ios::beg); |
| 77 | + |
| 78 | + const int64_t batch_size = 1; |
| 79 | + const int64_t feature_dim = 128; |
| 80 | + const int64_t time_steps = 3000; |
| 81 | + const int64_t expected_elements = batch_size * feature_dim * time_steps; |
| 82 | + const std::size_t expected_bytes = expected_elements * sizeof(float); |
| 83 | + |
| 84 | + if (byte_size != expected_bytes) { |
| 85 | + ET_LOG( |
| 86 | + Error, |
| 87 | + "Preprocessed audio file size mismatch. Expected %zu bytes, got %zu bytes", |
| 88 | + expected_bytes, |
| 89 | + byte_size); |
| 90 | + throw std::runtime_error("Preprocessed audio file size mismatch"); |
| 91 | + } |
| 92 | + |
| 93 | + std::vector<float> feature_data(expected_elements); |
| 94 | + stream.read(reinterpret_cast<char*>(feature_data.data()), byte_size); |
| 95 | + stream.close(); |
| 96 | + |
| 97 | + return feature_data; |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace |
| 101 | + |
| 102 | +int main(int argc, char** argv) { |
| 103 | + gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 104 | + |
| 105 | + ::executorch::extension::TensorPtr features; |
| 106 | + std::vector<float> audio_data; |
| 107 | + std::unique_ptr<Module> processor; |
| 108 | + |
| 109 | + if (!FLAGS_preprocessed_audio_path.empty()) { |
| 110 | + audio_data = load_preprocessed_audio(FLAGS_preprocessed_audio_path); |
| 111 | + |
| 112 | + const int64_t batch_size = 1; |
| 113 | + const int64_t feature_dim = 128; |
| 114 | + const int64_t time_steps = 3000; |
| 115 | + features = from_blob( |
| 116 | + audio_data.data(), |
| 117 | + /*sizes=*/{batch_size, feature_dim, time_steps}, |
| 118 | + /*strides=*/{feature_dim * time_steps, feature_dim, 1}, |
| 119 | + ::executorch::aten::ScalarType::Float); |
| 120 | + } else { |
| 121 | + // Original preprocessing path |
| 122 | + if (FLAGS_audio_path.empty()) { |
| 123 | + ET_LOG( |
| 124 | + Error, |
| 125 | + "Either audio_path or preprocessed_audio_path flag must be provided."); |
| 126 | + return 1; |
| 127 | + } |
| 128 | + |
| 129 | + audio_data = |
| 130 | + executorch::extension::llm::load_wav_audio_data(FLAGS_audio_path); |
| 131 | + ET_LOG( |
| 132 | + Info, |
| 133 | + "First 2 values of audio data: %f, %f", |
| 134 | + audio_data[0], |
| 135 | + audio_data[1]); |
| 136 | + // Preprocess audio |
| 137 | + processor = std::make_unique<Module>( |
| 138 | + FLAGS_preprocessor_path, Module::LoadMode::Mmap); |
| 139 | + auto load_error = processor->load(); |
| 140 | + if (load_error != ::executorch::runtime::Error::Ok) { |
| 141 | + ET_LOG(Error, "Failed to load preprocessor module."); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + auto audio_tensor = from_blob( |
| 146 | + audio_data.data(), |
| 147 | + {static_cast<::executorch::aten::SizesType>(audio_data.size())}, |
| 148 | + ::executorch::aten::ScalarType::Float); |
| 149 | + |
| 150 | + auto processed_result = processor->execute("forward", audio_tensor); |
| 151 | + if (processed_result.error() != ::executorch::runtime::Error::Ok) { |
| 152 | + ET_LOG(Error, "Audio preprocessing failed."); |
| 153 | + return 1; |
| 154 | + } |
| 155 | + auto outputs = std::move(processed_result.get()); |
| 156 | + if (outputs.empty() || !outputs[0].isTensor()) { |
| 157 | + ET_LOG(Error, "Preprocessor returned unexpected outputs."); |
| 158 | + return 1; |
| 159 | + } |
| 160 | + auto tensor = outputs[0].toTensor(); |
| 161 | + ET_LOG( |
| 162 | + Info, |
| 163 | + "Result scalar_type: %s, first value %f", |
| 164 | + ::executorch::runtime::toString(tensor.scalar_type()), |
| 165 | + tensor.mutable_data_ptr<float>()[0]); |
| 166 | + features = std::make_shared<::executorch::aten::Tensor>(std::move(tensor)); |
| 167 | + } |
| 168 | + |
| 169 | + example::WhisperRunner runner( |
| 170 | + FLAGS_model_path, FLAGS_data_path, FLAGS_tokenizer_path); |
| 171 | + auto load_err = runner.load(); |
| 172 | + if (load_err != ::executorch::runtime::Error::Ok) { |
| 173 | + ET_LOG(Error, "Failed to load Whisper model."); |
| 174 | + return 1; |
| 175 | + } |
| 176 | + |
| 177 | + example::WhisperTranscribeConfig config; |
| 178 | + config.max_new_tokens = FLAGS_max_new_tokens; |
| 179 | + config.temperature = static_cast<float>(FLAGS_temperature); |
| 180 | + |
| 181 | + std::string transcript; |
| 182 | + auto result = |
| 183 | + runner.transcribe(features, config, [&](const std::string& piece) { |
| 184 | + ::executorch::extension::llm::safe_printf(piece.c_str()); |
| 185 | + fflush(stdout); |
| 186 | + }); |
| 187 | + |
| 188 | + if (!result.ok()) { |
| 189 | + ET_LOG(Error, "Transcription failed."); |
| 190 | + return 1; |
| 191 | + } |
| 192 | + |
| 193 | + return 0; |
| 194 | +} |
0 commit comments