deepset-ai / haystack

:mag: AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.
https://haystack.deepset.ai
Apache License 2.0
16.73k stars 1.83k forks source link

FARMReader Is Unable to Load xlm-roberta-large-squad2 from My Computer #858

Closed ynusinovich closed 3 years ago

ynusinovich commented 3 years ago

Describe the bug When I run reader = FARMReader(model_name_or_path="deepset/xlm-roberta-large-squad2"), the reader correctly loads. When I run reader = FARMReader(model_name_or_path="./xlm-roberta-large-squad2/"), where ./xlm-roberta-large-squad2/ is the path to a cloned version of the xlm-roberta-large-squad2 model from Huggingface, the reader crashes instead of loading. I'm trying to learn Flask and Docker, so I need to have the model pre-loaded in my Docker container (and load when I point to its path) rather than downloading the 2 GB model every time I run inference in the Flask app.

Error message


UnpicklingError Traceback (most recent call last) ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs) 1034 try: -> 1035 state_dict = torch.load(resolved_archive_file, map_location="cpu") 1036 except Exception:

~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/torch/serialization.py in load(f, map_location, pickle_module, pickle_load_args) 594 return _load(opened_zipfile, map_location, pickle_module, pickle_load_args) --> 595 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args) 596

~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, pickle_load_args) 763 --> 764 magic_number = pickle_module.load(f, pickle_load_args) 765 if magic_number != MAGIC_NUMBER:

UnpicklingError: invalid load key, 'v'.

During handling of the above exception, another exception occurred:

OSError Traceback (most recent call last)

in ----> 1 reader = FARMReader(model_name_or_path="./xlm-roberta-large-squad2/") ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/haystack/reader/farm.py in __init__(self, model_name_or_path, model_version, context_window_size, batch_size, use_gpu, no_ans_boost, return_no_answer, top_k_per_candidate, top_k_per_sample, num_processes, max_seq_len, doc_stride, progress_bar) 94 self.return_no_answers = return_no_answer 95 self.top_k_per_candidate = top_k_per_candidate ---> 96 self.inferencer = QAInferencer.load(model_name_or_path, batch_size=batch_size, gpu=use_gpu, 97 task_type="question_answering", max_seq_len=max_seq_len, 98 doc_stride=doc_stride, num_processes=num_processes, revision=model_version, ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/farm/infer.py in load(cls, model_name_or_path, revision, batch_size, gpu, task_type, return_class_probs, strict, max_seq_len, doc_stride, extraction_layer, extraction_strategy, s3e_stats, num_processes, disable_tqdm, tokenizer_class, use_fast, tokenizer_args, multithreading_rust, dummy_ph, benchmarking) 250 # a) either from local dir 251 if os.path.exists(model_name_or_path): --> 252 model = BaseAdaptiveModel.load(load_dir=model_name_or_path, device=device, strict=strict) 253 if task_type == "embeddings": 254 processor = InferenceProcessor.load_from_dir(model_name_or_path) ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/farm/modeling/adaptive_model.py in load(cls, **kwargs) 51 model = cls.subclasses["ONNXAdaptiveModel"].load(**kwargs) 52 else: ---> 53 model = cls.subclasses["AdaptiveModel"].load(**kwargs) 54 return model 55 ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/farm/modeling/adaptive_model.py in load(cls, load_dir, device, strict, lm_name, processor) 331 language_model = LanguageModel.load(load_dir, farm_lm_name=lm_name) 332 else: --> 333 language_model = LanguageModel.load(load_dir) 334 335 # Prediction heads ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/farm/modeling/language_model.py in load(cls, pretrained_model_name_or_path, revision, n_added_tokens, language_model_class, **kwargs) 148 149 if language_model_class: --> 150 language_model = cls.subclasses[language_model_class].load(pretrained_model_name_or_path, **kwargs) 151 else: 152 language_model = None ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/farm/modeling/language_model.py in load(cls, pretrained_model_name_or_path, language, **kwargs) 730 else: 731 # Huggingface transformer Style --> 732 xlm_roberta.model = XLMRobertaModel.from_pretrained(str(pretrained_model_name_or_path), **kwargs) 733 xlm_roberta.language = cls._get_or_infer_language_from_name(language, pretrained_model_name_or_path) 734 return xlm_roberta ~/opt/anaconda3/envs/aisc-nlp/lib/python3.8/site-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs) 1035 state_dict = torch.load(resolved_archive_file, map_location="cpu") 1036 except Exception: -> 1037 raise OSError( 1038 f"Unable to load weights from pytorch checkpoint file for '{pretrained_model_name_or_path}' " 1039 f"at '{resolved_archive_file}'" OSError: Unable to load weights from pytorch checkpoint file for './xlm-roberta-large-squad2/' at './xlm-roberta-large-squad2/pytorch_model.bin'If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.

Expected behavior The reader should load when I give the reader a valid path to the model in my local drive, in the same way that the reader loads when I give it a model name (after downloading the model).

Additional context The Conda environment has the following packages:

Name Version Build Channel

alembic 1.4.1 pypi_0 pypi aniso8601 8.1.1 pypi_0 pypi appdirs 1.4.4 pypi_0 pypi appnope 0.1.2 py38hecd8cb5_1001
argon2-cffi 20.1.0 py38h5406a74_2 conda-forge async_generator 1.10 py_0 conda-forge attrs 20.3.0 pyhd3deb0d_0 conda-forge azure-core 1.11.0 pypi_0 pypi azure-storage-blob 12.7.1 pypi_0 pypi backcall 0.2.0 pyhd3eb1b0_0
blas 1.0 mkl conda-forge bleach 3.3.0 pyh44b312d_0 conda-forge boto3 1.17.8 pypi_0 pypi botocore 1.20.8 pypi_0 pypi ca-certificates 2020.12.5 h033912b_0 conda-forge certifi 2020.12.5 py38h50d1736_1 conda-forge cffi 1.14.5 py38ha97d567_0 conda-forge chardet 4.0.0 pypi_0 pypi click 7.1.2 pyh9f0ad1d_0 conda-forge cloudpickle 1.6.0 pypi_0 pypi coverage 5.4 pypi_0 pypi cryptography 3.4.5 pypi_0 pypi cython 0.29.21 py38h23ab428_0
databricks-cli 0.14.1 pypi_0 pypi decorator 4.4.2 pyhd3eb1b0_0
defusedxml 0.6.0 py_0 conda-forge dill 0.3.3 pypi_0 pypi distlib 0.3.1 pypi_0 pypi docker 4.4.2 pypi_0 pypi dotmap 1.3.23 pypi_0 pypi elastic-apm 6.0.0 pypi_0 pypi elasticsearch 7.10.0 pypi_0 pypi entrypoints 0.3 pyhd8ed1ab_1003 conda-forge faiss-cpu 1.6.3 pypi_0 pypi farm 0.6.2 pypi_0 pypi farm-haystack 0.7.0 pypi_0 pypi fastapi 0.63.0 pypi_0 pypi filelock 3.0.12 pypi_0 pypi flask 1.1.2 pypi_0 pypi flask-cors 3.0.10 pypi_0 pypi flask-restplus 0.13.0 pypi_0 pypi gitdb 4.0.5 pypi_0 pypi gitpython 3.1.13 pypi_0 pypi grpcio 1.35.0 pypi_0 pypi grpcio-tools 1.35.0 pypi_0 pypi gunicorn 20.0.4 pypi_0 pypi h11 0.12.0 pypi_0 pypi httptools 0.1.1 pypi_0 pypi idna 2.10 pypi_0 pypi importlib-metadata 3.4.0 py38h50d1736_0 conda-forge importlib_metadata 3.4.0 hd8ed1ab_0 conda-forge intel-openmp 2019.4 233
intelpython 2021.1.1 0 intel ipykernel 5.4.3 py38h9bb44b7_0 conda-forge ipython 7.20.0 py38h01d92e1_1
ipython_genutils 0.2.0 pyhd3eb1b0_1
ipywidgets 7.6.3 pyhd3deb0d_0 conda-forge isodate 0.6.0 pypi_0 pypi itsdangerous 1.1.0 py_0 conda-forge jedi 0.17.0 py38_0
jinja2 2.11.3 pyh44b312d_0 conda-forge jmespath 0.10.0 pypi_0 pypi joblib 1.0.1 pyhd8ed1ab_0 conda-forge jsonschema 3.2.0 py_2 conda-forge jupyter_client 6.1.7 py_0 conda-forge jupyter_core 4.7.1 py38hecd8cb5_0
jupyterlab_pygments 0.1.2 pyh9f0ad1d_0 conda-forge jupyterlab_widgets 1.0.0 pyhd8ed1ab_1 conda-forge langdetect 1.0.8 pypi_0 pypi libcxx 11.0.1 habf9029_0 conda-forge libedit 3.1.20191231 h0678c8f_2 conda-forge libffi 3.3 h046ec9c_2 conda-forge libgfortran 3.0.1 h93005f0_2
libsodium 1.0.18 h1de35cc_0
llvm-openmp 10.0.0 h28b9765_0 conda-forge lxml 4.6.2 pypi_0 pypi mako 1.1.4 pypi_0 pypi markupsafe 1.1.1 py38h5406a74_3 conda-forge mistune 0.8.4 py38h5406a74_1003 conda-forge mkl 2019.4 233
mkl-service 2.3.0 py38h9ed2024_0
mkl_fft 1.2.0 py38hc64f4ea_0
mkl_random 1.1.1 py38h959d312_0
mlflow 1.13.1 pypi_0 pypi more-itertools 8.7.0 pypi_0 pypi msrest 0.6.21 pypi_0 pypi nbclient 0.5.2 pyhd8ed1ab_0 conda-forge nbconvert 6.0.7 py38h50d1736_3 conda-forge nbformat 5.1.2 pyhd8ed1ab_1 conda-forge ncurses 6.2 h2e338ed_4 conda-forge nest-asyncio 1.4.3 pyhd8ed1ab_0 conda-forge networkx 2.5 pypi_0 pypi nltk 3.5 pypi_0 pypi notebook 6.2.0 py38h50d1736_0 conda-forge numpy 1.20.1 pypi_0 pypi numpy-base 1.19.2 py38hcfb5961_0
oauthlib 3.1.0 pypi_0 pypi openssl 1.1.1j hbcf498f_0 conda-forge packaging 20.9 pyh44b312d_0 conda-forge pandas 1.2.1 py38hb2f4e1b_0
pandoc 2.11.4 h35c211d_0 conda-forge pandocfilters 1.4.2 py_1 conda-forge parso 0.8.1 pyhd3eb1b0_0
pexpect 4.8.0 pyhd3eb1b0_3
pickleshare 0.7.5 pyhd3eb1b0_1003
pip 21.0.1 pyhd8ed1ab_0 conda-forge pluggy 0.13.1 pypi_0 pypi prometheus-flask-exporter 0.18.1 pypi_0 pypi prometheus_client 0.9.0 pyhd3deb0d_0 conda-forge prompt-toolkit 3.0.8 py_0 conda-forge protobuf 3.14.0 pypi_0 pypi psutil 5.8.0 pypi_0 pypi psycopg2-binary 2.8.6 pypi_0 pypi ptyprocess 0.7.0 pyhd3eb1b0_2
py 1.10.0 pypi_0 pypi pycparser 2.20 pyh9f0ad1d_2 conda-forge pydantic 1.7.3 pypi_0 pypi pygments 2.7.4 pyhd3eb1b0_0
pymilvus 0.4.0 pypi_0 pypi pyparsing 2.4.7 pyh9f0ad1d_0 conda-forge pyrsistent 0.17.3 py38h5406a74_2 conda-forge python 3.8.6 h624753d_5_cpython conda-forge python-dateutil 2.8.1 pyhd3eb1b0_0
python-docx 0.8.10 pypi_0 pypi python-editor 1.0.4 pypi_0 pypi python-multipart 0.0.5 pypi_0 pypi python_abi 3.8 1_cp38 conda-forge pytz 2021.1 pyhd3eb1b0_0
pyyaml 5.4.1 pypi_0 pypi pyzmq 20.0.0 py38h23ab428_1
querystring-parser 1.2.4 pypi_0 pypi readline 8.0 h0678c8f_2 conda-forge regex 2020.11.13 pypi_0 pypi requests 2.25.1 pypi_0 pypi requests-oauthlib 1.3.0 pypi_0 pypi s3transfer 0.3.4 pypi_0 pypi sacremoses 0.0.43 pypi_0 pypi scikit-learn 0.23.2 py38h959d312_0
scipy 1.6.1 pypi_0 pypi send2trash 1.5.0 py_0 conda-forge sentencepiece 0.1.95 pypi_0 pypi seqeval 1.2.2 pypi_0 pypi setuptools 49.6.0 py38h50d1736_3 conda-forge six 1.15.0 py38hecd8cb5_0
sklearn 0.0 pypi_0 pypi smmap 3.0.5 pypi_0 pypi sqlalchemy 1.3.23 pypi_0 pypi sqlalchemy-utils 0.36.8 pypi_0 pypi sqlite 3.34.0 h17101e1_0 conda-forge sqlparse 0.4.1 pypi_0 pypi starlette 0.13.6 pypi_0 pypi tabulate 0.8.7 pypi_0 pypi terminado 0.9.2 py38h50d1736_0 conda-forge testpath 0.4.4 py_0 conda-forge threadpoolctl 2.1.0 pyh5ca1d4c_0 conda-forge tika 1.19 pypi_0 pypi tk 8.6.10 h0419947_1 conda-forge tokenizers 0.9.4 pypi_0 pypi toml 0.10.2 pypi_0 pypi torch 1.7.1 pypi_0 pypi tornado 6.1 py38h9ed2024_0
tox 3.21.4 pypi_0 pypi tqdm 4.57.0 pypi_0 pypi traitlets 5.0.5 pyhd3eb1b0_0
transformers 4.1.1 pypi_0 pypi typing-extensions 3.7.4.3 pypi_0 pypi ujson 4.0.2 pypi_0 pypi urllib3 1.26.3 pypi_0 pypi uvicorn 0.13.3 pypi_0 pypi uvloop 0.14.0 pypi_0 pypi virtualenv 20.4.2 pypi_0 pypi wcwidth 0.2.5 py_0
webencodings 0.5.1 py_1 conda-forge websocket-client 0.57.0 pypi_0 pypi werkzeug 0.16.1 pypi_0 pypi wget 3.2 pypi_0 pypi wheel 0.36.2 pyhd3deb0d_0 conda-forge widgetsnbextension 3.5.1 py38h50d1736_4 conda-forge xz 5.2.5 haf1e3a3_1 conda-forge zeromq 4.3.3 hb1e8313_3
zipp 3.4.0 py_0 conda-forge zlib 1.2.11 h7795811_1010 conda-forge

To Reproduce

from haystack.reader.farm import FARMReader reader = FARMReader(model_name_or_path="deepset/xlm-roberta-large-squad2") # this works reader = FARMReader(model_name_or_path="./xlm-roberta-large-squad2/") # this crashes I git cloned xlm-roberta-large-squad2 into the same directory as the notebook that has the commands above. I used the terminal command git clone https://huggingface.co/deepset/xlm-roberta-large-squad2 in the directory of the notebook.

System:

Timoeller commented 3 years ago

Hey @ynusinovich did you try saving the FARMReadermodel with its save() method?

FARMReader can currently only load huggingface models from HF modelzoo (or local cache dir) not a "normal" local folder.

ynusinovich commented 3 years ago

@Timoeller Thank you so much, that solves the problem! reader.save("./saved_model/") in the notebook where I'm using the reader, followed by reader = FARMReader(model_name_or_path="./saved_model/") in the notebook where I'm loading the reader makes it work correctly!

Timoeller commented 3 years ago

Nice, glad I could help : )

giuliodz commented 3 years ago

I am having the same issue but it is not going away. I saved my transformer locally like this:

from haystack.reader.farm import FARMReader

reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=False).save('./trained_model')

And I am trying to access it in my local flask API with:

reader = FARMReader(model_name_or_path='./trained_model/',
                            use_gpu=False, return_no_answer=True, no_ans_boost=0.7, context_window_size=200)

But, when I run my api and make a post to the endpoint that loads the reader I am getting this error:

OSError: Can't load config for './trained_model/'. Make sure that:

- './trained_model/' is a correct model identifier listed on 'https://huggingface.co/models'

- or './trained_model/' is the correct path to a directory containing a config.json file

And I noticed that in my ./trained_model folder there is no config.json file.

So I copy pasted the config.json file from https://huggingface.co/deepset/roberta-base-squad2/resolve/main/config.json into my ./trained_model folder. but it is still throwing the same error.

Can you see what the problem is?

giuliodz commented 3 years ago

The weird thing is that it was working up to few days ago...

Timoeller commented 3 years ago

Hey @giuliodz are you sure that you have the correct current directory specified and the folder is residing there?

For sanity you could try saving and loading the model as you posted above outside your API:

from haystack.reader.farm import FARMReader

reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=False).save('./trained_model')
reader2 = FARMReader(model_name_or_path='./trained_model/',
                            use_gpu=False, return_no_answer=True, no_ans_boost=0.7, context_window_size=200)

If that works it should be a path problem related to your configs.

bappctl commented 3 years ago

@Timoeller Followed exact steps as below the model is not getting saved to trained_model local folder

from haystack.reader.farm import FARMReader

reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=False).save('./trained_model')
reader2 = FARMReader(model_name_or_path='./trained_model/',
                            use_gpu=False, return_no_answer=True, no_ans_boost=0.7, context_window_size=200)
Timoeller commented 3 years ago

Strange technology :see_no_evil: What do you mean by the model is not saved to the folder? Do you get an error message or do you just not see the folder? Which is your current working directory and which Operating System do you use?

You could try specifying the full path instead of a relative path (./trained_model), e.g. on Linux /home/user/foo/bar/trained_model