Fix TextGeneration and Text2TextGeneration pipeline issue with return_dict_in_generate
Author: tokestermw
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.
-->

When running `return_dict_in_generate=True` in pipelines, the `generate` method errors out, e.g.

```python
        generate_kwargs["min_length"] = generate_kwargs.get("min_length", self.model.config.min_length)
        generate_kwargs["max_length"] = generate_kwargs.get("max_length", self.model.config.max_length)
        self.check_inputs(input_length, generate_kwargs["min_length"], generate_kwargs["max_length"])
        output_ids = self.model.generate(**model_inputs, **generate_kwargs)
E       AttributeError: 'GreedySearchEncoderDecoderOutput' object has no attribute 'shape'
```

Reproducible code:

```python
from transformers import pipeline
generator = pipeline('text-generation', 'gpt2')
generator('hello', return_dict_in_generate=True)
```

Fix is to add a check if `return_dict_in_generate=True`, then let `generated_sequence = generated_sequence.sequences`.

## 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?
- [ ] 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?


## Who can review?

@gante, @Narsil

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.



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

> Hi thank you for this PR. Do you mind sharing in which context you need to use this return changing flag?

Currently I use the scores for a custom stopping criterion (e.g. stop when cumulative probs are > x).

@tokestermw interesting! `pipeline` only returns the output sequence, but to store the `scores` internally in `.generate()`, `return_dict_in_generate` is indeed needed. Is this what is happening in your use case?

@gante yes that's right! to access the `scores` here: https://github.com/huggingface/transformers/blob/42b60f8b02941b0c40c42e150a101eb372c3856e/src/transformers/generation/stopping_criteria.py#L37


> Currently I use the scores for a custom stopping criterion (e.g. stop when cumulative probs are > x).

Shouldn't that be done with a custom `StoppingCriteria` ? 

```python
class MyStoppingCriteria:
    def __init__():
       self.cumulative = 0.0

    def __call__():
       self.cumulative += scores
       if self.cumulative > 
           return True
       else:
           return False
```

This PR a small enough to be ok anyway, just wondering if there's not a "cleaner" way for  you to solve your issue.

@Narsil we do use a custom `StoppingCriteria`, and use it inside pipelines.

But yeah arguably custom stuff should maybe be done outside of pipelines.

Sidenote is that we can't currently pass in tokenizer args like `truncation=True` in pipelines, so we've had to either write a custom pipeline, or rewrite to not use pipelines :)

> Sidenote is that we can't currently pass in tokenizer args like truncation=True in pipelines, so we've had to either write a custom pipeline, or rewrite to not use pipelines :)

We could definitely add `tokenizer_kwargs` to enable all the args you want on the tokenizer.

Another way of doing it would be by subclassing

https://huggingface.co/docs/transformers/v4.26.0/en/add_new_pipeline#how-to-create-a-custom-pipeline

And then using `pipeline(...., pipeline_class=MyPipelineClass)` to use it instead of the default one.
Then you can add all the fancy logic you need.

The docs also refer on how to share it !


Thanks @Narsil ! i can take a look at `tokenizer_kwargs` in a separate PR, please let me know if anything else is needed in this PR

Seems good, just don't pass all `tokenizer_kwargs` as a generic `**kwargs`. 

Like let's capture `pipeline(...tokenizer_kwargs={"truncation": True})` so that it cannot clash with `generate_kwargs` (inconveninent but necessary, `max_length` is an argument for both)

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.

cc @echarlaix, potentialy including `tokenizer_kwargs` is planned 😉 

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.