unity.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "ggml/ggml.h"
  2. #include "ggml/ggml-alloc.h"
  3. #include "math.h"
  4. #include "model_loader.h"
  5. #include "fairseq2.h"
  6. #include "lib/unity_lib.h"
  7. #include <sndfile.h>
  8. #include <cstdlib>
  9. #include "ggml-alloc.h"
  10. #include <numeric>
  11. #include <algorithm>
  12. struct unity_params {
  13. int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
  14. std::string model = "seamlessM4T_medium.ggml"; // model path
  15. std::string tgt_lang = "eng";
  16. std::vector<std::string> files = {};
  17. bool text = false;
  18. SequenceGeneratorOptions opts = {
  19. /*beam_size*/ 5,
  20. /*min_seq_len*/ 1,
  21. /*soft_max_seq_len_a*/ 1,
  22. /*soft_max_seq_len_b*/ 200,
  23. /*hard_max_seq_len*/ 1000,
  24. /*len_penalty*/ 1.0,
  25. /*unk_penalty*/ 0.0,
  26. /*normalize_scores*/ true,
  27. /*mem_mb*/ 512,
  28. };
  29. bool verbose = false;
  30. };
  31. void unity_print_usage(int /*argc*/, char ** argv, const unity_params & params) {
  32. fprintf(stderr, "usage: %s [options] file1 file2 ...\n", argv[0]);
  33. fprintf(stderr, "\n");
  34. fprintf(stderr, "options:\n");
  35. fprintf(stderr, " -h, --help show this help message and exit\n");
  36. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  37. fprintf(stderr, " -v, --verbose Print out word level confidence score and LID score (default: off)");
  38. fprintf(stderr, " -m FNAME, --model FNAME\n");
  39. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  40. fprintf(stderr, " --text text output\n");
  41. fprintf(stderr, " --beam-size beam size (default: %d)\n", params.opts.beam_size);
  42. fprintf(stderr, " -M, --mem memory buffer, increase for long inputs (default: %d)\n", params.opts.mem_mb);
  43. fprintf(stderr, "\n");
  44. }
  45. std::string get_next_arg(int& i, int argc, char** argv, const std::string& flag, unity_params& params) {
  46. if (i + 1 < argc && argv[i + 1][0] != '-') {
  47. return argv[++i];
  48. } else {
  49. fprintf(stderr, "error: %s requires one argument.\n", flag.c_str());
  50. unity_print_usage(argc, argv, params);
  51. exit(0);
  52. }
  53. }
  54. bool unity_params_parse(int argc, char ** argv, unity_params & params) {
  55. for (int i = 1; i < argc; i++) {
  56. std::string arg = argv[i];
  57. if (arg == "-h" || arg == "--help") {
  58. unity_print_usage(argc, argv, params);
  59. } else if (arg == "-t" || arg == "--threads") {
  60. params.n_threads = std::stoi(get_next_arg(i, argc, argv, arg, params));
  61. } else if (arg == "-m" || arg == "--model") {
  62. params.model = get_next_arg(i, argc, argv, arg, params);
  63. } else if (arg == "-l" || arg == "--tgt-lang") {
  64. params.tgt_lang = get_next_arg(i, argc, argv, arg, params);
  65. } else if (arg == "--text") {
  66. params.text = true;
  67. } else if (arg == "-b" || arg == "--beam-size") {
  68. params.opts.beam_size = std::stoi(get_next_arg(i, argc, argv, arg, params));
  69. } else if (arg == "-v" || arg == "--verbose") {
  70. params.verbose = true;
  71. } else if (arg == "-M" || arg == "--mem") {
  72. params.opts.mem_mb = std::stoi(get_next_arg(i, argc, argv, arg, params));
  73. } else {
  74. params.files.push_back(std::string(arg));
  75. }
  76. }
  77. return true;
  78. }
  79. int main(int argc, char ** argv) {
  80. unity_params params;
  81. if (unity_params_parse(argc, argv, params) == false) {
  82. return 1;
  83. }
  84. fairseq2_model model;
  85. // load the model
  86. if (load_fairseq2_ggml_file(model, params.model.c_str())) {
  87. fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, params.model.c_str());
  88. return 1;
  89. }
  90. // The ctx_size_mb mostly depends of input length and model dim.
  91. int ctx_size_mb = params.opts.mem_mb;
  92. auto encoder_buf = std::vector<uint8_t>(8 * 1024 * 1024); // Only tensor metadata goes in there
  93. auto encoder_fwd_buf = std::vector<uint8_t>(ctx_size_mb * 1024 * 1024 / 2);
  94. ggml_allocr* fwd_alloc = ggml_allocr_new(encoder_fwd_buf.data(), encoder_fwd_buf.capacity(), 8);
  95. char result_str[4096];
  96. std::string input;
  97. bool interactive = params.files.size() == 0;
  98. auto next_file = params.files.begin();
  99. while (true) {
  100. if (interactive) {
  101. std::cout << "\nEnter audio_path and tgt_lang, separated by space (or 'exit' to quit):\n";
  102. std::getline(std::cin, input);
  103. if (input == "exit") {
  104. break;
  105. }
  106. } else {
  107. if (next_file == params.files.end()) break;
  108. input = *(next_file++);
  109. }
  110. std::istringstream iss(input);
  111. std::string audio_path;
  112. std::string tgt_lang = params.tgt_lang;
  113. iss >> audio_path >> tgt_lang;
  114. if (audio_path == "-") {
  115. audio_path = "/proc/self/fd/0";
  116. }
  117. std::cerr << "Translating (Transcribing) " << audio_path << " to " << tgt_lang << "\n";
  118. SF_INFO info;
  119. SNDFILE* sndfile = sf_open(audio_path.c_str(), SFM_READ, &info);
  120. if (!sndfile) {
  121. std::cerr << "Could not open file\n";
  122. if (interactive) continue;
  123. else return 1;
  124. }
  125. // Load audio input
  126. GGML_ASSERT(info.samplerate == 16000);
  127. GGML_ASSERT(info.channels == 1);
  128. // stop at 30s. Ideally we should chunk input audio, but this will prevent most obvious OOM.
  129. int n_frames = std::min(info.samplerate * 30, (int)info.frames);
  130. std::vector<float> data(n_frames * info.channels);
  131. sf_readf_float(sndfile, data.data(), n_frames);
  132. Result result = unity_eval(model, data, params.opts, tgt_lang, params.n_threads, ctx_size_mb);
  133. std::string concat_transcription = std::accumulate(std::next(result.transcription.begin()), result.transcription.end(), result.transcription[0],
  134. [](const std::string& a, const std::string& b) {
  135. return a + " " + b;
  136. }
  137. );
  138. if (params.verbose) {
  139. std::cout << "Final transcription: " << concat_transcription << std::endl;
  140. std::cout << std::endl;
  141. std::cout << "Word level confidence score:" << std::endl;
  142. for (size_t i = 0; i < result.transcription.size(); ++i) {
  143. std::cout << "Word: " << result.transcription[i] << " | Score: " << result.word_confidence_scores[i] << std::endl;
  144. }
  145. std::cout << std::endl;
  146. std::cout << "LID scores: " << std::endl;
  147. for (const auto& kv : result.lid_scores) {
  148. std::cout << "Language: " << kv.first << "| Score: " << kv.second << std::endl;
  149. }
  150. } else {
  151. std::cout << concat_transcription << std::endl;
  152. }
  153. }
  154. return 0;
  155. }