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

TypeError: forward() got an unexpected keyword argument 'return_dict' #1553

Open
lulmer opened this issue May 12, 2022 · 5 comments
Open

TypeError: forward() got an unexpected keyword argument 'return_dict' #1553

lulmer opened this issue May 12, 2022 · 5 comments

Comments

@lulmer
Copy link

lulmer commented May 12, 2022

I have an error with SentenceTransformers that throws TypeError: forward() got an unexpected keyword argument 'return_dict'

Here is the code to reproduce :

model_name = 'all-MiniLM-L6-v2'  # I also tried : 'distiluse-base-multilingual-cased-v1'
m= SentenceTransformer(model_name)
m.encode('This is an English sentence') 

Version tested :

  • sentence-transformers : 2.2.0 and 2.1.0
  • transformers : 4.18.0 and 4.16.2
@nreimers
Copy link
Member

Could you share a colab that shows this error? The above code works for me

@lulmer
Copy link
Author

lulmer commented May 13, 2022

Hello Nils,
I couldn't reproduce the bug on colab neither.

To be more precise, it happens in a conda environnent when installing the packages from conda-forge.
Can you try by creating a small test conda environment ?

conda create --name crashtest python=3.8
conda install -c conda-forge sentence-transformers
conda activate crashtest
python3

and running the lines in the python terminal :

from sentence_transformers import SentenceTransformer
model_name = 'all-MiniLM-L6-v2'  # I also tried : 'distiluse-base-multilingual-cased-v1'
m= SentenceTransformer(model_name)
m.encode('This is an English sentence') 

@lulmer
Copy link
Author

lulmer commented May 13, 2022

Here is the full detail of the libraries used in such environment :

# packages in environment at /blablabla/envs/crashtest:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
_openmp_mutex             4.5                       1_gnu  
blas                      1.0                         mkl  
brotlipy                  0.7.0           py38h497a2fe_1001    conda-forge
ca-certificates           2021.10.8            ha878542_0    conda-forge
certifi                   2021.10.8        py38h578d9bd_2    conda-forge
cffi                      1.15.0           py38hd667e15_1  
charset-normalizer        2.0.12             pyhd8ed1ab_0    conda-forge
click                     8.1.3            py38h578d9bd_0    conda-forge
colorama                  0.4.4              pyh9f0ad1d_0    conda-forge
cryptography              35.0.0           py38ha5dfef3_0    conda-forge
filelock                  3.6.0              pyhd8ed1ab_0    conda-forge
freetype                  2.10.4               h0708190_1    conda-forge
future                    0.18.2           py38h578d9bd_5    conda-forge
huggingface_hub           0.6.0              pyhd8ed1ab_0    conda-forge
idna                      3.3                pyhd8ed1ab_0    conda-forge
importlib-metadata        4.11.3           py38h578d9bd_1    conda-forge
importlib_metadata        4.11.3               hd8ed1ab_1    conda-forge
intel-openmp              2021.4.0          h06a4308_3561  
joblib                    1.1.0              pyhd8ed1ab_0    conda-forge
jpeg                      9e                   h7f8727e_0  
ld_impl_linux-64          2.35.1               h7274673_9  
libffi                    3.3                  he6710b0_2  
libgcc-ng                 9.3.0               h5101ec6_17  
libgfortran-ng            7.5.0               h14aa051_20    conda-forge
libgfortran4              7.5.0               h14aa051_20    conda-forge
libgomp                   9.3.0               h5101ec6_17  
libpng                    1.6.37               h21135ba_2    conda-forge
libstdcxx-ng              9.3.0               hd4cf53a_17  
libtiff                   4.0.10            hc3755c2_1005    conda-forge
lz4-c                     1.9.3                h9c3ff4c_1    conda-forge
mkl                       2021.4.0           h06a4308_640  
mkl-service               2.4.0            py38h497a2fe_0    conda-forge
mkl_fft                   1.3.1            py38hd3c417c_0  
mkl_random                1.2.2            py38h1abd341_0    conda-forge
ncurses                   6.3                  h7f8727e_2  
ninja                     1.10.2               h4bd325d_0    conda-forge
nltk                      3.6.7              pyhd8ed1ab_0    conda-forge
numpy                     1.21.5           py38he7a7128_2  
numpy-base                1.21.5           py38hf524024_2  
olefile                   0.46               pyh9f0ad1d_1    conda-forge
openssl                   1.1.1o               h7f8727e_0  
packaging                 21.3               pyhd8ed1ab_0    conda-forge
pillow                    6.2.1            py38h6b7be26_0    conda-forge
pip                       21.2.4           py38h06a4308_0  
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pyopenssl                 22.0.0             pyhd8ed1ab_0    conda-forge
pyparsing                 3.0.9              pyhd8ed1ab_0    conda-forge
pysocks                   1.7.1            py38h578d9bd_5    conda-forge
python                    3.8.13               h12debd9_0  
python_abi                3.8                      2_cp38    conda-forge
pytorch                   1.10.2          cpu_py38hfa7516b_0  
pyyaml                    5.4.1            py38h497a2fe_0    conda-forge
readline                  8.1.2                h7f8727e_1  
regex                     2022.3.15        py38h7f8727e_0  
requests                  2.27.1             pyhd8ed1ab_0    conda-forge
sacremoses                0.0.53             pyhd8ed1ab_0    conda-forge
scikit-learn              1.0.2            py38h51133e4_1  
scipy                     1.7.3            py38hc147768_0  
sentence-transformers     2.2.0              pyhd8ed1ab_0    conda-forge
sentencepiece             0.1.95           py38hd09550d_0  
setuptools                61.2.0           py38h06a4308_0  
six                       1.16.0             pyh6c4a22f_0    conda-forge
sqlite                    3.38.3               hc218d9a_0  
threadpoolctl             3.1.0              pyh8a188c0_0    conda-forge
tk                        8.6.11               h1ccaba5_1  
tokenizers                0.11.4           py38h3dcd8bd_1  
torchvision               0.9.1           py38h3f3a366_1_cpu    conda-forge
tqdm                      4.64.0             pyhd8ed1ab_0    conda-forge
transformers              4.18.0           py38h06a4308_0  
typing-extensions         4.2.0                hd8ed1ab_1    conda-forge
typing_extensions         4.2.0              pyha770c72_1    conda-forge
urllib3                   1.26.9             pyhd8ed1ab_0    conda-forge
wheel                     0.37.1             pyhd3eb1b0_0  
xz                        5.2.5                h7f8727e_1  
yaml                      0.2.5                h516909a_0    conda-forge
zipp                      3.8.0              pyhd8ed1ab_0    conda-forge
zlib                      1.2.12               h7f8727e_2  
zstd                      1.4.9                ha95c52a_0    conda-forge

@nreimers
Copy link
Member

I ran this on my Windows machine using Anaconda and it works, giving me back the embeddings.

Maybe re-installing your conda? Or maybe installing sentence transformers via pip?

@lulmer
Copy link
Author

lulmer commented May 16, 2022

Ok I finally found a workaround, I share it here so it can help someone that could have the same problem.
The problem was caused by conda using some packages from /home/my_username/.local/lib/python3.8/site-packages/ instead of /home/my_username/miniconda3/envs/crashtest/lib/python3.8/site-packages/.
as you can see in the full error traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/my_username/miniconda3/envs/crashtest/lib/python3.8/site-packages/sentence_transformers/SentenceTransformer.py", line 164, in encode
    out_features = self.forward(features)
  File "/home/my_username/.local/lib/python3.8/site-packages/torch/nn/modules/container.py", line 100, in forward
    input = module(input)
  File "/home/my_username/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/my_username/miniconda3/envs/crashtest/lib/python3.8/site-packages/sentence_transformers/models/Transformer.py", line 66, in forward
    output_states = self.auto_model(**trans_features, return_dict=False)
  File "/home/my_username/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'return_dict'

Maybe I had an old pytorch version installed in my /home/my_username/.local/lib/python3.8/site-packages/ or I don't know?

The following command solved my problem:
conda env config vars set PYTHONNOUSERSITE=1 -n crashtest
The line can be found here

Thank you anyway @nreimers, sorry for disturbing :)

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

No branches or pull requests

2 participants