Blame view

src/lat/lattice-functions.cc 64 KB
8dcb6dfcb   Yannick Estève   first commit
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
  // 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;
  
  void GetPerFrameAcousticCosts(const Lattice &nbest, Vector<BaseFloat> *per_frame_loglikes) {
    using namespace fst;
    typedef Lattice::Arc::Weight Weight;
    vector<BaseFloat> loglikes;
  
    int32 cur_state = nbest.Start();
    int32 prev_frame = -1;
    BaseFloat eps_acwt = 0.0;
    while(1) {
      Weight w = nbest.Final(cur_state);
      if (w != Weight::Zero()) {
        KALDI_ASSERT(nbest.NumArcs(cur_state) == 0);
        if (per_frame_loglikes != NULL)  {
          SubVector<BaseFloat> subvec(&(loglikes[0]), loglikes.size());
          Vector<BaseFloat> vec(subvec);
          *per_frame_loglikes = vec;
        }
        break;
      } else {
        KALDI_ASSERT(nbest.NumArcs(cur_state) == 1);
        fst::ArcIterator<Lattice> iter(nbest, cur_state);
        const Lattice::Arc &arc = iter.Value();
        BaseFloat acwt = arc.weight.Value2();
        if (arc.ilabel != 0) {
          if (eps_acwt > 0) {
            acwt += eps_acwt;
            eps_acwt = 0.0;
          }
          loglikes.push_back(acwt);
          prev_frame++;
        } else if (acwt == acwt){
          if (prev_frame > -1) {
            loglikes[prev_frame] += acwt;
          } else {
            eps_acwt += acwt;
          }
        }
        cur_state = arc.nextstate;
      }
    }
  }
  
  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);
  }
  
  template<typename LatticeType>
  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->clear();
    beta->clear();
    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);
  }
  
  // instantiate the template for Lattice and CompactLattice
  template
  double ComputeLatticeAlphasAndBetas(const Lattice &lat,
                                      bool viterbi,
                                      vector<double> *alpha,
                                      vector<double> *beta);
  
  template
  double ComputeLatticeAlphasAndBetas(const CompactLattice &lat,
                                      bool viterbi,
                                      vector<double> *alpha,
                                      vector<double> *beta);
  
  
  
  /// 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) {
          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);
        const std::vector<int32> &arc_alignment = arc.weight.String();
        std::vector<std::vector<int32> > split_alignment;
        SplitToPhones(tmodel, arc_alignment, &split_alignment);
        std::vector<int32> phones(split_alignment.size());
        std::vector<int32> plengths(split_alignment.size());
        for (size_t i = 0; i < split_alignment.size(); i++) {
          KALDI_ASSERT(!split_alignment[i].empty());
          phones[i] = tmodel.TransitionIdToPhone(split_alignment[i][0]);
          plengths[i] = split_alignment[i].size();
        }
        prons->push_back(phones);
        phone_lengths->push_back(plengths);
  
        cur_time += length;
        state = arc.nextstate;
      }
    }
  }
  
  
  
  void CompactLatticeShortestPath(const CompactLattice &clat,
                                  CompactLattice *shortest_path) {
    using namespace fst;
    if (clat.Properties(fst::kTopSorted, true) == 0) {
      CompactLattice clat_copy(clat);
      if (!TopSort(&clat_copy))
        KALDI_ERR << "Was not able to topologically sort lattice (cycles found?)";
      CompactLatticeShortestPath(clat_copy, shortest_path);
      return;
    }
    // Now we can assume it's topologically sorted.
    shortest_path->DeleteStates();
    if (clat.Start() == kNoStateId) return;
    KALDI_ASSERT(clat.Start() == 0); // since top-sorted.
    typedef CompactLatticeArc Arc;
    typedef Arc::StateId StateId;
    typedef CompactLatticeWeight Weight;
    vector<std::pair<double, StateId> > best_cost_and_pred(clat.NumStates() + 1);
    StateId superfinal = clat.NumStates();
    for (StateId s = 0; s <= clat.NumStates(); s++) {
      best_cost_and_pred[s].first = numeric_limits<double>::infinity();
      best_cost_and_pred[s].second = fst::kNoStateId;
    }
    best_cost_and_pred[0].first = 0;
    for (StateId s = 0; s < clat.NumStates(); s++) {
      double my_cost = best_cost_and_pred[s].first;
      for (ArcIterator<CompactLattice> aiter(clat, s);
           !aiter.Done();
           aiter.Next()) {
        const Arc &arc = aiter.Value();
        double arc_cost = ConvertToCost(arc.weight),
            next_cost = my_cost + arc_cost;
        if (next_cost < best_cost_and_pred[arc.nextstate].first) {
          best_cost_and_pred[arc.nextstate].first = next_cost;
          best_cost_and_pred[arc.nextstate].second = s;
        }
      }
      double final_cost = ConvertToCost(clat.Final(s)),
          tot_final = my_cost + final_cost;
      if (tot_final < best_cost_and_pred[superfinal].first) {
        best_cost_and_pred[superfinal].first = tot_final;
        best_cost_and_pred[superfinal].second = s;
      }
    }
    std::vector<StateId> states; // states on best path.
    StateId cur_state = superfinal;
    while (cur_state != 0) {
      StateId prev_state = best_cost_and_pred[cur_state].second;
      if (prev_state == kNoStateId) {
        KALDI_WARN << "Failure in best-path algorithm for lattice (infinite costs?)";
        return; // return empty best-path.
      }
      states.push_back(prev_state);
      KALDI_ASSERT(cur_state != prev_state && "Lattice with cycles");
      cur_state = prev_state;
    }
    std::reverse(states.begin(), states.end());
    for (size_t i = 0; i < states.size(); i++)
      shortest_path->AddState();
    for (StateId s = 0; static_cast<size_t>(s) < states.size(); s++) {
      if (s == 0) shortest_path->SetStart(s);
      if (static_cast<size_t>(s + 1) < states.size()) { // transition to next state.
        bool have_arc = false;
        Arc cur_arc;
        for (ArcIterator<CompactLattice> aiter(clat, states[s]);
             !aiter.Done();
             aiter.Next()) {
          const Arc &arc = aiter.Value();
          if (arc.nextstate == states[s+1]) {
            if (!have_arc ||
                ConvertToCost(arc.weight) < ConvertToCost(cur_arc.weight)) {
              cur_arc = arc;
              have_arc = true;
            }
          }
        }
        KALDI_ASSERT(have_arc && "Code error.");
        shortest_path->AddArc(s, Arc(cur_arc.ilabel, cur_arc.olabel,
                                     cur_arc.weight, s+1));
      } else { // final-prob.
        shortest_path->SetFinal(s, clat.Final(states[s]));
      }
    }
  }
  
  void AddWordInsPenToCompactLattice(BaseFloat word_ins_penalty,
                                     CompactLattice *clat) {
    typedef CompactLatticeArc Arc;
    int32 num_states = clat->NumStates();
  
    //scan the lattice
    for (int32 state = 0; state < num_states; state++) {
      for (fst::MutableArcIterator<CompactLattice> aiter(clat, state);
           !aiter.Done(); aiter.Next()) {
  
        Arc arc(aiter.Value());
  
        if (arc.ilabel != 0) { // if there is a word on this arc
          LatticeWeight weight = arc.weight.Weight();
          // add word insertion penalty to lattice
          weight.SetValue1( weight.Value1() + word_ins_penalty);
          arc.weight.SetWeight(weight);
          aiter.SetValue(arc);
        }
      } // end looping over arcs
    }  // end looping over states
  }
  
  struct ClatRescoreTuple {
    ClatRescoreTuple(int32 state, int32 arc, int32 tid):
        state_id(state), arc_id(arc), tid(tid) { }
    int32 state_id;
    int32 arc_id;
    int32 tid;
  };
  
  /** RescoreCompactLatticeInternal is the internal code for both
      RescoreCompactLattice and RescoreCompatLatticeSpeedup.  For
      RescoreCompactLattice, "tmodel" will be NULL and speedup_factor will be 1.0.
   */
  bool RescoreCompactLatticeInternal(
      const TransitionModel *tmodel,
      BaseFloat speedup_factor,
      DecodableInterface *decodable,
      CompactLattice *clat) {
    KALDI_ASSERT(speedup_factor >= 1.0);
    if (clat->NumStates() == 0) {
      KALDI_WARN << "Rescoring empty lattice";
      return false;
    }
    if (!clat->Properties(fst::kTopSorted, true)) {
      if (fst::TopSort(clat) == false) {
        KALDI_WARN << "Cycles detected in lattice.";
        return false;
      }
    }
    std::vector<int32> state_times;
    int32 utt_len = kaldi::CompactLatticeStateTimes(*clat, &state_times);
  
    std::vector<std::vector<ClatRescoreTuple> > time_to_state(utt_len);
  
    int32 num_states = clat->NumStates();
    KALDI_ASSERT(num_states == state_times.size());
    for (size_t state = 0; state < num_states; state++) {
      KALDI_ASSERT(state_times[state] >= 0);
      int32 t = state_times[state];
      int32 arc_id = 0;
      for (fst::MutableArcIterator<CompactLattice> aiter(clat, state);
           !aiter.Done(); aiter.Next(), arc_id++) {
        CompactLatticeArc arc = aiter.Value();
        std::vector<int32> arc_string = arc.weight.String();
  
        for (size_t offset = 0; offset < arc_string.size(); offset++) {
          if (t < utt_len) { // end state may be past this..
            int32 tid = arc_string[offset];
            time_to_state[t+offset].push_back(ClatRescoreTuple(state, arc_id, tid));
          } else {
            if (t != utt_len) {
              KALDI_WARN << "There appears to be lattice/feature mismatch, "
                         << "aborting.";
              return false;
            }
          }
        }
      }
      if (clat->Final(state) != CompactLatticeWeight::Zero()) {
        arc_id = -1;
        std::vector<int32> arc_string = clat->Final(state).String();
        for (size_t offset = 0; offset < arc_string.size(); offset++) {
          KALDI_ASSERT(t + offset < utt_len); // already checked in
          // CompactLatticeStateTimes, so would be code error.
          time_to_state[t+offset].push_back(
              ClatRescoreTuple(state, arc_id, arc_string[offset]));
        }
      }
    }
  
    for (int32 t = 0; t < utt_len; t++) {
      if ((t < utt_len - 1) && decodable->IsLastFrame(t)) {
        KALDI_WARN << "Features are too short for lattice: utt-len is "
                   << utt_len << ", " << t << " is last frame";
        return false;
      }
      // frame_scale is the scale we put on the computed acoustic probs for this
      // frame.  It will always be 1.0 if tmodel == NULL (i.e. if we are not doing
      // the "speedup" code).  For frames with multiple pdf-ids it will be one.
      // For frames with only one pdf-id, it will equal speedup_factor (>=1.0)
      // with probability 1.0 / speedup_factor, and zero otherwise.  If it is zero,
      // we can avoid computing the probabilities.
      BaseFloat frame_scale = 1.0;
      KALDI_ASSERT(!time_to_state[t].empty());
      if (tmodel != NULL) {
        int32 pdf_id = tmodel->TransitionIdToPdf(time_to_state[t][0].tid);
        bool frame_has_multiple_pdfs = false;
        for (size_t i = 1; i < time_to_state[t].size(); i++) {
          if (tmodel->TransitionIdToPdf(time_to_state[t][i].tid) != pdf_id) {
            frame_has_multiple_pdfs = true;
            break;
          }
        }
        if (frame_has_multiple_pdfs) {
          frame_scale = 1.0;
        } else {
          if (WithProb(1.0 / speedup_factor)) {
            frame_scale = speedup_factor;
          } else {
            frame_scale = 0.0;
          }
        }
        if (frame_scale == 0.0)
          continue; // the code below would be pointless.
      }
  
      for (size_t i = 0; i < time_to_state[t].size(); i++) {
        int32 state = time_to_state[t][i].state_id;
        int32 arc_id = time_to_state[t][i].arc_id;
        int32 tid = time_to_state[t][i].tid;
  
        if (arc_id == -1) { // Final state
          // Access the trans_id
          CompactLatticeWeight curr_clat_weight = clat->Final(state);
  
          // Calculate likelihood
          BaseFloat log_like = decodable->LogLikelihood(t, tid) * frame_scale;
          // update weight
          CompactLatticeWeight new_clat_weight = curr_clat_weight;
          LatticeWeight new_lat_weight = new_clat_weight.Weight();
          new_lat_weight.SetValue2(-log_like + curr_clat_weight.Weight().Value2());
          new_clat_weight.SetWeight(new_lat_weight);
          clat->SetFinal(state, new_clat_weight);
        } else {
          fst::MutableArcIterator<CompactLattice> aiter(clat, state);
  
          aiter.Seek(arc_id);
          CompactLatticeArc arc = aiter.Value();
  
          // Calculate likelihood
          BaseFloat log_like = decodable->LogLikelihood(t, tid) * frame_scale;
          // update weight
          LatticeWeight new_weight = arc.weight.Weight();
          new_weight.SetValue2(-log_like + arc.weight.Weight().Value2());
          arc.weight.SetWeight(new_weight);
          aiter.SetValue(arc);
        }
      }
    }
    return true;
  }
  
  
  bool RescoreCompactLatticeSpeedup(
      const TransitionModel &tmodel,
      BaseFloat speedup_factor,
      DecodableInterface *decodable,
      CompactLattice *clat) {
    return RescoreCompactLatticeInternal(&tmodel, speedup_factor, decodable, clat);
  }
  
  bool RescoreCompactLattice(DecodableInterface *decodable,
                             CompactLattice *clat) {
    return RescoreCompactLatticeInternal(NULL, 1.0, decodable, clat);
  }
  
  
  bool RescoreLattice(DecodableInterface *decodable,
                      Lattice *lat) {
    if (lat->NumStates() == 0) {
      KALDI_WARN << "Rescoring empty lattice";
      return false;
    }
    if (!lat->Properties(fst::kTopSorted, true)) {
      if (fst::TopSort(lat) == false) {
        KALDI_WARN << "Cycles detected in lattice.";
        return false;
      }
    }
    std::vector<int32> state_times;
    int32 utt_len = kaldi::LatticeStateTimes(*lat, &state_times);
  
    std::vector<std::vector<int32> > time_to_state(utt_len );
  
    int32 num_states = lat->NumStates();
    KALDI_ASSERT(num_states == state_times.size());
    for (size_t state = 0; state < num_states; state++) {
      int32 t = state_times[state];
      // Don't check t >= 0 because non-accessible states could have t = -1.
      KALDI_ASSERT(t <= utt_len);
      if (t >= 0 && t < utt_len)
        time_to_state[t].push_back(state);
    }
  
    for (int32 t = 0; t < utt_len; t++) {
      if ((t < utt_len - 1) && decodable->IsLastFrame(t)) {
        KALDI_WARN << "Features are too short for lattice: utt-len is "
                   << utt_len << ", " << t << " is last frame";
        return false;
      }
      for (size_t i = 0; i < time_to_state[t].size(); i++) {
        int32 state = time_to_state[t][i];
        for (fst::MutableArcIterator<Lattice> aiter(lat, state);
             !aiter.Done(); aiter.Next()) {
          LatticeArc arc = aiter.Value();
          if (arc.ilabel != 0) {
            int32 trans_id = arc.ilabel; // Note: it doesn't necessarily
            // have to be a transition-id, just whatever the Decodable
            // object is expecting, but it's normally a transition-id.
  
            BaseFloat log_like = decodable->LogLikelihood(t, trans_id);
            arc.weight.SetValue2(-log_like + arc.weight.Value2());
            aiter.SetValue(arc);
          }
        }
      }
    }
    return true;
  }
  
  
  BaseFloat LatticeForwardBackwardMmi(
      const TransitionModel &tmodel,
      const Lattice &lat,
      const std::vector<int32> &num_ali,
      bool drop_frames,
      bool convert_to_pdf_ids,
      bool cancel,
      Posterior *post) {
    // First compute the MMI posteriors.
  
    Posterior den_post;
    BaseFloat ans = LatticeForwardBackward(lat,
                                           &den_post,
                                           NULL);
  
    Posterior num_post;
    AlignmentToPosterior(num_ali, &num_post);
  
    // Now negate the MMI posteriors and add the numerator
    // posteriors.
    ScalePosterior(-1.0, &den_post);
  
    if (convert_to_pdf_ids) {
      Posterior num_tmp;
      ConvertPosteriorToPdfs(tmodel, num_post, &num_tmp);
      num_tmp.swap(num_post);
      Posterior den_tmp;
      ConvertPosteriorToPdfs(tmodel, den_post, &den_tmp);
      den_tmp.swap(den_post);
    }
  
    MergePosteriors(num_post, den_post,
                    cancel, drop_frames, post);
  
    return ans;
  }
  
  
  int32 LongestSentenceLength(const Lattice &lat) {
    typedef Lattice::Arc Arc;
    typedef Arc::Label Label;
    typedef Arc::StateId StateId;
  
    if (lat.Properties(fst::kTopSorted, true) == 0) {
      Lattice lat_copy(lat);
      if (!TopSort(&lat_copy))
        KALDI_ERR << "Was not able to topologically sort lattice (cycles found?)";
      return LongestSentenceLength(lat_copy);
    }
    std::vector<int32> max_length(lat.NumStates(), 0);
    int32 lattice_max_length = 0;
    for (StateId s = 0; s < lat.NumStates(); s++) {
      int32 this_max_length = max_length[s];
      for (fst::ArcIterator<Lattice> aiter(lat, s); !aiter.Done(); aiter.Next()) {
        const Arc &arc = aiter.Value();
        bool arc_has_word = (arc.olabel != 0);
        StateId nextstate = arc.nextstate;
        KALDI_ASSERT(static_cast<size_t>(nextstate) < max_length.size());
        if (arc_has_word) {
          // A lattice should ideally not have cycles anyway; a cycle with a word
          // on is something very bad.
          KALDI_ASSERT(nextstate > s && "Lattice has cycles with words on.");
          max_length[nextstate] = std::max(max_length[nextstate],
                                           this_max_length + 1);
        } else {
          max_length[nextstate] = std::max(max_length[nextstate],
                                           this_max_length);
        }
      }
      if (lat.Final(s) != LatticeWeight::Zero())
        lattice_max_length = std::max(lattice_max_length, max_length[s]);
    }
    return lattice_max_length;
  }
  
  int32 LongestSentenceLength(const CompactLattice &clat) {
    typedef CompactLattice::Arc Arc;
    typedef Arc::Label Label;
    typedef Arc::StateId StateId;
  
    if (clat.Properties(fst::kTopSorted, true) == 0) {
      CompactLattice clat_copy(clat);
      if (!TopSort(&clat_copy))
        KALDI_ERR << "Was not able to topologically sort lattice (cycles found?)";
      return LongestSentenceLength(clat_copy);
    }
    std::vector<int32> max_length(clat.NumStates(), 0);
    int32 lattice_max_length = 0;
    for (StateId s = 0; s < clat.NumStates(); s++) {
      int32 this_max_length = max_length[s];
      for (fst::ArcIterator<CompactLattice> aiter(clat, s);
           !aiter.Done(); aiter.Next()) {
        const Arc &arc = aiter.Value();
        bool arc_has_word = (arc.ilabel != 0); // note: olabel == ilabel.
        // also note: for normal CompactLattice, e.g. as produced by
        // determinization, all arcs will have nonzero labels, but the user might
        // decide to remplace some of the labels with zero for some reason, and we
        // want to support this.
        StateId nextstate = arc.nextstate;
        KALDI_ASSERT(static_cast<size_t>(nextstate) < max_length.size());
        KALDI_ASSERT(nextstate > s && "CompactLattice has cycles");
        if (arc_has_word)
          max_length[nextstate] = std::max(max_length[nextstate],
                                           this_max_length + 1);
        else
          max_length[nextstate] = std::max(max_length[nextstate],
                                           this_max_length);
      }
      if (clat.Final(s) != CompactLatticeWeight::Zero())
        lattice_max_length = std::max(lattice_max_length, max_length[s]);
    }
    return lattice_max_length;
  }
  
  void ComposeCompactLatticeDeterministic(
      const CompactLattice& clat,
      fst::DeterministicOnDemandFst<fst::StdArc>* det_fst,
      CompactLattice* composed_clat) {
    // StdFst::Arc and CompactLatticeArc has the same StateId type.
    typedef fst::StdArc::StateId StateId;
    typedef fst::StdArc::Weight Weight1;
    typedef CompactLatticeArc::Weight Weight2;
    typedef std::pair<StateId, StateId> StatePair;
    typedef unordered_map<StatePair, StateId, PairHasher<StateId> > MapType;
    typedef MapType::iterator IterType;
  
    // Empties the output FST.
    KALDI_ASSERT(composed_clat != NULL);
    composed_clat->DeleteStates();
  
    MapType state_map;
    std::queue<StatePair> state_queue;
  
    // Sets start state in <composed_clat>.
    StateId start_state = composed_clat->AddState();
    StatePair start_pair(clat.Start(), det_fst->Start());
    composed_clat->SetStart(start_state);
    state_queue.push(start_pair);
    std::pair<IterType, bool> result =
        state_map.insert(std::make_pair(start_pair, start_state));
    KALDI_ASSERT(result.second == true);
  
    // Starts composition here.
    while (!state_queue.empty()) {
      // Gets the first state in the queue.
      StatePair s = state_queue.front();
      StateId s1 = s.first;
      StateId s2 = s.second;
      state_queue.pop();
  
  
      Weight2 clat_final = clat.Final(s1);
      if (clat_final.Weight().Value1() !=
          std::numeric_limits<BaseFloat>::infinity()) {
        // Test for whether the final-prob of state s1 was zero.
        Weight1 det_fst_final = det_fst->Final(s2);
        if (det_fst_final.Value() !=
            std::numeric_limits<BaseFloat>::infinity()) {
          // Test for whether the final-prob of state s2 was zero.  If neither
          // source-state final prob was zero, then we should create final state
          // in fst_composed. We compute the product manually since this is more
          // efficient.
          Weight2 final_weight(LatticeWeight(clat_final.Weight().Value1() +
                                             det_fst_final.Value(),
                                             clat_final.Weight().Value2()),
                               clat_final.String());
          // we can assume final_weight is not Zero(), since neither of
          // the sources was zero.
          KALDI_ASSERT(state_map.find(s) != state_map.end());
          composed_clat->SetFinal(state_map[s], final_weight);
        }
      }
  
      // Loops over pair of edges at s1 and s2.
      for (fst::ArcIterator<CompactLattice> aiter(clat, s1);
           !aiter.Done(); aiter.Next()) {
        const CompactLatticeArc& arc1 = aiter.Value();
        fst::StdArc arc2;
        StateId next_state1 = arc1.nextstate, next_state2;
        bool matched = false;
  
        if (arc1.olabel == 0) {
          // If the symbol on <arc1> is <epsilon>, we transit to the next state
          // for <clat>, but keep <det_fst> at the current state.
          matched = true;
          next_state2 = s2;
        } else {
          // Otherwise try to find the matched arc in <det_fst>.
          matched = det_fst->GetArc(s2, arc1.olabel, &arc2);
          if (matched) {
            next_state2 = arc2.nextstate;
          }
        }
  
        // If matched arc is found in <det_fst>, then we have to add new arcs to
        // <composed_clat>.
        if (matched) {
          StatePair next_state_pair(next_state1, next_state2);
          IterType siter = state_map.find(next_state_pair);
          StateId next_state;
  
          // Adds composed state to <state_map>.
          if (siter == state_map.end()) {
            // If the composed state has not been created yet, create it.
            next_state = composed_clat->AddState();
            std::pair<const StatePair, StateId> next_state_map(next_state_pair,
                                                               next_state);
            std::pair<IterType, bool> result = state_map.insert(next_state_map);
            KALDI_ASSERT(result.second);
            state_queue.push(next_state_pair);
          } else {
            // If the composed state is already in <state_map>, we can directly
            // use that.
            next_state = siter->second;
          }
  
          // Adds arc to <composed_clat>.
          if (arc1.olabel == 0) {
            composed_clat->AddArc(state_map[s],
                                  CompactLatticeArc(arc1.ilabel, 0,
                                                    arc1.weight, next_state));
          } else {
            Weight2 composed_weight(
                LatticeWeight(arc1.weight.Weight().Value1() +
                              arc2.weight.Value(),
                              arc1.weight.Weight().Value2()),
                arc1.weight.String());
            composed_clat->AddArc(state_map[s],
                                  CompactLatticeArc(arc1.ilabel, arc2.olabel,
                                                    composed_weight, next_state));
          }
        }
      }
    }
    fst::Connect(composed_clat);
  }
  
  
  void ComputeAcousticScoresMap(
      const Lattice &lat,
      unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
                                          PairHasher<int32> > *acoustic_scores) {
    // typedef the arc, weight types
    typedef Lattice::Arc Arc;
    typedef Arc::Weight LatticeWeight;
    typedef Arc::StateId StateId;
  
    acoustic_scores->clear();
  
    std::vector<int32> state_times;
    LatticeStateTimes(lat, &state_times);   // Assumes the input is top sorted
  
    KALDI_ASSERT(lat.Start() == 0);
  
    for (StateId s = 0; s < lat.NumStates(); s++) {
      int32 t = state_times[s];
      for (fst::ArcIterator<Lattice> aiter(lat, s); !aiter.Done();
            aiter.Next()) {
        const Arc &arc = aiter.Value();
        const LatticeWeight &weight = arc.weight;
  
        int32 tid = arc.ilabel;
  
        if (tid != 0) {
          unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
            PairHasher<int32> >::iterator it = acoustic_scores->find(std::make_pair(t, tid));
          if (it == acoustic_scores->end()) {
            acoustic_scores->insert(std::make_pair(std::make_pair(t, tid),
                                            std::make_pair(weight.Value2(), 1)));
          } else {
            if (it->second.second == 2
                  && it->second.first / it->second.second != weight.Value2()) {
              KALDI_VLOG(2) << "Transitions on the same frame have different "
                            << "acoustic costs for tid " << tid << "; "
                            << it->second.first / it->second.second
                            << " vs " << weight.Value2();
            }
            it->second.first += weight.Value2();
            it->second.second++;
          }
        } else {
          // Arcs with epsilon input label (tid) must have 0 acoustic cost
          KALDI_ASSERT(weight.Value2() == 0);
        }
      }
  
      LatticeWeight f = lat.Final(s);
      if (f != LatticeWeight::Zero()) {
        // Final acoustic cost must be 0 as we are reading from
        // non-determinized, non-compact lattice
        KALDI_ASSERT(f.Value2() == 0.0);
      }
    }
  }
  
  void ReplaceAcousticScoresFromMap(
      const unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
                                          PairHasher<int32> > &acoustic_scores,
      Lattice *lat) {
    // typedef the arc, weight types
    typedef Lattice::Arc Arc;
    typedef Arc::Weight LatticeWeight;
    typedef Arc::StateId StateId;
  
    TopSortLatticeIfNeeded(lat);
  
    std::vector<int32> state_times;
    LatticeStateTimes(*lat, &state_times);
  
    KALDI_ASSERT(lat->Start() == 0);
  
    for (StateId s = 0; s < lat->NumStates(); s++) {
      int32 t = state_times[s];
      for (fst::MutableArcIterator<Lattice> aiter(lat, s);
            !aiter.Done(); aiter.Next()) {
        Arc arc(aiter.Value());
  
        int32 tid = arc.ilabel;
        if (tid != 0) {
          unordered_map<std::pair<int32, int32>, std::pair<BaseFloat, int32>,
            PairHasher<int32> >::const_iterator it = acoustic_scores.find(std::make_pair(t, tid));
          if (it == acoustic_scores.end()) {
            KALDI_ERR << "Could not find tid " << tid << " at time " << t
                      << " in the acoustic scores map.";
          } else {
            arc.weight.SetValue2(it->second.first / it->second.second);
          }
        } else {
          // For epsilon arcs, set acoustic cost to 0.0
          arc.weight.SetValue2(0.0);
        }
        aiter.SetValue(arc);
      }
  
      LatticeWeight f = lat->Final(s);
      if (f != LatticeWeight::Zero()) {
        // Set final acoustic cost to 0.0
        f.SetValue2(0.0);
        lat->SetFinal(s, f);
      }
    }
  }
  
  }  // namespace kaldi