Camb-ai / MARS5-TTS

MARS5 speech model (TTS) from CAMB.AI
https://www.camb.ai
GNU Affero General Public License v3.0
1.37k stars 95 forks source link

Windows: PermisssionError: File In Use #22

Closed nolanblew closed 6 days ago

nolanblew commented 2 weeks ago

I've followed all the instructions, but on Windows I cannot get the model to load. It keeps getting stuck attempting to delete the temporary .model file and crashing.

Stacktrace:

Traceback (most recent call last):
  File "E:\Users\MyUser\Documents\git\mars5\clone_tts.py", line 6, in <module>
    mars5, config_class = torch.hub.load('Camb-ai/mars5-tts', 'mars5_english', trust_repo=True)
  File "E:\Users\MyUser\Documents\git\mars5\.venv\lib\site-packages\torch\hub.py", line 568, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "E:\Users\MyUser\Documents\git\mars5\.venv\lib\site-packages\torch\hub.py", line 597, in _load_local
    model = entry(*args, **kwargs)
  File "E:\Users\MyUser\Documents\git\mars5\./hub\Camb-ai_mars5-tts_master\hubconf.py", line 31, in mars5_english       
    mars5 = Mars5TTS(ar_ckpt, nar_ckpt, device=device)
  File "E:\Users\Nolan\Documents\git\mars5\./hub\Camb-ai_mars5-tts_master\inference.py", line 85, in __init__
    os.remove(tfn)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\myuser\\AppData\\Local\\Temp\\tmp1hrrwls1texttok.model'

The crash seems to be happening here: https://github.com/Camb-ai/MARS5-TTS/blob/e5e92a9c62b8099a96cbe95cd11536a78dbf3c5d/inference.py#L85

When I inspect the user of this file, it's locked by python.exe, meaning the file is open somewhere in python and hasn't been closed yet.

I'll do my best to debug and add anything more, but my knowledge and expertise is not in this area.

System Info

OS: Windows 11 Pro 23H2 build 22631.3593 Processor: Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz 3.79 GHz RAM: 64.0 GB Graphics Card: NVIDIA GeForce RTX 4090 Graphics VRAM: 24 GB Graphics Driver: 555.99 Python Version: 3.10.11 Torch Version: 2.3.1

nolanblew commented 2 weeks ago

After some digging, it looks like we can use NamedTemporaryFile to get around the issue of holding on to a file.

It would look like this:

        with tempfile.NamedTemporaryFile(suffix='texttok.model', delete=False) as tmp:
            tfn = tmp.name
            Path(tfn).write_text(ar_ckpt['vocab']['texttok.model'])
            self.texttok.load(tfn)
        os.remove(tfn)

I can create a PR if you'd like, but I have not tested on linux/osx and don't have the means to do so.

alexjbusch commented 1 week ago

I can confirm that this fix works on windows 10 if you also do the same thing for speechtok

akshhack commented 1 week ago

@nolanblew would appreciate if you can make the PR!

botslop commented 1 week ago

Might be a good idea to confirm if these changes cause any issues with Linux/OSX first. Also confirming the suggested changes are working on Windows 10. If you're running into issues and the error still pops up, check the cached version of inference.py and make sure the changes are listed there (not just inference.py in the repo.) My cache file was in C:\user\.cache\torch\hub\Camb-ai_mars5-tts_master.

        # save and load text tokenize
        self.texttok = RegexTokenizer(GPT4_SPLIT_PATTERN)
        with tempfile.NamedTemporaryFile(suffix='texttok.model', delete=False, mode='w') as tmp:
            tfn = tmp.name
            Path(tfn).write_text(ar_ckpt['vocab']['texttok.model'])
            self.texttok.load(tfn)
        os.remove(tfn)
        # save and load speech tokenizer
        self.speechtok = CodebookTokenizer(GPT4_SPLIT_PATTERN)
        with tempfile.NamedTemporaryFile(suffix='speechtok.model', delete=False, mode='w') as tmp:
            sfn = tmp.name
            Path(sfn).write_text(ar_ckpt['vocab']['speechtok.model'])
            self.speechtok.load(sfn)
        os.remove(sfn)
RF5 commented 1 week ago

Hi @nolanblew , we're in the process of porting the checkpoints to a safetensors format and that should remove this issue you are facing (i.e. we won't be writing new tempfiles in the init function). Please check back in a few days and it should be fixed.

nolanblew commented 1 week ago

Perfect I'll hold off on a PR to see if the safetensors fix the issue.

RF5 commented 1 week ago

@nolanblew This should be fixed now, we no longer safe any tempfiles in the init function. Let us know if it works now for you? Reminder you may need force_reload=True in the torch.hub.load() function to use the latest code.

arnavmehta7 commented 6 days ago

@nolanblew safetensors are released!

nolanblew commented 6 days ago

Can confirm that this fixes the issue!