funasr-onnx-2pass-rtf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <map>
  14. #include <atomic>
  15. #include <mutex>
  16. #include <thread>
  17. #include <glog/logging.h>
  18. #include "funasrruntime.h"
  19. #include "tclap/CmdLine.h"
  20. #include "com-define.h"
  21. #include "audio.h"
  22. using namespace std;
  23. std::atomic<int> wav_index(0);
  24. std::mutex mtx;
  25. bool is_target_file(const std::string& filename, const std::string target) {
  26. std::size_t pos = filename.find_last_of(".");
  27. if (pos == std::string::npos) {
  28. return false;
  29. }
  30. std::string extension = filename.substr(pos + 1);
  31. return (extension == target);
  32. }
  33. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  34. {
  35. model_path.insert({key, value_arg.getValue()});
  36. LOG(INFO)<< key << " : " << value_arg.getValue();
  37. }
  38. void runReg(FUNASR_HANDLE tpass_handle, std::vector<int> chunk_size, vector<string> wav_list, vector<string> wav_ids,
  39. float* total_length, long* total_time, int core_id, ASR_TYPE asr_mode_) {
  40. struct timeval start, end;
  41. long seconds = 0;
  42. float n_total_length = 0.0f;
  43. long n_total_time = 0;
  44. // init online features
  45. FUNASR_HANDLE tpass_online_handle=FunTpassOnlineInit(tpass_handle, chunk_size);
  46. // warm up
  47. for (size_t i = 0; i < 2; i++)
  48. {
  49. int32_t sampling_rate_ = 16000;
  50. funasr::Audio audio(1);
  51. if(is_target_file(wav_list[0].c_str(), "wav")){
  52. if(!audio.LoadWav2Char(wav_list[0].c_str(), &sampling_rate_)){
  53. LOG(ERROR)<<"Failed to load "<< wav_list[0];
  54. exit(-1);
  55. }
  56. }else if(is_target_file(wav_list[0].c_str(), "pcm")){
  57. if (!audio.LoadPcmwav2Char(wav_list[0].c_str(), &sampling_rate_)){
  58. LOG(ERROR)<<"Failed to load "<< wav_list[0];
  59. exit(-1);
  60. }
  61. }else{
  62. if (!audio.FfmpegLoad(wav_list[0].c_str(), true)){
  63. LOG(ERROR)<<"Failed to load "<< wav_list[0];
  64. exit(-1);
  65. }
  66. }
  67. char* speech_buff = audio.GetSpeechChar();
  68. int buff_len = audio.GetSpeechLen()*2;
  69. int step = 1600*2;
  70. bool is_final = false;
  71. std::vector<std::vector<string>> punc_cache(2);
  72. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  73. if (sample_offset + step >= buff_len - 1) {
  74. step = buff_len - sample_offset;
  75. is_final = true;
  76. } else {
  77. is_final = false;
  78. }
  79. FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle, speech_buff+sample_offset, step, punc_cache, is_final, sampling_rate_, "pcm", (ASR_TYPE)asr_mode_);
  80. if (result)
  81. {
  82. FunASRFreeResult(result);
  83. }
  84. }
  85. }
  86. while (true) {
  87. // 使用原子变量获取索引并递增
  88. int i = wav_index.fetch_add(1);
  89. if (i >= wav_list.size()) {
  90. break;
  91. }
  92. int32_t sampling_rate_ = 16000;
  93. funasr::Audio audio(1);
  94. if(is_target_file(wav_list[i].c_str(), "wav")){
  95. if(!audio.LoadWav2Char(wav_list[i].c_str(), &sampling_rate_)){
  96. LOG(ERROR)<<"Failed to load "<< wav_list[i];
  97. exit(-1);
  98. }
  99. }else if(is_target_file(wav_list[i].c_str(), "pcm")){
  100. if (!audio.LoadPcmwav2Char(wav_list[i].c_str(), &sampling_rate_)){
  101. LOG(ERROR)<<"Failed to load "<< wav_list[i];
  102. exit(-1);
  103. }
  104. }else{
  105. if (!audio.FfmpegLoad(wav_list[i].c_str(), true)){
  106. LOG(ERROR)<<"Failed to load "<< wav_list[i];
  107. exit(-1);
  108. }
  109. }
  110. char* speech_buff = audio.GetSpeechChar();
  111. int buff_len = audio.GetSpeechLen()*2;
  112. int step = 1600*2;
  113. bool is_final = false;
  114. string online_res="";
  115. string tpass_res="";
  116. std::vector<std::vector<string>> punc_cache(2);
  117. for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
  118. if (sample_offset + step >= buff_len - 1) {
  119. step = buff_len - sample_offset;
  120. is_final = true;
  121. } else {
  122. is_final = false;
  123. }
  124. gettimeofday(&start, NULL);
  125. FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle, speech_buff+sample_offset, step, punc_cache, is_final, sampling_rate_, "pcm", (ASR_TYPE)asr_mode_);
  126. gettimeofday(&end, NULL);
  127. seconds = (end.tv_sec - start.tv_sec);
  128. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  129. n_total_time += taking_micros;
  130. if (result)
  131. {
  132. string online_msg = FunASRGetResult(result, 0);
  133. online_res += online_msg;
  134. if(online_msg != ""){
  135. LOG(INFO)<< wav_ids[i] <<" : "<<online_msg;
  136. }
  137. string tpass_msg = FunASRGetTpassResult(result, 0);
  138. tpass_res += tpass_msg;
  139. if(tpass_msg != ""){
  140. LOG(INFO)<< wav_ids[i] <<" offline results : "<<tpass_msg;
  141. }
  142. float snippet_time = FunASRGetRetSnippetTime(result);
  143. n_total_length += snippet_time;
  144. FunASRFreeResult(result);
  145. }
  146. else
  147. {
  148. LOG(ERROR) << ("No return data!\n");
  149. }
  150. }
  151. if(asr_mode_ == 2){
  152. LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final online results "<<" : "<<online_res;
  153. }
  154. if(asr_mode_==1){
  155. LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final online results "<<" : "<<tpass_res;
  156. }
  157. if(asr_mode_ == 0 || asr_mode_==2){
  158. LOG(INFO) <<"Thread: " << this_thread::get_id() <<" " << wav_ids[i] << " Final offline results " <<" : "<<tpass_res;
  159. }
  160. }
  161. {
  162. lock_guard<mutex> guard(mtx);
  163. *total_length += n_total_length;
  164. if(*total_time < n_total_time){
  165. *total_time = n_total_time;
  166. }
  167. }
  168. FunTpassOnlineUninit(tpass_online_handle);
  169. }
  170. int main(int argc, char** argv)
  171. {
  172. google::InitGoogleLogging(argv[0]);
  173. FLAGS_logtostderr = true;
  174. TCLAP::CmdLine cmd("funasr-onnx-2pass", ' ', "1.0");
  175. TCLAP::ValueArg<std::string> offline_model_dir("", OFFLINE_MODEL_DIR, "the asr offline model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  176. TCLAP::ValueArg<std::string> online_model_dir("", ONLINE_MODEL_DIR, "the asr online model path, which contains encoder.onnx, decoder.onnx, config.yaml, am.mvn", true, "", "string");
  177. TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "false", "string");
  178. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad online model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  179. TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "false (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");
  180. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc online model path, which contains model.onnx, punc.yaml", false, "", "string");
  181. TCLAP::ValueArg<std::string> punc_quant("", PUNC_QUANT, "false (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");
  182. TCLAP::ValueArg<std::string> asr_mode("", ASR_MODE, "offline, online, 2pass", false, "2pass", "string");
  183. TCLAP::ValueArg<std::int32_t> onnx_thread("", "onnx-inter-thread", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
  184. 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");
  185. cmd.add(offline_model_dir);
  186. cmd.add(online_model_dir);
  187. cmd.add(quantize);
  188. cmd.add(vad_dir);
  189. cmd.add(vad_quant);
  190. cmd.add(punc_dir);
  191. cmd.add(punc_quant);
  192. cmd.add(wav_path);
  193. cmd.add(asr_mode);
  194. cmd.add(onnx_thread);
  195. cmd.parse(argc, argv);
  196. std::map<std::string, std::string> model_path;
  197. GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
  198. GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
  199. GetValue(quantize, QUANTIZE, model_path);
  200. GetValue(vad_dir, VAD_DIR, model_path);
  201. GetValue(vad_quant, VAD_QUANT, model_path);
  202. GetValue(punc_dir, PUNC_DIR, model_path);
  203. GetValue(punc_quant, PUNC_QUANT, model_path);
  204. GetValue(wav_path, WAV_PATH, model_path);
  205. GetValue(asr_mode, ASR_MODE, model_path);
  206. struct timeval start, end;
  207. gettimeofday(&start, NULL);
  208. int thread_num = onnx_thread.getValue();
  209. int asr_mode_ = -1;
  210. if(model_path[ASR_MODE] == "offline"){
  211. asr_mode_ = 0;
  212. }else if(model_path[ASR_MODE] == "online"){
  213. asr_mode_ = 1;
  214. }else if(model_path[ASR_MODE] == "2pass"){
  215. asr_mode_ = 2;
  216. }else{
  217. LOG(ERROR) << "Wrong asr-mode : " << model_path[ASR_MODE];
  218. exit(-1);
  219. }
  220. FUNASR_HANDLE tpass_hanlde=FunTpassInit(model_path, thread_num);
  221. if (!tpass_hanlde)
  222. {
  223. LOG(ERROR) << "FunTpassInit init failed";
  224. exit(-1);
  225. }
  226. gettimeofday(&end, NULL);
  227. long seconds = (end.tv_sec - start.tv_sec);
  228. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  229. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  230. // read wav_path
  231. vector<string> wav_list;
  232. vector<string> wav_ids;
  233. string default_id = "wav_default_id";
  234. string wav_path_ = model_path.at(WAV_PATH);
  235. if(is_target_file(wav_path_, "scp")){
  236. ifstream in(wav_path_);
  237. if (!in.is_open()) {
  238. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  239. return 0;
  240. }
  241. string line;
  242. while(getline(in, line))
  243. {
  244. istringstream iss(line);
  245. string column1, column2;
  246. iss >> column1 >> column2;
  247. wav_list.emplace_back(column2);
  248. wav_ids.emplace_back(column1);
  249. }
  250. in.close();
  251. }else{
  252. wav_list.emplace_back(wav_path_);
  253. wav_ids.emplace_back(default_id);
  254. }
  255. std::vector<int> chunk_size = {5,10,5};
  256. // 多线程测试
  257. float total_length = 0.0f;
  258. long total_time = 0;
  259. std::vector<std::thread> threads;
  260. int rtf_threds = 5;
  261. for (int i = 0; i < rtf_threds; i++)
  262. {
  263. threads.emplace_back(thread(runReg, tpass_hanlde, chunk_size, wav_list, wav_ids, &total_length, &total_time, i, (ASR_TYPE)asr_mode_));
  264. }
  265. for (auto& thread : threads)
  266. {
  267. thread.join();
  268. }
  269. LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
  270. LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
  271. LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
  272. LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
  273. FunTpassUninit(tpass_hanlde);
  274. return 0;
  275. }