tokenization_marian.py: use current_spm for decoding
Author: Mehrad0711
Pull request: yes

# What does this PR do?

<!--
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 #10294 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [X] 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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors which may be interested in your PR.

<!-- 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: @jplu

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- nlp datasets: [different repo](https://github.com/huggingface/nlp)
- 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:
Hi @patil-suraj! 
Thanks for your review. As you suggested, I started updating the code and docs where `decode` or `batch_decode` is used.
Doing so, I noticed RAG model also has the same issue: in `decode` and `batch_decode`, `generator` is used instead of `current_tokenizer`. Do you want me to also update that model and its docs accordingly in this PR? 

Hey, I submitted my changes and also fixed the RAG tokenizer. Please let me know if I missed something or you want me to change any of the fixes. I can rebase and force-push again. 

> do that inside the context manager, so we should also update all the ex

@patil-suraj @sgugger, wouldn't it be nicer to just do `as_target_tokenizer` in the `batch_decode` and `decode` function itself? Because decoding usually corresponds to the "target tokenizer" -> I think this would be nicer for the user

We could do that, but the reported issue is about not being able to decode the source tokens as the `decode` always uses `spm_target`.  So if we do `as_target_tokenizer` inside `decode` then source tokens will be decoded using `spm_target`, which will cause the same issue.

True, yeah I was a bit off there!

Ok, I understand the fix now. It's a very problematic fix however because it's a big backward breaking change. In 99% of the cases people use `batch_decode` for the target outputs and we don't really want people to update their code just so that the source targets work correctly I think...If I understand correctly this PR would change the default behavior of `batch_decode(...)` which is a no-go sadly...

Could we maybe somehow let the `current_spm` default to `target_spm` when using `batch_decode`, `decode` so that we don't have any breaking changes & then add maybe a new context manager `as_source_tokenizer`? or just add a optional arg to `decode` for Marian?

cc @LysandreJik @sgugger 

It's not very complicated to add the `as_source_tokenizer` context manager. Another solution is to add a flag `use_source_tokenizer` (defaults to False) to `decode` and `batch_decode`.

In any case, backward-compatibility is paramount so it needs to be fully enforced.

`use_source_tokenizer` seems a better option to me, since the tokenizer already behaves like a source tokenizer by default so adding `as_source_tokenizer` seems a bit confusing IMO.

@Mehrad0711 , here's how we could now implement this
1. `current_spm` should always default to `source_spm` except inside the  `as_target_tokenizer`.
2. As Sylvain suggested, add the `use_source_tokenizer` argument to `decode` and `batch_decode`, if it's `True` use `source_spm` in `convert_tokens_to_string`. 
3. `convert_tokens_to_string` should never use `current_spm` as it defaults to `source_spm` and this would break backward-compatibility.

And the user should now pass `use_source_tokenizer` to decode source tokens

Thanks. I can proceed with the suggested implementation.
 However, passing `use_source_tokenizer` to `convert_tokens_to_string` requires updating `PreTrainedTokenizer`'s `_decode` method. Since `use_source_tokenizer` is passed to `convert_tokens_to_string` for all tokenizers, afaiu, the tokenizer classes for all models that implement their own `convert_tokens_to_string` should be updated individually to accept `use_source_tokenizer` (even though it's not used for some such as encoder-only models). 
I think a potential workaround can be adding instance checks within `_decode` to see if the tokenizer accepts that argument, or perhaps use function overloading.
Please let me know how you want me to proceed.

I think we can work around this by setting an internal attribute with the value of `use_source_tokenizer` passed by the user. This way we can recover it in `convert_tokens_to_string` without having to overload any other methods. What do you think?

Thanks for the suggestion. It makes sense. If backward-compatibility wasn't a big issue, I think it would be better to use `current_spm` (set to None) in all tokenizer methods and switch it to source or target spm using two (`as_source_tokenizer` and `as_target_tokenizer`) context managers as needed. This way encoding and decoding methods become source/ target agnositc. 
However, since it's an issue, I think your first suggestion which is using `use_source_tokenizer` is still better than setting an internal value from the user perspective because now they have to use a context_manager during encoding but then set an attribute during decoding which can persist during next decoding if not unset. What do you think?

> than setting an internal value from the user perspective

I never meant the user would have to set it. I meant for us to set it in `decode`/`decode_batch` with the `use_source_tokenizer` argument received.

Gotcha. Yeah, that should work.

Hi, the PR is ready for review. please let me know if the changes look good. Thanks.

Ok I think these changes address the comments. If there are still improvements to the docstring/ code you want to make, please feel free to push directly to this branch. Thanks.

The docstrings don't accumulate when you subclass and overload, they get reset. So we have to copy the whole things *and* add the extra argument. Will push on your branch the change.

Thanks. I think the PR is ready for the final review.
cc: @sgugger @patrickvonplaten @patil-suraj


Thanks a lot for your work on this PR!

Thanks a lot for your feedback and a great PR experience!