OnlineCacheFeature Class Reference

This feature type can be used to cache its input, to avoid repetition of computation in a multi-pass decoding context. More...

#include <online-feature.h>

Inheritance diagram for OnlineCacheFeature:
Collaboration diagram for OnlineCacheFeature:

Public Member Functions

virtual int32 Dim () const
 
virtual bool IsLastFrame (int32 frame) const
 Returns true if this is the last frame. More...
 
virtual BaseFloat FrameShiftInSeconds () const
 
virtual int32 NumFramesReady () const
 returns the feature dimension. More...
 
virtual void GetFrame (int32 frame, VectorBase< BaseFloat > *feat)
 Gets the feature vector for this frame. More...
 
virtual void GetFrames (const std::vector< int32 > &frames, MatrixBase< BaseFloat > *feats)
 This is like GetFrame() but for a collection of frames. More...
 
virtual ~OnlineCacheFeature ()
 
void ClearCache ()
 
 OnlineCacheFeature (OnlineFeatureInterface *src)
 
- Public Member Functions inherited from OnlineFeatureInterface
virtual ~OnlineFeatureInterface ()
 Virtual destructor. More...
 

Private Attributes

OnlineFeatureInterfacesrc_
 
std::vector< Vector< BaseFloat > *> cache_
 

Detailed Description

This feature type can be used to cache its input, to avoid repetition of computation in a multi-pass decoding context.

Definition at line 564 of file online-feature.h.

Constructor & Destructor Documentation

◆ ~OnlineCacheFeature()

virtual ~OnlineCacheFeature ( )
inlinevirtual

Definition at line 582 of file online-feature.h.

◆ OnlineCacheFeature()

OnlineCacheFeature ( OnlineFeatureInterface src)
inlineexplicit

Definition at line 589 of file online-feature.h.

589 : src_(src) { }
OnlineFeatureInterface * src_

Member Function Documentation

◆ ClearCache()

void ClearCache ( )

Definition at line 662 of file online-feature.cc.

References rnnlm::i.

Referenced by kaldi::GetOutput().

662  {
663  for (size_t i = 0; i < cache_.size(); i++)
664  delete cache_[i];
665  cache_.resize(0);
666 }
std::vector< Vector< BaseFloat > *> cache_

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

Implements OnlineFeatureInterface.

Definition at line 566 of file online-feature.h.

566 { return src_->Dim(); }
OnlineFeatureInterface * src_
virtual int32 Dim() const =0

◆ FrameShiftInSeconds()

virtual BaseFloat FrameShiftInSeconds ( ) const
inlinevirtual

Implements OnlineFeatureInterface.

Definition at line 571 of file online-feature.h.

571  {
572  return src_->FrameShiftInSeconds();
573  }
OnlineFeatureInterface * src_
virtual BaseFloat FrameShiftInSeconds() const =0

◆ GetFrame()

void GetFrame ( int32  frame,
VectorBase< BaseFloat > *  feat 
)
virtual

Gets the feature vector for this frame.

Before calling this for a given frame, it is assumed that you called NumFramesReady() and it returned a number greater than "frame". Otherwise this call will likely crash with an assert failure. This function is not declared const, in case there is some kind of caching going on, but most of the time it shouldn't modify the class.

Implements OnlineFeatureInterface.

Definition at line 604 of file online-feature.cc.

References VectorBase< Real >::CopyFromVec(), OnlineDeltaFeature::Dim(), OnlineFeatureInterface::GetFrame(), KALDI_ASSERT, and OnlineDeltaFeature::src_.

Referenced by kaldi::GetOutput().

604  {
605  KALDI_ASSERT(frame >= 0);
606  if (static_cast<size_t>(frame) < cache_.size() && cache_[frame] != NULL) {
607  feat->CopyFromVec(*(cache_[frame]));
608  } else {
609  if (static_cast<size_t>(frame) >= cache_.size())
610  cache_.resize(frame + 1, NULL);
611  int32 dim = this->Dim();
612  cache_[frame] = new Vector<BaseFloat>(dim);
613  // The following call will crash if frame "frame" is not ready.
614  src_->GetFrame(frame, cache_[frame]);
615  feat->CopyFromVec(*(cache_[frame]));
616  }
617 }
virtual int32 Dim() const
virtual void GetFrame(int32 frame, VectorBase< BaseFloat > *feat)=0
Gets the feature vector for this frame.
kaldi::int32 int32
OnlineFeatureInterface * src_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< Vector< BaseFloat > *> cache_

◆ GetFrames()

void GetFrames ( const std::vector< int32 > &  frames,
MatrixBase< BaseFloat > *  feats 
)
virtual

This is like GetFrame() but for a collection of frames.

There is a default implementation that just gets the frames one by one, but it may be overridden for efficiency by child classes (since sometimes it's more efficient to do things in a batch).

Reimplemented from OnlineFeatureInterface.

Definition at line 619 of file online-feature.cc.

References OnlineDeltaFeature::Dim(), OnlineFeatureInterface::GetFrames(), rnnlm::i, kaldi::kUndefined, MatrixBase< Real >::Row(), and OnlineDeltaFeature::src_.

620  {
621  int32 num_frames = frames.size();
622  // non_cached_frames will be the subset of 't' values in 'frames' which were
623  // not previously cached, which we therefore need to get from src_.
624  std::vector<int32> non_cached_frames;
625  // 'non_cached_indexes' stores the indexes 'i' into 'frames' corresponding to
626  // the corresponding frames in 'non_cached_frames'.
627  std::vector<int32> non_cached_indexes;
628  non_cached_frames.reserve(frames.size());
629  non_cached_indexes.reserve(frames.size());
630  for (int32 i = 0; i < num_frames; i++) {
631  int32 t = frames[i];
632  if (static_cast<size_t>(t) < cache_.size() && cache_[t] != NULL) {
633  feats->Row(i).CopyFromVec(*(cache_[t]));
634  } else {
635  non_cached_frames.push_back(t);
636  non_cached_indexes.push_back(i);
637  }
638  }
639  if (non_cached_frames.empty())
640  return;
641  int32 num_non_cached_frames = non_cached_frames.size(),
642  dim = this->Dim();
643  Matrix<BaseFloat> non_cached_feats(num_non_cached_frames, dim,
644  kUndefined);
645  src_->GetFrames(non_cached_frames, &non_cached_feats);
646  for (int32 i = 0; i < num_non_cached_frames; i++) {
647  int32 t = non_cached_frames[i];
648  if (static_cast<size_t>(t) < cache_.size() && cache_[t] != NULL) {
649  // We can reach this point due to repeat indexes in 'non_cached_frames'.
650  feats->Row(non_cached_indexes[i]).CopyFromVec(*(cache_[t]));
651  } else {
652  SubVector<BaseFloat> this_feat(non_cached_feats, i);
653  feats->Row(non_cached_indexes[i]).CopyFromVec(this_feat);
654  if (static_cast<size_t>(t) >= cache_.size())
655  cache_.resize(t + 1, NULL);
656  cache_[t] = new Vector<BaseFloat>(this_feat);
657  }
658  }
659 }
virtual int32 Dim() const
kaldi::int32 int32
OnlineFeatureInterface * src_
virtual void GetFrames(const std::vector< int32 > &frames, MatrixBase< BaseFloat > *feats)
This is like GetFrame() but for a collection of frames.
std::vector< Vector< BaseFloat > *> cache_

◆ IsLastFrame()

virtual bool IsLastFrame ( int32  frame) const
inlinevirtual

Returns true if this is the last frame.

Frame indices 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). This function may return false for some frame if we haven't yet decided to terminate decoding, but later true if we decide to terminate decoding. This function exists mainly to correctly handle end effects in feature extraction, and is not a mechanism to determine how many frames are in the decodable object (as it used to be, and for backward compatibility, still is, in the Decodable interface).

Implements OnlineFeatureInterface.

Definition at line 568 of file online-feature.h.

Referenced by kaldi::GetOutput().

568  {
569  return src_->IsLastFrame(frame);
570  }
OnlineFeatureInterface * src_
virtual bool IsLastFrame(int32 frame) const =0
Returns true if this is the last frame.

◆ NumFramesReady()

virtual int32 NumFramesReady ( ) const
inlinevirtual

returns the feature dimension.

Returns the total number of frames, since the start of the utterance, that are now available. In an online-decoding context, this will likely increase with time as more data becomes available.

Implements OnlineFeatureInterface.

Definition at line 575 of file online-feature.h.

575 { return src_->NumFramesReady(); }
OnlineFeatureInterface * src_
virtual int32 NumFramesReady() const =0
returns the feature dimension.

Member Data Documentation

◆ cache_

std::vector<Vector<BaseFloat>* > cache_
private

Definition at line 593 of file online-feature.h.

◆ src_

OnlineFeatureInterface* src_
private

Definition at line 592 of file online-feature.h.


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