|
| 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 <cmath> |
| 10 | +#include <cstring> |
| 11 | +#include <fstream> |
| 12 | +#include <iostream> |
| 13 | +#include <limits> |
| 14 | +#include <memory> |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include <gflags/gflags.h> |
| 19 | + |
| 20 | +#include <executorch/extension/asr/runner/runner.h> |
| 21 | +#include <executorch/extension/llm/runner/util.h> |
| 22 | +#include <executorch/extension/llm/runner/wav_loader.h> |
| 23 | +#include <executorch/extension/module/module.h> |
| 24 | +#include <executorch/extension/tensor/tensor_ptr_maker.h> |
| 25 | +#include <executorch/runtime/core/evalue.h> |
| 26 | +#include <executorch/runtime/platform/log.h> |
| 27 | + |
| 28 | +DEFINE_string(model_path, "model.pte", "Path to Whisper model (.pte)."); |
| 29 | +DEFINE_string(data_path, "", "Optional path to Whisper weights (.ptd)."); |
| 30 | +DEFINE_string( |
| 31 | + tokenizer_path, |
| 32 | + ".", |
| 33 | + "Path to tokenizer directory containing tokenizer.json, tokenizer_config.json, and special_tokens_map.json."); |
| 34 | +DEFINE_string( |
| 35 | + processor_path, |
| 36 | + "", |
| 37 | + "Path to preprocessor .pte for converting raw audio."); |
| 38 | +DEFINE_string( |
| 39 | + audio_path, |
| 40 | + "", |
| 41 | + "Path to input audio file. Accepts .wav or raw float .bin."); |
| 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 | +using ::executorch::extension::from_blob; |
| 49 | +using ::executorch::extension::Module; |
| 50 | + |
| 51 | +int main(int argc, char** argv) { |
| 52 | + gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 53 | + ::executorch::extension::TensorPtr features; |
| 54 | + std::vector<float> audio_data; |
| 55 | + std::unique_ptr<Module> processor; |
| 56 | + |
| 57 | + if (FLAGS_audio_path.empty()) { |
| 58 | + ET_LOG(Error, "audio_path flag must be provided."); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + audio_data = |
| 63 | + executorch::extension::llm::load_wav_audio_data(FLAGS_audio_path); |
| 64 | + ET_LOG( |
| 65 | + Info, |
| 66 | + "First 2 values of audio data: %f, %f", |
| 67 | + audio_data[0], |
| 68 | + audio_data[1]); |
| 69 | + |
| 70 | + processor = |
| 71 | + std::make_unique<Module>(FLAGS_processor_path, Module::LoadMode::Mmap); |
| 72 | + auto load_error = processor->load(); |
| 73 | + if (load_error != ::executorch::runtime::Error::Ok) { |
| 74 | + ET_LOG(Error, "Failed to load preprocessor module."); |
| 75 | + return 1; |
| 76 | + } |
| 77 | + |
| 78 | + auto audio_tensor = from_blob( |
| 79 | + audio_data.data(), |
| 80 | + {static_cast<::executorch::aten::SizesType>(audio_data.size())}, |
| 81 | + ::executorch::aten::ScalarType::Float); |
| 82 | + |
| 83 | + auto processed_result = processor->execute("forward", audio_tensor); |
| 84 | + if (processed_result.error() != ::executorch::runtime::Error::Ok) { |
| 85 | + ET_LOG(Error, "Audio preprocessing failed."); |
| 86 | + return 1; |
| 87 | + } |
| 88 | + auto outputs = std::move(processed_result.get()); |
| 89 | + if (outputs.empty() || !outputs[0].isTensor()) { |
| 90 | + ET_LOG(Error, "Preprocessor returned unexpected outputs."); |
| 91 | + return 1; |
| 92 | + } |
| 93 | + auto tensor = outputs[0].toTensor(); |
| 94 | + ET_LOG( |
| 95 | + Info, |
| 96 | + "Result scalar_type: %s, first value %f", |
| 97 | + ::executorch::runtime::toString(tensor.scalar_type()), |
| 98 | + tensor.mutable_data_ptr<float>()[0]); |
| 99 | + features = std::make_shared<::executorch::aten::Tensor>(std::move(tensor)); |
| 100 | + |
| 101 | + executorch::extension::asr::AsrRunner runner( |
| 102 | + FLAGS_model_path, FLAGS_data_path, FLAGS_tokenizer_path); |
| 103 | + auto load_err = runner.load(); |
| 104 | + if (load_err != ::executorch::runtime::Error::Ok) { |
| 105 | + ET_LOG(Error, "Failed to load Whisper model."); |
| 106 | + return 1; |
| 107 | + } |
| 108 | + |
| 109 | + executorch::extension::asr::AsrTranscribeConfig config; |
| 110 | + config.max_new_tokens = FLAGS_max_new_tokens; |
| 111 | + config.temperature = static_cast<float>(FLAGS_temperature); |
| 112 | + config.decoder_start_token_id = 50257; |
| 113 | + |
| 114 | + auto result = |
| 115 | + runner.transcribe(features, config, [&](const std::string& piece) { |
| 116 | + ::executorch::extension::llm::safe_printf(piece.c_str()); |
| 117 | + fflush(stdout); |
| 118 | + }); |
| 119 | + |
| 120 | + if (!result.ok()) { |
| 121 | + ET_LOG(Error, "Transcription failed."); |
| 122 | + return 1; |
| 123 | + } |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments