Exporting DeBerta using custom onnx configuration
Author: RaiAmanRai
Pull request: no

### Feature request

I am trying to export a DeBerta model, but since the current version of transformers[onnx] doesn't support DeBerta architecture, I am trying to do it by implementing a custom onnx configuration. Although, I am able to provide the required inputs, I am not really getting the required output shape for **Sequence Classification** task. 

I also tried to use the approach mentioned below, but to no good

~~~
from collections import OrderedDict
from typing import Mapping
from pathlib import Path
from transformers.onnx import export
from transformers.onnx import OnnxConfig
from transformers import BartTokenizer, BartModel, BartConfig

onnx_path = Path("C:/Users/Hp/zsc/onnx_deberta/model3.onnx")

class DebertaConfig(OnnxConfig):
    @property
    def inputs(self) -> Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("input_ids", {0: "batch", 1: "sequence"}),
                ("attention_mask", {0: "batch", 1: "sequence"}),
            ]
        )

config = AutoConfig.from_pretrained("Narsil/deberta-large-mnli-zero-cls")

base_model = AutoModel.from_pretrained("Narsil/deberta-large-mnli-zero-cls")
tokenizer = AutoTokenizer.from_pretrained("Narsil/deberta-large-mnli-zero-cls")
onnx_config = DebertaConfig(config, task="sequence-classification")
onnx_inputs, onnx_outputs = export(tokenizer, base_model, onnx_config, onnx_config.default_onnx_opset, onnx_path)
~~~

the onnx_outputs shape is of kind (1, 10, 1024) instead of (1, 3).
Any way to achieve it or am I doing something wrong?




Comments:
cc @lewtun @michaelbenayoun 

Hi @RaiAmanRai thanks for reporting this issue! Would you be able to share a reproducible code snippet that also shows the type of inputs you're feeding to the exported model (e.g. with ONNX Runtime)?

Hi @lewtun , thanks for stopping by. The above mentioned code snippet was used to export the model into .onnx format. 

The following code snippet was used to check the outputs of the model and its shape
~~~
import onnxruntime as ort
import numpy as np

ort_session = ort.InferenceSession('C:/Users/Hp/zsc/onnx_deberta/model3.onnx')

inputs = tokenizer("Using BERT in ONNX and we are doing this as a test to check the output shape!", return_tensors="np", return_token_type_ids=False)

inputs['attention_mask'] = inputs['attention_mask'].astype(np.int64)
inputs['input_ids'] = inputs['input_ids'].astype(np.int64)
outputs = ort_session.run(onnx_outputs, dict(inputs))
outputs[0].shape
~~~

 Here, tokenizer is the same instance used above.

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.

@lewtun @michaelbenayoun can you guys please look into this issue.
This has become a major issue in the development I am working on, and would request to resolve it.

Thank you.

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.

I try to fix the same issue. Is there any solution about this issue

Hi @forrestfaraday , @RaiAmanRai ,

The support for the ONNX export is now under [`optimum.exporters.onnx`](https://github.com/huggingface/optimum/releases/tag/v1.5.0), and we actually support the export of Deberta.

All you need to do is installing `optimum`:

```bash
pip install optimum
```

Then run the `optimum.exporters.onnx` CLI:

```bash
python -m optimum.exporters.onnx --model Narsil/deberta-large-mnli-zero-cls deberta_onnx/
```

**Thanks for the explanation. Could you please help me with this error. Because I tried a lot of time this pipeline with different deberta models.** 

`hg_checkpoint   = "microsoft/deberta-v3-base"
save_hg         = "tmp/hg_onnx/"`

**Load a model from transformers and export it to ONNX**
`ort_model_hg = ORTModelForTokenClassification.from_pretrained(hg_checkpoint, from_transformers=True)
tokenizer_hg = AutoTokenizer.from_pretrained(hg_checkpoint)`

**Save the onnx model and tokenizer**
`ort_model_hg.save_pretrained(save_hg)
tokenizer_hg.save_pretrained(save_hg)`

**Define the quantization methodology**
`qconfig      = AutoQuantizationConfig.arm64(is_static=False, per_channel=False)
quantizer_hg = ORTQuantizer.from_pretrained(ort_model_hg)`

**Apply dynamic quantization on the model**

`quantizer_hg.quantize(save_dir=save_hg, quantization_config=qconfig)
from optimum.onnxruntime import ORTModelForTokenClassification
from transformers import pipeline, AutoTokenizer`

`model_hg = ORTModelForTokenClassification.from_pretrained(save_hg, file_name="model_quantized.onnx")
tokenizer_hg = AutoTokenizer.from_pretrained(save_hg)
pipeline_hg = pipeline("token-classification", model=model_hg, tokenizer=tokenizer_hg, aggregation_strategy = 'first')
results = pipeline_hg(text)
results`

**InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid Feed Input Name:token_type_ids**


I have no problems quantizing/optimizing the deberta model and then loading it. I am facing the below error when importing predict when using pipeline.

**InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid Feed Input Name:token_type_ids**

Could you open a PR on the [optimum repo](https://github.com/huggingface/optimum/issues) please?
We will try to figure it out there!