UKPLab / sentence-transformers

State-of-the-Art Text Embeddings
https://www.sbert.net
Apache License 2.0
15.36k stars 2.49k forks source link

No module named 'torch.utils._pytree' #2297

Closed hududed closed 2 months ago

hududed commented 1 year ago

Just running the minimal code:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')

#Our sentences we like to encode
sentences = ['This framework generates embeddings for each input sentence',
    'Sentences are passed as a list of string.',
    'The quick brown fox jumps over the lazy dog.']

#Sentences are encoded by calling model.encode()
embeddings = model.encode(sentences)

#Print the embeddings
for sentence, embedding in zip(sentences, embeddings):
    print("Sentence:", sentence)
    print("Embedding:", embedding)
    print("")

but running to this issue:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File [~/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py:1130](https://file+.vscode-resource.vscode-cdn.net/Users/hud/projects/mm-sat/~/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py:1130), in _LazyModule._get_module(self, module_name)
   [1129](file:///Users/hud/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py?line=1128) try:
-> [1130](file:///Users/hud/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py?line=1129)     return importlib.import_module("." + module_name, self.__name__)
   [1131](file:///Users/hud/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py?line=1130) except Exception as e:

File [~/.pyenv/versions/3.9.17/lib/python3.9/importlib/__init__.py:127](https://file+.vscode-resource.vscode-cdn.net/Users/hud/projects/mm-sat/~/.pyenv/versions/3.9.17/lib/python3.9/importlib/__init__.py:127), in import_module(name, package)
    [126](file:///Users/hud/.pyenv/versions/3.9.17/lib/python3.9/importlib/__init__.py?line=125)         level += 1
--> [127](file:///Users/hud/.pyenv/versions/3.9.17/lib/python3.9/importlib/__init__.py?line=126) return _bootstrap._gcd_import(name[level:], package, level)

File :1030, in _gcd_import(name, package, level)

File :1007, in _find_and_load(name, import_)

File :986, in _find_and_load_unlocked(name, import_)

File :680, in _load_unlocked(spec)

File :850, in exec_module(self, module)

File :228, in _call_with_frames_removed(f, *args, **kwds)

File [~/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/models/bert/modeling_bert.py:31](https://file+.vscode-resource.vscode-cdn.net/Users/hud/projects/mm-sat/~/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/models/bert/modeling_bert.py:31)
     [30](file:///Users/hud/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/models/bert/modeling_bert.py?line=29) from ...activations import ACT2FN
...
   [1134](file:///Users/hud/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py?line=1133)         f" traceback):\n{e}"
   [1135](file:///Users/hud/projects/mm-sat/.venv/lib/python3.9/site-packages/transformers/utils/import_utils.py?line=1134)     ) from e

RuntimeError: Failed to import transformers.models.bert.modeling_bert because of the following error (look up to see its traceback):
No module named 'torch.utils._pytree'

I am on a mac 11.7.8 (20G1351) no GPUs - please advise

panchaml commented 1 year ago

Facing the same issue with from ._utils import _import_dotted_name, classproperty ModuleNotFoundError: No module named 'torch._utils'

tomaarsen commented 10 months ago

Hello!

I'm unable to reproduce this. It seems that perhaps your torch install is problematic. Perhaps you can try reinstalling it? Searching for ModuleNotFoundError: No module named 'torch._utils' will produce other people with the same issue for different projects, so it's not sentence-transformers related. Some users say that they encountered it after installing torch without enough space left on their file-system, perhaps that was the cause?

MingchangLi commented 2 months ago

I met same error and I fixed it by upgrading torch, sentence-transformers, torchvision to latest version.

tomaarsen commented 2 months ago

That sounds like a good recommendation. Because I think your solution will solve this problem for everyone facing this, I'll mark this as resolved.