@@ -901,6 +901,12 @@ const CompactLattice& LatticeIncrementalDecoderTpl<FST, Token>::GetLattice(
901901 this will do very little work. */
902902 PruneActiveTokens (config_.lattice_beam * config_.prune_scale );
903903
904+ if (determinizer_.GetLattice ().NumStates () == 0 ||
905+ determinizer_.GetLattice ().Final (0 ) != CompactLatticeWeight::Zero ()) {
906+ num_frames_in_lattice_ = 0 ;
907+ determinizer_.Init ();
908+ }
909+
904910 Lattice chunk_lat;
905911
906912 unordered_map<Label, LatticeArc::StateId> token_label2state;
@@ -1331,7 +1337,7 @@ void LatticeIncrementalDeterminizer::GetRawLatticeFinalCosts(
13311337 if (value.olabel >= (Label)kTokenLabelOffset &&
13321338 value.olabel < (Label)kMaxTokenLabel ) {
13331339 LatticeWeight final_weight = raw_fst.Final (value.nextstate );
1334- if (final_weight == LatticeWeight::Zero () ||
1340+ if (final_weight != LatticeWeight::Zero () &&
13351341 final_weight.Value2 () != 0 ) {
13361342 KALDI_ERR << " Label " << value.olabel << " from state " << s
13371343 << " looks like a token-label but its next-state "
@@ -1356,8 +1362,7 @@ void LatticeIncrementalDeterminizer::GetRawLatticeFinalCosts(
13561362
13571363bool LatticeIncrementalDeterminizer::ProcessArcsFromChunkStartState (
13581364 const CompactLattice &chunk_clat,
1359- std::unordered_map<CompactLattice::StateId, CompactLattice::StateId> *state_map,
1360- CompactLatticeWeight *extra_start_weight) {
1365+ std::unordered_map<CompactLattice::StateId, CompactLattice::StateId> *state_map) {
13611366 using StateId = CompactLattice::StateId;
13621367 StateId clat_num_states = clat_.NumStates ();
13631368
@@ -1408,21 +1413,17 @@ bool LatticeIncrementalDeterminizer::ProcessArcsFromChunkStartState(
14081413 // in_weight is an extra weight that we'll include on arcs entering this
14091414 // state from the previous chunk. We need to cancel out
14101415 // `forward_costs[clat_state]`, which was included in the corresponding arc
1411- // in the raw lattice for pruning purposes; and we need to include
1412- // the weight from the start-state of `chunk_clat` to this state.
1416+ // in the raw lattice for pruning purposes; and we need to include the
1417+ // weight on the arc from the start-state of `chunk_clat` to this state.
14131418 CompactLatticeWeight extra_weight_in = arc.weight ;
14141419 extra_weight_in.SetWeight (
14151420 fst::Times (extra_weight_in.Weight (),
14161421 LatticeWeight (-forward_costs_[clat_state], 0.0 )));
14171422
1418- if (clat_state == 0 ) {
1419- // if clat_state is the star-state of clat_ (state 0), we can't modify
1420- // incoming arcs; we need to modify outgoing arcs, but we'll do that
1421- // later, after we add them.
1422- *extra_start_weight = extra_weight_in;
1423- forward_costs_[0 ] = forward_costs_[0 ] + ConvertToCost (extra_weight_in);
1424- continue ;
1425- }
1423+ // We don't allow state 0 to be a redeterminized-state; calling code assures
1424+ // this. Search for `determinizer_.GetLattice().Final(0) !=
1425+ // CompactLatticeWeight::Zero())` to find that calling code.
1426+ KALDI_ASSERT (clat_state != 0 );
14261427
14271428 // Note: 0 is the start state of clat_. This was checked.
14281429 forward_costs_[clat_state] = (clat_state == 0 ? 0 :
@@ -1431,11 +1432,12 @@ bool LatticeIncrementalDeterminizer::ProcessArcsFromChunkStartState(
14311432 arcs_in.swap (arcs_in_[clat_state]);
14321433 for (auto p: arcs_in) {
14331434 // Note: we'll be doing `continue` below if this input arc came from
1434- // another redeterminized-state, because we did DeleteStates () for them in
1435+ // another redeterminized-state, because we did DeleteArcs () for them in
14351436 // InitializeRawLatticeChunk(). Those arcs will be transferred
14361437 // from chunk_clat later on.
14371438 CompactLattice::StateId src_state = p.first ;
14381439 int32 arc_pos = p.second ;
1440+
14391441 if (arc_pos >= (int32)clat_.NumArcs (src_state))
14401442 continue ;
14411443 fst::MutableArcIterator<CompactLattice> aiter (&clat_, src_state);
@@ -1459,16 +1461,6 @@ bool LatticeIncrementalDeterminizer::ProcessArcsFromChunkStartState(
14591461 return false ; // this is not the first chunk.
14601462}
14611463
1462- void LatticeIncrementalDeterminizer::ReweightStartState (
1463- CompactLatticeWeight &extra_start_weight) {
1464- for (fst::MutableArcIterator<CompactLattice> aiter (&clat_, 0 );
1465- !aiter.Done (); aiter.Next ()) {
1466- CompactLatticeArc arc (aiter.Value ());
1467- arc.weight = fst::Times (extra_start_weight, arc.weight );
1468- aiter.SetValue (arc);
1469- }
1470- }
1471-
14721464void LatticeIncrementalDeterminizer::TransferArcsToClat (
14731465 const CompactLattice &chunk_clat,
14741466 bool is_first_chunk,
@@ -1602,9 +1594,7 @@ bool LatticeIncrementalDeterminizer::AcceptRawLatticeChunk(
16021594 std::unordered_map<StateId, StateId> state_map;
16031595
16041596
1605- CompactLatticeWeight extra_start_weight = CompactLatticeWeight::One ();
1606- bool is_first_chunk = ProcessArcsFromChunkStartState (chunk_clat, &state_map,
1607- &extra_start_weight);
1597+ bool is_first_chunk = ProcessArcsFromChunkStartState (chunk_clat, &state_map);
16081598
16091599 // Remove any existing arcs in clat_ that leave redeterminized-states, and
16101600 // make those states non-final. Below, we'll add arcs leaving those states
@@ -1645,9 +1635,6 @@ bool LatticeIncrementalDeterminizer::AcceptRawLatticeChunk(
16451635 TransferArcsToClat (chunk_clat, is_first_chunk,
16461636 state_map, chunk_state_to_token, old_final_costs);
16471637
1648- if (extra_start_weight != CompactLatticeWeight::One ())
1649- ReweightStartState (extra_start_weight);
1650-
16511638 GetNonFinalRedetStates ();
16521639
16531640 return determinized_till_beam;
0 commit comments