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

RuntimeError: Error(s) in loading state_dict for RobertaModel: Unexpected key(s) in state_dict: "embeddings.position_ids". #12982

Closed
dzenilee opened this issue Sep 14, 2023 · 6 comments
Labels
feat / transformer Feature: Transformer install Installation issues

Comments

@dzenilee
Copy link

dzenilee commented Sep 14, 2023

How to reproduce the behaviour

import spacy 
nlp = spacy.load('en_core_web_trf')

Full traceback:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[7], line 1
----> 1 nlp = spacy.load('en_core_web_trf')

File /opt/conda/lib/python3.8/site-packages/spacy/__init__.py:51, in load(name, vocab, disable, enable, exclude, config)
     27 def load(
     28     name: Union[str, Path],
     29     *,
   (...)
     34     config: Union[Dict[str, Any], Config] = util.SimpleFrozenDict(),
     35 ) -> Language:
     36     """Load a spaCy model from an installed package or a local path.
     37 
     38     name (str): Package name or model path.
   (...)
     49     RETURNS (Language): The loaded nlp object.
     50     """
---> 51     return util.load_model(
     52         name,
     53         vocab=vocab,
     54         disable=disable,
     55         enable=enable,
     56         exclude=exclude,
     57         config=config,
     58     )

File /opt/conda/lib/python3.8/site-packages/spacy/util.py:465, in load_model(name, vocab, disable, enable, exclude, config)
    463     return get_lang_class(name.replace("blank:", ""))()
    464 if is_package(name):  # installed as package
--> 465     return load_model_from_package(name, **kwargs)  # type: ignore[arg-type]
    466 if Path(name).exists():  # path to model data directory
    467     return load_model_from_path(Path(name), **kwargs)  # type: ignore[arg-type]

File /opt/conda/lib/python3.8/site-packages/spacy/util.py:501, in load_model_from_package(name, vocab, disable, enable, exclude, config)
    484 """Load a model from an installed package.
    485 
    486 name (str): The package name.
   (...)
    498 RETURNS (Language): The loaded nlp object.
    499 """
    500 cls = importlib.import_module(name)
--> 501 return cls.load(vocab=vocab, disable=disable, enable=enable, exclude=exclude, config=config)

File /opt/conda/lib/python3.8/site-packages/en_core_web_trf/__init__.py:10, in load(**overrides)
      9 def load(**overrides):
---> 10     return load_model_from_init_py(__file__, **overrides)

File /opt/conda/lib/python3.8/site-packages/spacy/util.py:682, in load_model_from_init_py(init_file, vocab, disable, enable, exclude, config)
    680 if not model_path.exists():
    681     raise IOError(Errors.E052.format(path=data_path))
--> 682 return load_model_from_path(
    683     data_path,
    684     vocab=vocab,
    685     meta=meta,
    686     disable=disable,
    687     enable=enable,
    688     exclude=exclude,
    689     config=config,
    690 )

File /opt/conda/lib/python3.8/site-packages/spacy/util.py:547, in load_model_from_path(model_path, meta, vocab, disable, enable, exclude, config)
    538 config = load_config(config_path, overrides=overrides)
    539 nlp = load_model_from_config(
    540     config,
    541     vocab=vocab,
   (...)
    545     meta=meta,
    546 )
--> 547 return nlp.from_disk(model_path, exclude=exclude, overrides=overrides)

File /opt/conda/lib/python3.8/site-packages/spacy/language.py:2155, in Language.from_disk(self, path, exclude, overrides)
   2152 if not (path / "vocab").exists() and "vocab" not in exclude:  # type: ignore[operator]
   2153     # Convert to list here in case exclude is (default) tuple
   2154     exclude = list(exclude) + ["vocab"]
-> 2155 util.from_disk(path, deserializers, exclude)  # type: ignore[arg-type]
   2156 self._path = path  # type: ignore[assignment]
   2157 self._link_components()

File /opt/conda/lib/python3.8/site-packages/spacy/util.py:1392, in from_disk(path, readers, exclude)
   1389 for key, reader in readers.items():
   1390     # Split to support file names like meta.json
   1391     if key.split(".")[0] not in exclude:
-> 1392         reader(path / key)
   1393 return path

File /opt/conda/lib/python3.8/site-packages/spacy/language.py:2149, in Language.from_disk.<locals>.<lambda>(p, proc)
   2147     if not hasattr(proc, "from_disk"):
   2148         continue
-> 2149     deserializers[name] = lambda p, proc=proc: proc.from_disk(  # type: ignore[misc]
   2150         p, exclude=["vocab"]
   2151     )
   2152 if not (path / "vocab").exists() and "vocab" not in exclude:  # type: ignore[operator]
   2153     # Convert to list here in case exclude is (default) tuple
   2154     exclude = list(exclude) + ["vocab"]

File /opt/conda/lib/python3.8/site-packages/spacy_transformers/pipeline_component.py:416, in Transformer.from_disk(self, path, exclude)
    409         self.model.attrs["set_transformer"](self.model, hf_model)
    411 deserialize = {
    412     "vocab": self.vocab.from_disk,
    413     "cfg": lambda p: self.cfg.update(deserialize_config(p)),
    414     "model": load_model,
    415 }
--> 416 util.from_disk(path, deserialize, exclude)  # type: ignore
    417 return self

File /opt/conda/lib/python3.8/site-packages/spacy/util.py:1392, in from_disk(path, readers, exclude)
   1389 for key, reader in readers.items():
   1390     # Split to support file names like meta.json
   1391     if key.split(".")[0] not in exclude:
-> 1392         reader(path / key)
   1393 return path

File /opt/conda/lib/python3.8/site-packages/spacy_transformers/pipeline_component.py:390, in Transformer.from_disk.<locals>.load_model(p)
    388 try:
    389     with open(p, "rb") as mfile:
--> 390         self.model.from_bytes(mfile.read())
    391 except AttributeError:
    392     raise ValueError(Errors.E149) from None

File /opt/conda/lib/python3.8/site-packages/thinc/model.py:638, in Model.from_bytes(self, bytes_data)
    636 msg = srsly.msgpack_loads(bytes_data)
    637 msg = convert_recursive(is_xp_array, self.ops.asarray, msg)
--> 638 return self.from_dict(msg)

File /opt/conda/lib/python3.8/site-packages/thinc/model.py:676, in Model.from_dict(self, msg)
    674         node.set_param(param_name, value)
    675     for i, shim_bytes in enumerate(msg["shims"][i]):
--> 676         node.shims[i].from_bytes(shim_bytes)
    677 return self

File /opt/conda/lib/python3.8/site-packages/spacy_transformers/layers/hf_shim.py:120, in HFShim.from_bytes(self, bytes_data)
    118     filelike.seek(0)
    119     device = get_torch_default_device()
--> 120     self._model.load_state_dict(torch.load(filelike, map_location=device))
    121     self._model.to(device)
    122 else:

File /opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py:2041, in Module.load_state_dict(self, state_dict, strict)
   2036         error_msgs.insert(
   2037             0, 'Missing key(s) in state_dict: {}. '.format(
   2038                 ', '.join('"{}"'.format(k) for k in missing_keys)))
   2040 if len(error_msgs) > 0:
-> 2041     raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
   2042                        self.__class__.__name__, "\n\t".join(error_msgs)))
   2043 return _IncompatibleKeys(missing_keys, unexpected_keys)

RuntimeError: Error(s) in loading state_dict for RobertaModel:
	Unexpected key(s) in state_dict: "embeddings.position_ids". 

Also:

~$ conda list torch
# packages in environment at /opt/conda:
#
# Name                    Version                   Build  Channel
efficientnet-pytorch      0.7.1              pyhd8ed1ab_1    conda-forge
pytorch                   2.0.1           py3.8_cuda11.7_cudnn8.5.0_0    pytorch
pytorch-cuda              11.7                 h67b0de4_0    pytorch
pytorch-lightning         2.0.1.post0              pypi_0    pypi
pytorch-mutex             1.0                        cuda    pytorch
rotary-embedding-torch    0.2.1                    pypi_0    pypi
torchaudio                2.0.2                py38_cu117    pytorch
torchmetrics              0.11.4                   pypi_0    pypi
torchtriton               2.0.0                      py38    pytorch
torchvision               0.15.2               py38_cu117    pytorch
torchviz                  0.0.2                    pypi_0    pypi

Your Environment

  • Operating System:
  • Python Version Used:
  • spaCy Version Used:
  • Environment Information:
spaCy version    3.6.1
Location         /opt/conda/lib/python3.8/site-packages/spacy
Platform         Linux-5.13.0-1023-aws-x86_64-with-glibc2.17
Python version   3.8.17
Pipelines        en_core_web_trf (3.6.1)
@adrianeboyd
Copy link
Contributor

Most likely you have a version of transformers that is too new for the current spacy-transformers release. Can you show the versions of spacy-transformers and transformers?

It can also be helpful to run pip check to see if it finds some conflicting package versions.

@adrianeboyd adrianeboyd added the feat / transformer Feature: Transformer label Sep 14, 2023
@adrianeboyd
Copy link
Contributor

This is related to #12900, but spacy-transformers v1.3.0 was yanked from both PyPI and conda-forge, so it's hopefully unlikely that you have that installed.

@dzenilee
Copy link
Author

It can also be helpful to run pip check to see if it finds some conflicting package versions.

^ Thanks for this. Downgrading to transformers==4.30.2 resolved it.

@adrianeboyd adrianeboyd added the install Installation issues label Sep 15, 2023
@seanaedmiston
Copy link

Is there any indication when spacy-transformers will be compatible with hf transformers>=4.31? 4.31 is the minimum for running Llama 2 models.

@adrianeboyd
Copy link
Contributor

We are working on it (explosion/spacy-transformers#398, explosion/spacy-transformers#401) but haven't made a final decision about extending the transformers requirements beyond the current (and therefore tested) release.

@github-actions
Copy link
Contributor

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat / transformer Feature: Transformer install Installation issues
Projects
None yet
Development

No branches or pull requests

3 participants