funasr-onnx-offline-rtf.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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,
  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. FunOfflineReset(asr_handle, decoder_handle);
  47. FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[0].c_str(), RASR_NONE, NULL, hotwords_embedding, 16000, false, decoder_handle);
  48. if(result){
  49. FunASRFreeResult(result);
  50. }
  51. }
  52. while (true) {
  53. // 使用原子变量获取索引并递增
  54. int i = wav_index.fetch_add(1);
  55. if (i >= wav_list.size()) {
  56. break;
  57. }
  58. gettimeofday(&start, NULL);
  59. FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[i].c_str(), RASR_NONE, NULL, hotwords_embedding, 16000, false, decoder_handle);
  60. gettimeofday(&end, NULL);
  61. seconds = (end.tv_sec - start.tv_sec);
  62. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  63. n_total_time += taking_micros;
  64. if(result){
  65. string msg = FunASRGetResult(result, 0);
  66. LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << msg;
  67. string stamp = FunASRGetStamp(result);
  68. if(stamp !=""){
  69. LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << stamp;
  70. }
  71. float snippet_time = FunASRGetRetSnippetTime(result);
  72. n_total_length += snippet_time;
  73. FunASRFreeResult(result);
  74. }else{
  75. LOG(ERROR) << wav_ids[i] << (": No return data!\n");
  76. }
  77. }
  78. {
  79. lock_guard<mutex> guard(mtx);
  80. *total_length += n_total_length;
  81. if(*total_time < n_total_time){
  82. *total_time = n_total_time;
  83. }
  84. }
  85. FunWfstDecoderUnloadHwsRes(decoder_handle);
  86. FunASRWfstDecoderUninit(decoder_handle);
  87. }
  88. bool is_target_file(const std::string& filename, const std::string target) {
  89. std::size_t pos = filename.find_last_of(".");
  90. if (pos == std::string::npos) {
  91. return false;
  92. }
  93. std::string extension = filename.substr(pos + 1);
  94. return (extension == target);
  95. }
  96. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  97. {
  98. if (value_arg.isSet()){
  99. model_path.insert({key, value_arg.getValue()});
  100. LOG(INFO)<< key << " : " << value_arg.getValue();
  101. }
  102. }
  103. int main(int argc, char *argv[])
  104. {
  105. google::InitGoogleLogging(argv[0]);
  106. FLAGS_logtostderr = true;
  107. TCLAP::CmdLine cmd("funasr-onnx-offline-rtf", ' ', "1.0");
  108. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  109. 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");
  110. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  111. 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");
  112. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
  113. 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");
  114. TCLAP::ValueArg<std::string> lm_dir("", LM_DIR, "the lm model path, which contains compiled models: TLG.fst, config.yaml ", false, "", "string");
  115. TCLAP::ValueArg<float> global_beam("", GLOB_BEAM, "the decoding beam for beam searching ", false, 3.0, "float");
  116. TCLAP::ValueArg<float> lattice_beam("", LAT_BEAM, "the lattice generation beam for beam searching ", false, 3.0, "float");
  117. TCLAP::ValueArg<float> am_scale("", AM_SCALE, "the acoustic scale for beam searching ", false, 10.0, "float");
  118. TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS, "the fst hotwords incremental bias", false, 20, "int32_t");
  119. 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");
  120. 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");
  121. TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", true, 0, "int32_t");
  122. TCLAP::ValueArg<std::string> hotword("", HOTWORD, "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)", false, "", "string");
  123. cmd.add(model_dir);
  124. cmd.add(quantize);
  125. cmd.add(vad_dir);
  126. cmd.add(vad_quant);
  127. cmd.add(punc_dir);
  128. cmd.add(punc_quant);
  129. cmd.add(itn_dir);
  130. cmd.add(lm_dir);
  131. cmd.add(global_beam);
  132. cmd.add(lattice_beam);
  133. cmd.add(am_scale);
  134. cmd.add(hotword);
  135. cmd.add(fst_inc_wts);
  136. cmd.add(wav_path);
  137. cmd.add(thread_num);
  138. cmd.parse(argc, argv);
  139. std::map<std::string, std::string> model_path;
  140. GetValue(model_dir, MODEL_DIR, model_path);
  141. GetValue(quantize, QUANTIZE, model_path);
  142. GetValue(vad_dir, VAD_DIR, model_path);
  143. GetValue(vad_quant, VAD_QUANT, model_path);
  144. GetValue(punc_dir, PUNC_DIR, model_path);
  145. GetValue(punc_quant, PUNC_QUANT, model_path);
  146. GetValue(itn_dir, ITN_DIR, model_path);
  147. GetValue(lm_dir, LM_DIR, model_path);
  148. GetValue(hotword, HOTWORD, model_path);
  149. GetValue(wav_path, WAV_PATH, model_path);
  150. struct timeval start, end;
  151. gettimeofday(&start, NULL);
  152. FUNASR_HANDLE asr_handle=FunOfflineInit(model_path, 1);
  153. if (!asr_handle)
  154. {
  155. LOG(ERROR) << "FunASR init failed";
  156. exit(-1);
  157. }
  158. gettimeofday(&end, NULL);
  159. long seconds = (end.tv_sec - start.tv_sec);
  160. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  161. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  162. // read wav_path
  163. vector<string> wav_list;
  164. vector<string> wav_ids;
  165. string default_id = "wav_default_id";
  166. string wav_path_ = model_path.at(WAV_PATH);
  167. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  168. wav_list.emplace_back(wav_path_);
  169. wav_ids.emplace_back(default_id);
  170. }
  171. else if(is_target_file(wav_path_, "scp")){
  172. ifstream in(wav_path_);
  173. if (!in.is_open()) {
  174. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  175. return 0;
  176. }
  177. string line;
  178. while(getline(in, line))
  179. {
  180. istringstream iss(line);
  181. string column1, column2;
  182. iss >> column1 >> column2;
  183. wav_list.emplace_back(column2);
  184. wav_ids.emplace_back(column1);
  185. }
  186. in.close();
  187. }else{
  188. LOG(ERROR)<<"Please check the wav extension!";
  189. exit(-1);
  190. }
  191. // 多线程测试
  192. float total_length = 0.0f;
  193. long total_time = 0;
  194. std::vector<std::thread> threads;
  195. int rtf_threds = thread_num.getValue();
  196. std::string hotword_path = hotword.getValue();
  197. int value_bias = 20;
  198. value_bias = fst_inc_wts.getValue();
  199. float glob_beam = 3.0f;
  200. float lat_beam = 3.0f;
  201. float am_sc = 10.0f;
  202. if (lm_dir.isSet()) {
  203. glob_beam = global_beam.getValue();
  204. lat_beam = lattice_beam.getValue();
  205. am_sc = am_scale.getValue();
  206. }
  207. for (int i = 0; i < rtf_threds; i++)
  208. {
  209. threads.emplace_back(thread(runReg, asr_handle, wav_list, wav_ids, &total_length, &total_time, i, glob_beam, lat_beam, am_sc, value_bias, hotword_path));
  210. }
  211. for (auto& thread : threads)
  212. {
  213. thread.join();
  214. }
  215. LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
  216. LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
  217. LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
  218. LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
  219. FunOfflineUninit(asr_handle);
  220. return 0;
  221. }