online-timing.cc
Go to the documentation of this file.
1 // online2/online-timing.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 "online2/online-timing.h"
21 
22 namespace kaldi {
23 
25  num_utts_(0), total_audio_(0.0), total_time_taken_(0.0),
26  total_time_waited_(0.0), max_delay_(0.0) {
27 }
28 
29 void OnlineTimingStats::Print(bool online){
30  if (online) {
31  double real_time_factor = total_time_taken_ / total_audio_,
32  average_wait = (total_time_taken_ - total_audio_) / num_utts_,
33  idle_proportion = total_time_waited_ / total_audio_,
34  idle_percent = 100.0 * idle_proportion;
35 
36  KALDI_LOG << "Timing stats: real-time factor was " << real_time_factor
37  << " (note: this cannot be less than one.)";
38  KALDI_LOG << "Average delay was " << average_wait << " seconds.";
39  if (idle_percent != 0.0) {
40  // If the user was calling SleepUntil instead of WaitUntil, this will
41  // always be zero; so don't print it in that case.
42  KALDI_LOG << "Percentage of time spent idling was " << idle_percent;
43  }
44  KALDI_LOG << "Longest delay was " << max_delay_ << " seconds for utterance "
45  << '\'' << max_delay_utt_ << '\'';
46  } else {
47  // we have processed each utterance in one chunk.
48  // the decoding code will have "pretended to wait" (using WaitUntil())
49  // till the end of the utterance before starting to decode it.
50  // We want to subtract this waiting time, which is not really
51  // of interest in an offline scenario, before printing the
52  // real-time factor.
53  double real_time_factor = (total_time_taken_ - total_time_waited_) /
55  KALDI_LOG << "Timing stats: real-time factor for offline decoding was "
56  << real_time_factor << " = "
57  << (total_time_taken_ - total_time_waited_) << " seconds "
58  << " / " << total_audio_ << " seconds.";
59  }
60 }
61 
62 OnlineTimer::OnlineTimer(const std::string &utterance_id):
63  utterance_id_(utterance_id), waited_(0.0), utterance_length_(0.0) { }
64 
65 void OnlineTimer::WaitUntil(double cur_utterance_length) {
66  double elapsed = timer_.Elapsed();
67  // it's been cur_utterance_length seconds since we would have
68  // started processing this utterance, in a real-time decoding
69  // scenario. We've been actually processing it for "elapsed"
70  // seconds, plus we would have been waiting on some kind of
71  // semaphore for waited_ seconds. If we have to wait further
72  // at this point, increase "waited_".
73  // (I have to think of a better way of explaining this).
74  double to_wait = cur_utterance_length - (elapsed + waited_);
75  if (to_wait > 0.0)
76  waited_ += to_wait;
77 
78  utterance_length_ = cur_utterance_length;
79 }
80 
81 void OnlineTimer::SleepUntil(double cur_utterance_length) {
82  KALDI_ASSERT(waited_ == 0 && "Do not mix SleepUntil with WaitUntil.");
83  double elapsed = timer_.Elapsed();
84 
85  double to_wait = cur_utterance_length - elapsed;
86  if (to_wait > 0.0) {
87  Sleep(to_wait);
88  }
89  utterance_length_ = cur_utterance_length;
90 }
91 
93  return timer_.Elapsed() + waited_;
94 }
95 
97  double processing_time = timer_.Elapsed() + waited_,
98  wait_time = processing_time - utterance_length_;
99  if (wait_time < 0.0) {
100  // My first though was to make this a KALDI_ERR, but perhaps
101  // clocks can go backwards under some weird circumstance, so
102  // let's just make it a warning.
103  KALDI_WARN << "Negative wait time " << wait_time
104  << " does not make sense.";
105  }
106  KALDI_VLOG(2) << "Latency " << wait_time << " seconds out of "
107  << utterance_length_ << ", for utterance "
108  << utterance_id_;
109 
110  stats->num_utts_++;
112  stats->total_time_taken_ += processing_time;
113  stats->total_time_waited_ += waited_;
114  if (wait_time > stats->max_delay_) {
115  stats->max_delay_ = wait_time;
116  stats->max_delay_utt_ = utterance_id_;
117  }
118 }
119 
120 
121 } // namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
std::string utterance_id_
void Sleep(float seconds)
Definition: kaldi-utils.cc:45
void OutputStats(OnlineTimingStats *stats)
This call, which should be made after decoding is done, writes the stats to the object that accumulat...
void Print(bool online=true)
Here, if "online == false" we take into account that the setup was used in not-really-online mode whe...
void SleepUntil(double cur_utterance_length)
The call to SleepUntil(t) will sleep until cur_utterance_length seconds after this object was initial...
#define KALDI_WARN
Definition: kaldi-error.h:150
double Elapsed()
Returns the simulated time elapsed in seconds since the timer was started; this equals waited_ plus t...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
class OnlineTimingStats stores statistics from timing of online decoding, which will enable the Print...
Definition: online-timing.h:41
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
void WaitUntil(double cur_utterance_length)
The call to WaitUntil(t) simulates the effect of sleeping until cur_utterance_length seconds after th...
OnlineTimer(const std::string &utterance_id)