k2-fsa / snowfall

Moved to https://github.com/k2-fsa/icefall
Apache License 2.0
143 stars 42 forks source link

Get empty paths when using k2.random_paths() in the function rescore_with_n_best_list #239

Closed luomingshuang closed 2 years ago

luomingshuang commented 2 years ago

When I use rescore_with_n_best_list to decode, I get an error. The error is as follows:

71326dd7cf1c858886db4b70b060a76

And this error just happens to one sample.
I check the code and try to debug. I find the error happens in the following code line (the 145 line) in the function rescore_with_n_best_list from lm_rescore.py. My num_paths (3, 10, 50, ....) is over 1.

# First, extract `num_paths` paths for each sequence.
# paths is a k2.RaggedInt with axes [seq][path][arc_pos]
paths = k2.random_paths(lats, num_paths=num_paths, use_double_scores=True)

I use the following code to get the shape of paths.

print(paths)
>>> [ [ ] ]
print(paths.shape().tot_size(1))
>>> 0

So, it is obvious that the empty path causes this error. And I draw the fsa svg ( you can download and see the fsa dot picture from this url) about the above lats.

For finishing my testing process, I try to skip this sample by the following code:

paths = k2.random_paths(lats, num_paths=num_paths, use_double_scores=True)
if paths.shape().tot_size(1) == 0:
   print('Get None paths.')
   return None

So, I want to know if this lats is reasonable or it is a special case? Or the function k2.random_paths doesn't consider this case? And are there any other methods to solve this case?

alucassch commented 2 years ago

Same problem here also in one single sample

pkufool commented 2 years ago

Same problem here also in one single sample

In which file and run which script when you encountered this problem, can you post your error log, let's see where the problem actually is. As for @luomingshuang 's issue, I think it has nothing to do with random_path().

alucassch commented 2 years ago

Here is how lattice was created

asr_model is an espnet model object HLG is the decoding graph speech contains speech samples and lengths contains speech shape

enc_out, _ = asr_model.encode(speech, lengths) 

nnet_output = torch.nn.functional.log_softmax(asr_model.ctc.ctc_lo(enc_out), dim=2)  

supervision_segments = torch.tensor([[0, 0, enc_out.shape[1]]], dtype=torch.int32)
indices = torch.tensor([0])

dense_fsa_vec = k2.DenseFsaVec(nnet_output , supervision_segments)

lattices = k2.intersect_dense_pruned(HLG, dense_fsa_vec, 20.0, 8, 30, 10000)

Then with lattices, this is the first line of nbest_decoding

paths = k2.random_paths(lattices , num_paths=10, use_double_scores=True)

And the result paths is

print(paths)

[ [ ] ] 

I saved the lattice file

lattice = torch.save(lattices.as_dict(), "lattice.pt")

and uploaded the Iattice to filebin and I think you could test it like

import torch
import k2

lattices = k2.Fsa.from_dict(torch.load("lattice.pt"))

paths = k2.random_paths(lattices , num_paths=10, use_double_scores=True)

print(paths)
 [ [ ] ] 

If you use this lattice in the nbest_decoding function in mmi_att_transformer_decode.py file you would get the error

from mmi_att_transformer_decode import nbest_decoding

best_paths = nbest_decoding(lattices, 10)

File "mmi_att_transformer_decode.py", line 175, in nbest_decoding
    best_path_fsas.aux_labels = aux_labels
File "source/nn/k2/k2/python/k2/fsa.py", line 408, in __setattr__
    assert value.dim0() == self.arcs.values().shape[0], \
AssertionError: value.dim0(): 0, shape[0]: 1

This happens only with this one test case. All the others are ok