change `encoder_input_ids` value from `inputs_tensor` to `input_ids`
Author: zhoubay
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)


## 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?
- [ ] 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: @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

 -->

This issue is originated from PR #14784, which there's a [discussion](https://github.com/huggingface/transformers/pull/14784/files#r770422777) between @Narsil and @patrickvonplaten [in this lines](https://github.com/patrickvonplaten/transformers/blob/27c5509bfa072a096bb209af10c02f11af37824e/src/transformers/generation_utils.py#L1013-L1014)

After that, the generation file has changed to support `inputs_embeds` for decoder-only model as long as its signature contains `inputs_embeds` [in these lines](https://github.com/huggingface/transformers/blob/bbaa8ceff696c479aecdb4575b2deb1349efd3aa/src/transformers/generation/utils.py#L403-L411).

And the only classes using the `encoder_input_ids` is EncoderRepetitionPenaltyLogitsProcessor [in these lines](https://github.com/huggingface/transformers/blob/bbaa8ceff696c479aecdb4575b2deb1349efd3aa/src/transformers/generation/utils.py#L805-L807) and EncoderNoRepeatNGramLogitsProcessor [in these lines](https://github.com/huggingface/transformers/blob/bbaa8ceff696c479aecdb4575b2deb1349efd3aa/src/transformers/generation/utils.py#L817-L819). 

While I'm passing `inputs_embeds` to `generate` for a decoder-only model with `encoder_no_repeat_ngram_size` set, [this line](https://github.com/huggingface/transformers/blob/bbaa8ceff696c479aecdb4575b2deb1349efd3aa/src/transformers/generation/logits_process.py#L786) will throw out a `TypeError: unhashable type: 'list'`. After applying this commit, the error disappears.

Comments:
cc @gante 

hello @gante , could you take some time to review this PR? Feel free to tell me what should I do to make this PR more professional since this is my first time. Maybe I should add a test case?

hello @gante , could you take some time to see this and tell me what should I do to move this PR further? Thank you for your time!

> While I'm passing inputs_embeds to generate for a decoder-only model with encoder_no_repeat_ngram_size set

@zhoubay decoder-only models should not use `encoder_no_repeat_ngram_size`, that is meant for encoder-decoder models. 

The alternatives are:
1. if you want to prevent repetitions in the generated text, try the `no_repeat_ngram_size` flag ([docs](https://huggingface.co/docs/transformers/v4.41.0/en/main_classes/text_generation#transformers.GenerationConfig.no_repeat_ngram_size))
2. If you want to prevent prompt repetition, you need to pass `input_ids` containing the prompt to `generate`, then use the `no_repeat_ngram_size` flag

Do these options solve your use case? 🤗 

> > While I'm passing inputs_embeds to generate for a decoder-only model with encoder_no_repeat_ngram_size set
> 
> @zhoubay decoder-only models should not use `encoder_no_repeat_ngram_size`, that is meant for encoder-decoder models.
> 
> The alternatives are:
> 
> 1. if you want to prevent repetitions in the generated text, try the `no_repeat_ngram_size` flag ([docs](https://huggingface.co/docs/transformers/v4.41.0/en/main_classes/text_generation#transformers.GenerationConfig.no_repeat_ngram_size))
> 2. If you want to prevent prompt repetition, you need to pass `input_ids` containing the prompt to `generate`, then use the `no_repeat_ngram_size` flag
> 
> Do these options solve your use case? 🤗

Hi @gante,  thank you for your advice! However, I've implement a test function in my [latest commit](https://github.com/huggingface/transformers/pull/30598/commits/e2da45da525cf693d2734df433cbeda4e731cb09) to demonstrate the bug. Without [the commit](https://github.com/huggingface/transformers/pull/30598/commits/aa3398d48c7424b31929696a65008b728baddc98), the test would fail, and apply the commit, the test would pass. 

It's not nonsense to use `encoder_no_repeat_ngram_size` in `decoder-only` model, since the class said that it supports the decoder-only model [in the lines](https://github.com/huggingface/transformers/blob/1518508467d96b3866fc4ebcb7a5b3a2e0df2aa4/src/transformers/generation/logits_process.py#L356-L357)

Here I quote:
> Although originally intended for encoder-decoder models, it can also be used with decoder-only models like LLMs.

Thank you for your suggestions, and hope you could take some time and review my PR!

Have a good day!

Hi @gante , could you take some time to give a look to my PR? Thank you for your time!

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.

@zhoubay 

The test case as in the script below

```py
from transformers import AutoModelForCausalLM, AutoTokenizer

article = "Today a dragon flew over Paris."
model = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2").to("cuda")
tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")
input_ids = tokenizer(article, return_tensors="pt").input_ids.to("cuda")
inputs_embeds = model.get_input_embeddings()(input_ids)

model.generate(inputs_embeds=inputs_embeds, encoder_no_repeat_ngram_size=3, max_length=20)
```

does not make sense. `inputs_embeds` does not contain tokens (only their embeddings), so `encoder_no_repeat_ngram_size` has no prompt tokens to prevent the repetition of.

However, if you call instead

```py
model.generate(input_ids=input_ids, encoder_no_repeat_ngram_size=3, max_length=20)
```

it works.

Moreover, the proposed change breaks the intended use case, with encoder-decoder models (the encoder input in that case is contained in `inputs_tensor`, not in `input_ids`).

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.