paraformer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  3. * MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. #include "precomp.h"
  6. #include "paraformer.h"
  7. #include "encode_converter.h"
  8. #include <cstddef>
  9. using namespace std;
  10. namespace funasr {
  11. Paraformer::Paraformer()
  12. :use_hotword(false),
  13. env_(ORT_LOGGING_LEVEL_ERROR, "paraformer"),session_options_{},
  14. hw_env_(ORT_LOGGING_LEVEL_ERROR, "paraformer_hw"),hw_session_options{} {
  15. }
  16. // offline
  17. void Paraformer::InitAsr(const std::string &am_model, const std::string &am_cmvn, const std::string &am_config, int thread_num){
  18. LoadConfigFromYaml(am_config.c_str());
  19. // knf options
  20. fbank_opts_.frame_opts.dither = 0;
  21. fbank_opts_.mel_opts.num_bins = n_mels;
  22. fbank_opts_.frame_opts.samp_freq = asr_sample_rate;
  23. fbank_opts_.frame_opts.window_type = window_type;
  24. fbank_opts_.frame_opts.frame_shift_ms = frame_shift;
  25. fbank_opts_.frame_opts.frame_length_ms = frame_length;
  26. fbank_opts_.energy_floor = 0;
  27. fbank_opts_.mel_opts.debug_mel = false;
  28. // fbank_ = std::make_unique<knf::OnlineFbank>(fbank_opts);
  29. // session_options_.SetInterOpNumThreads(1);
  30. session_options_.SetIntraOpNumThreads(thread_num);
  31. session_options_.SetGraphOptimizationLevel(ORT_ENABLE_ALL);
  32. // DisableCpuMemArena can improve performance
  33. session_options_.DisableCpuMemArena();
  34. try {
  35. m_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(am_model).c_str(), session_options_);
  36. LOG(INFO) << "Successfully load model from " << am_model;
  37. } catch (std::exception const &e) {
  38. LOG(ERROR) << "Error when load am onnx model: " << e.what();
  39. exit(-1);
  40. }
  41. string strName;
  42. GetInputName(m_session_.get(), strName);
  43. m_strInputNames.push_back(strName.c_str());
  44. GetInputName(m_session_.get(), strName,1);
  45. m_strInputNames.push_back(strName);
  46. if (use_hotword) {
  47. GetInputName(m_session_.get(), strName, 2);
  48. m_strInputNames.push_back(strName);
  49. }
  50. size_t numOutputNodes = m_session_->GetOutputCount();
  51. for(int index=0; index<numOutputNodes; index++){
  52. GetOutputName(m_session_.get(), strName, index);
  53. m_strOutputNames.push_back(strName);
  54. }
  55. for (auto& item : m_strInputNames)
  56. m_szInputNames.push_back(item.c_str());
  57. for (auto& item : m_strOutputNames)
  58. m_szOutputNames.push_back(item.c_str());
  59. vocab = new Vocab(am_config.c_str());
  60. phone_set_ = new PhoneSet(am_config.c_str());
  61. LoadCmvn(am_cmvn.c_str());
  62. }
  63. // online
  64. void Paraformer::InitAsr(const std::string &en_model, const std::string &de_model, const std::string &am_cmvn, const std::string &am_config, int thread_num){
  65. LoadOnlineConfigFromYaml(am_config.c_str());
  66. // knf options
  67. fbank_opts_.frame_opts.dither = 0;
  68. fbank_opts_.mel_opts.num_bins = n_mels;
  69. fbank_opts_.frame_opts.samp_freq = asr_sample_rate;
  70. fbank_opts_.frame_opts.window_type = window_type;
  71. fbank_opts_.frame_opts.frame_shift_ms = frame_shift;
  72. fbank_opts_.frame_opts.frame_length_ms = frame_length;
  73. fbank_opts_.energy_floor = 0;
  74. fbank_opts_.mel_opts.debug_mel = false;
  75. // session_options_.SetInterOpNumThreads(1);
  76. session_options_.SetIntraOpNumThreads(thread_num);
  77. session_options_.SetGraphOptimizationLevel(ORT_ENABLE_ALL);
  78. // DisableCpuMemArena can improve performance
  79. session_options_.DisableCpuMemArena();
  80. try {
  81. encoder_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(en_model).c_str(), session_options_);
  82. LOG(INFO) << "Successfully load model from " << en_model;
  83. } catch (std::exception const &e) {
  84. LOG(ERROR) << "Error when load am encoder model: " << e.what();
  85. exit(-1);
  86. }
  87. try {
  88. decoder_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(de_model).c_str(), session_options_);
  89. LOG(INFO) << "Successfully load model from " << de_model;
  90. } catch (std::exception const &e) {
  91. LOG(ERROR) << "Error when load am decoder model: " << e.what();
  92. exit(-1);
  93. }
  94. // encoder
  95. string strName;
  96. GetInputName(encoder_session_.get(), strName);
  97. en_strInputNames.push_back(strName.c_str());
  98. GetInputName(encoder_session_.get(), strName,1);
  99. en_strInputNames.push_back(strName);
  100. GetOutputName(encoder_session_.get(), strName);
  101. en_strOutputNames.push_back(strName);
  102. GetOutputName(encoder_session_.get(), strName,1);
  103. en_strOutputNames.push_back(strName);
  104. GetOutputName(encoder_session_.get(), strName,2);
  105. en_strOutputNames.push_back(strName);
  106. for (auto& item : en_strInputNames)
  107. en_szInputNames_.push_back(item.c_str());
  108. for (auto& item : en_strOutputNames)
  109. en_szOutputNames_.push_back(item.c_str());
  110. // decoder
  111. int de_input_len = 4 + fsmn_layers;
  112. int de_out_len = 2 + fsmn_layers;
  113. for(int i=0;i<de_input_len; i++){
  114. GetInputName(decoder_session_.get(), strName, i);
  115. de_strInputNames.push_back(strName.c_str());
  116. }
  117. for(int i=0;i<de_out_len; i++){
  118. GetOutputName(decoder_session_.get(), strName,i);
  119. de_strOutputNames.push_back(strName);
  120. }
  121. for (auto& item : de_strInputNames)
  122. de_szInputNames_.push_back(item.c_str());
  123. for (auto& item : de_strOutputNames)
  124. de_szOutputNames_.push_back(item.c_str());
  125. vocab = new Vocab(am_config.c_str());
  126. phone_set_ = new PhoneSet(am_config.c_str());
  127. LoadCmvn(am_cmvn.c_str());
  128. }
  129. // 2pass
  130. void Paraformer::InitAsr(const std::string &am_model, const std::string &en_model, const std::string &de_model, const std::string &am_cmvn, const std::string &am_config, int thread_num){
  131. // online
  132. InitAsr(en_model, de_model, am_cmvn, am_config, thread_num);
  133. // offline
  134. try {
  135. m_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(am_model).c_str(), session_options_);
  136. LOG(INFO) << "Successfully load model from " << am_model;
  137. } catch (std::exception const &e) {
  138. LOG(ERROR) << "Error when load am onnx model: " << e.what();
  139. exit(-1);
  140. }
  141. string strName;
  142. GetInputName(m_session_.get(), strName);
  143. m_strInputNames.push_back(strName.c_str());
  144. GetInputName(m_session_.get(), strName,1);
  145. m_strInputNames.push_back(strName);
  146. if (use_hotword) {
  147. GetInputName(m_session_.get(), strName, 2);
  148. m_strInputNames.push_back(strName);
  149. }
  150. // support time stamp
  151. size_t numOutputNodes = m_session_->GetOutputCount();
  152. for(int index=0; index<numOutputNodes; index++){
  153. GetOutputName(m_session_.get(), strName, index);
  154. m_strOutputNames.push_back(strName);
  155. }
  156. for (auto& item : m_strInputNames)
  157. m_szInputNames.push_back(item.c_str());
  158. for (auto& item : m_strOutputNames)
  159. m_szOutputNames.push_back(item.c_str());
  160. }
  161. void Paraformer::InitLm(const std::string &lm_file,
  162. const std::string &lm_cfg_file,
  163. const std::string &lex_file) {
  164. try {
  165. lm_ = std::shared_ptr<fst::Fst<fst::StdArc>>(
  166. fst::Fst<fst::StdArc>::Read(lm_file));
  167. if (lm_){
  168. if (vocab) { delete vocab; }
  169. vocab = new Vocab(lm_cfg_file.c_str(), lex_file.c_str());
  170. LOG(INFO) << "Successfully load lm file " << lm_file;
  171. }else{
  172. LOG(ERROR) << "Failed to load lm file " << lm_file;
  173. }
  174. } catch (std::exception const &e) {
  175. LOG(ERROR) << "Error when load lm file: " << e.what();
  176. exit(0);
  177. }
  178. }
  179. void Paraformer::LoadConfigFromYaml(const char* filename){
  180. YAML::Node config;
  181. try{
  182. config = YAML::LoadFile(filename);
  183. }catch(exception const &e){
  184. LOG(ERROR) << "Error loading file, yaml file error or not exist.";
  185. exit(-1);
  186. }
  187. try{
  188. YAML::Node frontend_conf = config["frontend_conf"];
  189. this->asr_sample_rate = frontend_conf["fs"].as<int>();
  190. YAML::Node lang_conf = config["lang"];
  191. if (lang_conf.IsDefined()){
  192. language = lang_conf.as<string>();
  193. }
  194. }catch(exception const &e){
  195. LOG(ERROR) << "Error when load argument from vad config YAML.";
  196. exit(-1);
  197. }
  198. }
  199. void Paraformer::LoadOnlineConfigFromYaml(const char* filename){
  200. YAML::Node config;
  201. try{
  202. config = YAML::LoadFile(filename);
  203. }catch(exception const &e){
  204. LOG(ERROR) << "Error loading file, yaml file error or not exist.";
  205. exit(-1);
  206. }
  207. try{
  208. YAML::Node frontend_conf = config["frontend_conf"];
  209. YAML::Node encoder_conf = config["encoder_conf"];
  210. YAML::Node decoder_conf = config["decoder_conf"];
  211. YAML::Node predictor_conf = config["predictor_conf"];
  212. this->window_type = frontend_conf["window"].as<string>();
  213. this->n_mels = frontend_conf["n_mels"].as<int>();
  214. this->frame_length = frontend_conf["frame_length"].as<int>();
  215. this->frame_shift = frontend_conf["frame_shift"].as<int>();
  216. this->lfr_m = frontend_conf["lfr_m"].as<int>();
  217. this->lfr_n = frontend_conf["lfr_n"].as<int>();
  218. this->encoder_size = encoder_conf["output_size"].as<int>();
  219. this->fsmn_dims = encoder_conf["output_size"].as<int>();
  220. this->fsmn_layers = decoder_conf["num_blocks"].as<int>();
  221. this->fsmn_lorder = decoder_conf["kernel_size"].as<int>()-1;
  222. this->cif_threshold = predictor_conf["threshold"].as<double>();
  223. this->tail_alphas = predictor_conf["tail_threshold"].as<double>();
  224. this->asr_sample_rate = frontend_conf["fs"].as<int>();
  225. }catch(exception const &e){
  226. LOG(ERROR) << "Error when load argument from vad config YAML.";
  227. exit(-1);
  228. }
  229. }
  230. void Paraformer::InitHwCompiler(const std::string &hw_model, int thread_num) {
  231. hw_session_options.SetIntraOpNumThreads(thread_num);
  232. hw_session_options.SetGraphOptimizationLevel(ORT_ENABLE_ALL);
  233. // DisableCpuMemArena can improve performance
  234. hw_session_options.DisableCpuMemArena();
  235. try {
  236. hw_m_session = std::make_unique<Ort::Session>(hw_env_, ORTSTRING(hw_model).c_str(), hw_session_options);
  237. LOG(INFO) << "Successfully load model from " << hw_model;
  238. } catch (std::exception const &e) {
  239. LOG(ERROR) << "Error when load hw compiler onnx model: " << e.what();
  240. exit(-1);
  241. }
  242. string strName;
  243. GetInputName(hw_m_session.get(), strName);
  244. hw_m_strInputNames.push_back(strName.c_str());
  245. //GetInputName(hw_m_session.get(), strName,1);
  246. //hw_m_strInputNames.push_back(strName);
  247. GetOutputName(hw_m_session.get(), strName);
  248. hw_m_strOutputNames.push_back(strName);
  249. for (auto& item : hw_m_strInputNames)
  250. hw_m_szInputNames.push_back(item.c_str());
  251. for (auto& item : hw_m_strOutputNames)
  252. hw_m_szOutputNames.push_back(item.c_str());
  253. // if init hotword compiler is called, this is a hotword paraformer model
  254. use_hotword = true;
  255. }
  256. void Paraformer::InitSegDict(const std::string &seg_dict_model) {
  257. seg_dict = new SegDict(seg_dict_model.c_str());
  258. }
  259. Paraformer::~Paraformer()
  260. {
  261. if(vocab){
  262. delete vocab;
  263. }
  264. if(seg_dict){
  265. delete seg_dict;
  266. }
  267. if(phone_set_){
  268. delete phone_set_;
  269. }
  270. }
  271. void Paraformer::StartUtterance()
  272. {
  273. }
  274. void Paraformer::EndUtterance()
  275. {
  276. }
  277. void Paraformer::Reset()
  278. {
  279. }
  280. void Paraformer::FbankKaldi(float sample_rate, const float* waves, int len, std::vector<std::vector<float>> &asr_feats) {
  281. knf::OnlineFbank fbank_(fbank_opts_);
  282. std::vector<float> buf(len);
  283. for (int32_t i = 0; i != len; ++i) {
  284. buf[i] = waves[i] * 32768;
  285. }
  286. fbank_.AcceptWaveform(sample_rate, buf.data(), buf.size());
  287. int32_t frames = fbank_.NumFramesReady();
  288. for (int32_t i = 0; i != frames; ++i) {
  289. const float *frame = fbank_.GetFrame(i);
  290. std::vector<float> frame_vector(frame, frame + fbank_opts_.mel_opts.num_bins);
  291. asr_feats.emplace_back(frame_vector);
  292. }
  293. }
  294. void Paraformer::LoadCmvn(const char *filename)
  295. {
  296. ifstream cmvn_stream(filename);
  297. if (!cmvn_stream.is_open()) {
  298. LOG(ERROR) << "Failed to open file: " << filename;
  299. exit(-1);
  300. }
  301. string line;
  302. while (getline(cmvn_stream, line)) {
  303. istringstream iss(line);
  304. vector<string> line_item{istream_iterator<string>{iss}, istream_iterator<string>{}};
  305. if (line_item[0] == "<AddShift>") {
  306. getline(cmvn_stream, line);
  307. istringstream means_lines_stream(line);
  308. vector<string> means_lines{istream_iterator<string>{means_lines_stream}, istream_iterator<string>{}};
  309. if (means_lines[0] == "<LearnRateCoef>") {
  310. for (int j = 3; j < means_lines.size() - 1; j++) {
  311. means_list_.push_back(stof(means_lines[j]));
  312. }
  313. continue;
  314. }
  315. }
  316. else if (line_item[0] == "<Rescale>") {
  317. getline(cmvn_stream, line);
  318. istringstream vars_lines_stream(line);
  319. vector<string> vars_lines{istream_iterator<string>{vars_lines_stream}, istream_iterator<string>{}};
  320. if (vars_lines[0] == "<LearnRateCoef>") {
  321. for (int j = 3; j < vars_lines.size() - 1; j++) {
  322. vars_list_.push_back(stof(vars_lines[j])*scale);
  323. }
  324. continue;
  325. }
  326. }
  327. }
  328. }
  329. string Paraformer::GreedySearch(float * in, int n_len, int64_t token_nums, bool is_stamp, std::vector<float> us_alphas, std::vector<float> us_cif_peak)
  330. {
  331. vector<int> hyps;
  332. int Tmax = n_len;
  333. for (int i = 0; i < Tmax; i++) {
  334. int max_idx;
  335. float max_val;
  336. FindMax(in + i * token_nums, token_nums, max_val, max_idx);
  337. hyps.push_back(max_idx);
  338. }
  339. if(!is_stamp){
  340. return vocab->Vector2StringV2(hyps, language);
  341. }else{
  342. std::vector<string> char_list;
  343. std::vector<std::vector<float>> timestamp_list;
  344. std::string res_str;
  345. vocab->Vector2String(hyps, char_list);
  346. std::vector<string> raw_char(char_list);
  347. TimestampOnnx(us_alphas, us_cif_peak, char_list, res_str, timestamp_list);
  348. return PostProcess(raw_char, timestamp_list);
  349. }
  350. }
  351. string Paraformer::BeamSearch(WfstDecoder* &wfst_decoder, float *in, int len, int64_t token_nums)
  352. {
  353. return wfst_decoder->Search(in, len, token_nums);
  354. }
  355. string Paraformer::FinalizeDecode(WfstDecoder* &wfst_decoder,
  356. bool is_stamp, std::vector<float> us_alphas, std::vector<float> us_cif_peak)
  357. {
  358. return wfst_decoder->FinalizeDecode(is_stamp, us_alphas, us_cif_peak);
  359. }
  360. void Paraformer::LfrCmvn(std::vector<std::vector<float>> &asr_feats) {
  361. std::vector<std::vector<float>> out_feats;
  362. int T = asr_feats.size();
  363. int T_lrf = ceil(1.0 * T / lfr_n);
  364. // Pad frames at start(copy first frame)
  365. for (int i = 0; i < (lfr_m - 1) / 2; i++) {
  366. asr_feats.insert(asr_feats.begin(), asr_feats[0]);
  367. }
  368. // Merge lfr_m frames as one,lfr_n frames per window
  369. T = T + (lfr_m - 1) / 2;
  370. std::vector<float> p;
  371. for (int i = 0; i < T_lrf; i++) {
  372. if (lfr_m <= T - i * lfr_n) {
  373. for (int j = 0; j < lfr_m; j++) {
  374. p.insert(p.end(), asr_feats[i * lfr_n + j].begin(), asr_feats[i * lfr_n + j].end());
  375. }
  376. out_feats.emplace_back(p);
  377. p.clear();
  378. } else {
  379. // Fill to lfr_m frames at last window if less than lfr_m frames (copy last frame)
  380. int num_padding = lfr_m - (T - i * lfr_n);
  381. for (int j = 0; j < (asr_feats.size() - i * lfr_n); j++) {
  382. p.insert(p.end(), asr_feats[i * lfr_n + j].begin(), asr_feats[i * lfr_n + j].end());
  383. }
  384. for (int j = 0; j < num_padding; j++) {
  385. p.insert(p.end(), asr_feats[asr_feats.size() - 1].begin(), asr_feats[asr_feats.size() - 1].end());
  386. }
  387. out_feats.emplace_back(p);
  388. p.clear();
  389. }
  390. }
  391. // Apply cmvn
  392. for (auto &out_feat: out_feats) {
  393. for (int j = 0; j < means_list_.size(); j++) {
  394. out_feat[j] = (out_feat[j] + means_list_[j]) * vars_list_[j];
  395. }
  396. }
  397. asr_feats = out_feats;
  398. }
  399. string Paraformer::Forward(float* din, int len, bool input_finished, const std::vector<std::vector<float>> &hw_emb, void* decoder_handle)
  400. {
  401. WfstDecoder* wfst_decoder = (WfstDecoder*)decoder_handle;
  402. int32_t in_feat_dim = fbank_opts_.mel_opts.num_bins;
  403. std::vector<std::vector<float>> asr_feats;
  404. FbankKaldi(MODEL_SAMPLE_RATE, din, len, asr_feats);
  405. if(asr_feats.size() == 0){
  406. return "";
  407. }
  408. LfrCmvn(asr_feats);
  409. int32_t feat_dim = lfr_m*in_feat_dim;
  410. int32_t num_frames = asr_feats.size();
  411. std::vector<float> wav_feats;
  412. for (const auto &frame_feat: asr_feats) {
  413. wav_feats.insert(wav_feats.end(), frame_feat.begin(), frame_feat.end());
  414. }
  415. #ifdef _WIN_X86
  416. Ort::MemoryInfo m_memoryInfo = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
  417. #else
  418. Ort::MemoryInfo m_memoryInfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
  419. #endif
  420. const int64_t input_shape_[3] = {1, num_frames, feat_dim};
  421. Ort::Value onnx_feats = Ort::Value::CreateTensor<float>(m_memoryInfo,
  422. wav_feats.data(),
  423. wav_feats.size(),
  424. input_shape_,
  425. 3);
  426. const int64_t paraformer_length_shape[1] = {1};
  427. std::vector<int32_t> paraformer_length;
  428. paraformer_length.emplace_back(num_frames);
  429. Ort::Value onnx_feats_len = Ort::Value::CreateTensor<int32_t>(
  430. m_memoryInfo, paraformer_length.data(), paraformer_length.size(), paraformer_length_shape, 1);
  431. std::vector<Ort::Value> input_onnx;
  432. input_onnx.emplace_back(std::move(onnx_feats));
  433. input_onnx.emplace_back(std::move(onnx_feats_len));
  434. std::vector<float> embedding;
  435. try{
  436. if (use_hotword) {
  437. if(hw_emb.size()<=0){
  438. LOG(ERROR) << "hw_emb is null";
  439. return "";
  440. }
  441. //PrintMat(hw_emb, "input_clas_emb");
  442. const int64_t hotword_shape[3] = {1, static_cast<int64_t>(hw_emb.size()), static_cast<int64_t>(hw_emb[0].size())};
  443. embedding.reserve(hw_emb.size() * hw_emb[0].size());
  444. for (auto item : hw_emb) {
  445. embedding.insert(embedding.end(), item.begin(), item.end());
  446. }
  447. //LOG(INFO) << "hotword shape " << hotword_shape[0] << " " << hotword_shape[1] << " " << hotword_shape[2] << " size " << embedding.size();
  448. Ort::Value onnx_hw_emb = Ort::Value::CreateTensor<float>(
  449. m_memoryInfo, embedding.data(), embedding.size(), hotword_shape, 3);
  450. input_onnx.emplace_back(std::move(onnx_hw_emb));
  451. }
  452. }catch (std::exception const &e)
  453. {
  454. LOG(ERROR)<<e.what();
  455. return "";
  456. }
  457. string result="";
  458. try {
  459. auto outputTensor = m_session_->Run(Ort::RunOptions{nullptr}, m_szInputNames.data(), input_onnx.data(), input_onnx.size(), m_szOutputNames.data(), m_szOutputNames.size());
  460. std::vector<int64_t> outputShape = outputTensor[0].GetTensorTypeAndShapeInfo().GetShape();
  461. //LOG(INFO) << "paraformer out shape " << outputShape[0] << " " << outputShape[1] << " " << outputShape[2];
  462. int64_t outputCount = std::accumulate(outputShape.begin(), outputShape.end(), 1, std::multiplies<int64_t>());
  463. float* floatData = outputTensor[0].GetTensorMutableData<float>();
  464. auto encoder_out_lens = outputTensor[1].GetTensorMutableData<int64_t>();
  465. // timestamp
  466. if(outputTensor.size() == 4){
  467. std::vector<int64_t> us_alphas_shape = outputTensor[2].GetTensorTypeAndShapeInfo().GetShape();
  468. float* us_alphas_data = outputTensor[2].GetTensorMutableData<float>();
  469. std::vector<float> us_alphas(us_alphas_shape[1]);
  470. for (int i = 0; i < us_alphas_shape[1]; i++) {
  471. us_alphas[i] = us_alphas_data[i];
  472. }
  473. std::vector<int64_t> us_peaks_shape = outputTensor[3].GetTensorTypeAndShapeInfo().GetShape();
  474. float* us_peaks_data = outputTensor[3].GetTensorMutableData<float>();
  475. std::vector<float> us_peaks(us_peaks_shape[1]);
  476. for (int i = 0; i < us_peaks_shape[1]; i++) {
  477. us_peaks[i] = us_peaks_data[i];
  478. }
  479. if (lm_ == nullptr) {
  480. result = GreedySearch(floatData, *encoder_out_lens, outputShape[2], true, us_alphas, us_peaks);
  481. } else {
  482. result = BeamSearch(wfst_decoder, floatData, *encoder_out_lens, outputShape[2]);
  483. if (input_finished) {
  484. result = FinalizeDecode(wfst_decoder, true, us_alphas, us_peaks);
  485. }
  486. }
  487. }else{
  488. if (lm_ == nullptr) {
  489. result = GreedySearch(floatData, *encoder_out_lens, outputShape[2]);
  490. } else {
  491. result = BeamSearch(wfst_decoder, floatData, *encoder_out_lens, outputShape[2]);
  492. if (input_finished) {
  493. result = FinalizeDecode(wfst_decoder);
  494. }
  495. }
  496. }
  497. }
  498. catch (std::exception const &e)
  499. {
  500. LOG(ERROR)<<e.what();
  501. }
  502. return result;
  503. }
  504. std::vector<std::vector<float>> Paraformer::CompileHotwordEmbedding(std::string &hotwords) {
  505. int embedding_dim = encoder_size;
  506. std::vector<std::vector<float>> hw_emb;
  507. if (!use_hotword) {
  508. std::vector<float> vec(embedding_dim, 0);
  509. hw_emb.push_back(vec);
  510. return hw_emb;
  511. }
  512. int max_hotword_len = 10;
  513. std::vector<int32_t> hotword_matrix;
  514. std::vector<int32_t> lengths;
  515. int hotword_size = 1;
  516. int real_hw_size = 0;
  517. if (!hotwords.empty()) {
  518. std::vector<std::string> hotword_array = split(hotwords, ' ');
  519. hotword_size = hotword_array.size() + 1;
  520. hotword_matrix.reserve(hotword_size * max_hotword_len);
  521. for (auto hotword : hotword_array) {
  522. std::vector<std::string> chars;
  523. if (EncodeConverter::IsAllChineseCharactor((const U8CHAR_T*)hotword.c_str(), hotword.size())) {
  524. KeepChineseCharacterAndSplit(hotword, chars);
  525. } else {
  526. // for english
  527. std::vector<std::string> words = split(hotword, ' ');
  528. for (auto word : words) {
  529. std::vector<string> tokens = seg_dict->GetTokensByWord(word);
  530. chars.insert(chars.end(), tokens.begin(), tokens.end());
  531. }
  532. }
  533. if(chars.size()==0){
  534. continue;
  535. }
  536. std::vector<int32_t> hw_vector(max_hotword_len, 0);
  537. int vector_len = std::min(max_hotword_len, (int)chars.size());
  538. int chs_oov = false;
  539. for (int i=0; i<vector_len; i++) {
  540. hw_vector[i] = phone_set_->String2Id(chars[i]);
  541. if(hw_vector[i] == -1){
  542. chs_oov = true;
  543. break;
  544. }
  545. }
  546. if(chs_oov){
  547. LOG(INFO) << "OOV: " << hotword;
  548. continue;
  549. }
  550. LOG(INFO) << hotword;
  551. lengths.push_back(vector_len);
  552. real_hw_size += 1;
  553. hotword_matrix.insert(hotword_matrix.end(), hw_vector.begin(), hw_vector.end());
  554. }
  555. hotword_size = real_hw_size + 1;
  556. }
  557. std::vector<int32_t> blank_vec(max_hotword_len, 0);
  558. blank_vec[0] = 1;
  559. hotword_matrix.insert(hotword_matrix.end(), blank_vec.begin(), blank_vec.end());
  560. lengths.push_back(1);
  561. #ifdef _WIN_X86
  562. Ort::MemoryInfo m_memoryInfo = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
  563. #else
  564. Ort::MemoryInfo m_memoryInfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
  565. #endif
  566. const int64_t input_shape_[2] = {hotword_size, max_hotword_len};
  567. Ort::Value onnx_hotword = Ort::Value::CreateTensor<int32_t>(m_memoryInfo,
  568. (int32_t*)hotword_matrix.data(),
  569. hotword_size * max_hotword_len,
  570. input_shape_,
  571. 2);
  572. LOG(INFO) << "clas shape " << hotword_size << " " << max_hotword_len << std::endl;
  573. std::vector<Ort::Value> input_onnx;
  574. input_onnx.emplace_back(std::move(onnx_hotword));
  575. std::vector<std::vector<float>> result;
  576. try {
  577. auto outputTensor = hw_m_session->Run(Ort::RunOptions{nullptr}, hw_m_szInputNames.data(), input_onnx.data(), input_onnx.size(), hw_m_szOutputNames.data(), hw_m_szOutputNames.size());
  578. std::vector<int64_t> outputShape = outputTensor[0].GetTensorTypeAndShapeInfo().GetShape();
  579. int64_t outputCount = std::accumulate(outputShape.begin(), outputShape.end(), 1, std::multiplies<int64_t>());
  580. float* floatData = outputTensor[0].GetTensorMutableData<float>(); // shape [max_hotword_len, hotword_size, dim]
  581. // get embedding by real hotword length
  582. assert(outputShape[0] == max_hotword_len);
  583. assert(outputShape[1] == hotword_size);
  584. embedding_dim = outputShape[2];
  585. for (int j = 0; j < hotword_size; j++)
  586. {
  587. int start_pos = hotword_size * (lengths[j] - 1) * embedding_dim + j * embedding_dim;
  588. std::vector<float> embedding;
  589. embedding.insert(embedding.begin(), floatData + start_pos, floatData + start_pos + embedding_dim);
  590. result.push_back(embedding);
  591. }
  592. }
  593. catch (std::exception const &e)
  594. {
  595. LOG(ERROR)<<e.what();
  596. }
  597. //PrintMat(result, "clas_embedding_output");
  598. return result;
  599. }
  600. Vocab* Paraformer::GetVocab()
  601. {
  602. return vocab;
  603. }
  604. PhoneSet* Paraformer::GetPhoneSet()
  605. {
  606. return phone_set_;
  607. }
  608. string Paraformer::Rescoring()
  609. {
  610. LOG(ERROR)<<"Not Imp!!!!!!";
  611. return "";
  612. }
  613. } // namespace funasr