funasr-onnx-offline-rtf.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  3. * MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. #ifndef _WIN32
  6. #include <sys/time.h>
  7. #else
  8. #include <win_func.h>
  9. #endif
  10. #include <glog/logging.h>
  11. #include "funasrruntime.h"
  12. #include "tclap/CmdLine.h"
  13. #include "com-define.h"
  14. #include <iostream>
  15. #include <fstream>
  16. #include <sstream>
  17. #include <vector>
  18. #include <atomic>
  19. #include <mutex>
  20. #include <thread>
  21. #include <map>
  22. #include <unordered_map>
  23. #include "util.h"
  24. using namespace std;
  25. std::atomic<int> wav_index(0);
  26. std::mutex mtx;
  27. void runReg(FUNASR_HANDLE asr_handle, vector<string> wav_list, vector<string> wav_ids, int audio_fs,
  28. float* total_length, long* total_time, int core_id, float glob_beam = 3.0f, float lat_beam = 3.0f, float am_sc = 10.0f,
  29. int fst_inc_wts = 20, string hotword_path = "") {
  30. struct timeval start, end;
  31. long seconds = 0;
  32. float n_total_length = 0.0f;
  33. long n_total_time = 0;
  34. // init wfst decoder
  35. FUNASR_DEC_HANDLE decoder_handle = FunASRWfstDecoderInit(asr_handle, ASR_OFFLINE, glob_beam, lat_beam, am_sc);
  36. // process fst hotwords list
  37. unordered_map<string, int> hws_map;
  38. string nn_hotwords_ = "";
  39. funasr::ExtractHws(hotword_path, hws_map, nn_hotwords_);
  40. // load hotwords list and build graph
  41. FunWfstDecoderLoadHwsRes(decoder_handle, fst_inc_wts, hws_map);
  42. std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(asr_handle, nn_hotwords_);
  43. // warm up
  44. for (size_t i = 0; i < 1; i++)
  45. {
  46. FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[0].c_str(), RASR_NONE, nullptr, hotwords_embedding, audio_fs, true, decoder_handle);
  47. if(result){
  48. FunASRFreeResult(result);
  49. }
  50. }
  51. while (true) {
  52. // 使用原子变量获取索引并递增
  53. int i = wav_index.fetch_add(1);
  54. if (i >= wav_list.size()) {
  55. break;
  56. }
  57. gettimeofday(&start, nullptr);
  58. FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[i].c_str(), RASR_NONE, nullptr, hotwords_embedding, audio_fs, true, decoder_handle);
  59. gettimeofday(&end, nullptr);
  60. seconds = (end.tv_sec - start.tv_sec);
  61. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  62. n_total_time += taking_micros;
  63. if(result){
  64. string msg = FunASRGetResult(result, 0);
  65. LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << msg;
  66. string stamp = FunASRGetStamp(result);
  67. if(stamp !=""){
  68. LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << stamp;
  69. }
  70. string stamp_sents = FunASRGetStampSents(result);
  71. if(stamp_sents !=""){
  72. LOG(INFO)<< wav_ids[i] <<" : "<<stamp_sents;
  73. }
  74. float snippet_time = FunASRGetRetSnippetTime(result);
  75. n_total_length += snippet_time;
  76. FunASRFreeResult(result);
  77. }else{
  78. LOG(ERROR) << wav_ids[i] << (": No return data!\n");
  79. }
  80. }
  81. {
  82. lock_guard<mutex> guard(mtx);
  83. *total_length += n_total_length;
  84. if(*total_time < n_total_time){
  85. *total_time = n_total_time;
  86. }
  87. }
  88. FunWfstDecoderUnloadHwsRes(decoder_handle);
  89. FunASRWfstDecoderUninit(decoder_handle);
  90. }
  91. bool is_target_file(const std::string& filename, const std::string target) {
  92. std::size_t pos = filename.find_last_of(".");
  93. if (pos == std::string::npos) {
  94. return false;
  95. }
  96. std::string extension = filename.substr(pos + 1);
  97. return (extension == target);
  98. }
  99. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  100. {
  101. model_path.insert({key, value_arg.getValue()});
  102. LOG(INFO)<< key << " : " << value_arg.getValue();
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. google::InitGoogleLogging(argv[0]);
  107. FLAGS_logtostderr = true;
  108. TCLAP::CmdLine cmd("funasr-onnx-offline-rtf", ' ', "1.0");
  109. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  110. TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "true (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "true", "string");
  111. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  112. TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "true (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir", false, "true", "string");
  113. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
  114. TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "true (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir", false, "true", "string");
  115. TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml ", false, "", "string");
  116. TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
  117. TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
  118. TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
  119. TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
  120. TCLAP::ValueArg<std::string> itn_dir("", ITN_DIR, "the itn model(fst) path, which contains zh_itn_tagger.fst and zh_itn_verbalizer.fst", false, "", "string");
  121. TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
  122. TCLAP::ValueArg<std::int32_t> audio_fs("", AUDIO_FS, "the sample rate of audio", false, 16000, "int32_t");
  123. TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", true, 0, "int32_t");
  124. TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
  125. cmd.add(model_dir);
  126. cmd.add(quantize);
  127. cmd.add(vad_dir);
  128. cmd.add(vad_quant);
  129. cmd.add(punc_dir);
  130. cmd.add(punc_quant);
  131. cmd.add(itn_dir);
  132. cmd.add(lm_dir);
  133. cmd.add(global_beam);
  134. cmd.add(lattice_beam);
  135. cmd.add(am_scale);
  136. cmd.add(hotword);
  137. cmd.add(fst_inc_wts);
  138. cmd.add(wav_path);
  139. cmd.add(audio_fs);
  140. cmd.add(thread_num);
  141. cmd.parse(argc, argv);
  142. std::map<std::string, std::string> model_path;
  143. GetValue(model_dir, MODEL_DIR, model_path);
  144. GetValue(quantize, QUANTIZE, model_path);
  145. GetValue(vad_dir, VAD_DIR, model_path);
  146. GetValue(vad_quant, VAD_QUANT, model_path);
  147. GetValue(punc_dir, PUNC_DIR, model_path);
  148. GetValue(punc_quant, PUNC_QUANT, model_path);
  149. GetValue(itn_dir, ITN_DIR, model_path);
  150. GetValue(lm_dir, LM_DIR, model_path);
  151. GetValue(hotword, HOTWORD, model_path);
  152. GetValue(wav_path, WAV_PATH, model_path);
  153. struct timeval start, end;
  154. gettimeofday(&start, nullptr);
  155. FUNASR_HANDLE asr_handle=FunOfflineInit(model_path, 1);
  156. if (!asr_handle)
  157. {
  158. LOG(ERROR) << "FunASR init failed";
  159. exit(-1);
  160. }
  161. gettimeofday(&end, nullptr);
  162. long seconds = (end.tv_sec - start.tv_sec);
  163. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  164. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  165. // read wav_path
  166. vector<string> wav_list;
  167. vector<string> wav_ids;
  168. string default_id = "wav_default_id";
  169. string wav_path_ = model_path.at(WAV_PATH);
  170. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  171. wav_list.emplace_back(wav_path_);
  172. wav_ids.emplace_back(default_id);
  173. }
  174. else if(is_target_file(wav_path_, "scp")){
  175. ifstream in(wav_path_);
  176. if (!in.is_open()) {
  177. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  178. return 0;
  179. }
  180. string line;
  181. while(getline(in, line))
  182. {
  183. istringstream iss(line);
  184. string column1, column2;
  185. iss >> column1 >> column2;
  186. wav_list.emplace_back(column2);
  187. wav_ids.emplace_back(column1);
  188. }
  189. in.close();
  190. }else{
  191. LOG(ERROR)<<"Please check the wav extension!";
  192. exit(-1);
  193. }
  194. // 多线程测试
  195. float total_length = 0.0f;
  196. long total_time = 0;
  197. std::vector<std::thread> threads;
  198. int rtf_threds = thread_num.getValue();
  199. std::string hotword_path = hotword.getValue();
  200. int value_bias = 20;
  201. value_bias = fst_inc_wts.getValue();
  202. float glob_beam = 3.0f;
  203. float lat_beam = 3.0f;
  204. float am_sc = 10.0f;
  205. if (lm_dir.isSet()) {
  206. glob_beam = global_beam.getValue();
  207. lat_beam = lattice_beam.getValue();
  208. am_sc = am_scale.getValue();
  209. }
  210. for (int i = 0; i < rtf_threds; i++)
  211. {
  212. threads.emplace_back(thread(runReg, asr_handle, wav_list, wav_ids, audio_fs.getValue(), &total_length, &total_time, i, glob_beam, lat_beam, am_sc, value_bias, hotword_path));
  213. }
  214. for (auto& thread : threads)
  215. {
  216. thread.join();
  217. }
  218. LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
  219. LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
  220. LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
  221. LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
  222. FunOfflineUninit(asr_handle);
  223. return 0;
  224. }