Passing a custom stopping_criteria list to model.generate() yields a multiple value error for that keyword arg
Author: bitbanger
Pull request: no

---
name: "\U0001F41B Bug Report"
about: Submit a bug report to help us improve transformers
title: ''
labels: ''
assignees: ''

---


## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version: 4.6.1
- Platform: macOS-10.15.5-x86_64-i386-64bit
- Python version: 3.8.8
- PyTorch version (GPU?): 1.18.1 (no)
- Tensorflow version (GPU?): N/A
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

### Who can help
<!-- Your issue 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:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @Rocketknight1

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

Model hub:

- for issues with a model report at https://discuss.huggingface.co/ and tag the model's creator.

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->

- set model_kwargs programmatically: @patrickvonplaten
- set stopping_criteria programmatically: @Narsil

## Information

Model I am using (Bert, XLNet ...): GPT2DoubleHeadsModel (pretrained model: distilgpt2)

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [x] my own modified scripts: (give details below): Any script I write that passes a custom StoppingCriteriaList via the stopping_criteria keyword arg of generation_utils.GenerationMixin.generate() seems to reproduce this issue.

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below): a simple personal chatbot harness with a custom newline stopping criterion

## To reproduce

Steps to reproduce the behavior:

1. Load a trained model using transformer.generation_utils.GenerationMixin
2. Define a custom StoppingCriteria and StoppingCriteriaList
3. Pass the custom StoppingCriteriaList as a keyword arg to model.generate(), e.g. model.generate(...stopping_criteria=my_custom_list...)

The above steps will yield a "got multiple values for keyword argument 'stopping_criteria'" error message.

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->

Ideally, there would be no error message, and the stopping_criteria kwarg would be passed through normally.

Comments:
Hey @bitbanger,

Could you provide a reproducible code snippet that we could just copy paste into a python shell to reproduce the error? :-) Thanks!

Hi there! Thanks for your response! Sure, here you go. I've confirmed that this code yields the error when run in the environment described in my report:

```
import torch
from transformers import GPT2Tokenizer, GPT2DoubleHeadsModel 
from transformers.generation_stopping_criteria import StoppingCriteria, StoppingCriteriaList                                                                 

class DummyStopCriterion(StoppingCriteria):                                   
    def __call__(self, input_ids: torch.LongTensor, score: torch.FloatTensor, **kwargs):                                                                     
        return len(input_ids.squeeze()) > 10

tok = GPT2Tokenizer.from_pretrained('distilgpt2')                             
model = GPT2DoubleHeadsModel.from_pretrained('distilgpt2')                    

input_ids = tok.encode('This should reproduce the bug', return_tensors='pt')  
model.generate(input_ids, stopping_criteria=StoppingCriteriaList([DummyStopCriterion()]))
```

Adding a bit more context,

the error is 
```
transformers.generation_utils.GenerationMixin.greedy_search() got multiple values for keyword argument 'stopping_criteria'
```

The reason is,  stopping_criteria is **not** a valid argument to `generate` so it get passed as `model_kwargs` which in turn are passed to `greedy` which already receives `stopping_criteria` because it gets created within `generate`.

The proposed solution is simply to enable it (with `logits_processor`) as a real argument of `generate` (doc should specify it's intended for users with know-how, most users should use simple arguments)


wdyt ? 

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