funasr-onnx-offline-rtf.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. using namespace std;
  23. std::atomic<int> wav_index(0);
  24. std::mutex mtx;
  25. void runReg(FUNASR_HANDLE asr_handle, vector<string> wav_list, vector<string> wav_ids,
  26. float* total_length, long* total_time, int core_id) {
  27. struct timeval start, end;
  28. long seconds = 0;
  29. float n_total_length = 0.0f;
  30. long n_total_time = 0;
  31. // warm up
  32. for (size_t i = 0; i < 1; i++)
  33. {
  34. FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[0].c_str(), RASR_NONE, NULL, 16000);
  35. if(result){
  36. FunASRFreeResult(result);
  37. }
  38. }
  39. while (true) {
  40. // 使用原子变量获取索引并递增
  41. int i = wav_index.fetch_add(1);
  42. if (i >= wav_list.size()) {
  43. break;
  44. }
  45. gettimeofday(&start, NULL);
  46. FUNASR_RESULT result=FunOfflineInfer(asr_handle, wav_list[i].c_str(), RASR_NONE, NULL, 16000);
  47. gettimeofday(&end, NULL);
  48. seconds = (end.tv_sec - start.tv_sec);
  49. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  50. n_total_time += taking_micros;
  51. if(result){
  52. string msg = FunASRGetResult(result, 0);
  53. LOG(INFO) << "Thread: " << this_thread::get_id() << "," << wav_ids[i] << " : " << msg;
  54. float snippet_time = FunASRGetRetSnippetTime(result);
  55. n_total_length += snippet_time;
  56. FunASRFreeResult(result);
  57. }else{
  58. LOG(ERROR) << wav_ids[i] << (": No return data!\n");
  59. }
  60. }
  61. {
  62. lock_guard<mutex> guard(mtx);
  63. *total_length += n_total_length;
  64. if(*total_time < n_total_time){
  65. *total_time = n_total_time;
  66. }
  67. }
  68. }
  69. bool is_target_file(const std::string& filename, const std::string target) {
  70. std::size_t pos = filename.find_last_of(".");
  71. if (pos == std::string::npos) {
  72. return false;
  73. }
  74. std::string extension = filename.substr(pos + 1);
  75. return (extension == target);
  76. }
  77. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  78. {
  79. if (value_arg.isSet()){
  80. model_path.insert({key, value_arg.getValue()});
  81. LOG(INFO)<< key << " : " << value_arg.getValue();
  82. }
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. google::InitGoogleLogging(argv[0]);
  87. FLAGS_logtostderr = true;
  88. TCLAP::CmdLine cmd("funasr-onnx-offline-rtf", ' ', "1.0");
  89. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  90. 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");
  91. TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
  92. 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, "false", "string");
  93. TCLAP::ValueArg<std::string> punc_dir("", PUNC_DIR, "the punc model path, which contains model.onnx, punc.yaml", false, "", "string");
  94. 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, "false", "string");
  95. 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");
  96. TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", true, 0, "int32_t");
  97. cmd.add(model_dir);
  98. cmd.add(quantize);
  99. cmd.add(vad_dir);
  100. cmd.add(vad_quant);
  101. cmd.add(punc_dir);
  102. cmd.add(punc_quant);
  103. cmd.add(wav_path);
  104. cmd.add(thread_num);
  105. cmd.parse(argc, argv);
  106. std::map<std::string, std::string> model_path;
  107. GetValue(model_dir, MODEL_DIR, model_path);
  108. GetValue(quantize, QUANTIZE, model_path);
  109. GetValue(vad_dir, VAD_DIR, model_path);
  110. GetValue(vad_quant, VAD_QUANT, model_path);
  111. GetValue(punc_dir, PUNC_DIR, model_path);
  112. GetValue(punc_quant, PUNC_QUANT, model_path);
  113. GetValue(wav_path, WAV_PATH, model_path);
  114. struct timeval start, end;
  115. gettimeofday(&start, NULL);
  116. FUNASR_HANDLE asr_handle=FunOfflineInit(model_path, 1);
  117. if (!asr_handle)
  118. {
  119. LOG(ERROR) << "FunASR init failed";
  120. exit(-1);
  121. }
  122. gettimeofday(&end, NULL);
  123. long seconds = (end.tv_sec - start.tv_sec);
  124. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  125. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  126. // read wav_path
  127. vector<string> wav_list;
  128. vector<string> wav_ids;
  129. string default_id = "wav_default_id";
  130. string wav_path_ = model_path.at(WAV_PATH);
  131. if(is_target_file(wav_path_, "wav") || is_target_file(wav_path_, "pcm")){
  132. wav_list.emplace_back(wav_path_);
  133. wav_ids.emplace_back(default_id);
  134. }
  135. else if(is_target_file(wav_path_, "scp")){
  136. ifstream in(wav_path_);
  137. if (!in.is_open()) {
  138. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP) ;
  139. return 0;
  140. }
  141. string line;
  142. while(getline(in, line))
  143. {
  144. istringstream iss(line);
  145. string column1, column2;
  146. iss >> column1 >> column2;
  147. wav_list.emplace_back(column2);
  148. wav_ids.emplace_back(column1);
  149. }
  150. in.close();
  151. }else{
  152. LOG(ERROR)<<"Please check the wav extension!";
  153. exit(-1);
  154. }
  155. // 多线程测试
  156. float total_length = 0.0f;
  157. long total_time = 0;
  158. std::vector<std::thread> threads;
  159. int rtf_threds = thread_num.getValue();
  160. for (int i = 0; i < rtf_threds; i++)
  161. {
  162. threads.emplace_back(thread(runReg, asr_handle, wav_list, wav_ids, &total_length, &total_time, i));
  163. }
  164. for (auto& thread : threads)
  165. {
  166. thread.join();
  167. }
  168. LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
  169. LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
  170. LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
  171. LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
  172. FunOfflineUninit(asr_handle);
  173. return 0;
  174. }