Adding a siamese text similarity inference pipeline
Author: rohit1998
Pull request: no

### Feature request

Siamese networks have been really popular for variety of tasks in nlp. 

I am wondering if we can build a inference pipeline class for siamese like architecture.

Pipeline can look something like

```
class SiamesePipeline(Pipeline):
    def _sanitize_parameters(self):
        return {}, {}, {}

    def preprocess(self, texts):
        return self.tokenizer(texts['text'], return_tensors=self.framework),self.tokenizer(texts['text_pair'], return_tensors=self.framework)

    def _forward(self, model_inputs):
        output_text = self.model(**model_inputs[0])
        output_text_pair = self.model(**model_inputs[1])
        sentence_embedding_text = output_text['last_hidden_state'][:,0,:]
        sentence_embedding_text_pair = output_text_pair['last_hidden_state'][:,0,:]
        cos = torch.nn.functional.cosine_similarity(sentence_embedding_text, sentence_embedding_text_pair)
        return cos

    def postprocess(self, model_outputs):
        return model_outputs.item()
```
Please note that preprocess takes in a dictionary of text pairs eg ({'text':'I like you.', 'text_pair':'I love you.'}). 

Also we would need to make change in datacollator to handle the tuples for [base](https://github.com/huggingface/transformers/blob/e0d7c831c7691a0069a57ba03993a8d531343de1/src/transformers/pipelines/base.py#L172) pipeline.
```
    def inner_wrapper(items):
        if isinstance(items[0], tuple):
            items_text, items_text_pair = zip(*items)
            return inner(items_text), inner(items_text_pair)
        else:
            return inner(items)
    return inner_wrapper

```

inference will looks something like 

```
    tokenizer = AutoTokenizer.from_pretrained(input_path_model)
    model = AutoModel.from_pretrained(input_path_model)
    dataset = Dataset.from_pandas(dataset_df[['text', 'text_pair']])

    pipe = SiamesePipeline(
    model=model,
    tokenizer=tokenizer,
    device=model_params_parsed['device'],
    num_workers=4)

    score = list(tqdm(pipe(
        KeyPairDataset(dataset, 'text', 'text_pair'),
        batch_size=model_params_parsed['batch_size']),
        total=len(dataset)))
```

or without dataset

`pipe({'text':'I like you.', 'text_pair':'I love you.'})`

Would love to know if this approach is good and in line with pipeline conventions. If yes then does it makes sense to add this change permanently.

### Motivation

I am using this solution for my Siamese experiments.

### Your contribution

I can raise a PR.

Comments:
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.

@Narsil , please have a look 😇

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.

Hi, sorry for failing to see those messages.

In principle there's no issue with adding such a pipeline, however, do `transformers` have such models ?

Ideally pipelines do not reflect architecture (here siamese) only the input and outcome, so something like `text-similarity` comes to mind. (This is covered by `sentence-transformers` typically).

I dont think transformers has any siamese models right now. Package currently lacks a way to train `text-similarity` directly too. 

Sentence-transformers cover it but i has its own issues. You cannot use other pipeline abstractions or TrainerAPI that are part of transformers when using sentence-transformers.  Package doesn't even support multi-gpu training out of the box. So I believe there is a value to getting them to transformers in future.

We need to send a pair of text instead of text, and we need to tokenise the both texts separately. Also support datacollator separately for text pairs. Since pipeline supports these operations i believe pipeline would need change. I haven't looked deep into how `sentence-transformers` does it.

For now, I have moved to using pytorch-lighting for my training and inference, so I dont have immediate need for this now. We can mark this issue closed if it doesnt align with package direction right now. Else i can make a design and contribute some basic pipeline and training framework to add support after design gets a go ahead.

Tagging @sgugger for information.

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.

Siamese training & inference would be very useful. It is used in SetFit for instance.