Add support for non-rust implemented tokenization for `__getitem__` m…
Author: jacklanda
Pull request: yes

…ethod.

# 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 # (issue)


## 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/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] 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/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/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 who 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:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @sgugger

Integrations:

- deepspeed: HF Trainer: @stas00, Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam

Documentation: @sgugger, @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: @sgugger
- TensorFlow: @Rocketknight1

 -->


Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_23645). All of your documentation changes will be reflected on that endpoint.

Hi @jacklanda, thanks for opening this PR! 

Could you add some more detail to the PR description about the issue this is addressing or feature this is adding? 

cc @younesbelkada @ArthurZucker 

> Hi @jacklanda, thanks for opening this PR!
> 
> Could you add some more detail to the PR description about the issue this is addressing or feature this is adding?
> 
> cc @younesbelkada @ArthurZucker

Hi @amyeroberts , this PR is going to add a support for the usage scenario of "getting a slice from the batch-tokenized sequences". 

Without this PR, it seems to raise `KeyError` with the following message `KeyError: 'Indexing with integers (to access backend Encoding for a given batch index) is not available when using Python based tokenizers'`

P.S. The above scenario could be reproduced by using some models new uploaded but not support to Rust-implemented tokenization, such as `fnlp/moss-moon-003-sft`. Also we can run a examplar script for reproducing this issue:

```python3
from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("fnlp/moss-moon-003-sft", trust_remote_code=True)
tok.add_special_tokens({"pad_token": "[PAD]"})

texts = ["Today is a good day!", "It's a good idea!", "How's going?"]
batch_tok = tok(texts, padding=True)
print(batch_tok[0:3])  # report `KeyError` here
```

All in all, I think it seems useful to implement `__getitem__` method behind it in Python side :)

Why this auto pipeline always failed? 

@jacklanda By 'auto pipeline' are you referring to the CI test suite? If so, it seems there's two main issues causing failing CI runs: 
* Quality style check. Running `make style` and pushing the changes should resolve these.
* Unexpected change in behaviour in one of our custom tokenizers. It seems that some of the tests for `JumanppTokenizer` are now failing as a result of the changes in this PR. 

For the CI runs, you should be able to click on `Details` for each of the CI runs to see the full output including error messages / failed test reports. Let us know if this isn't working for you. 



> 

Yes, I mean `CI test suite`.

- Where to run `make style`?
- How to fix this? Should I reopen another PR for this commit?

Thanks!

@jacklanda 
> Where to run make style?

In the top level of your local `transformers` fork. This and more information about how to contribute to the library can found in the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests).

> How to fix this? Should I reopen another PR for this commit?

The changes should be part of this PR and pushed to this branch. However, I can see that this PR has been opened on the `main` branch of your fork, and not a new feature branch. For this PR we can still merge the changes, however if you wish to contribute again to transformers using this fork it may become tricky to manage conflicts. I would suggest deleting this branch once this PR is merged, fetching `main` from the parent repository and then working from feature branches. Again, see the contributor guidelines for details on typical workflows for PRs.