Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor decode.py to make it more readable and more modular. #44

Merged
merged 19 commits into from
Sep 20, 2021

Conversation

csukuangfj
Copy link
Collaborator

No description provided.

nbest2 = nbest.intersect(lattice)
tot_scores = nbest2.tot_scores()
argmax = tot_scores.argmax()
best_path = k2.index_fsa(nbest2.fsa, argmax)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows how to do nbest decoding.

It is more readable than before, I think.

Copy link
Collaborator

@pkufool pkufool Sep 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a implementation of rescoring nbest with attention decoder in #5, the return value is also a Nbest.

icefall/icefall/decode.py

Lines 892 to 900 in 355e324

def rescore_nbest_with_attention_decoder(
nbest: Nbest,
model: nn.Module,
memory: torch.Tensor,
memory_key_padding_mask: torch.Tensor,
sos_id: int,
eos_id: int,
) -> Nbest:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cool.
I'll let Fangjun decide what to do about this.

Yes I think the code in this PR is clearer than before

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, Fangjun could refactor decoding as his plan first, I will merge with his code later. It will cost more time to make that PR work, I am afraid :(

Nbest.fsa should always have token IDs as labels and
word IDs as aux_labels.
@danpovey
Copy link
Collaborator

Why not use the Nbest that's in k2? Just wondering.
Also, this seems to only be a partial replacement of the current code?

@danpovey
Copy link
Collaborator

.. of course if it's more convenient to work on the class in Icefall, to avoid compatibility issues, that's fine with me.

@danpovey
Copy link
Collaborator

danpovey commented Sep 13, 2021 via email

@csukuangfj csukuangfj changed the title Refactor decode.py to make it more readable and more modular. WIP: Refactor decode.py to make it more readable and more modular. Sep 13, 2021
levenshtein_graphs = [levenshtein_graph(ids) for ids in word_ids_list]
refs = k2.Fsa.from_fsas(levenshtein_graphs).to(device)

# Now compute the edit distance between hyps and refs
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows how to use k2 to compute the levenshtein distance among multiple sequences at the same time.
(Ideas are from @danpovey)

If anyone is interested in it, I wrote a colab notebook to demonstrate it step by step
https://colab.research.google.com/drive/1hAo5RUb8cMY4qhdn8HASJu9foXrRdo6m?usp=sharing

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s pretty cool, does it mean we could use it directly to optimize WER as a loss function?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would have to be some kind of expectation involved for that to work. There are other uses for this though.

@csukuangfj csukuangfj changed the title WIP: Refactor decode.py to make it more readable and more modular. Refactor decode.py to make it more readable and more modular. Sep 18, 2021
@csukuangfj
Copy link
Collaborator Author

This pull-request is ready for review.
(Will use k2.levenshtein_alignment and k2.levenshtein_graph once k2-fsa/k2#828 gets merged)

There are fewer code duplicates. Also, it is more readable, I believe.

It produces the same WER as the previous code. Would be great if someone can test it using an existing model.


I can move class Nbest to k2 if @danpovey you think that looks nicer.
(The code for Nbest here has several new functions (e.g., compute_am_scores, compute_lm_scores) )

@danpovey
Copy link
Collaborator

Cool!
Regarding moving that code to k2, we should definitely do that at some point, but I'm OK to keep it temporarily here if that would reduce compatibility issues in the short term. We can also duplicate it with here and k2, and later remove it from k2.

@csukuangfj csukuangfj added ready and removed ready labels Sep 20, 2021
@csukuangfj csukuangfj removed the ready label Sep 20, 2021
@csukuangfj
Copy link
Collaborator Author

Ok, I am merging it.

icefall now requires k2 >= v1.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants