decodable-matrix.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // decoder/decodable-matrix.cc
  2. // Copyright 2018 Johns Hopkins University (author: Daniel Povey)
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  13. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  14. // MERCHANTABLITY OR NON-INFRINGEMENT.
  15. // See the Apache 2 License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include "decoder/decodable-matrix.h"
  18. namespace kaldi {
  19. DecodableMatrixMapped::DecodableMatrixMapped(
  20. const TransitionInformation &tm,
  21. const MatrixBase<BaseFloat> &likes,
  22. int32 frame_offset):
  23. trans_model_(tm),
  24. tid_to_pdf_(trans_model_.TransitionIdToPdfArray()),
  25. likes_(&likes), likes_to_delete_(NULL),
  26. frame_offset_(frame_offset) {
  27. stride_ = likes.Stride();
  28. raw_data_ = likes.Data() - (stride_ * frame_offset);
  29. if (likes.NumCols() != tm.NumPdfs())
  30. KALDI_ERR << "Mismatch, matrix has "
  31. << likes.NumCols() << " cols but transition-model has "
  32. << tm.NumPdfs() << " pdf-ids.";
  33. }
  34. DecodableMatrixMapped::DecodableMatrixMapped(
  35. const TransitionInformation &tm, const Matrix<BaseFloat> *likes,
  36. int32 frame_offset):
  37. trans_model_(tm),
  38. tid_to_pdf_(trans_model_.TransitionIdToPdfArray()),
  39. likes_(likes), likes_to_delete_(likes),
  40. frame_offset_(frame_offset) {
  41. stride_ = likes->Stride();
  42. raw_data_ = likes->Data() - (stride_ * frame_offset_);
  43. if (likes->NumCols() != tm.NumPdfs())
  44. KALDI_ERR << "Mismatch, matrix has "
  45. << likes->NumCols() << " cols but transition-model has "
  46. << tm.NumPdfs() << " pdf-ids.";
  47. }
  48. BaseFloat DecodableMatrixMapped::LogLikelihood(int32 frame, int32 tid) {
  49. KALDI_PARANOID_ASSERT(tid >= 1 && tid < tid_to_pdf_.size());
  50. int32 pdf_id = tid_to_pdf_[tid];
  51. #ifdef KALDI_PARANOID
  52. return (*likes_)(frame - frame_offset_, pdf_id);
  53. #else
  54. return raw_data_[frame * stride_ + pdf_id];
  55. #endif
  56. }
  57. int32 DecodableMatrixMapped::NumFramesReady() const {
  58. return frame_offset_ + likes_->NumRows();
  59. }
  60. bool DecodableMatrixMapped::IsLastFrame(int32 frame) const {
  61. KALDI_ASSERT(frame < NumFramesReady());
  62. return (frame == NumFramesReady() - 1);
  63. }
  64. // Indices are one-based! This is for compatibility with OpenFst.
  65. int32 DecodableMatrixMapped::NumIndices() const {
  66. return trans_model_.NumTransitionIds();
  67. }
  68. DecodableMatrixMapped::~DecodableMatrixMapped() {
  69. delete likes_to_delete_;
  70. }
  71. void DecodableMatrixMappedOffset::AcceptLoglikes(
  72. Matrix<BaseFloat> *loglikes, int32 frames_to_discard) {
  73. if (loglikes->NumRows() == 0) return;
  74. KALDI_ASSERT(loglikes->NumCols() == trans_model_.NumPdfs());
  75. KALDI_ASSERT(frames_to_discard <= loglikes_.NumRows() &&
  76. frames_to_discard >= 0);
  77. if (frames_to_discard == loglikes_.NumRows()) {
  78. loglikes_.Swap(loglikes);
  79. loglikes->Resize(0, 0);
  80. } else {
  81. int32 old_rows_kept = loglikes_.NumRows() - frames_to_discard,
  82. new_num_rows = old_rows_kept + loglikes->NumRows();
  83. Matrix<BaseFloat> new_loglikes(new_num_rows, loglikes->NumCols());
  84. new_loglikes.RowRange(0, old_rows_kept).CopyFromMat(
  85. loglikes_.RowRange(frames_to_discard, old_rows_kept));
  86. new_loglikes.RowRange(old_rows_kept, loglikes->NumRows()).CopyFromMat(
  87. *loglikes);
  88. loglikes_.Swap(&new_loglikes);
  89. }
  90. frame_offset_ += frames_to_discard;
  91. stride_ = loglikes_.Stride();
  92. raw_data_ = loglikes_.Data() - (frame_offset_ * stride_);
  93. }
  94. } // end namespace kaldi.