bark icon indicating copy to clipboard operation
bark copied to clipboard

Compatiblity with PyTorch 2.4+ (weights_only default value changed)

Open const-volatile opened this issue 10 months ago • 5 comments

Starting from version 2.4 PyTorch introduces a stricter check for the objects which can be loaded with torch.load(); to use bark successfully with PyTorch>=2.4 the weights_only attribute needs to be set explicitly.

Without this change the following error will occur:

python3 -m bark --text "Hello, my name is Suno." --output_filename "example.wav"

Oops, an error occurred: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint. 
	(1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
	(2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
	WeightsUnpickler error: Unsupported global: GLOBAL numpy.core.multiarray.scalar was not an allowed global by default. Please use `torch.serialization.add_safe_globals([scalar])` or the `torch.serialization.safe_globals([scalar])` context manager to allowlist this global if you trust this class/function.

Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.

const-volatile avatar Dec 30 '24 14:12 const-volatile

Same here.

For the time being, or in case the PR does not get merged, a workaround as explained here would be to set TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD environment variable to "1".

You can do it in your operating system or just in python code (e.g. os.environ['TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD'] = '1') before using the library.

However, you'll get a warning like this:

UserWarning: Environment variable TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD detected, since the`weights_only` argument was not explicitly passed to torch.load, forcing weights_only=False.

As const-volatile pointed out, starting in version 2.4, pytorch wants you to explicitly pass weights_only parameter. If you do so, it would ignore the variable.

davidpedrosa avatar Feb 19 '25 09:02 davidpedrosa

When will this be merged?

suryakumaran2611 avatar Feb 19 '25 10:02 suryakumaran2611

please merge it thanks!

ROBERT-MCDOWELL avatar Mar 08 '25 14:03 ROBERT-MCDOWELL

Wish I had seen this. I monkeypatched the torch.load and got it to work.

import torch

# Save the original torch.load function
_original_torch_load = torch.load

# Define a new function that forces weights_only=False
def custom_torch_load(*args, **kwargs):
    if "weights_only" not in kwargs:
        kwargs["weights_only"] = False
    return _original_torch_load(*args, **kwargs)

# Override torch.load globally
torch.load = custom_torch_load

darthsinistro avatar Mar 15 '25 15:03 darthsinistro

Isn't it possible to make this project work with weights_only=True to avoid the arbitrary code execution attack? See https://github.com/MIC-DKFZ/nnUNet/issues/2681#issuecomment-2625179612, some people were able to do it on another project with add_safe_globals or safe_globals. A workaround was to downgrade torch and torchaudio to version 2.5.1.

baptx avatar Apr 11 '25 13:04 baptx