Repository: coqui-ai/TTS

Issue:
[Bug] RuntimeError: Given groups=1, weight of size [196, 196, 1], expected input[1, 192, 100] to have 196 channels, but got 192 channels instead
### Describe the bug

I tried to export the pretrained VITS model to ONNX but failed with models 
`tts_models/es/css10/vits`
`tts_models/fr/css10/vits`
and
`tts_models/pt/css10/vits` 
I think this occurred because these models were trained on multi-speaker datasets. Then I tried to solve this by re-installing the library through `pip install git+https://github.com/coqui-ai/TTS.git` seeing there is a recent PR fixing the multispeaker exporting issue. However, the problem is still there. Could you please give some advice on this?

### To Reproduce

Most of the code is the same as #2563:
```python
from TTS.tts.models.vits import Vits
from TTS.tts.configs.vits_config import VitsConfig
from TTS.utils.audio.numpy_transforms import save_wav
import numpy as np


def to_onnx_with_test():
    config = VitsConfig()
    config.load_json('./torch_models/pt/config.json')
    vits = Vits.init_from_config(config)
    vits.load_checkpoint(config, "./torch_models/pt/model_file.pth.tar", strict=False, eval=True)
    vits.export_onnx('./onnx_models/pt/pt.onnx', verbose=False)
    vits.load_onnx("./onnx_models/pt/pt.onnx")
    text = "Isto é um teste"
    text_inputs = np.asarray(
        vits.tokenizer.text_to_ids(text, language="pt"),
        dtype=np.int64,
    )[None, :]

    audio = vits.inference_onnx(text_inputs)
    print(audio.shape)

    save_wav(wav=audio[0], path="coqui_vits_pt.wav", sample_rate=config.audio.sample_rate)


if __name__ == '__main__':
    to_onnx_with_test()
```

### Expected behavior

_No response_

### Logs

_No response_

### Environment

```shell
{
    "CUDA": {
        "GPU": [
            "NVIDIA GeForce RTX 3090",
            "NVIDIA GeForce RTX 3090"
        ],
        "available": true,
        "version": "11.7"
    },
    "Packages": {
        "PyTorch_debug": false,
        "PyTorch_version": "2.0.1+cu117",
        "TTS": "0.15.6",
        "numpy": "1.22.0"
    },
    "System": {
        "OS": "Windows",
        "architecture": [
            "64bit",
            "WindowsPE"
        ],
        "processor": "Intel64 Family 6 Model 85 Stepping 4, GenuineIntel",
        "python": "3.9.17",
        "version": "10.0.22000"
    }
}
```


### Additional context

_No response_


Discussion:
Sure I will just follow this issue #2472 and see whether it works

Resolving patch:
diff --git a/TTS/tts/models/vits.py b/TTS/tts/models/vits.py
--- a/TTS/tts/models/vits.py
+++ b/TTS/tts/models/vits.py
@@ -1813,14 +1813,16 @@ def export_onnx(self, output_path: str = "coqui_vits.onnx", verbose: bool = True
 
         # rollback values
         _forward = self.forward
-        disc = self.disc
+        disc = None
+        if hasattr(self, 'disc'):
+            disc = self.disc
         training = self.training
 
         # set export mode
         self.disc = None
         self.eval()
 
-        def onnx_inference(text, text_lengths, scales, sid=None):
+        def onnx_inference(text, text_lengths, scales, sid=None, langid=None):
             noise_scale = scales[0]
             length_scale = scales[1]
             noise_scale_dp = scales[2]
@@ -1833,7 +1835,7 @@ def onnx_inference(text, text_lengths, scales, sid=None):
                     "x_lengths": text_lengths,
                     "d_vectors": None,
                     "speaker_ids": sid,
-                    "language_ids": None,
+                    "language_ids": langid,
                     "durations": None,
                 },
             )["model_outputs"]
@@ -1844,11 +1846,14 @@ def onnx_inference(text, text_lengths, scales, sid=None):
         dummy_input_length = 100
         sequences = torch.randint(low=0, high=self.args.num_chars, size=(1, dummy_input_length), dtype=torch.long)
         sequence_lengths = torch.LongTensor([sequences.size(1)])
-        sepaker_id = None
+        speaker_id = None
+        language_id = None
         if self.num_speakers > 1:
-            sepaker_id = torch.LongTensor([0])
+            speaker_id = torch.LongTensor([0])
+        if self.num_languages > 0 and self.embedded_language_dim > 0:
+            language_id = torch.LongTensor([0])
         scales = torch.FloatTensor([self.inference_noise_scale, self.length_scale, self.inference_noise_scale_dp])
-        dummy_input = (sequences, sequence_lengths, scales, sepaker_id)
+        dummy_input = (sequences, sequence_lengths, scales, speaker_id, language_id)
 
         # export to ONNX
         torch.onnx.export(
@@ -1857,7 +1862,7 @@ def onnx_inference(text, text_lengths, scales, sid=None):
             opset_version=15,
             f=output_path,
             verbose=verbose,
-            input_names=["input", "input_lengths", "scales", "sid"],
+            input_names=["input", "input_lengths", "scales", "sid", "langid"],
             output_names=["output"],
             dynamic_axes={
                 "input": {0: "batch_size", 1: "phonemes"},
@@ -1870,7 +1875,8 @@ def onnx_inference(text, text_lengths, scales, sid=None):
         self.forward = _forward
         if training:
             self.train()
-        self.disc = disc
+        if not disc is None:
+            self.disc = disc
 
     def load_onnx(self, model_path: str, cuda=False):
         import onnxruntime as ort
@@ -1887,7 +1893,7 @@ def load_onnx(self, model_path: str, cuda=False):
             providers=providers,
         )
 
-    def inference_onnx(self, x, x_lengths=None, speaker_id=None):
+    def inference_onnx(self, x, x_lengths=None, speaker_id=None, language_id=None):
         """ONNX inference"""
 
         if isinstance(x, torch.Tensor):
@@ -1902,13 +1908,15 @@ def inference_onnx(self, x, x_lengths=None, speaker_id=None):
             [self.inference_noise_scale, self.length_scale, self.inference_noise_scale_dp],
             dtype=np.float32,
         )
+		
         audio = self.onnx_sess.run(
             ["output"],
             {
                 "input": x,
                 "input_lengths": x_lengths,
                 "scales": scales,
-                "sid": torch.tensor([speaker_id]).cpu().numpy(),
+                "sid": None if speaker_id is None else torch.tensor([speaker_id]).cpu().numpy(),
+				"langid": None if language_id is None else torch.tensor([language_id]).cpu().numpy()
             },
         )
         return audio[0][0]


Test patch: