forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlattice-functions.cc
More file actions
1587 lines (1453 loc) · 58.6 KB
/
lattice-functions.cc
File metadata and controls
1587 lines (1453 loc) · 58.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// lat/lattice-functions.cc
// Copyright 2009-2011 Saarland University (Author: Arnab Ghoshal)
// 2012-2013 Johns Hopkins University (Author: Daniel Povey); Chao Weng;
// Bagher BabaAli
// 2013 Cisco Systems (author: Neha Agrawal) [code modified
// from original code in ../gmmbin/gmm-rescore-lattice.cc]
// 2014 Guoguo Chen
// 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 "lat/lattice-functions.h"
#include "hmm/transition-model.h"
#include "util/stl-utils.h"
#include "base/kaldi-math.h"
#include "hmm/hmm-utils.h"
namespace kaldi {
using std::map;
using std::vector;
int32 LatticeStateTimes(const Lattice &lat, vector<int32> *times) {
if (!lat.Properties(fst::kTopSorted, true))
KALDI_ERR << "Input lattice must be topologically sorted.";
KALDI_ASSERT(lat.Start() == 0);
int32 num_states = lat.NumStates();
times->clear();
times->resize(num_states, -1);
(*times)[0] = 0;
for (int32 state = 0; state < num_states; state++) {
int32 cur_time = (*times)[state];
for (fst::ArcIterator<Lattice> aiter(lat, state); !aiter.Done();
aiter.Next()) {
const LatticeArc &arc = aiter.Value();
if (arc.ilabel != 0) { // Non-epsilon input label on arc
// next time instance
if ((*times)[arc.nextstate] == -1) {
(*times)[arc.nextstate] = cur_time + 1;
} else {
KALDI_ASSERT((*times)[arc.nextstate] == cur_time + 1);
}
} else { // epsilon input label on arc
// Same time instance
if ((*times)[arc.nextstate] == -1)
(*times)[arc.nextstate] = cur_time;
else
KALDI_ASSERT((*times)[arc.nextstate] == cur_time);
}
}
}
return (*std::max_element(times->begin(), times->end()));
}
int32 CompactLatticeStateTimes(const CompactLattice &lat, vector<int32> *times) {
if (!lat.Properties(fst::kTopSorted, true))
KALDI_ERR << "Input lattice must be topologically sorted.";
KALDI_ASSERT(lat.Start() == 0);
int32 num_states = lat.NumStates();
times->clear();
times->resize(num_states, -1);
(*times)[0] = 0;
int32 utt_len = -1;
for (int32 state = 0; state < num_states; state++) {
int32 cur_time = (*times)[state];
for (fst::ArcIterator<CompactLattice> aiter(lat, state); !aiter.Done();
aiter.Next()) {
const CompactLatticeArc &arc = aiter.Value();
int32 arc_len = static_cast<int32>(arc.weight.String().size());
if ((*times)[arc.nextstate] == -1)
(*times)[arc.nextstate] = cur_time + arc_len;
else
KALDI_ASSERT((*times)[arc.nextstate] == cur_time + arc_len);
}
if (lat.Final(state) != CompactLatticeWeight::Zero()) {
int32 this_utt_len = (*times)[state] + lat.Final(state).String().size();
if (utt_len == -1) utt_len = this_utt_len;
else {
if (this_utt_len != utt_len) {
KALDI_WARN << "Utterance does not "
"seem to have a consistent length.";
utt_len = std::max(utt_len, this_utt_len);
}
}
}
}
if (utt_len == -1) {
KALDI_WARN << "Utterance does not have a final-state.";
return 0;
}
return utt_len;
}
bool ComputeCompactLatticeAlphas(const CompactLattice &clat,
vector<double> *alpha) {
using namespace fst;
// typedef the arc, weight types
typedef CompactLattice::Arc Arc;
typedef Arc::Weight Weight;
typedef Arc::StateId StateId;
//Make sure the lattice is topologically sorted.
if (clat.Properties(fst::kTopSorted, true) == 0) {
KALDI_WARN << "Input lattice must be topologically sorted.";
return false;
}
if (clat.Start() != 0) {
KALDI_WARN << "Input lattice must start from state 0.";
return false;
}
int32 num_states = clat.NumStates();
(*alpha).resize(0);
(*alpha).resize(num_states, kLogZeroDouble);
// Now propagate alphas forward. Note that we don't acount the weight of the
// final state to alpha[final_state] -- we acount it to beta[final_state];
(*alpha)[0] = 0.0;
for (StateId s = 0; s < num_states; s++) {
double this_alpha = (*alpha)[s];
for (ArcIterator<CompactLattice> aiter(clat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -(arc.weight.Weight().Value1() + arc.weight.Weight().Value2());
(*alpha)[arc.nextstate] = LogAdd((*alpha)[arc.nextstate], this_alpha + arc_like);
}
}
return true;
}
bool ComputeCompactLatticeBetas(const CompactLattice &clat,
vector<double> *beta) {
using namespace fst;
// typedef the arc, weight types
typedef CompactLattice::Arc Arc;
typedef Arc::Weight Weight;
typedef Arc::StateId StateId;
// Make sure the lattice is topologically sorted.
if (clat.Properties(fst::kTopSorted, true) == 0) {
KALDI_WARN << "Input lattice must be topologically sorted.";
return false;
}
if (clat.Start() != 0) {
KALDI_WARN << "Input lattice must start from state 0.";
return false;
}
int32 num_states = clat.NumStates();
(*beta).resize(0);
(*beta).resize(num_states, kLogZeroDouble);
// Now propagate betas backward. Note that beta[final_state] contains the
// weight of the final state in the lattice -- compare that with alpha.
for (StateId s = num_states-1; s >= 0; s--) {
Weight f = clat.Final(s);
double this_beta = -(f.Weight().Value1()+f.Weight().Value2());
for (ArcIterator<CompactLattice> aiter(clat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -(arc.weight.Weight().Value1()+arc.weight.Weight().Value2());
double arc_beta = (*beta)[arc.nextstate] + arc_like;
this_beta = LogAdd(this_beta, arc_beta);
}
(*beta)[s] = this_beta;
}
return true;
}
template<class LatType> // could be Lattice or CompactLattice
bool PruneLattice(BaseFloat beam, LatType *lat) {
typedef typename LatType::Arc Arc;
typedef typename Arc::Weight Weight;
typedef typename Arc::StateId StateId;
KALDI_ASSERT(beam > 0.0);
if (!lat->Properties(fst::kTopSorted, true)) {
if (fst::TopSort(lat) == false) {
KALDI_WARN << "Cycles detected in lattice";
return false;
}
}
// We assume states before "start" are not reachable, since
// the lattice is topologically sorted.
int32 start = lat->Start();
int32 num_states = lat->NumStates();
if (num_states == 0) return false;
std::vector<double> forward_cost(num_states,
std::numeric_limits<double>::infinity()); // viterbi forward.
forward_cost[start] = 0.0; // lattice can't have cycles so couldn't be
// less than this.
double best_final_cost = std::numeric_limits<double>::infinity();
// Update the forward probs.
// Thanks to Jing Zheng for finding a bug here.
for (int32 state = 0; state < num_states; state++) {
double this_forward_cost = forward_cost[state];
for (fst::ArcIterator<LatType> aiter(*lat, state);
!aiter.Done();
aiter.Next()) {
const Arc &arc(aiter.Value());
StateId nextstate = arc.nextstate;
KALDI_ASSERT(nextstate > state && nextstate < num_states);
double next_forward_cost = this_forward_cost +
ConvertToCost(arc.weight);
if (forward_cost[nextstate] > next_forward_cost)
forward_cost[nextstate] = next_forward_cost;
}
Weight final_weight = lat->Final(state);
double this_final_cost = this_forward_cost +
ConvertToCost(final_weight);
if (this_final_cost < best_final_cost)
best_final_cost = this_final_cost;
}
int32 bad_state = lat->AddState(); // this state is not final.
double cutoff = best_final_cost + beam;
// Go backwards updating the backward probs (which share memory with the
// forward probs), and pruning arcs and deleting final-probs. We prune arcs
// by making them point to the non-final state "bad_state". We'll then use
// Trim() to remove unnecessary arcs and states. [this is just easier than
// doing it ourselves.]
std::vector<double> &backward_cost(forward_cost);
for (int32 state = num_states - 1; state >= 0; state--) {
double this_forward_cost = forward_cost[state];
double this_backward_cost = ConvertToCost(lat->Final(state));
if (this_backward_cost + this_forward_cost > cutoff
&& this_backward_cost != std::numeric_limits<double>::infinity())
lat->SetFinal(state, Weight::Zero());
for (fst::MutableArcIterator<LatType> aiter(lat, state);
!aiter.Done();
aiter.Next()) {
Arc arc(aiter.Value());
StateId nextstate = arc.nextstate;
KALDI_ASSERT(nextstate > state && nextstate < num_states);
double arc_cost = ConvertToCost(arc.weight),
arc_backward_cost = arc_cost + backward_cost[nextstate],
this_fb_cost = this_forward_cost + arc_backward_cost;
if (arc_backward_cost < this_backward_cost)
this_backward_cost = arc_backward_cost;
if (this_fb_cost > cutoff) { // Prune the arc.
arc.nextstate = bad_state;
aiter.SetValue(arc);
}
}
backward_cost[state] = this_backward_cost;
}
fst::Connect(lat);
return (lat->NumStates() > 0);
}
// instantiate the template for lattice and CompactLattice.
template bool PruneLattice(BaseFloat beam, Lattice *lat);
template bool PruneLattice(BaseFloat beam, CompactLattice *lat);
BaseFloat LatticeForwardBackward(const Lattice &lat, Posterior *post,
double *acoustic_like_sum) {
// Note, Posterior is defined as follows: Indexed [frame], then a list
// of (transition-id, posterior-probability) pairs.
// typedef std::vector<std::vector<std::pair<int32, BaseFloat> > > Posterior;
using namespace fst;
typedef Lattice::Arc Arc;
typedef Arc::Weight Weight;
typedef Arc::StateId StateId;
if (acoustic_like_sum) *acoustic_like_sum = 0.0;
// Make sure the lattice is topologically sorted.
if (lat.Properties(fst::kTopSorted, true) == 0)
KALDI_ERR << "Input lattice must be topologically sorted.";
KALDI_ASSERT(lat.Start() == 0);
int32 num_states = lat.NumStates();
vector<int32> state_times;
int32 max_time = LatticeStateTimes(lat, &state_times);
std::vector<double> alpha(num_states, kLogZeroDouble);
std::vector<double> &beta(alpha); // we re-use the same memory for
// this, but it's semantically distinct so we name it differently.
double tot_forward_prob = kLogZeroDouble;
post->clear();
post->resize(max_time);
alpha[0] = 0.0;
// Propagate alphas forward.
for (StateId s = 0; s < num_states; s++) {
double this_alpha = alpha[s];
for (ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight);
alpha[arc.nextstate] = LogAdd(alpha[arc.nextstate], this_alpha + arc_like);
}
Weight f = lat.Final(s);
if (f != Weight::Zero()) {
double final_like = this_alpha - (f.Value1() + f.Value2());
tot_forward_prob = LogAdd(tot_forward_prob, final_like);
KALDI_ASSERT(state_times[s] == max_time &&
"Lattice is inconsistent (final-prob not at max_time)");
}
}
for (StateId s = num_states-1; s >= 0; s--) {
Weight f = lat.Final(s);
double this_beta = -(f.Value1() + f.Value2());
for (ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight),
arc_beta = beta[arc.nextstate] + arc_like;
this_beta = LogAdd(this_beta, arc_beta);
int32 transition_id = arc.ilabel;
// The following "if" is an optimization to avoid un-needed exp().
if (transition_id != 0 || acoustic_like_sum != NULL) {
double posterior = Exp(alpha[s] + arc_beta - tot_forward_prob);
if (transition_id != 0) // Arc has a transition-id on it [not epsilon]
(*post)[state_times[s]].push_back(std::make_pair(transition_id,
static_cast<kaldi::BaseFloat>(posterior)));
if (acoustic_like_sum != NULL)
*acoustic_like_sum -= posterior * arc.weight.Value2();
}
}
if (acoustic_like_sum != NULL && f != Weight::Zero()) {
double final_logprob = - ConvertToCost(f),
posterior = Exp(alpha[s] + final_logprob - tot_forward_prob);
*acoustic_like_sum -= posterior * f.Value2();
}
beta[s] = this_beta;
}
double tot_backward_prob = beta[0];
if (!ApproxEqual(tot_forward_prob, tot_backward_prob, 1e-8)) {
KALDI_WARN << "Total forward probability over lattice = " << tot_forward_prob
<< ", while total backward probability = " << tot_backward_prob;
}
// Now combine any posteriors with the same transition-id.
for (int32 t = 0; t < max_time; t++)
MergePairVectorSumming(&((*post)[t]));
return tot_backward_prob;
}
void LatticeActivePhones(const Lattice &lat, const TransitionModel &trans,
const vector<int32> &silence_phones,
vector< std::set<int32> > *active_phones) {
KALDI_ASSERT(IsSortedAndUniq(silence_phones));
vector<int32> state_times;
int32 num_states = lat.NumStates();
int32 max_time = LatticeStateTimes(lat, &state_times);
active_phones->clear();
active_phones->resize(max_time);
for (int32 state = 0; state < num_states; state++) {
int32 cur_time = state_times[state];
for (fst::ArcIterator<Lattice> aiter(lat, state); !aiter.Done();
aiter.Next()) {
const LatticeArc &arc = aiter.Value();
if (arc.ilabel != 0) { // Non-epsilon arc
int32 phone = trans.TransitionIdToPhone(arc.ilabel);
if (!std::binary_search(silence_phones.begin(),
silence_phones.end(), phone))
(*active_phones)[cur_time].insert(phone);
}
} // end looping over arcs
} // end looping over states
}
void ConvertLatticeToPhones(const TransitionModel &trans,
Lattice *lat) {
typedef LatticeArc Arc;
int32 num_states = lat->NumStates();
for (int32 state = 0; state < num_states; state++) {
for (fst::MutableArcIterator<Lattice> aiter(lat, state); !aiter.Done();
aiter.Next()) {
Arc arc(aiter.Value());
arc.olabel = 0; // remove any word.
if ((arc.ilabel != 0) // has a transition-id on input..
&& (trans.TransitionIdToHmmState(arc.ilabel) == 0)
&& (!trans.IsSelfLoop(arc.ilabel)))
// && trans.IsFinal(arc.ilabel)) // there is one of these per phone...
arc.olabel = trans.TransitionIdToPhone(arc.ilabel);
aiter.SetValue(arc);
} // end looping over arcs
} // end looping over states
}
static inline double LogAddOrMax(bool viterbi, double a, double b) {
if (viterbi)
return std::max(a, b);
else
return LogAdd(a, b);
}
// Computes (normal or Viterbi) alphas and betas; returns (total-prob, or
// best-path negated cost) Note: in either case, the alphas and betas are
// negated costs. Requires that lat be topologically sorted. This code
// will work for either CompactLattice or Latice.
template<typename LatticeType>
static double ComputeLatticeAlphasAndBetas(const LatticeType &lat,
bool viterbi,
vector<double> *alpha,
vector<double> *beta) {
typedef typename LatticeType::Arc Arc;
typedef typename Arc::Weight Weight;
typedef typename Arc::StateId StateId;
StateId num_states = lat.NumStates();
KALDI_ASSERT(lat.Properties(fst::kTopSorted, true) == fst::kTopSorted);
KALDI_ASSERT(lat.Start() == 0);
alpha->resize(num_states, kLogZeroDouble);
beta->resize(num_states, kLogZeroDouble);
double tot_forward_prob = kLogZeroDouble;
(*alpha)[0] = 0.0;
// Propagate alphas forward.
for (StateId s = 0; s < num_states; s++) {
double this_alpha = (*alpha)[s];
for (fst::ArcIterator<LatticeType> aiter(lat, s); !aiter.Done();
aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight);
(*alpha)[arc.nextstate] = LogAddOrMax(viterbi, (*alpha)[arc.nextstate],
this_alpha + arc_like);
}
Weight f = lat.Final(s);
if (f != Weight::Zero()) {
double final_like = this_alpha - ConvertToCost(f);
tot_forward_prob = LogAddOrMax(viterbi, tot_forward_prob, final_like);
}
}
for (StateId s = num_states-1; s >= 0; s--) { // it's guaranteed signed.
double this_beta = -ConvertToCost(lat.Final(s));
for (fst::ArcIterator<LatticeType> aiter(lat, s); !aiter.Done();
aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight),
arc_beta = (*beta)[arc.nextstate] + arc_like;
this_beta = LogAddOrMax(viterbi, this_beta, arc_beta);
}
(*beta)[s] = this_beta;
}
double tot_backward_prob = (*beta)[lat.Start()];
if (!ApproxEqual(tot_forward_prob, tot_backward_prob, 1e-8)) {
KALDI_WARN << "Total forward probability over lattice = " << tot_forward_prob
<< ", while total backward probability = " << tot_backward_prob;
}
// Split the difference when returning... they should be the same.
return 0.5 * (tot_backward_prob + tot_forward_prob);
}
/// This is used in CompactLatticeLimitDepth.
struct LatticeArcRecord {
BaseFloat logprob; // logprob <= 0 is the best Viterbi logprob of this arc,
// minus the overall best-cost of the lattice.
CompactLatticeArc::StateId state; // state in the lattice.
size_t arc; // arc index within the state.
bool operator < (const LatticeArcRecord &other) const {
return logprob < other.logprob;
}
};
void CompactLatticeLimitDepth(int32 max_depth_per_frame,
CompactLattice *clat) {
typedef CompactLatticeArc Arc;
typedef Arc::Weight Weight;
typedef Arc::StateId StateId;
if (clat->Start() == fst::kNoStateId) {
KALDI_WARN << "Limiting depth of empty lattice.";
return;
}
if (clat->Properties(fst::kTopSorted, true) == 0) {
if (!TopSort(clat))
KALDI_ERR << "Topological sorting of lattice failed.";
}
vector<int32> state_times;
int32 T = CompactLatticeStateTimes(*clat, &state_times);
// The alpha and beta quantities here are "viterbi" alphas and beta.
std::vector<double> alpha;
std::vector<double> beta;
bool viterbi = true;
double best_prob = ComputeLatticeAlphasAndBetas(*clat, viterbi,
&alpha, &beta);
std::vector<std::vector<LatticeArcRecord> > arc_records(T);
StateId num_states = clat->NumStates();
for (StateId s = 0; s < num_states; s++) {
for (fst::ArcIterator<CompactLattice> aiter(*clat, s); !aiter.Done();
aiter.Next()) {
const Arc &arc = aiter.Value();
LatticeArcRecord arc_record;
arc_record.state = s;
arc_record.arc = aiter.Position();
arc_record.logprob =
(alpha[s] + beta[arc.nextstate] - ConvertToCost(arc.weight))
- best_prob;
KALDI_ASSERT(arc_record.logprob < 0.1); // Should be zero or negative.
int32 num_frames = arc.weight.String().size(), start_t = state_times[s];
for (int32 t = start_t; t < start_t + num_frames; t++) {
KALDI_ASSERT(t < T);
arc_records[t].push_back(arc_record);
}
}
}
StateId dead_state = clat->AddState(); // A non-coaccesible state which we use
// to remove arcs (make them end
// there).
size_t max_depth = max_depth_per_frame;
for (int32 t = 0; t < T; t++) {
size_t size = arc_records[t].size();
if (size > max_depth) {
// we sort from worst to best, so we keep the later-numbered ones,
// and delete the lower-numbered ones.
size_t cutoff = size - max_depth;
std::nth_element(arc_records[t].begin(),
arc_records[t].begin() + cutoff,
arc_records[t].end());
for (size_t index = 0; index < cutoff; index++) {
LatticeArcRecord record(arc_records[t][index]);
fst::MutableArcIterator<CompactLattice> aiter(clat, record.state);
aiter.Seek(record.arc);
Arc arc = aiter.Value();
if (arc.nextstate != dead_state) { // not already killed.
arc.nextstate = dead_state;
aiter.SetValue(arc);
}
}
}
}
Connect(clat);
TopSortCompactLatticeIfNeeded(clat);
}
void TopSortCompactLatticeIfNeeded(CompactLattice *clat) {
if (clat->Properties(fst::kTopSorted, true) == 0) {
if (fst::TopSort(clat) == false) {
KALDI_ERR << "Topological sorting failed";
}
}
}
void TopSortLatticeIfNeeded(Lattice *lat) {
if (lat->Properties(fst::kTopSorted, true) == 0) {
if (fst::TopSort(lat) == false) {
KALDI_ERR << "Topological sorting failed";
}
}
}
/// Returns the depth of the lattice, defined as the average number of
/// arcs crossing any given frame. Returns 1 for empty lattices.
/// Requires that input is topologically sorted.
BaseFloat CompactLatticeDepth(const CompactLattice &clat,
int32 *num_frames) {
typedef CompactLattice::Arc::StateId StateId;
if (clat.Properties(fst::kTopSorted, true) == 0) {
KALDI_ERR << "Lattice input to CompactLatticeDepth was not topologically "
<< "sorted.";
}
if (clat.Start() == fst::kNoStateId) {
*num_frames = 0;
return 1.0;
}
size_t num_arc_frames = 0;
int32 t;
{
vector<int32> state_times;
t = CompactLatticeStateTimes(clat, &state_times);
}
if (num_frames != NULL)
*num_frames = t;
for (StateId s = 0; s < clat.NumStates(); s++) {
for (fst::ArcIterator<CompactLattice> aiter(clat, s); !aiter.Done();
aiter.Next()) {
const CompactLatticeArc &arc = aiter.Value();
num_arc_frames += arc.weight.String().size();
}
num_arc_frames += clat.Final(s).String().size();
}
return num_arc_frames / static_cast<BaseFloat>(t);
}
void CompactLatticeDepthPerFrame(const CompactLattice &clat,
std::vector<int32> *depth_per_frame) {
typedef CompactLattice::Arc::StateId StateId;
if (clat.Properties(fst::kTopSorted, true) == 0) {
KALDI_ERR << "Lattice input to CompactLatticeDepthPerFrame was not "
<< "topologically sorted.";
}
if (clat.Start() == fst::kNoStateId) {
depth_per_frame->clear();
return;
}
vector<int32> state_times;
int32 T = CompactLatticeStateTimes(clat, &state_times);
depth_per_frame->clear();
if (T <= 0) {
return;
} else {
depth_per_frame->resize(T, 0);
for (StateId s = 0; s < clat.NumStates(); s++) {
int32 start_time = state_times[s];
for (fst::ArcIterator<CompactLattice> aiter(clat, s); !aiter.Done();
aiter.Next()) {
const CompactLatticeArc &arc = aiter.Value();
int32 len = arc.weight.String().size();
for (int32 t = start_time; t < start_time + len; t++) {
KALDI_ASSERT(t < T);
(*depth_per_frame)[t]++;
}
}
int32 final_len = clat.Final(s).String().size();
for (int32 t = start_time; t < start_time + final_len; t++) {
KALDI_ASSERT(t < T);
(*depth_per_frame)[t]++;
}
}
}
}
void ConvertCompactLatticeToPhones(const TransitionModel &trans,
CompactLattice *clat) {
typedef CompactLatticeArc Arc;
typedef Arc::Weight Weight;
int32 num_states = clat->NumStates();
for (int32 state = 0; state < num_states; state++) {
for (fst::MutableArcIterator<CompactLattice> aiter(clat, state);
!aiter.Done();
aiter.Next()) {
Arc arc(aiter.Value());
std::vector<int32> phone_seq;
const std::vector<int32> &tid_seq = arc.weight.String();
for (std::vector<int32>::const_iterator iter = tid_seq.begin();
iter != tid_seq.end(); ++iter) {
if (trans.IsFinal(*iter))// note: there is one of these per phone...
phone_seq.push_back(trans.TransitionIdToPhone(*iter));
}
arc.weight.SetString(phone_seq);
aiter.SetValue(arc);
} // end looping over arcs
Weight f = clat->Final(state);
if (f != Weight::Zero()) {
std::vector<int32> phone_seq;
const std::vector<int32> &tid_seq = f.String();
for (std::vector<int32>::const_iterator iter = tid_seq.begin();
iter != tid_seq.end(); ++iter) {
if (trans.IsFinal(*iter))// note: there is one of these per phone...
phone_seq.push_back(trans.TransitionIdToPhone(*iter));
}
f.SetString(phone_seq);
clat->SetFinal(state, f);
}
} // end looping over states
}
bool LatticeBoost(const TransitionModel &trans,
const std::vector<int32> &alignment,
const std::vector<int32> &silence_phones,
BaseFloat b,
BaseFloat max_silence_error,
Lattice *lat) {
TopSortLatticeIfNeeded(lat);
// get all stored properties (test==false means don't test if not known).
uint64 props = lat->Properties(fst::kFstProperties,
false);
KALDI_ASSERT(IsSortedAndUniq(silence_phones));
KALDI_ASSERT(max_silence_error >= 0.0 && max_silence_error <= 1.0);
vector<int32> state_times;
int32 num_states = lat->NumStates();
int32 num_frames = LatticeStateTimes(*lat, &state_times);
KALDI_ASSERT(num_frames == static_cast<int32>(alignment.size()));
for (int32 state = 0; state < num_states; state++) {
int32 cur_time = state_times[state];
for (fst::MutableArcIterator<Lattice> aiter(lat, state); !aiter.Done();
aiter.Next()) {
LatticeArc arc = aiter.Value();
if (arc.ilabel != 0) { // Non-epsilon arc
if (arc.ilabel < 0 || arc.ilabel > trans.NumTransitionIds()) {
KALDI_WARN << "Lattice has out-of-range transition-ids: "
<< "lattice/model mismatch?";
return false;
}
int32 phone = trans.TransitionIdToPhone(arc.ilabel),
ref_phone = trans.TransitionIdToPhone(alignment[cur_time]);
BaseFloat frame_error;
if (phone == ref_phone) {
frame_error = 0.0;
} else { // an error...
if (std::binary_search(silence_phones.begin(), silence_phones.end(), phone))
frame_error = max_silence_error;
else
frame_error = 1.0;
}
BaseFloat delta_cost = -b * frame_error; // negative cost if
// frame is wrong, to boost likelihood of arcs with errors on them.
// Add this cost to the graph part.
arc.weight.SetValue1(arc.weight.Value1() + delta_cost);
aiter.SetValue(arc);
}
}
}
// All we changed is the weights, so any properties that were
// known before, are still known, except for whether or not the
// lattice was weighted.
lat->SetProperties(props,
~(fst::kWeighted|fst::kUnweighted));
return true;
}
BaseFloat LatticeForwardBackwardMpeVariants(
const TransitionModel &trans,
const std::vector<int32> &silence_phones,
const Lattice &lat,
const std::vector<int32> &num_ali,
std::string criterion,
bool one_silence_class,
Posterior *post) {
using namespace fst;
typedef Lattice::Arc Arc;
typedef Arc::Weight Weight;
typedef Arc::StateId StateId;
KALDI_ASSERT(criterion == "mpfe" || criterion == "smbr");
bool is_mpfe = (criterion == "mpfe");
if (lat.Properties(fst::kTopSorted, true) == 0)
KALDI_ERR << "Input lattice must be topologically sorted.";
KALDI_ASSERT(lat.Start() == 0);
int32 num_states = lat.NumStates();
vector<int32> state_times;
int32 max_time = LatticeStateTimes(lat, &state_times);
KALDI_ASSERT(max_time == static_cast<int32>(num_ali.size()));
std::vector<double> alpha(num_states, kLogZeroDouble),
alpha_smbr(num_states, 0), //forward variable for sMBR
beta(num_states, kLogZeroDouble),
beta_smbr(num_states, 0); //backward variable for sMBR
double tot_forward_prob = kLogZeroDouble;
double tot_forward_score = 0;
post->clear();
post->resize(max_time);
alpha[0] = 0.0;
// First Pass Forward,
for (StateId s = 0; s < num_states; s++) {
double this_alpha = alpha[s];
for (ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight);
alpha[arc.nextstate] = LogAdd(alpha[arc.nextstate], this_alpha + arc_like);
}
Weight f = lat.Final(s);
if (f != Weight::Zero()) {
double final_like = this_alpha - (f.Value1() + f.Value2());
tot_forward_prob = LogAdd(tot_forward_prob, final_like);
KALDI_ASSERT(state_times[s] == max_time &&
"Lattice is inconsistent (final-prob not at max_time)");
}
}
// First Pass Backward,
for (StateId s = num_states-1; s >= 0; s--) {
Weight f = lat.Final(s);
double this_beta = -(f.Value1() + f.Value2());
for (ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight),
arc_beta = beta[arc.nextstate] + arc_like;
this_beta = LogAdd(this_beta, arc_beta);
}
beta[s] = this_beta;
}
// First Pass Forward-Backward Check
double tot_backward_prob = beta[0];
// may loose the condition somehow here 1e-6 (was 1e-8)
if (!ApproxEqual(tot_forward_prob, tot_backward_prob, 1e-6)) {
KALDI_ERR << "Total forward probability over lattice = " << tot_forward_prob
<< ", while total backward probability = " << tot_backward_prob;
}
alpha_smbr[0] = 0.0;
// Second Pass Forward, calculate forward for MPFE/SMBR
for (StateId s = 0; s < num_states; s++) {
double this_alpha = alpha[s];
for (ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight);
double frame_acc = 0.0;
if (arc.ilabel != 0) {
int32 cur_time = state_times[s];
int32 phone = trans.TransitionIdToPhone(arc.ilabel),
ref_phone = trans.TransitionIdToPhone(num_ali[cur_time]);
bool phone_is_sil = std::binary_search(silence_phones.begin(),
silence_phones.end(),
phone),
ref_phone_is_sil = std::binary_search(silence_phones.begin(),
silence_phones.end(),
ref_phone),
both_sil = phone_is_sil && ref_phone_is_sil;
if (!is_mpfe) { // smbr.
int32 pdf = trans.TransitionIdToPdf(arc.ilabel),
ref_pdf = trans.TransitionIdToPdf(num_ali[cur_time]);
if (!one_silence_class) // old behavior
frame_acc = (pdf == ref_pdf && !phone_is_sil) ? 1.0 : 0.0;
else
frame_acc = (pdf == ref_pdf || both_sil) ? 1.0 : 0.0;
} else {
if (!one_silence_class) // old behavior
frame_acc = (phone == ref_phone && !phone_is_sil) ? 1.0 : 0.0;
else
frame_acc = (phone == ref_phone || both_sil) ? 1.0 : 0.0;
}
}
double arc_scale = Exp(alpha[s] + arc_like - alpha[arc.nextstate]);
alpha_smbr[arc.nextstate] += arc_scale * (alpha_smbr[s] + frame_acc);
}
Weight f = lat.Final(s);
if (f != Weight::Zero()) {
double final_like = this_alpha - (f.Value1() + f.Value2());
double arc_scale = Exp(final_like - tot_forward_prob);
tot_forward_score += arc_scale * alpha_smbr[s];
KALDI_ASSERT(state_times[s] == max_time &&
"Lattice is inconsistent (final-prob not at max_time)");
}
}
// Second Pass Backward, collect Mpe style posteriors
for (StateId s = num_states-1; s >= 0; s--) {
for (ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
const Arc &arc = aiter.Value();
double arc_like = -ConvertToCost(arc.weight),
arc_beta = beta[arc.nextstate] + arc_like;
double frame_acc = 0.0;
int32 transition_id = arc.ilabel;
if (arc.ilabel != 0) {
int32 cur_time = state_times[s];
int32 phone = trans.TransitionIdToPhone(arc.ilabel),
ref_phone = trans.TransitionIdToPhone(num_ali[cur_time]);
bool phone_is_sil = std::binary_search(silence_phones.begin(),
silence_phones.end(), phone),
ref_phone_is_sil = std::binary_search(silence_phones.begin(),
silence_phones.end(),
ref_phone),
both_sil = phone_is_sil && ref_phone_is_sil;
if (!is_mpfe) { // smbr.
int32 pdf = trans.TransitionIdToPdf(arc.ilabel),
ref_pdf = trans.TransitionIdToPdf(num_ali[cur_time]);
if (!one_silence_class) // old behavior
frame_acc = (pdf == ref_pdf && !phone_is_sil) ? 1.0 : 0.0;
else
frame_acc = (pdf == ref_pdf || both_sil) ? 1.0 : 0.0;
} else {
if (!one_silence_class) // old behavior
frame_acc = (phone == ref_phone && !phone_is_sil) ? 1.0 : 0.0;
else
frame_acc = (phone == ref_phone || both_sil) ? 1.0 : 0.0;
}
}
double arc_scale = Exp(beta[arc.nextstate] + arc_like - beta[s]);
// check arc_scale NAN,
// this is to prevent partial paths in Lattices
// i.e., paths don't survive to the final state
if (KALDI_ISNAN(arc_scale)) arc_scale = 0;
beta_smbr[s] += arc_scale * (beta_smbr[arc.nextstate] + frame_acc);
if (transition_id != 0) { // Arc has a transition-id on it [not epsilon]
double posterior = Exp(alpha[s] + arc_beta - tot_forward_prob);
double acc_diff = alpha_smbr[s] + frame_acc + beta_smbr[arc.nextstate]
- tot_forward_score;
double posterior_smbr = posterior * acc_diff;
(*post)[state_times[s]].push_back(std::make_pair(transition_id,
static_cast<BaseFloat>(posterior_smbr)));
}
}
}
//Second Pass Forward Backward check
double tot_backward_score = beta_smbr[0]; // Initial state id == 0
// may loose the condition somehow here 1e-5/1e-4
if (!ApproxEqual(tot_forward_score, tot_backward_score, 1e-4)) {
KALDI_ERR << "Total forward score over lattice = " << tot_forward_score
<< ", while total backward score = " << tot_backward_score;
}
// Output the computed posteriors
for (int32 t = 0; t < max_time; t++)
MergePairVectorSumming(&((*post)[t]));
return tot_forward_score;
}
bool CompactLatticeToWordAlignment(const CompactLattice &clat,
std::vector<int32> *words,
std::vector<int32> *begin_times,
std::vector<int32> *lengths) {
words->clear();
begin_times->clear();
lengths->clear();
typedef CompactLattice::Arc Arc;
typedef Arc::Label Label;
typedef CompactLattice::StateId StateId;
typedef CompactLattice::Weight Weight;
using namespace fst;
StateId state = clat.Start();
int32 cur_time = 0;
if (state == kNoStateId) {
KALDI_WARN << "Empty lattice.";
return false;
}
while (1) {
Weight final = clat.Final(state);
size_t num_arcs = clat.NumArcs(state);
if (final != Weight::Zero()) {
if (num_arcs != 0) {
KALDI_WARN << "Lattice is not linear.";
return false;
}
if (! final.String().empty()) {
KALDI_WARN << "Lattice has alignments on final-weight: probably "
"was not word-aligned (alignments will be approximate)";
}
return true;
} else {
if (num_arcs != 1) {
KALDI_WARN << "Lattice is not linear: num-arcs = " << num_arcs;
return false;
}
fst::ArcIterator<CompactLattice> aiter(clat, state);
const Arc &arc = aiter.Value();
Label word_id = arc.ilabel; // Note: ilabel==olabel, since acceptor.
// Also note: word_id may be zero; we output it anyway.
int32 length = arc.weight.String().size();
words->push_back(word_id);
begin_times->push_back(cur_time);
lengths->push_back(length);
cur_time += length;
state = arc.nextstate;
}
}
}
bool CompactLatticeToWordProns(
const TransitionModel &tmodel,
const CompactLattice &clat,
std::vector<int32> *words,
std::vector<int32> *begin_times,
std::vector<int32> *lengths,
std::vector<std::vector<int32> > *prons,
std::vector<std::vector<int32> > *phone_lengths) {
words->clear();
begin_times->clear();
lengths->clear();
prons->clear();
phone_lengths->clear();
typedef CompactLattice::Arc Arc;
typedef Arc::Label Label;
typedef CompactLattice::StateId StateId;
typedef CompactLattice::Weight Weight;
using namespace fst;
StateId state = clat.Start();
int32 cur_time = 0;
if (state == kNoStateId) {
KALDI_WARN << "Empty lattice.";
return false;
}
while (1) {
Weight final = clat.Final(state);
size_t num_arcs = clat.NumArcs(state);
if (final != Weight::Zero()) {
if (num_arcs != 0) {