Adding LM-Infinite support to Llama models
Author: Glaciohound
Pull request: yes

# What does this PR do?

In this PR, we implement [LM-Infinite: Simple On-the-Fly Length Generalization for Large Language Models](https://browse.arxiv.org/abs/2308.16137) proposed in August 2023 on Llama models, which removes length limits of large language models, and enables them to generate to infinite lengths with intact performance similar to training time, without any parameter updates.  Results show that LM-Infinite can encode as long as 128k tokens on a single A100 GPU, and allows generating to infinite tokens, thanks to its $O(n)$ time and space complexity for encoding and $O(1)$ complexity for decoding.  Interestingly, later [StreamingLLM](https://github.com/mit-han-lab/streaming-llm) recently also observed alike results on a similar technique.

This implementation is related to and in response to [an issue](https://github.com/huggingface/transformers/issues/26553) discussing about integrating LM-Infinite into Huggingface Transformers.

This implementation defaults back to exactly the original behaviors if the sequence length is less than 4k, and only differ in performance when sequence is longer. In this way, normal users will feel minimal differences. All usages of functionalities remain the same. Moreover, this implementation encodes sequence one at a time, similar to other normal Transformer models, compared with StreamingLLM which encodes token by token (i.e., 2k `forward` passes for encoding context of length 2k).

<!-- Remove if not applicable -->


## 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/main/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. It is related to [this issue](https://github.com/huggingface/transformers/issues/26553).
- [x] 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).
- [x] 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.

- @ArthurZucker 
- @Rocketknight1 
- @LysandreJik

<!-- 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: @muellerzr and @pacman100

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @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: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
LM-Infinite seems not equals to standard XFM attention ablitiy in very long range context, due to its attention mask may not overlap all tokens it that scenario

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/main/CONTRIBUTING.md) are likely to be ignored.