Skip to content

Commit a4fb201

Browse files
authored
[src] Fix issue found by Remi Francis in incremental decoder, causing failure (#3874)
1 parent 56ee192 commit a4fb201

File tree

2 files changed

+19
-55
lines changed

2 files changed

+19
-55
lines changed

src/decoder/lattice-incremental-decoder.cc

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

13571363
bool 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-
14721464
void 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;

src/decoder/lattice-incremental-decoder.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct LatticeIncrementalDecoderConfig {
138138
prune_interval(25),
139139
beam_delta(0.5),
140140
hash_ratio(2.0),
141-
prune_scale(0.1),
141+
prune_scale(0.01),
142142
determinize_max_delay(60),
143143
determinize_min_chunk_size(20) {
144144
det_opts.minimize = false;
@@ -328,34 +328,11 @@ class LatticeIncrementalDeterminizer {
328328
processing
329329
@param [out] state_map Mapping from states in chunk_clat to
330330
the state in clat_ they correspond to.
331-
@param [out] extra_start_weight If the start-state of
332-
clat_ (its state 0) needs to be modified as
333-
if its incoming arcs were multiplied by
334-
`extra_start_weight`, this isn't possible
335-
using the `in_arcs_` data-structure,
336-
so we remember the extra weight and multiply
337-
it in later, after processing arcs leaving
338-
the start state of clat_. This is set
339-
only if the start-state of clat_ is a
340-
redeterminized state.
341331
@return Returns true if this is the first chunk.
342332
*/
343333
bool ProcessArcsFromChunkStartState(
344334
const CompactLattice &chunk_clat,
345-
std::unordered_map<CompactLattice::StateId, CompactLattice::StateId> *state_map,
346-
CompactLatticeWeight *extra_start_weight);
347-
348-
/**
349-
This function, called from AcceptRawLatticeChunk(), takes care of an
350-
unusual situation where we need to reweight the start state of clat_. This
351-
`extra_start_weight` is to be thought of as an extra `incoming` weight, and
352-
we need to left-multiply all the arcs leaving the start state, by it.
353-
354-
This function does not need to modify forward_costs_; that will
355-
already have been done by ProcessArcsFromChunkStartState().
356-
*/
357-
void ReweightStartState(CompactLatticeWeight &extra_start_weight);
358-
335+
std::unordered_map<CompactLattice::StateId, CompactLattice::StateId> *state_map);
359336

360337
/**
361338
This function, called from AcceptRawLatticeChunk(), transfers arcs from

0 commit comments

Comments
 (0)