Adding support for `truncation` parameter on `feature-extraction` pipeline.
Author: Narsil
Pull request: yes

Fixes #14183


# 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).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/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/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/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:

- 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: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

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

 -->

Comments:
Can you also include `padding` as well? Since we're extracting features, I'd like to be able to specify both padding and truncation strategies. Thanks! @Narsil 

Padding for pipelines is something, I would like to keep orthogonal to business logic (See https://github.com/huggingface/transformers/pull/13724).

It's more batching than padding, but I imagine you pad mostly for the batch.
Currently, pipelines do not batch ever, meaning the padding is not used. Would adding padding change the results of `feature-extraction`?

Only slightly, I think. Right now you get embeddings of varying size depending on the size of your input sequence. If you want to use somehow these embeddings in a downstream task, it's weird to have varying size. In my case, I think I'm going to use only the embedding corresponding to the CLS, so I'm good. 

@Narsil Note that padding is supported in other pipelines. I think as a user, it's maddening to have varied behavior depending on which pipeline you use. I personally think that lack of consistency across pipelines is problematic. Take a look at this pipeline code, note there is logic around padding:

https://github.com/huggingface/transformers/blob/026866df92afe40cdf928839864111015a62d3b5/src/transformers/pipelines/text2text_generation.py#L92


>  it's maddening to have varied behavior depending on which pipeline you use. 

You're 100% correct, that's part of the reason of the large rewrite which is happening. 

For instance, the rewrite enables you to write either `pipeline(..., truncation=True)` or `pipe = pipeline(..); pipe(..., truncation=True)`. 
And that for all pipelines, and all parameters. This was far from the case before.

If anything dropping padding from the code you're quoting would be the way to go. (At least, deprecating it first, we have to maintain compatibility as much as possible). This code is currently legacy, and should be rewritten sometime in the future. The thing is there are a couple of directions to be considered for `text2text-generation` and we're also trying to align pipelines with other libraries. (https://github.com/huggingface/huggingface_hub/tree/main/api-inference-community)

Padding, is like batching, it was very spurious support across pipelines, we're closing the gap, but it takes time, and backward compatibility is important. The core idea is to get orthogonal behavior whereever possible. So as much as possible, individual pipelines should NOT handle them, all this logic should be enabled in the parent class. Not all models are even capable of padding (`gpt2` for instance).

Truncation for instance, is not orthogonal, since `question-answering` and `zero-shot-classification` will handle long prompts by chunking the input. Some pipelines input cannot really be chunked: `summarization` for instance uses and encoder-decoder, if the prompt does not fit the size of the model, then the summary cannot realistically chunk (or it will come with its own set of drawbacks let's say). 

That's also the reason why adding new parameters is something we try to think about before jumping to it.

`truncation` in `feature-extraction` is important because afaik, sentence embeddings do use the feature-extraction capability, and missing the last part of a sentence is indeed OK in a lot of cases (you want to use only the first token embedding, and missing part of the sentence is OK since it's only about matching later). It still needs to be opt-in as you need to explicitly know you can miss part of the sentence. Ideally, we would also prompt a warning since we're ignoring part of the sentence. And since a user sending text has no idea how long it is token-wise, it would be better to tell which part of the sentence is being chunked.

Hope this clears a bit what's going on.
Happy to receive feedback here too.