funasr-onnx-offline-rtf.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "libfunasrapi.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,
  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=FunASRRecogFile(asr_handle, wav_list[0].c_str(), RASR_NONE, NULL);
  35. }
  36. while (true) {
  37. // 使用原子变量获取索引并递增
  38. int i = wav_index.fetch_add(1);
  39. if (i >= wav_list.size()) {
  40. break;
  41. }
  42. gettimeofday(&start, NULL);
  43. FUNASR_RESULT result=FunASRRecogFile(asr_handle, wav_list[i].c_str(), RASR_NONE, NULL);
  44. gettimeofday(&end, NULL);
  45. seconds = (end.tv_sec - start.tv_sec);
  46. long taking_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  47. n_total_time += taking_micros;
  48. if(result){
  49. string msg = FunASRGetResult(result, 0);
  50. LOG(INFO) << "Thread: " << this_thread::get_id() <<" Result: " << msg.c_str();
  51. float snippet_time = FunASRGetRetSnippetTime(result);
  52. n_total_length += snippet_time;
  53. FunASRFreeResult(result);
  54. }else{
  55. LOG(ERROR) << ("No return data!\n");
  56. }
  57. }
  58. {
  59. lock_guard<mutex> guard(mtx);
  60. *total_length += n_total_length;
  61. if(*total_time < n_total_time){
  62. *total_time = n_total_time;
  63. }
  64. }
  65. }
  66. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  67. {
  68. if (value_arg.isSet()){
  69. model_path.insert({key, value_arg.getValue()});
  70. LOG(INFO)<< key << " : " << value_arg.getValue();
  71. }
  72. }
  73. int main(int argc, char *argv[])
  74. {
  75. google::InitGoogleLogging(argv[0]);
  76. FLAGS_logtostderr = true;
  77. TCLAP::CmdLine cmd("funasr-onnx-offline-rtf", ' ', "1.0");
  78. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
  79. 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");
  80. TCLAP::ValueArg<std::string> wav_scp("", WAV_SCP, "wave scp path", true, "", "string");
  81. TCLAP::ValueArg<std::int32_t> thread_num("", THREAD_NUM, "multi-thread num for rtf", true, 0, "int32_t");
  82. cmd.add(model_dir);
  83. cmd.add(quantize);
  84. cmd.add(wav_scp);
  85. cmd.add(thread_num);
  86. cmd.parse(argc, argv);
  87. std::map<std::string, std::string> model_path;
  88. GetValue(model_dir, MODEL_DIR, model_path);
  89. GetValue(quantize, QUANTIZE, model_path);
  90. GetValue(wav_scp, WAV_SCP, model_path);
  91. struct timeval start, end;
  92. gettimeofday(&start, NULL);
  93. FUNASR_HANDLE asr_handle=FunASRInit(model_path, 1);
  94. if (!asr_handle)
  95. {
  96. LOG(ERROR) << "FunASR init failed";
  97. exit(-1);
  98. }
  99. gettimeofday(&end, NULL);
  100. long seconds = (end.tv_sec - start.tv_sec);
  101. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  102. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  103. // read wav_scp
  104. vector<string> wav_list;
  105. if(model_path.find(WAV_SCP)!=model_path.end()){
  106. ifstream in(model_path.at(WAV_SCP));
  107. if (!in.is_open()) {
  108. LOG(ERROR) << "Failed to open file: " << model_path.at(WAV_SCP);
  109. return 0;
  110. }
  111. string line;
  112. while(getline(in, line))
  113. {
  114. istringstream iss(line);
  115. string column1, column2;
  116. iss >> column1 >> column2;
  117. wav_list.emplace_back(column2);
  118. }
  119. in.close();
  120. }
  121. // 多线程测试
  122. float total_length = 0.0f;
  123. long total_time = 0;
  124. std::vector<std::thread> threads;
  125. int rtf_threds = thread_num.getValue();
  126. for (int i = 0; i < rtf_threds; i++)
  127. {
  128. threads.emplace_back(thread(runReg, asr_handle, wav_list, &total_length, &total_time, i));
  129. }
  130. for (auto& thread : threads)
  131. {
  132. thread.join();
  133. }
  134. LOG(INFO) << "total_time_wav " << (long)(total_length * 1000) << " ms";
  135. LOG(INFO) << "total_time_comput " << total_time / 1000 << " ms";
  136. LOG(INFO) << "total_rtf " << (double)total_time/ (total_length*1000000);
  137. LOG(INFO) << "speedup " << 1.0/((double)total_time/ (total_length*1000000));
  138. FunASRUninit(asr_handle);
  139. return 0;
  140. }