Optimized dola decoding generation function for faster performance
Author: haiduo
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 # [issue](https://github.com/huggingface/transformers/issues/34958)

## 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#create-a-pull-request),
      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/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?

Test code:
```
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
import torch

tokenizer = AutoTokenizer.from_pretrained("huggyllama/llama-7b")
model = AutoModelForCausalLM.from_pretrained("huggyllama/llama-7b", torch_dtype=torch.float16)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
set_seed(42)

text = "On what date was the Declaration of Independence officially signed?"
inputs = tokenizer(text, return_tensors="pt").to(device)

# vanilla_output = model.generate(**inputs, do_sample=False, max_new_tokens=50)
# tokenizer.batch_decode(vanilla_output[:, inputs.input_ids.shape[-1]:], skip_special_tokens=True)

dola_high_output = model.generate(**inputs, do_sample=False, max_new_tokens=50, dola_layers='high')
output1 = tokenizer.batch_decode(dola_high_output[:, inputs.input_ids.shape[-1]:], skip_special_tokens=True)
print("dola_high_output:\n", output1)

dola_custom_output = model.generate(**inputs, do_sample=False, max_new_tokens=50, dola_layers=[16,], repetition_penalty=1.2)
output2 = tokenizer.batch_decode(dola_custom_output[:, inputs.input_ids.shape[-1]:], skip_special_tokens=True)
print("dola_custom_output:\n", output2)
```

Before Modification：
```
dola_high_output:
 ['\nJuly 4, 1776, when the Continental Congress voted to separate from Great Britain. The 56 delegates to the Continental Congress signed the Declaration on August 2, 1776.']
dola_custom_output:
 ['\nOn July 4, 1776 the Continental Congress voted to approve the wording of the Declaration of Independence. Two days later the delegates signed the document. The signing took place in Philadelphia at Independence Hall']
```

After Modification：
```
dola_high_output:
 ['\nJuly 4, 1776, when the Continental Congress voted to separate from Great Britain. The 56 delegates to the Continental Congress signed the Declaration on August 2, 1776.']
dola_custom_output:
 ['\nOn July 4, 1776 the Continental Congress voted to approve the wording of the Declaration of Independence. Two days later the delegates signed the document. The signing took place in Philadelphia at Independence Hall']
```

## 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.
 @gante @zucchini-nlp

<!-- 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
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

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:
Hi @haiduo 👋 

I agree most of the ops removed are redundant (except for `baseline_scores_normalized[scores_normalized < probs_thresh] = base_filter_value`), but that wouldn't be my key question here :)

The code was added by the authors according to what they envisioned as a base implementation. Preserving the original implementation is preferable unless we are speaking about significant speedups -- how much faster is DoLa after your changes? 

> Hi @haiduo 👋
> 
> I agree most of the ops removed are redundant (except for `baseline_scores_normalized[scores_normalized < probs_thresh] = base_filter_value`), but that wouldn't be my key question here :)
> 
> The code was added by the authors according to what they envisioned as a base implementation. Preserving the original implementation is preferable unless we are speaking about significant speedups -- how much faster is DoLa after your changes?

Hi @gante , 

In fact, I haven't actually compared the speed differences before and after. Previously, I only intended to combine it with speculative decoding, but it seems that it doesn't significantly affect the speed. I believe that to maintain the integrity of the initial implementation, we can retain the original code content.

Thank you for the follow-up, @haiduo 🤗