Skip to content

Conversation

@const-volatile
Copy link

@const-volatile const-volatile commented Dec 30, 2024

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.

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
@davidpedrosa
Copy link

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.

@suryakumaran2611
Copy link

When will this be merged?

@ROBERT-MCDOWELL
Copy link

please merge it thanks!

@darthsinistro
Copy link

darthsinistro commented Mar 15, 2025

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

@baptx
Copy link

baptx commented Apr 11, 2025

Isn't it possible to make this project work with weights_only=True to avoid the arbitrary code execution attack?
See MIC-DKFZ/nnUNet#2681 (comment), 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants