Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scvi.model.SCVI.load error #3186

Open
francicco opened this issue Feb 12, 2025 · 1 comment
Open

scvi.model.SCVI.load error #3186

francicco opened this issue Feb 12, 2025 · 1 comment
Labels

Comments

@francicco
Copy link

Hi,

I'm trying to reload a saved model with the command:
model.save('Diul_model/')

This is how I'm trying to load it:
model = scvi.model.SCVI.load('/Users/tk19812/Documents/scRNA-Project/H5AD_datssets/Diul_model/', adata)

But I'm getting this error:

INFO     File /Users/tk19812/Documents/scRNA-Project/H5AD_datssets/Diul_model/model.pt already downloaded          
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[56], line 7
      1 import torch
      3 torch.serialization.add_safe_globals({
      4     'numpy.core.multiarray._reconstruct': __import__('numpy.core.multiarray').core.multiarray._reconstruct
      5 })
----> 7 model = scvi.model.SCVI.load('/Users/tk19812/Documents/scRNA-Project/H5AD_datssets/Diul_model/', adata)

File ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/scvi/model/base/_base_model.py:699, in BaseModelClass.load(cls, dir_path, adata, accelerator, device, prefix, backup_url)
    686 load_adata = adata is None
    687 _, _, device = parse_device_args(
    688     accelerator=accelerator,
    689     devices=device,
    690     return_device="torch",
    691     validate_single_device=True,
    692 )
    694 (
    695     attr_dict,
    696     var_names,
    697     model_state_dict,
    698     new_adata,
--> 699 ) = _load_saved_files(
    700     dir_path,
    701     load_adata,
    702     map_location=device,
    703     prefix=prefix,
    704     backup_url=backup_url,
    705 )
    706 adata = new_adata if new_adata is not None else adata
    708 _validate_var_names(adata, var_names)

File ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/scvi/model/base/_save_load.py:76, in _load_saved_files(dir_path, load_adata, prefix, map_location, backup_url)
     74 try:
     75     _download(backup_url, dir_path, model_file_name)
---> 76     model = torch.load(model_path, map_location=map_location)
     77 except FileNotFoundError as exc:
     78     raise ValueError(
     79         f"Failed to load model file at {model_path}. "
     80         "If attempting to load a saved model from <v0.15.0, please use the util function "
     81         "`convert_legacy_save` to convert to an updated format."
     82     ) from exc

File ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/torch/serialization.py:1462, in load(f, map_location, pickle_module, weights_only, mmap, **pickle_load_args)
   1460 if weights_only:
   1461     try:
-> 1462         return _load(
   1463             opened_zipfile,
   1464             map_location,
   1465             _weights_only_unpickler,
   1466             overall_storage=overall_storage,
   1467             **pickle_load_args,
   1468         )
   1469     except pickle.UnpicklingError as e:
   1470         raise pickle.UnpicklingError(_get_wo_message(str(e))) from None

File ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/torch/serialization.py:1964, in _load(zip_file, map_location, pickle_module, pickle_file, overall_storage, **pickle_load_args)
   1962 global _serialization_tls
   1963 _serialization_tls.map_location = map_location
-> 1964 result = unpickler.load()
   1965 _serialization_tls.map_location = None
   1967 torch._utils._validate_loaded_sparse_tensors()

File ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/torch/_weights_only_unpickler.py:334, in Unpickler.load(self)
    332 if full_path in _get_allowed_globals():
    333     self.append(_get_allowed_globals()[full_path])
--> 334 elif full_path in _get_user_allowed_globals():
    335     self.append(_get_user_allowed_globals()[full_path])
    336 elif full_path in (
    337     [
    338         "torch.nested._internal.nested_tensor.NestedTensor",
   (...)
    341     ]
    342 ):

File ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/torch/_weights_only_unpickler.py:144, in _get_user_allowed_globals()
    142         rc[name] = f
    143     else:
--> 144         module, name = f.__module__, f.__name__
    145         rc[f"{module}.{name}"] = f
    146 return rc

AttributeError: 'str' object has no attribute '__module__'

Thanks a lot
Francesco

@francicco francicco added the bug label Feb 12, 2025
@canergen
Copy link
Member

Hi, it looks like torch tries to load weights only. This is supposed to be fixed in the most recent versions of scvi-tools. I assume that you use an old version of scvi-tools and the most recent torch version. Can you update scvi-tools to 1.2.2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants