Repository: huggingface/optimum

Issue:
transformers 4.26 breaks speech_to_text ONNX monolith export
### System Info

```shell
optimum dev
transformers 4.26
```


### Who can help?

@mht

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

```optimum-cli export onnx --model hf-internal-testing/tiny-random-Speech2TextModel --task speech2seq-lm-with-past speech_to_text```

logs:
```
Framework not specified. Using pt to export to ONNX.
Using framework PyTorch: 1.13.1+cu117
Overriding 1 configuration item(s)
        - use_cache -> True
Asked a sequence length of 16, but a sequence length of 1 will be used with use_past ==True for `decoder_input_ids`.
Validating ONNX model...
An error occured with the error message: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid Feed Input Name:past_key_values.1.encoder.value.
 The exported model was saved at: speech_to_text
```

Specific tests failing:
```
2023-01-25T09:53:04.2942908Z FAILED exporters/test_onnx_export.py::OnnxExportTestCase::test_pytorch_export_280_speech_to_text_default_with_past - onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid Feed Input Name:past_key_values.1.encoder.value
2023-01-25T09:53:04.2943563Z FAILED exporters/test_onnx_export.py::OnnxExportTestCase::test_pytorch_export_282_speech_to_text_speech2seq_lm_with_past - onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid Feed Input Name:past_key_values.1.encoder.value
```

### Expected behavior

No error like for 4.25


Discussion:

Resolving patch:
diff --git a/optimum/exporters/onnx/model_configs.py b/optimum/exporters/onnx/model_configs.py
--- a/optimum/exporters/onnx/model_configs.py
+++ b/optimum/exporters/onnx/model_configs.py
@@ -936,11 +936,50 @@ def generate(self, input_name: str, framework: str = "pt"):
 
 class Speech2TextOnnxConfig(AudioToTextOnnxConfig):
     NORMALIZED_CONFIG_CLASS = NormalizedSeq2SeqConfig.with_args(
-        input_features_per_channel="input_feat_per_channel", allow_new=True
+        decoder_num_layers="decoder_layers",
+        num_layers="decoder_layers",
+        input_features_per_channel="input_feat_per_channel",
+        allow_new=True,
     )
     DUMMY_INPUT_GENERATOR_CLASSES = (
         Speech2TextDummyAudioInputGenerator,
     ) + AudioToTextOnnxConfig.DUMMY_INPUT_GENERATOR_CLASSES[1:]
+    ATOL_FOR_VALIDATION = 1e-4
+
+    def _create_dummy_input_generator_classes(self, **kwargs) -> List["DummyInputGenerator"]:
+        dummy_inputs_generators = super()._create_dummy_input_generator_classes(**kwargs)
+        if self._behavior is ConfigBehavior.MONOLITH:
+            dummy_seq2seq_past_key_values_generator = self.DUMMY_INPUT_GENERATOR_CLASSES[2](
+                self.task,
+                self._normalized_config,
+                encoder_sequence_length=dummy_inputs_generators[0].sequence_length
+                // (2 * self._config.num_conv_layers),
+                **kwargs,
+            )
+            dummy_inputs_generators[2] = dummy_seq2seq_past_key_values_generator
+
+        return dummy_inputs_generators
+
+    @property
+    def inputs(self) -> Mapping[str, Mapping[int, str]]:
+        common_inputs = super().inputs
+        if self._behavior is ConfigBehavior.DECODER:
+            common_inputs["encoder_outputs"][
+                1
+            ] = f"{common_inputs['encoder_outputs'][1]} / {( 2 * self._config.num_conv_layers)}"
+        return common_inputs
+
+    @property
+    def outputs(self) -> Mapping[str, Mapping[int, str]]:
+        common_outputs = super().outputs
+        if self._behavior is ConfigBehavior.ENCODER:
+            # for Speech2text, we need to name the second axis as
+            # encoder_sequence_length / 2 * self._config.num_conv_layers as the axis name is
+            # used for dummy input generation
+            common_outputs["last_hidden_state"][
+                1
+            ] = f"{common_outputs['last_hidden_state'][1]} / {( 2 * self._config.num_conv_layers)}"
+        return common_outputs
 
 
 # TODO: Replace the TextSeq2SeqOnnxConfig inheritance with VisionToTextOnnxConfig when added.


Test patch: