-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathonline-nnet3-wake-word-faster-decoder.cc
205 lines (185 loc) · 6.91 KB
/
online-nnet3-wake-word-faster-decoder.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// online2/online-nnet3-wake-word-faster-decoder.cc
// Copyright 2019 Daniel Povey
// 2019 Yiming Wang
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "base/timer.h"
#include "online-nnet3-wake-word-faster-decoder.h"
#include "fstext/fstext-utils.h"
#include "hmm/hmm-utils.h"
namespace kaldi {
void OnlineWakeWordFasterDecoder::InitDecoding() {
ClearToks(toks_.Clear());
StateId start_state = fst_.Start();
KALDI_ASSERT(start_state != fst::kNoStateId);
Arc dummy_arc(0, 0, Weight::One(), start_state);
Token *dummy_token = new Token(dummy_arc, NULL);
toks_.Insert(start_state, dummy_token);
ProcessNonemitting(std::numeric_limits<float>::max());
num_frames_decoded_ = 0;
prev_immortal_tok_ = immortal_tok_ = dummy_token;
}
void OnlineWakeWordFasterDecoder::MakeLattice(const Token *start,
const Token *end, fst::MutableFst<LatticeArc> *out_fst) const {
out_fst->DeleteStates();
if (start == NULL) return;
bool is_final = false;
double this_cost = start->cost_ + fst_.Final(start->arc_.nextstate).Value();
if (this_cost != std::numeric_limits<double>::infinity())
is_final = true;
std::vector<LatticeArc> arcs_reverse; // arcs in reverse order.
for (const Token *tok = start; tok != end; tok = tok->prev_) {
BaseFloat tot_cost = tok->cost_ -
(tok->prev_ ? tok->prev_->cost_ : 0.0),
graph_cost = tok->arc_.weight.Value(),
ac_cost = tot_cost - graph_cost;
LatticeArc l_arc(tok->arc_.ilabel,
tok->arc_.olabel,
LatticeWeight(graph_cost, ac_cost),
tok->arc_.nextstate);
arcs_reverse.push_back(l_arc);
}
if(arcs_reverse.back().nextstate == fst_.Start()) {
arcs_reverse.pop_back(); // that was a "fake" token... gives no info.
}
StateId cur_state = out_fst->AddState();
out_fst->SetStart(cur_state);
for (ssize_t i = static_cast<ssize_t>(arcs_reverse.size())-1; i >= 0; i--) {
LatticeArc arc = arcs_reverse[i];
arc.nextstate = out_fst->AddState();
out_fst->AddArc(cur_state, arc);
cur_state = arc.nextstate;
}
if (is_final) {
Weight final_weight = fst_.Final(start->arc_.nextstate);
out_fst->SetFinal(cur_state, LatticeWeight(final_weight.Value(), 0.0));
} else {
out_fst->SetFinal(cur_state, LatticeWeight::One());
}
RemoveEpsLocal(out_fst);
}
void OnlineWakeWordFasterDecoder::UpdateImmortalToken() {
unordered_set<Token*> emitting;
for (const Elem *e = toks_.GetList(); e != NULL; e = e->tail) {
Token* tok = e->val;
while (tok != NULL && tok->arc_.ilabel == 0) //deal with non-emitting ones ...
tok = tok->prev_;
if (tok != NULL)
emitting.insert(tok);
}
Token* the_one = NULL;
while (1) {
if (emitting.size() == 1) {
the_one = *(emitting.begin());
break;
}
if (emitting.size() == 0)
break;
unordered_set<Token*> prev_emitting;
unordered_set<Token*>::iterator it;
for (it = emitting.begin(); it != emitting.end(); ++it) {
Token* tok = *it;
Token* prev_token = tok->prev_;
while ((prev_token != NULL) && (prev_token->arc_.ilabel == 0))
prev_token = prev_token->prev_; //deal with non-emitting ones
if (prev_token == NULL)
continue;
prev_emitting.insert(prev_token);
} // for
emitting = prev_emitting;
} // while
if (the_one != NULL) {
prev_immortal_tok_ = immortal_tok_;
immortal_tok_ = the_one;
return;
}
}
bool OnlineWakeWordFasterDecoder::PartialTraceback(
fst::MutableFst<LatticeArc> *out_fst) {
UpdateImmortalToken();
if(immortal_tok_ == prev_immortal_tok_)
return false; //no partial traceback at that point of time
MakeLattice(immortal_tok_, prev_immortal_tok_, out_fst);
return true;
}
void OnlineWakeWordFasterDecoder::FinishTraceBack(
fst::MutableFst<LatticeArc> *out_fst) {
Token *best_tok = NULL;
bool is_final = ReachedFinal();
if (!is_final) {
for (const Elem *e = toks_.GetList(); e != NULL; e = e->tail)
if (best_tok == NULL || *best_tok < *(e->val) )
best_tok = e->val;
} else {
double best_cost = std::numeric_limits<double>::infinity();
for (const Elem *e = toks_.GetList(); e != NULL; e = e->tail) {
double this_cost = e->val->cost_ + fst_.Final(e->key).Value();
if (this_cost != std::numeric_limits<double>::infinity() &&
this_cost < best_cost) {
best_cost = this_cost;
best_tok = e->val;
}
}
}
MakeLattice(best_tok, immortal_tok_, out_fst);
}
void OnlineWakeWordFasterDecoder::TracebackNFrames(int32 nframes,
fst::MutableFst<LatticeArc> *out_fst) {
Token *best_tok = NULL;
for (const Elem *e = toks_.GetList(); e != NULL; e = e->tail)
if (best_tok == NULL || *best_tok < *(e->val) )
best_tok = e->val;
if (best_tok == NULL) {
out_fst->DeleteStates();
return;
}
bool is_final = false;
double this_cost = best_tok->cost_ +
fst_.Final(best_tok->arc_.nextstate).Value();
if (this_cost != std::numeric_limits<double>::infinity())
is_final = true;
std::vector<LatticeArc> arcs_reverse; // arcs in reverse order.
for (Token *tok = best_tok; (tok != NULL) && (nframes > 0); tok = tok->prev_) {
if (tok->arc_.ilabel != 0) // count only the non-epsilon arcs
--nframes;
BaseFloat tot_cost = tok->cost_ -
(tok->prev_ ? tok->prev_->cost_ : 0.0);
BaseFloat graph_cost = tok->arc_.weight.Value();
BaseFloat ac_cost = tot_cost - graph_cost;
LatticeArc larc(tok->arc_.ilabel,
tok->arc_.olabel,
LatticeWeight(graph_cost, ac_cost),
tok->arc_.nextstate);
arcs_reverse.push_back(larc);
}
if(arcs_reverse.back().nextstate == fst_.Start())
arcs_reverse.pop_back(); // that was a "fake" token... gives no info.
StateId cur_state = out_fst->AddState();
out_fst->SetStart(cur_state);
for (ssize_t i = static_cast<ssize_t>(arcs_reverse.size())-1; i >= 0; i--) {
LatticeArc arc = arcs_reverse[i];
arc.nextstate = out_fst->AddState();
out_fst->AddArc(cur_state, arc);
cur_state = arc.nextstate;
}
if (is_final) {
Weight final_weight = fst_.Final(best_tok->arc_.nextstate);
out_fst->SetFinal(cur_state, LatticeWeight(final_weight.Value(), 0.0));
} else {
out_fst->SetFinal(cur_state, LatticeWeight::One());
}
RemoveEpsLocal(out_fst);
}
} // namespace kaldi