Added Feature: Prefix decoding for wav2vec2 models
Author: deepang17
Pull request: yes

# What does this PR do?
Added the code for prefix decoding for wav2vec2 based models.
<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #11283 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

@patrickvonplaten @patil-suraj 

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->


Comments:
- [x]  Currently the code supports prefix decoding without LM. I am still working to integrate the kenlm version.

Problem faced currently: I created a custom kenlm and tried to run the code, but it stops without throwing any error at line `results = self.decoder.decode(emissions_ptr, T, N)` I am currently trying to fix it. (RESOLVED)

- [x] Shall I create a .sh or .txt file to guide on how to install flashlight dependencies?

Performance: 
                      Model:- `facebook/wav2vec2-base-960h`
                      Dataset:- `timit_asr`, `clean`, `test[:5%]`
                      Viterbi Decoding:- `wer: 0.115`
                      KenLM Decoding:- `wer: 0.098` 

Wuhuhu! This is an amazing contribution @deepang17 - Super exciting to merge this notebook :-) And yes, it would be great if you could add a section to the README.md that explains how to use your script + maybe with some results (using Prefix decoding vs. not using it on *e.g.* Timit_asr and/or Librispeech evaluation - kinda like you already did above). I'm also very happy to help you run some evals! 

Thank you for the appreciation. I will do the required changes to `README.md` and push a commit soon.

> * [x]   Currently the code supports prefix decoding without LM. I am still working to integrate the kenlm version.
> 
> Problem faced currently: I created a custom kenlm and tried to run the code, but it stops without throwing any error at line `results = self.decoder.decode(emissions_ptr, T, N)` I am currently trying to fix it. (RESOLVED)
> 
> * [x]  Shall I create a .sh or .txt file to guide on how to install flashlight dependencies?

@deepang17
Did you pushed that fix? I've tried your code and it is crushing at the "self.decoder.decode". What was your fix?

What is the status of this PR?


> > * [x]    Currently the code supports prefix decoding without LM. I am still working to integrate the kenlm version.
> > 
> > Problem faced currently: I created a custom kenlm and tried to run the code, but it stops without throwing any error at line `results = self.decoder.decode(emissions_ptr, T, N)` I am currently trying to fix it. (RESOLVED)
> > 
> > * [x]   Shall I create a .sh or .txt file to guide on how to install flashlight dependencies?
> 
> @deepang17
> Did you pushed that fix? I've tried your code and it is crushing at the "self.decoder.decode". What was your fix?
> 
> What is the status of this PR?

You can fix it by replacing `!cmake .. -DCMAKE_BUILD_TYPE=Release -DKENLM_MAX_ORDER=20 -DCMAKE_POSITION_INDEPENDENT_CODE=ON` to `!cmake ..`

> > > * [x]     Currently the code supports prefix decoding without LM. I am still working to integrate the kenlm version.
> > > 
> > > Problem faced currently: I created a custom kenlm and tried to run the code, but it stops without throwing any error at line `results = self.decoder.decode(emissions_ptr, T, N)` I am currently trying to fix it. (RESOLVED)
> > > 
> > > * [x]    Shall I create a .sh or .txt file to guide on how to install flashlight dependencies?
> > 
> > 
> > @deepang17
> > Did you pushed that fix? I've tried your code and it is crushing at the "self.decoder.decode". What was your fix?
> > What is the status of this PR?
> 
> You can fix it by replacing `!cmake .. -DCMAKE_BUILD_TYPE=Release -DKENLM_MAX_ORDER=20 -DCMAKE_POSITION_INDEPENDENT_CODE=ON` to `!cmake ..`

Can you please publish a Google Colab or a bash script to do the installation? I could't figure out where to do the change you suggested in the build, I'v used the Google Colab example from flashlight.

@deepang17 Thank you for your amazing work!
I made Google Colab to reproduce this pull request. @samuelazran You can check this.
https://colab.research.google.com/drive/1HHEBS3I4biQ8ZDyfJDtHi4E4onOtYe46?usp=sharing

Viterbi decoding works well, but KenLM decoding has the following error.

```
  File "run_wav2vec2_eval_with_lm.py", line 292, in <module>
    main()
  File "run_wav2vec2_eval_with_lm.py", line 281, in main
    results = selected_dataset.map(map_to_result)
  File "/usr/local/lib/python3.7/dist-packages/datasets/arrow_dataset.py", line 1606, in map
    desc=desc,
  File "/usr/local/lib/python3.7/dist-packages/datasets/arrow_dataset.py", line 176, in wrapper
    out: Union["Dataset", "DatasetDict"] = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/datasets/fingerprint.py", line 397, in wrapper
    out = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/datasets/arrow_dataset.py", line 1911, in _map_single
    example = apply_function_on_filtered_inputs(example, i, offset=offset)
  File "/usr/local/lib/python3.7/dist-packages/datasets/arrow_dataset.py", line 1826, in apply_function_on_filtered_inputs
    function(*fn_args, effective_indices, **fn_kwargs) if with_indices else function(*fn_args, **fn_kwargs)
  File "run_wav2vec2_eval_with_lm.py", line 265, in map_to_result
    decoder = W2lKenLMDecoder(eval_args, target_dictionary)
  File "run_wav2vec2_eval_with_lm.py", line 201, in __init__
    self.lm = KenLM(args.kenlm_model, self.word_dict)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. flashlight.lib.text.flashlight_lib_text_decoder.KenLM(path: str, usr_token_dict: fl::lib::text::Dictionary)

Invoked with: None, <flashlight.lib.text.flashlight_lib_text_dictionary.Dictionary object at 0x7fe0ef7294b0>
```

@deepang17  Do you know this error?  It exactly gives as an argument of flashlight.lib.text.flashlight_lib_text_decoder.KenLM the dict obtained from flashlight.lib.text.dictionary.create_word_dict.

@deepang17 - do you have updates regarding the README.md script? :-) I can take over the PR by next week otherwise!

Hello @patrickvonplaten, Sorry for the delay. I was occupied due to some personal issues. I am on the verge of completing the README.md. I will commit the updated README soon.

@deepang17  Any updates?

@deepang17 Any updates? 

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

This PR seems to be stuck since quite some time now. Is anyone interested in finishing / testing this PR? 

Might be better to start fresh otherwise with a blog post ) colab that explains how to make a complete ASR end-to-end system - cc @anton-l 

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

@patrickvonplaten 
I'm in! I've searched this topic but it seems there is no official implementation on this topic, and It would be so nice to add this feature. If this feature is still in the backlog, I would be happy to contribute. Looking forward to your alert!

Hey @hbasafa,

I'm now working on this topic full time. 

We will most likely foster a closer collaboration between [pyctcdecode](https://github.com/kensho-technologies/pyctcdecode) and Transformers. [Here](https://github.com/patrickvonplaten/Wav2Vec2_PyCTCDecode) is a github repo that shows how to use `pyctcdecode` with Wav2Vec2 for LM supported decoding. It works quite well with KenLM.

Nice one! I will check it out.

As I was in hurry, I've already used [this code](https://github.com/hbasafa/py-ctc-decode) that could be easily installed via pip.
The code sample is also provided in [here](https://github.com/hbasafa/wav2vec_decode)
And Now I also focused on other decoding strategies to add there.

However, Thank you for sharing!
@patrickvonplaten

> Hey @hbasafa,
> 
> I'm now working on this topic full time.
> 
> We will most likely foster a closer collaboration between [pyctcdecode](https://github.com/kensho-technologies/pyctcdecode) and Transformers. [Here](https://github.com/patrickvonplaten/Wav2Vec2_PyCTCDecode) is a github repo that shows how to use `pyctcdecode` with Wav2Vec2 for LM supported decoding. It works quite well with KenLM.

hi @patrickvonplaten  - this is great news. Where is the best place to follow your progress? 

This PR: https://github.com/huggingface/transformers/pull/14339

It all depends a bit on how fast we can merge a `load_from_hf_hub` function to `pyctcdecode`