|
| 1 | +// nnet3/online-nnet3-decodable.cc |
| 2 | + |
| 3 | +// Copyright 2014 Johns Hopkins University (author: Daniel Povey) |
| 4 | + |
| 5 | +// See ../../COPYING for clarification regarding multiple authors |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 15 | +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 16 | +// MERCHANTABLITY OR NON-INFRINGEMENT. |
| 17 | +// See the Apache 2 License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | + |
| 20 | +#include "nnet3/online-nnet3-decodable.h" |
| 21 | +#include "nnet3/nnet-utils.h" |
| 22 | + |
| 23 | +namespace kaldi { |
| 24 | +namespace nnet3 { |
| 25 | + |
| 26 | +DecodableNnet3Online::DecodableNnet3Online( |
| 27 | + const AmNnetSimple &nnet, |
| 28 | + const TransitionModel &trans_model, |
| 29 | + const DecodableNnet3OnlineOptions &opts, |
| 30 | + OnlineFeatureInterface *input_feats): |
| 31 | + compiler_(nnet.GetNnet(), opts_.optimize_config), |
| 32 | + features_(input_feats), |
| 33 | + nnet_(nnet), |
| 34 | + trans_model_(trans_model), |
| 35 | + opts_(opts), |
| 36 | + feat_dim_(input_feats->Dim()), |
| 37 | + num_pdfs_(nnet.GetNnet().OutputDim("output")), |
| 38 | + begin_frame_(-1) { |
| 39 | + KALDI_ASSERT(opts_.max_nnet_batch_size > 0); |
| 40 | + log_priors_ = nnet_.Priors(); |
| 41 | + KALDI_ASSERT((log_priors_.Dim() == 0 || log_priors_.Dim() == trans_model_.NumPdfs()) && |
| 42 | + "Priors in neural network must match with transition model (if exist)."); |
| 43 | + |
| 44 | + ComputeSimpleNnetContext(nnet_.GetNnet(), &left_context_, &right_context_); |
| 45 | + log_priors_.ApplyLog(); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +BaseFloat DecodableNnet3Online::LogLikelihood(int32 frame, int32 index) { |
| 51 | + ComputeForFrame(frame); |
| 52 | + int32 pdf_id = trans_model_.TransitionIdToPdf(index); |
| 53 | + KALDI_ASSERT(frame >= begin_frame_ && |
| 54 | + frame < begin_frame_ + scaled_loglikes_.NumRows()); |
| 55 | + return scaled_loglikes_(frame - begin_frame_, pdf_id); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +bool DecodableNnet3Online::IsLastFrame(int32 frame) const { |
| 60 | + KALDI_ASSERT(false && "Method is not imlemented"); |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +int32 DecodableNnet3Online::NumFramesReady() const { |
| 65 | + int32 features_ready = features_->NumFramesReady(); |
| 66 | + if (features_ready == 0) |
| 67 | + return 0; |
| 68 | + bool input_finished = features_->IsLastFrame(features_ready - 1); |
| 69 | + if (opts_.pad_input) { |
| 70 | + // normal case... we'll pad with duplicates of first + last frame to get the |
| 71 | + // required left and right context. |
| 72 | + if (input_finished) return subsampling(features_ready); |
| 73 | + else return std::max<int32>(0, subsampling(features_ready - right_context_)); |
| 74 | + } else { |
| 75 | + return std::max<int32>(0, subsampling(features_ready - right_context_ - left_context_)); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +int32 DecodableNnet3Online::subsampling(int32 num_frames) const { |
| 80 | + return (num_frames) / opts_.frame_subsampling_factor; |
| 81 | +} |
| 82 | + |
| 83 | +void DecodableNnet3Online::ComputeForFrame(int32 subsampled_frame) { |
| 84 | + int32 features_ready = features_->NumFramesReady(); |
| 85 | + bool input_finished = features_->IsLastFrame(features_ready - 1); |
| 86 | + KALDI_ASSERT(subsampled_frame >= 0); |
| 87 | + if (subsampled_frame >= begin_frame_ && |
| 88 | + subsampled_frame < begin_frame_ + scaled_loglikes_.NumRows()) |
| 89 | + return; |
| 90 | + KALDI_ASSERT(subsampled_frame < NumFramesReady()); |
| 91 | + |
| 92 | + int32 subsample = opts_.frame_subsampling_factor; |
| 93 | + |
| 94 | + int32 input_frame_begin; |
| 95 | + if (opts_.pad_input) |
| 96 | + input_frame_begin = subsampled_frame * subsample - left_context_; |
| 97 | + else |
| 98 | + input_frame_begin = subsampled_frame * subsample; |
| 99 | + int32 max_possible_input_frame_end = features_ready /* - ( features_ready - right_context_) % subsample */; |
| 100 | + if (input_finished && opts_.pad_input) |
| 101 | + max_possible_input_frame_end += right_context_; |
| 102 | + int32 input_frame_end = std::min<int32>(max_possible_input_frame_end, |
| 103 | + input_frame_begin + |
| 104 | + left_context_ + right_context_ + |
| 105 | + opts_.max_nnet_batch_size); |
| 106 | + KALDI_ASSERT(input_frame_end > input_frame_begin); |
| 107 | + Matrix<BaseFloat> features(input_frame_end - input_frame_begin, |
| 108 | + feat_dim_); |
| 109 | + for (int32 t = input_frame_begin; t < input_frame_end; t++) { |
| 110 | + SubVector<BaseFloat> row(features, t - input_frame_begin); |
| 111 | + int32 t_modified = t; |
| 112 | + // The next two if-statements take care of "pad_input" |
| 113 | + if (t_modified < 0) |
| 114 | + t_modified = 0; |
| 115 | + if (t_modified >= features_ready) |
| 116 | + t_modified = features_ready - 1; |
| 117 | + features_->GetFrame(t_modified, &row); |
| 118 | + } |
| 119 | + |
| 120 | + int32 num_subsampled_frames = subsampling(input_frame_end - input_frame_begin - |
| 121 | + left_context_ - right_context_); |
| 122 | + DoNnetComputation(input_frame_begin, |
| 123 | + features, subsampled_frame * subsample, num_subsampled_frames); |
| 124 | + |
| 125 | + begin_frame_ = subsampled_frame; |
| 126 | +} |
| 127 | + |
| 128 | +void DecodableNnet3Online::DoNnetComputation( |
| 129 | + int32 input_t_start, |
| 130 | + const MatrixBase<BaseFloat> &input_feats, |
| 131 | + int32 output_t_start, |
| 132 | + int32 num_subsampled_frames) { |
| 133 | + ComputationRequest request; |
| 134 | + request.need_model_derivative = false; |
| 135 | + request.store_component_stats = false; |
| 136 | + |
| 137 | + bool shift_time = true; // shift the 'input' and 'output' to a consistent |
| 138 | + // time, to take advantage of caching in the compiler. |
| 139 | + // An optimization. |
| 140 | + int32 time_offset = (shift_time ? -output_t_start : 0); |
| 141 | + |
| 142 | + // First add the regular features-- named "input". |
| 143 | + request.inputs.reserve(2); |
| 144 | + request.inputs.push_back( |
| 145 | + IoSpecification("input", time_offset + input_t_start, |
| 146 | + time_offset + input_t_start + input_feats.NumRows())); |
| 147 | + IoSpecification output_spec; |
| 148 | + output_spec.name = "output"; |
| 149 | + output_spec.has_deriv = false; |
| 150 | + int32 subsample = opts_.frame_subsampling_factor; |
| 151 | + output_spec.indexes.resize(num_subsampled_frames); |
| 152 | + // leave n and x values at 0 (the constructor sets these). |
| 153 | + for (int32 i = 0; i < num_subsampled_frames; i++) |
| 154 | + output_spec.indexes[i].t = time_offset + output_t_start + i * subsample; |
| 155 | + request.outputs.resize(1); |
| 156 | + request.outputs[0].Swap(&output_spec); |
| 157 | + |
| 158 | + const NnetComputation *computation = compiler_.Compile(request); |
| 159 | + Nnet *nnet_to_update = NULL; // we're not doing any update. |
| 160 | + NnetComputer computer(opts_.compute_config, *computation, |
| 161 | + nnet_.GetNnet(), nnet_to_update); |
| 162 | + |
| 163 | + CuMatrix<BaseFloat> input_feats_cu(input_feats); |
| 164 | + computer.AcceptInput("input", &input_feats_cu); |
| 165 | + CuMatrix<BaseFloat> ivector_feats_cu; |
| 166 | + computer.Forward(); |
| 167 | + CuMatrix<BaseFloat> cu_output; |
| 168 | + computer.GetOutputDestructive("output", &cu_output); |
| 169 | + // subtract log-prior (divide by prior) |
| 170 | + if (log_priors_.Dim() != 0) |
| 171 | + cu_output.AddVecToRows(-1.0, log_priors_); |
| 172 | + // apply the acoustic scale |
| 173 | + cu_output.Scale(opts_.acoustic_scale); |
| 174 | + scaled_loglikes_.Resize(0, 0); |
| 175 | + // the following statement just swaps the pointers if we're not using a GPU. |
| 176 | + cu_output.Swap(&scaled_loglikes_); |
| 177 | +// current_log_post_subsampled_offset_ = output_t_start / subsample; |
| 178 | +} |
| 179 | + |
| 180 | +} // namespace nnet3 |
| 181 | +} // namespace kaldi |
0 commit comments