DecodableMatrixMappedOffset Class Reference

This decodable class returns log-likes stored in a matrix; it supports repeatedly writing to the matrix and setting a time-offset representing the frame-index of the first row of the matrix. More...

#include <decodable-matrix.h>

Inheritance diagram for DecodableMatrixMappedOffset:
Collaboration diagram for DecodableMatrixMappedOffset:

Public Member Functions

 DecodableMatrixMappedOffset (const TransitionModel &tm)
 
int32 FirstAvailableFrame () const
 
void AcceptLoglikes (Matrix< BaseFloat > *loglikes, int32 frames_to_discard)
 
void InputIsFinished ()
 
virtual int32 NumFramesReady () const
 The call NumFramesReady() will return the number of frames currently available for this decodable object. More...
 
virtual bool IsLastFrame (int32 frame) const
 Returns true if this is the last frame. More...
 
virtual BaseFloat LogLikelihood (int32 frame, int32 tid)
 Returns the log likelihood, which will be negated in the decoder. More...
 
virtual int32 NumIndices () const
 Returns the number of states in the acoustic model (they will be indexed one-based, i.e. More...
 
virtual ~DecodableMatrixMappedOffset ()
 
- Public Member Functions inherited from DecodableInterface
virtual ~DecodableInterface ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (DecodableMatrixMappedOffset)
 

Private Attributes

const TransitionModeltrans_model_
 
Matrix< BaseFloatloglikes_
 
int32 frame_offset_
 
bool input_is_finished_
 
BaseFloatraw_data_
 
int32 stride_
 

Detailed Description

This decodable class returns log-likes stored in a matrix; it supports repeatedly writing to the matrix and setting a time-offset representing the frame-index of the first row of the matrix.

It's intended for use in multi-threaded decoding; mutex and semaphores are not included. External code will call SetLoglikes() each time more log-likelihods are available. If you try to access a log-likelihood that's no longer available because the frame index is less than the current offset, it is of course an error.

See also DecodableMatrixMapped, which supports the same type of thing but with a different interface where you are expected to re-construct the object each time you want to decode.

Definition at line 152 of file decodable-matrix.h.

Constructor & Destructor Documentation

◆ DecodableMatrixMappedOffset()

◆ ~DecodableMatrixMappedOffset()

virtual ~DecodableMatrixMappedOffset ( )
inlinevirtual

Definition at line 193 of file decodable-matrix.h.

193 { }

Member Function Documentation

◆ AcceptLoglikes()

void AcceptLoglikes ( Matrix< BaseFloat > *  loglikes,
int32  frames_to_discard 
)

Definition at line 81 of file decodable-matrix.cc.

References DecodableMatrixMapped::frame_offset_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), TransitionModel::NumPdfs(), MatrixBase< Real >::NumRows(), DecodableMatrixMapped::raw_data_, Matrix< Real >::Resize(), MatrixBase< Real >::RowRange(), DecodableMatrixMapped::stride_, and DecodableMatrixMapped::trans_model_.

Referenced by SingleUtteranceNnet2DecoderThreaded::RunNnetEvaluationInternal().

82  {
83  if (loglikes->NumRows() == 0) return;
84  KALDI_ASSERT(loglikes->NumCols() == trans_model_.NumPdfs());
85  KALDI_ASSERT(frames_to_discard <= loglikes_.NumRows() &&
86  frames_to_discard >= 0);
87  if (frames_to_discard == loglikes_.NumRows()) {
88  loglikes_.Swap(loglikes);
89  loglikes->Resize(0, 0);
90  } else {
91  int32 old_rows_kept = loglikes_.NumRows() - frames_to_discard,
92  new_num_rows = old_rows_kept + loglikes->NumRows();
93  Matrix<BaseFloat> new_loglikes(new_num_rows, loglikes->NumCols());
94  new_loglikes.RowRange(0, old_rows_kept).CopyFromMat(
95  loglikes_.RowRange(frames_to_discard, old_rows_kept));
96  new_loglikes.RowRange(old_rows_kept, loglikes->NumRows()).CopyFromMat(
97  *loglikes);
98  loglikes_.Swap(&new_loglikes);
99  }
100  frame_offset_ += frames_to_discard;
103 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:79
kaldi::int32 int32
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:70
SubMatrix< Real > RowRange(const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
Definition: kaldi-matrix.h:209
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
const TransitionModel & trans_model_
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ FirstAvailableFrame()

int32 FirstAvailableFrame ( ) const
inline

◆ InputIsFinished()

void InputIsFinished ( )
inline

◆ IsLastFrame()

virtual bool IsLastFrame ( int32  frame) const
inlinevirtual

Returns true if this is the last frame.

Frames are zero-based, so the first frame is zero. IsLastFrame(-1) will return false, unless the file is empty (which is a case that I'm not sure all the code will handle, so be careful). Caution: the behavior of this function in an online setting is being changed somewhat. In future it may return false in cases where we haven't yet decided to terminate decoding, but later true if we decide to terminate decoding. The plan in future is to rely more on NumFramesReady(), and in future, IsLastFrame() would always return false in an online-decoding setting, and would only return true in a decoding-from-matrix setting where we want to allow the last delta or LDA features to be flushed out for compatibility with the baseline setup.

Implements DecodableInterface.

Definition at line 175 of file decodable-matrix.h.

References KALDI_ASSERT, and DecodableMatrixScaledMapped::NumFramesReady().

Referenced by SingleUtteranceNnet2DecoderThreaded::RunDecoderSearchInternal().

175  {
176  KALDI_ASSERT(frame < NumFramesReady());
177  return (frame == NumFramesReady() - 1 && input_is_finished_);
178  }
virtual int32 NumFramesReady() const
The call NumFramesReady() will return the number of frames currently available for this decodable obj...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( DecodableMatrixMappedOffset  )
private

◆ LogLikelihood()

virtual BaseFloat LogLikelihood ( int32  frame,
int32  index 
)
inlinevirtual

Returns the log likelihood, which will be negated in the decoder.

The "frame" starts from zero. You should verify that NumFramesReady() > frame before calling this.

Implements DecodableInterface.

Definition at line 180 of file decodable-matrix.h.

References DecodableMatrixScaledMapped::trans_model_, and TransitionModel::TransitionIdToPdfFast().

180  {
182 #ifdef KALDI_PARANOID
183  return loglikes_(frame - frame_offset_, pdf_id);
184 #else
185  // This does no checking, so will be faster.
186  return raw_data_[frame * stride_ + pdf_id];
187 #endif
188  }
int32 TransitionIdToPdfFast(int32 trans_id) const
kaldi::int32 int32
const TransitionModel & trans_model_

◆ NumFramesReady()

virtual int32 NumFramesReady ( ) const
inlinevirtual

The call NumFramesReady() will return the number of frames currently available for this decodable object.

This is for use in setups where you don't want the decoder to block while waiting for input. This is newly added as of Jan 2014, and I hope, going forward, to rely on this mechanism more than IsLastFrame to know when to stop decoding.

Reimplemented from DecodableInterface.

Definition at line 171 of file decodable-matrix.h.

Referenced by SingleUtteranceNnet2DecoderThreaded::RunDecoderSearchInternal().

171  {
172  return loglikes_.NumRows() + frame_offset_;
173  }
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ NumIndices()

virtual int32 NumIndices ( ) const
inlinevirtual

Returns the number of states in the acoustic model (they will be indexed one-based, i.e.

from 1 to NumIndices(); this is for compatibility with OpenFst).

Implements DecodableInterface.

Definition at line 190 of file decodable-matrix.h.

References TransitionModel::NumTransitionIds(), and DecodableMatrixScaledMapped::trans_model_.

190 { return trans_model_.NumTransitionIds(); }
int32 NumTransitionIds() const
Returns the total number of transition-ids (note, these are one-based).
const TransitionModel & trans_model_

Member Data Documentation

◆ frame_offset_

int32 frame_offset_
private

Definition at line 197 of file decodable-matrix.h.

◆ input_is_finished_

bool input_is_finished_
private

Definition at line 198 of file decodable-matrix.h.

◆ loglikes_

Matrix<BaseFloat> loglikes_
private

Definition at line 196 of file decodable-matrix.h.

◆ raw_data_

BaseFloat* raw_data_
private

Definition at line 205 of file decodable-matrix.h.

◆ stride_

int32 stride_
private

Definition at line 206 of file decodable-matrix.h.

◆ trans_model_

const TransitionModel& trans_model_
private

Definition at line 195 of file decodable-matrix.h.


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