max_length ignored in summarization pipeline, overridden by default max_new_tokens
Author: noah-13
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.56.1
- Platform: Linux-6.8.0-79-generic-x86_64-with-glibc2.39
- Python version: 3.11.13
- Huggingface_hub version: 0.34.4
- Safetensors version: 0.6.2
- Accelerate version: 1.10.1
- Accelerate config:    not found
- DeepSpeed version: not installed
- PyTorch version (accelerator?): 2.3.1+cu118 (CUDA)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: <fill in>
- Using GPU in script?: <fill in>
- GPU type: NVIDIA GeForce GTX 1080 Ti

### Who can help?

@Rocketknight1 



### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

### Bug description
When using the summarization pipeline, setting only `max_length` (and `min_length`) still triggers a warning about `max_new_tokens`.  
More importantly, the output length follows `max_new_tokens` (=256) instead of my specified `max_length` (=64).  

---

### Reproduction

```python
from transformers import pipeline
from transformers import AutoTokenizer

summarizer = pipeline("summarization", model="Falconsai/text_summarization", device_map="auto")
    tokenizer = AutoTokenizer.from_pretrained("Falconsai/text_summarization")

outputs = summarizer(
    ["This is a long article about ..."],
    max_length=64,
    min_length=10,
    do_sample=False,
)
print(outputs)
print(len(tokenizer.encode(text, add_special_tokens=False)))
```
### Expected behavior

Output length should respect max_length=64.

###  Actual behavior

Warning appears:

```
Both `max_new_tokens` (=256) and `max_length`(=64) seem to have been set.
`max_new_tokens` will take precedence.
```

Output length matches max_new_tokens=256, not my specified max_length.


### Expected behavior

Output length should respect max_length=64.

Comments:
Hi @noah-13, the argument `max_length` is being deprecated. `max_new_tokens` is supposed to take precedence over `max_length`, this is expected behavior.
Please try using max_new_tokens = 64 and you should get expected results.

Thank you for the clarification. I tried setting `max_new_tokens = 64`.  
It is strange that I still get the following warning:

``
Your max_length is set to 200, but your input_length is only 68. Since this is a summarization task, where outputs shorter than the input are typically wanted, you might consider decreasing max_length manually, e.g. summarizer('...', max_length=34)
``

However, the actual outputs are always under 64 tokens, as expected. 

You can safely ignore that warning. It’s just a general warning from the generate method comparing `max_length` with your input length. Since the pipeline internally sets `max_length = input_length + max_new_tokens`, you’ll often see that message but the model will respect your `max_new_tokens` arg only

If the pipeline is emitting unnecessary warnings, we'd definitely appreciate a PR to fix that!

Sure Matt, would raise a small PR for the same soon

I just checked it, the warning isn't redundant if max_length is provided 

```
    def check_inputs(self, input_length: int, min_length: int, max_length: int) -> bool:
        """
        Checks whether there might be something wrong with given input with regard to the model.
        """
        if max_length < min_length:
            logger.warning(f"Your min_length={min_length} must be inferior than your max_length={max_length}.")

        if input_length < max_length:
            logger.warning(
                f"Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is "
                "a summarization task, where outputs shorter than the input are typically wanted, you might "
                f"consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length // 2})"
            )
```

But if we use max_new_tokens this warning won't pop up. Should anything be done about this?

If the pipeline is only emitting warnings when `max_length` is passed as an argument, then it's fine, we don't need to change anything!

In my case if I only use `max_new_token`, I will still get the warning ``Your max_length is set to 200, but your input_length is only 68. Since this is a summarization task, where outputs shorter than the input are typically wanted, you might consider decreasing max_length manually, e.g. summarizer('...', max_length=34) ``

Hello,

I have successfully reproduced the issue discussed in #40768.

**Steps to Reproduce:**

1.  Use the following Python script:

    ```python
    from transformers import pipeline, AutoTokenizer

    # Create summarization pipeline
    summarizer = pipeline("summarization", model="Falconsai/text_summarization", device_map="auto")

    # Prepare input text
    text = "This is a long article about the history of artificial intelligence, covering its origins, key milestones, and future prospects. It delves into various subfields like machine learning, natural language processing, and computer vision, highlighting their impact on modern technology and society. The article also discusses the ethical considerations and challenges associated with the rapid advancement of AI."

    # Let's get the actual input_ids length for a more accurate warning message
    tokenizer = AutoTokenizer.from_pretrained("Falconsai/text_summarization")
    input_ids = tokenizer.encode(text, add_special_tokens=False)
    input_length = len(input_ids)

    print(f"Input text length (after tokenization): {input_length}")

    print("\nRunning summarization pipeline...")
    outputs = summarizer(
        [text],
        max_new_tokens=64,
        min_length=10,
        do_sample=False,
    )
        
    print("\nSummarization result:")
    print(outputs)
    ```

2.  Execute the script.

**Observed Behavior:**

The script produced the following output:

```
Device set to use cuda:0 
Input text length (after tokenization): 75 

Running summarization pipeline... 
Your max_length is set to 200, but your input_length is only 78. Since this is a summarization task, where outputs shorter than the input are typically wanted, you might consider decreasing max_length manually, e.g. summarizer('...', max_length=39) 

Summarization result: 
[{'summary_text': 'article delves into various subfields like machine learning, natural language processing, and computer vision . The article also discusses the ethical considerations and challenges associated with the rapid advancement of AI .'}] 
```

Despite `max_new_tokens` being set to 64, and the final summarized output correctly adhering to this limit (41 tokens), a warning regarding `max_length` is still issued. This behavior is consistent with the report from `noah-13` in the original issue.

I hope this detailed reproduction is helpful in addressing the issue.