StyleTTS2 icon indicating copy to clipboard operation
StyleTTS2 copied to clipboard

Fix batch size 1 by specifying squeeze dims

Open hecko-yes opened this issue 1 year ago • 10 comments

Fixes #104 (as far as I can tell).

hecko-yes avatar Dec 19 '23 10:12 hecko-yes

Thanks for your fix, but what does .squeeze(0) do? Doesn’t it squeeze over the batch dimensions and cause issues?

yl4579 avatar Dec 22 '23 06:12 yl4579

...hm. I'll be honest, I don't remember my reasoning behind it; I just know it doesn't break.

Looking at the relevant code again, it seems if len(sp) <= 1: makes it return early anyway if the first dimension is squeezable. Perhaps the .squeeze(0)s should be removed entirely (maybe along with the early return?).

hecko-yes avatar Dec 22 '23 07:12 hecko-yes

So does it mean it's probably just redundant? Does it still work with batch size greater than 1? Also it would be great if you could implement gradient accumulate for the accelerate version of fine-tuning with 1 GPU but someone else could do it too. Sorry I am too busy to test the code nor adding new stuff.

yl4579 avatar Dec 22 '23 07:12 yl4579

@Sobsz I tried your branch and I tried to finetune a model but I got the ZeroDivisionError: division by zero error message after 1 epoch. Batch size is set to 1. Error Message IMG_20240205_142707_323

GUUser91 avatar Feb 05 '24 22:02 GUUser91

Seems like the testing iterations are failing silently because of the except: continue at lines 673-674. (That, or your validation list is empty...?) Try replacing the continue with raise and see what error you get.

hecko-yes avatar Feb 05 '24 23:02 hecko-yes

@Sobsz Now I get the dimension out of range (expected to be in range of [-1, 0], but got 1) error message. I'm using the rocm5.7 nightly pytorch build if it helps. Error Range Message IMG_20240205_174637_769

GUUser91 avatar Feb 06 '24 01:02 GUUser91

@Sobsz Here's a more clear version

Traceback (most recent call last): File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/train_finetune.py", line 707, in main() File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/click/core.py", line 1157, in call return self.main(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/click/core.py", line 1078, in main rv = self.invoke(ctx) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/click/core.py", line 1434, in invoke return ctx.invoke(self.callback, **ctx.params) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/click/core.py", line 783, in invoke return __callback(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/train_finetune.py", line 614, in main d, p = model.predictor(d_en, s, File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/parallel/data_parallel.py", line 183, in forward return self.module(*inputs[0], **module_kwargs[0]) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/models.py", line 469, in forward d = self.text_encoder(texts, style, text_lengths, m) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/models.py", line 550, in forward x = block(x.transpose(-1, -2), style).transpose(-1, -2) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) File "/run/media/user/e1745494-af46-4749-9e1a-89d2b2289699/StyleTTS2/models.py", line 431, in forward h = h.view(h.size(0), h.size(1), 1) IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

GUUser91 avatar Feb 06 '24 15:02 GUUser91

turns out that this also breaks higher batch sizes

korakoe avatar Mar 18 '24 11:03 korakoe

.squeeze(0) is going to squeeze the wrong dimension when batch size > 1, need to use .squeeze(dim=1) in most of these places (from manually stepping through with a debugger).

brthor avatar Apr 12 '24 23:04 brthor

.squeeze(0) is going to squeeze the wrong dimension when batch size > 1, need to use .squeeze(dim=1) in most of these places (from manually stepping through with a debugger).

@brthor can all of them be changed to dim=1 or do some need to remain 0? if so which ones?

korakoe avatar May 08 '24 11:05 korakoe