NnetBatchDecoder Class Reference

Decoder object that uses multiple CPU threads for the graph search, plus a GPU for the neural net inference (that's done by a separate NnetBatchComputer object). More...

#include <nnet-batch-compute.h>

Collaboration diagram for NnetBatchDecoder:

Classes

struct  UtteranceInput
 
struct  UtteranceOutput
 

Public Member Functions

 NnetBatchDecoder (const fst::Fst< fst::StdArc > &fst, const LatticeFasterDecoderConfig &decoder_config, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, bool allow_partial, int32 num_threads, NnetBatchComputer *computer)
 Constructor. More...
 
void AcceptInput (const std::string &utterance_id, const Matrix< BaseFloat > &input, const Vector< BaseFloat > *ivector, const Matrix< BaseFloat > *online_ivectors, int32 online_ivector_period)
 The user should call this one by one for the utterances that it needs to compute (interspersed with calls to GetOutput()). More...
 
void UtteranceFailed ()
 
int32 Finished ()
 
bool GetOutput (std::string *utterance_id, CompactLattice *clat, std::string *sentence)
 The user should call this to obtain output (This version should only be called if config.determinize_lattice == true (w.r.t. More...
 
bool GetOutput (std::string *utterance_id, Lattice *lat, std::string *sentence)
 
 ~NnetBatchDecoder ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (NnetBatchDecoder)
 
void Decode ()
 
void Compute ()
 
void SetPriorities (std::vector< NnetInferenceTask > *tasks)
 
void UpdatePriorityOffset (double priority)
 
void ProcessOutputUtterance (UtteranceOutput *output)
 

Static Private Member Functions

static void DecodeFunc (NnetBatchDecoder *object)
 
static void ComputeFunc (NnetBatchDecoder *object)
 

Private Attributes

const fst::Fst< fst::StdArc > & fst_
 
const LatticeFasterDecoderConfigdecoder_opts_
 
const TransitionModeltrans_model_
 
const fst::SymbolTable * word_syms_
 
bool allow_partial_
 
NnetBatchComputercomputer_
 
std::vector< std::thread * > decode_threads_
 
std::thread compute_thread_
 
UtteranceInput input_utterance_
 
Semaphore input_ready_semaphore_
 
Semaphore input_consumed_semaphore_
 
Semaphore tasks_ready_semaphore_
 
bool is_finished_
 
bool tasks_finished_
 
std::list< UtteranceOutput * > pending_utts_
 
double priority_offset_
 
double tot_like_
 
int64 frame_count_
 
int32 num_success_
 
int32 num_fail_
 
int32 num_partial_
 
std::mutex stats_mutex_
 
Timer timer_
 

Detailed Description

Decoder object that uses multiple CPU threads for the graph search, plus a GPU for the neural net inference (that's done by a separate NnetBatchComputer object).

The interface of this object should accessed from only one thread, though– presumably the main thread of the program.

Definition at line 613 of file nnet-batch-compute.h.

Constructor & Destructor Documentation

◆ NnetBatchDecoder()

NnetBatchDecoder ( const fst::Fst< fst::StdArc > &  fst,
const LatticeFasterDecoderConfig decoder_config,
const TransitionModel trans_model,
const fst::SymbolTable *  word_syms,
bool  allow_partial,
int32  num_threads,
NnetBatchComputer computer 
)

Constructor.

Parameters
[in]fstFST that we are decoding with, will be shared between all decoder threads.
[in]decoder_configConfiguration object for the decoders.
[in]trans_modelThe transition model– needed to construct the decoders, and for determinization.
[in]word_symsA pointer to a symbol table of words, used for printing the decoded words to stderr. If NULL, the word-level output will not be logged.
[in]allow_partialIf true, in cases where no final-state was reached on the final frame of the decoding, we still output a lattice; it just may contain partial words (words that are cut off in the middle). If false, we just won't output anything for those lattices.
[in]num_threadsThe number of decoder threads to use. It will use two more threads on top of this: the main thread, for I/O, and a thread for possibly-GPU-based inference.
[in]computerThe NnetBatchComputer object, through which the neural net will be evaluated.

Definition at line 1188 of file nnet-batch-compute.cc.

References NnetBatchDecoder::compute_thread_, NnetBatchDecoder::ComputeFunc(), NnetBatchDecoder::decode_threads_, NnetBatchDecoder::DecodeFunc(), rnnlm::i, and KALDI_ASSERT.

1195  :
1196  fst_(fst), decoder_opts_(decoder_opts),
1197  trans_model_(trans_model), word_syms_(word_syms),
1198  allow_partial_(allow_partial), computer_(computer),
1199  is_finished_(false), tasks_finished_(false), priority_offset_(0.0),
1200  tot_like_(0.0), frame_count_(0), num_success_(0), num_fail_(0),
1201  num_partial_(0) {
1202  KALDI_ASSERT(num_threads > 0);
1203  for (int32 i = 0; i < num_threads; i++)
1204  decode_threads_.push_back(new std::thread(DecodeFunc, this));
1205  compute_thread_ = std::thread(ComputeFunc, this);
1206 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
static void ComputeFunc(NnetBatchDecoder *object)
const fst::SymbolTable * word_syms_
const fst::Fst< fst::StdArc > & fst_
static void DecodeFunc(NnetBatchDecoder *object)
const TransitionModel & trans_model_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
const LatticeFasterDecoderConfig & decoder_opts_
std::vector< std::thread * > decode_threads_

◆ ~NnetBatchDecoder()

Definition at line 1497 of file nnet-batch-compute.cc.

References NnetBatchDecoder::computer_, NnetBatchDecoder::decode_threads_, Timer::Elapsed(), NnetBatchDecoder::frame_count_, NnetSimpleComputationOptions::frame_subsampling_factor, NnetBatchComputer::GetOptions(), NnetBatchDecoder::is_finished_, KALDI_ERR, KALDI_LOG, NnetBatchDecoder::num_fail_, NnetBatchDecoder::num_partial_, NnetBatchDecoder::num_success_, NnetBatchDecoder::pending_utts_, NnetBatchDecoder::timer_, and NnetBatchDecoder::tot_like_.

1497  {
1498  if (!is_finished_ || !pending_utts_.empty()) {
1499  // At this point the application is bound to fail so raising another
1500  // exception is not a big problem.
1501  KALDI_ERR << "Destroying NnetBatchDecoder object without calling "
1502  "Finished() and consuming the remaining output";
1503  }
1504  // Print diagnostics.
1505 
1506  kaldi::int64 input_frame_count =
1508  int32 num_threads = static_cast<int32>(decode_threads_.size());
1509 
1510  KALDI_LOG << "Overall likelihood per frame was "
1511  << tot_like_ / std::max<int64>(1, frame_count_)
1512  << " over " << frame_count_ << " frames.";
1513 
1514  double elapsed = timer_.Elapsed();
1515  // the +1 below is just to avoid division-by-zero errors.
1516  KALDI_LOG << "Time taken "<< elapsed
1517  << "s: real-time factor assuming 100 frames/sec is "
1518  << (num_threads * elapsed * 100.0 /
1519  std::max<int64>(input_frame_count, 1))
1520  << " (per thread; with " << num_threads << " threads).";
1521  KALDI_LOG << "Done " << num_success_ << " utterances ("
1522  << num_partial_ << " forced out); failed for "
1523  << num_fail_;
1524 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
std::list< UtteranceOutput * > pending_utts_
const NnetBatchComputerOptions & GetOptions()
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
std::vector< std::thread * > decode_threads_

Member Function Documentation

◆ AcceptInput()

void AcceptInput ( const std::string &  utterance_id,
const Matrix< BaseFloat > &  input,
const Vector< BaseFloat > *  ivector,
const Matrix< BaseFloat > *  online_ivectors,
int32  online_ivector_period 
)

The user should call this one by one for the utterances that it needs to compute (interspersed with calls to GetOutput()).

This call will block when no threads are ready to start processing this utterance.

Parameters
[in]utterance_idThe string representing the utterance-id; it will be provided back to the user when GetOutput() is called.
[in]inputThe input features (e.g. MFCCs)
[in]ivectorIf non-NULL, this is expected to be the i-vector for this utterance (and 'online_ivectors' should be NULL).
[in]online_ivector_periodOnly relevant if 'online_ivector' is non-NULL, this says how many frames of 'input' is covered by each row of 'online_ivectors'.

Definition at line 1224 of file nnet-batch-compute.cc.

References NnetBatchDecoder::UtteranceInput::input, NnetBatchDecoder::input_consumed_semaphore_, NnetBatchDecoder::input_ready_semaphore_, NnetBatchDecoder::input_utterance_, NnetBatchDecoder::UtteranceInput::ivector, NnetBatchDecoder::UtteranceInput::online_ivector_period, NnetBatchDecoder::UtteranceInput::online_ivectors, NnetBatchDecoder::pending_utts_, Semaphore::Signal(), NnetBatchDecoder::UtteranceInput::utterance_id, NnetBatchDecoder::UtteranceOutput::utterance_id, and Semaphore::Wait().

1229  {
1230  // This function basically does a handshake with one of the decoder threads.
1231  // It may have to wait till one of the decoder threads becomes ready.
1232  input_utterance_.utterance_id = utterance_id;
1233  input_utterance_.input = &input;
1234  input_utterance_.ivector = ivector;
1235  input_utterance_.online_ivectors = online_ivectors;
1236  input_utterance_.online_ivector_period = online_ivector_period;
1237 
1238 
1239  UtteranceOutput *this_output = new UtteranceOutput();
1240  this_output->utterance_id = utterance_id;
1241  pending_utts_.push_back(this_output);
1242 
1245 }
void Signal()
increase the counter
std::list< UtteranceOutput * > pending_utts_
void Wait()
decrease the counter

◆ Compute()

void Compute ( )
private

Definition at line 1329 of file nnet-batch-compute.cc.

References NnetBatchComputer::Compute(), NnetBatchDecoder::computer_, NnetBatchDecoder::tasks_finished_, NnetBatchDecoder::tasks_ready_semaphore_, and Semaphore::Wait().

1329  {
1330  while (!tasks_finished_) {
1332  bool allow_partial_minibatch = true;
1333  while (computer_->Compute(allow_partial_minibatch));
1334  }
1335 }
bool Compute(bool allow_partial_minibatch)
Does some kind of computation, choosing the highest-priority thing to compute.
void Wait()
decrease the counter

◆ ComputeFunc()

static void ComputeFunc ( NnetBatchDecoder object)
inlinestaticprivate

Definition at line 760 of file nnet-batch-compute.h.

References NnetInferenceTask::priority.

Referenced by NnetBatchDecoder::NnetBatchDecoder().

760 { object->Compute(); }

◆ Decode()

void Decode ( )
private

Definition at line 1337 of file nnet-batch-compute.cc.

References NnetBatchComputer::AcceptTask(), LatticeFasterDecoderTpl< FST, Token >::AdvanceDecoding(), NnetBatchDecoder::allow_partial_, NnetBatchDecoder::computer_, NnetBatchDecoder::decoder_opts_, NnetBatchDecoder::fst_, LatticeFasterDecoderTpl< FST, Token >::GetRawLattice(), rnnlm::i, LatticeFasterDecoderTpl< FST, Token >::InitDecoding(), NnetBatchDecoder::UtteranceInput::input, NnetBatchDecoder::input_consumed_semaphore_, NnetBatchDecoder::input_ready_semaphore_, NnetBatchDecoder::input_utterance_, NnetBatchDecoder::is_finished_, NnetBatchDecoder::UtteranceInput::ivector, KALDI_ASSERT, KALDI_WARN, NnetBatchDecoder::UtteranceOutput::lat, NnetBatchDecoder::num_fail_, NnetInferenceTask::num_initial_unused_output_frames, NnetBatchDecoder::num_partial_, NnetInferenceTask::num_used_output_frames, MatrixBase< Real >::NumCols(), NnetBatchDecoder::UtteranceInput::online_ivector_period, NnetBatchDecoder::UtteranceInput::online_ivectors, NnetInferenceTask::output, NnetInferenceTask::output_cpu, NnetBatchDecoder::pending_utts_, NnetInferenceTask::priority, NnetBatchDecoder::ProcessOutputUtterance(), LatticeFasterDecoderTpl< FST, Token >::ReachedFinal(), NnetInferenceTask::semaphore, NnetBatchDecoder::SetPriorities(), Semaphore::Signal(), NnetBatchComputer::SplitUtteranceIntoTasks(), NnetBatchDecoder::stats_mutex_, NnetBatchDecoder::tasks_ready_semaphore_, NnetBatchDecoder::trans_model_, NnetBatchDecoder::UpdatePriorityOffset(), NnetBatchDecoder::UtteranceInput::utterance_id, NnetBatchDecoder::UtteranceOutput::utterance_id, and Semaphore::Wait().

1337  {
1338  while (true) {
1340  if (is_finished_)
1341  return;
1342 
1343  std::vector<NnetInferenceTask> tasks;
1344  std::string utterance_id;
1345  // we can be confident that the last element of 'pending_utts_' is the one
1346  // for this utterance, as we know exactly at what point in the code the main
1347  // thread will be in AcceptInput().
1348  UtteranceOutput *output_utterance = pending_utts_.back();
1349  {
1350  UtteranceInput input_utterance(input_utterance_);
1351  utterance_id = input_utterance.utterance_id;
1352  bool output_to_cpu = true;
1353  computer_->SplitUtteranceIntoTasks(output_to_cpu,
1354  *(input_utterance.input),
1355  input_utterance.ivector,
1356  input_utterance.online_ivectors,
1357  input_utterance.online_ivector_period,
1358  &tasks);
1359  KALDI_ASSERT(output_utterance->utterance_id == utterance_id);
1361  // Now let input_utterance go out of scope; it's no longer valid as it may
1362  // be overwritten by something else.
1363  }
1364 
1365  SetPriorities(&tasks);
1366  for (size_t i = 0; i < tasks.size(); i++)
1367  computer_->AcceptTask(&(tasks[i]));
1369 
1370  {
1371  int32 frame_offset = 0;
1373  decoder.InitDecoding();
1374 
1375 
1376  for (size_t i = 0; i < tasks.size(); i++) {
1377  NnetInferenceTask &task = tasks[i];
1378  task.semaphore.Wait();
1379  UpdatePriorityOffset(task.priority);
1380 
1381  SubMatrix<BaseFloat> post(task.output_cpu,
1382  task.num_initial_unused_output_frames,
1383  task.num_used_output_frames,
1384  0, task.output_cpu.NumCols());
1385  DecodableMatrixMapped decodable(trans_model_, post, frame_offset);
1386  frame_offset += post.NumRows();
1387  decoder.AdvanceDecoding(&decodable);
1388  task.output.Resize(0, 0); // Free some memory.
1389  }
1390 
1391  bool use_final_probs = true;
1392  if (!decoder.ReachedFinal()) {
1393  if (allow_partial_) {
1394  KALDI_WARN << "Outputting partial output for utterance "
1395  << utterance_id << " since no final-state reached\n";
1396  use_final_probs = false;
1397  std::unique_lock<std::mutex> lock(stats_mutex_);
1398  num_partial_++;
1399  } else {
1400  KALDI_WARN << "Not producing output for utterance " << utterance_id
1401  << " since no final-state reached and "
1402  << "--allow-partial=false.\n";
1403  std::unique_lock<std::mutex> lock(stats_mutex_);
1404  num_fail_++;
1405  continue;
1406  }
1407  }
1408  // if we reached this point, we are getting a lattice.
1409  decoder.GetRawLattice(&output_utterance->lat, use_final_probs);
1410  // Let the decoder and the decodable object go out of scope, to save
1411  // memory.
1412  }
1413  ProcessOutputUtterance(output_utterance);
1414  }
1415 }
void Signal()
increase the counter
kaldi::int32 int32
const fst::Fst< fst::StdArc > & fst_
void SplitUtteranceIntoTasks(bool output_to_cpu, const Matrix< BaseFloat > &input, const Vector< BaseFloat > *ivector, const Matrix< BaseFloat > *online_ivectors, int32 online_ivector_period, std::vector< NnetInferenceTask > *tasks)
Split a single utterance into a list of separate tasks which can then be given to this class by Accep...
void AcceptTask(NnetInferenceTask *task, int32 max_minibatches_full=-1)
Accepts a task, meaning the task will be queued.
const TransitionModel & trans_model_
#define KALDI_WARN
Definition: kaldi-error.h:150
void UpdatePriorityOffset(double priority)
void SetPriorities(std::vector< NnetInferenceTask > *tasks)
std::list< UtteranceOutput * > pending_utts_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
LatticeFasterDecoderTpl< fst::StdFst, decoder::StdToken > LatticeFasterDecoder
void ProcessOutputUtterance(UtteranceOutput *output)
const LatticeFasterDecoderConfig & decoder_opts_
void Wait()
decrease the counter

◆ DecodeFunc()

static void DecodeFunc ( NnetBatchDecoder object)
inlinestaticprivate

Definition at line 755 of file nnet-batch-compute.h.

Referenced by NnetBatchDecoder::NnetBatchDecoder().

755 { object->Decode(); }

◆ Finished()

int32 Finished ( )

Definition at line 1247 of file nnet-batch-compute.cc.

References NnetBatchDecoder::compute_thread_, NnetBatchDecoder::decode_threads_, rnnlm::i, NnetBatchDecoder::input_ready_semaphore_, NnetBatchDecoder::is_finished_, NnetBatchDecoder::num_success_, Semaphore::Signal(), NnetBatchDecoder::tasks_finished_, and NnetBatchDecoder::tasks_ready_semaphore_.

1247  {
1248  is_finished_ = true;
1249  for (size_t i = 0; i < decode_threads_.size(); i++)
1251  for (size_t i = 0; i < decode_threads_.size(); i++) {
1252  decode_threads_[i]->join();
1253  delete decode_threads_[i];
1254  decode_threads_[i] = NULL;
1255  }
1256  // don't clear decode_threads_, since its size is needed in the destructor to
1257  // compute timing.
1258 
1259  tasks_finished_ = true;
1261  compute_thread_.join();
1262  return num_success_;
1263 }
void Signal()
increase the counter
std::vector< std::thread * > decode_threads_

◆ GetOutput() [1/2]

bool GetOutput ( std::string *  utterance_id,
CompactLattice clat,
std::string *  sentence 
)

The user should call this to obtain output (This version should only be called if config.determinize_lattice == true (w.r.t.

the config provided to the constructor). The output is guaranteed to be in the same order as the input was provided, but it may be delayed, *and* some outputs may be missing, for example because of search failures (allow_partial will affect this).

The acoustic scores in the output lattice will already be divided by the acoustic scale we decoded with.

This call does not block (i.e. does not wait on any semaphores). It returns true if it actually got any output; if none was ready it will return false.

Parameters
[out]utterance_idIf an output was ready, its utterance-id is written to here.
[out]clatIf an output was ready, it compact lattice will be written to here.
[out]sentenceIf an output was ready and a nonempty symbol table was provided to the constructor of this class, contains the word-sequence decoded as a string. Otherwise will be empty.
Returns
Returns true if a decoded output was ready. (These appear asynchronously as the decoding is done in background threads).

Definition at line 1266 of file nnet-batch-compute.cc.

References NnetBatchDecoder::UtteranceOutput::compact_lat, NnetBatchDecoder::decoder_opts_, LatticeFasterDecoderConfig::determinize_lattice, KALDI_ERR, NnetBatchDecoder::pending_utts_, NnetBatchDecoder::UtteranceOutput::sentence, and NnetBatchDecoder::UtteranceOutput::utterance_id.

Referenced by kaldi::HandleOutput().

1269  {
1271  KALDI_ERR << "Don't call this version of GetOutput if you are "
1272  "not determinizing.";
1273  while (true) {
1274  if (pending_utts_.empty())
1275  return false;
1276  if (!pending_utts_.front()->finished)
1277  return false;
1278  UtteranceOutput *this_output = pending_utts_.front();
1279  pending_utts_.pop_front();
1280  if (this_output->compact_lat.NumStates() == 0) {
1281  delete this_output;
1282  // ... and continue round the loop, without returning any output to the
1283  // user for this utterance. Something went wrong in decoding: for
1284  // example, the user specified allow_partial == false and no final-states
1285  // were active on the last frame, or something more unexpected. A warning
1286  // would have been printed in the decoder thread.
1287  } else {
1288  *clat = this_output->compact_lat;
1289  utterance_id->swap(this_output->utterance_id);
1290  sentence->swap(this_output->sentence);
1291  delete this_output;
1292  return true;
1293  }
1294  }
1295 }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::list< UtteranceOutput * > pending_utts_
const LatticeFasterDecoderConfig & decoder_opts_

◆ GetOutput() [2/2]

bool GetOutput ( std::string *  utterance_id,
Lattice lat,
std::string *  sentence 
)

Definition at line 1298 of file nnet-batch-compute.cc.

References NnetBatchDecoder::decoder_opts_, LatticeFasterDecoderConfig::determinize_lattice, KALDI_ERR, NnetBatchDecoder::UtteranceOutput::lat, NnetBatchDecoder::pending_utts_, NnetBatchDecoder::UtteranceOutput::sentence, and NnetBatchDecoder::UtteranceOutput::utterance_id.

1301  {
1303  KALDI_ERR << "Don't call this version of GetOutput if you are "
1304  "determinizing.";
1305  while (true) {
1306  if (pending_utts_.empty())
1307  return false;
1308  if (!pending_utts_.front()->finished)
1309  return false;
1310  UtteranceOutput *this_output = pending_utts_.front();
1311  pending_utts_.pop_front();
1312  if (this_output->lat.NumStates() == 0) {
1313  delete this_output;
1314  // ... and continue round the loop, without returning any output to the
1315  // user for this utterance. Something went wrong in decoding: for
1316  // example, the user specified allow_partial == false and no final-states
1317  // were active on the last frame, or something more unexpected. A warning
1318  // would have been printed in the decoder thread.
1319  } else {
1320  *lat = this_output->lat; // OpenFST has shallow copy so no need to swap.
1321  utterance_id->swap(this_output->utterance_id);
1322  sentence->swap(this_output->sentence);
1323  delete this_output;
1324  return true;
1325  }
1326  }
1327 }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::list< UtteranceOutput * > pending_utts_
const LatticeFasterDecoderConfig & decoder_opts_

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( NnetBatchDecoder  )
private

◆ ProcessOutputUtterance()

void ProcessOutputUtterance ( UtteranceOutput output)
private

Definition at line 1423 of file nnet-batch-compute.cc.

References NnetSimpleComputationOptions::acoustic_scale, fst::AcousticLatticeScale(), NnetBatchDecoder::UtteranceOutput::compact_lat, NnetBatchDecoder::computer_, NnetBatchDecoder::decoder_opts_, LatticeFasterDecoderConfig::det_opts, LatticeFasterDecoderConfig::determinize_lattice, fst::DeterminizeLatticePhonePrunedWrapper(), NnetBatchDecoder::UtteranceOutput::finished, NnetBatchDecoder::frame_count_, fst::GetLinearSymbolSequence(), NnetBatchComputer::GetOptions(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, NnetBatchDecoder::UtteranceOutput::lat, LatticeFasterDecoderConfig::lattice_beam, NnetBatchDecoder::num_fail_, NnetBatchDecoder::num_success_, fst::ScaleLattice(), NnetBatchDecoder::UtteranceOutput::sentence, NnetBatchDecoder::stats_mutex_, NnetBatchDecoder::tot_like_, NnetBatchDecoder::trans_model_, NnetBatchDecoder::UtteranceOutput::utterance_id, LatticeWeightTpl< FloatType >::Value1(), LatticeWeightTpl< FloatType >::Value2(), NnetBatchDecoder::word_syms_, and words.

Referenced by NnetBatchDecoder::Decode().

1423  {
1424  fst::Connect(&(output->lat));
1425  if (output->lat.NumStates() == 0) {
1426  KALDI_WARN << "Unexpected problem getting lattice for utterance "
1427  << output->utterance_id;
1428  std::unique_lock<std::mutex> lock(stats_mutex_);
1429  num_fail_++;
1430  return;
1431  }
1432 
1433  { // This block accumulates diagnostics, prints log messages, and sets
1434  // output->sentence.
1435  Lattice best_path;
1436  LatticeWeight weight;
1437  ShortestPath(output->lat, &best_path);
1438  std::vector<int32> alignment;
1439  std::vector<int32> words;
1440  GetLinearSymbolSequence(best_path, &alignment, &words, &weight);
1441  int32 num_frames = alignment.size();
1442  if (word_syms_ != NULL) {
1443  std::ostringstream os;
1444  for (size_t i = 0; i < words.size(); i++) {
1445  std::string s = word_syms_->Find(words[i]);
1446  if (s == "")
1447  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
1448  os << s << ' ';
1449  }
1450  output->sentence = os.str();
1451  }
1452  double likelihood = -(weight.Value1() + weight.Value2());
1453  // Note: these logging messages will be out-of-order w.r.t. the transcripts
1454  // that are printed to cerr; we keep those transcripts in the same order
1455  // that the utterances were in, but these logging messages may be out of
1456  // order (due to multiple threads).
1457  KALDI_LOG << "Log-like per frame for utterance " << output->utterance_id
1458  << " is " << (likelihood / num_frames) << " over "
1459  << num_frames << " frames.";
1460  KALDI_VLOG(2) << "Cost for utterance " << output->utterance_id << " is "
1461  << weight.Value1() << " + " << weight.Value2();
1462 
1463  std::unique_lock<std::mutex> lock(stats_mutex_);
1464  tot_like_ += likelihood;
1465  frame_count_ += num_frames;
1466  num_success_ += 1;
1467  }
1468 
1471  trans_model_,
1472  &output->lat,
1474  &(output->compact_lat),
1476  KALDI_WARN << "Determinization finished earlier than the beam for "
1477  << "utterance " << output->utterance_id;
1478  output->lat.DeleteStates(); // Save memory.
1479  }
1480 
1481  // We'll write the lattice without acoustic scaling, so we need to reverse
1482  // the scale that we applied when decoding.
1483  BaseFloat acoustic_scale = computer_->GetOptions().acoustic_scale;
1484  if (acoustic_scale != 0.0) {
1486  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale),
1487  &(output->compact_lat));
1488  else
1489  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale),
1490  &(output->lat));
1491  }
1492  output->finished = true;
1493 }
int32 words[kMaxOrder]
kaldi::int32 int32
const fst::SymbolTable * word_syms_
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
Definition: kaldi-lattice.h:32
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
float BaseFloat
Definition: kaldi-types.h:29
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_ERR
Definition: kaldi-error.h:147
const TransitionModel & trans_model_
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::DeterminizeLatticePhonePrunedOptions det_opts
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
const NnetBatchComputerOptions & GetOptions()
const LatticeFasterDecoderConfig & decoder_opts_
#define KALDI_LOG
Definition: kaldi-error.h:153
bool DeterminizeLatticePhonePrunedWrapper(const kaldi::TransitionModel &trans_model, MutableFst< kaldi::LatticeArc > *ifst, double beam, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts)
This function is a wrapper of DeterminizeLatticePhonePruned() that works for Lattice type FSTs...

◆ SetPriorities()

void SetPriorities ( std::vector< NnetInferenceTask > *  tasks)
private

Definition at line 1208 of file nnet-batch-compute.cc.

References rnnlm::i, and NnetBatchDecoder::priority_offset_.

Referenced by NnetBatchDecoder::Decode().

1208  {
1209  size_t num_tasks = tasks->size();
1210  double priority_offset = priority_offset_;
1211  for (size_t i = 0; i < num_tasks; i++)
1212  (*tasks)[i].priority = priority_offset - (double)i;
1213 }

◆ UpdatePriorityOffset()

void UpdatePriorityOffset ( double  priority)
private

Definition at line 1215 of file nnet-batch-compute.cc.

References NnetBatchDecoder::decode_threads_, and NnetBatchDecoder::priority_offset_.

Referenced by NnetBatchDecoder::Decode().

1215  {
1216  size_t num_tasks = decode_threads_.size(),
1217  new_weight = 1.0 / num_tasks,
1218  old_weight = 1.0 - new_weight;
1219  // The next line is vulnerable to a race condition but if it happened it
1220  // wouldn't matter.
1221  priority_offset_ = priority_offset_ * old_weight + priority * new_weight;
1222 }
std::vector< std::thread * > decode_threads_

◆ UtteranceFailed()

void UtteranceFailed ( )

Definition at line 1418 of file nnet-batch-compute.cc.

References NnetBatchDecoder::num_fail_, and NnetBatchDecoder::stats_mutex_.

1418  {
1419  std::unique_lock<std::mutex> lock(stats_mutex_);
1420  num_fail_++;
1421 }

Member Data Documentation

◆ allow_partial_

bool allow_partial_
private

Definition at line 780 of file nnet-batch-compute.h.

Referenced by NnetBatchDecoder::Decode().

◆ compute_thread_

std::thread compute_thread_
private

◆ computer_

◆ decode_threads_

◆ decoder_opts_

◆ frame_count_

int64 frame_count_
private

◆ fst_

const fst::Fst<fst::StdArc>& fst_
private

Definition at line 776 of file nnet-batch-compute.h.

Referenced by NnetBatchDecoder::Decode().

◆ input_consumed_semaphore_

Semaphore input_consumed_semaphore_
private

◆ input_ready_semaphore_

Semaphore input_ready_semaphore_
private

◆ input_utterance_

UtteranceInput input_utterance_
private

◆ is_finished_

◆ num_fail_

◆ num_partial_

int32 num_partial_
private

◆ num_success_

◆ pending_utts_

◆ priority_offset_

double priority_offset_
private

◆ stats_mutex_

std::mutex stats_mutex_
private

◆ tasks_finished_

bool tasks_finished_
private

Definition at line 810 of file nnet-batch-compute.h.

Referenced by NnetBatchDecoder::Compute(), and NnetBatchDecoder::Finished().

◆ tasks_ready_semaphore_

Semaphore tasks_ready_semaphore_
private

◆ timer_

Timer timer_
private

Definition at line 839 of file nnet-batch-compute.h.

Referenced by NnetBatchDecoder::~NnetBatchDecoder().

◆ tot_like_

double tot_like_
private

◆ trans_model_

const TransitionModel& trans_model_
private

◆ word_syms_

const fst::SymbolTable* word_syms_
private

Definition at line 779 of file nnet-batch-compute.h.

Referenced by NnetBatchDecoder::ProcessOutputUtterance().


The documentation for this class was generated from the following files: