deepset-ai / FARM

:house_with_garden: Fast & easy transfer learning for NLP. Harvesting language models for the industry. Focus on Question Answering.
https://farm.deepset.ai
Apache License 2.0
1.74k stars 247 forks source link

ONNXAdaptiveModel causes NameError: name 'onnxruntime' is not defined #847

Closed cjb06776 closed 3 years ago

cjb06776 commented 3 years ago

When trying to load an ONNX model:

 File "/home/ubuntu/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/haystack/reader/farm.py", line 103, in __init__
    strict=False)
  File "/home/ubuntu/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/farm/infer.py", line 252, in load
    model = BaseAdaptiveModel.load(load_dir=model_name_or_path, device=device, strict=strict)
  File "/home/ubuntu/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/farm/modeling/adaptive_model.py", line 51, in load
    model = cls.subclasses["ONNXAdaptiveModel"].load(**kwargs)
  File "/home/ubuntu/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/farm/modeling/adaptive_model.py", line 670, in load
    return cls(onnx_session, language_model_class, language, prediction_heads, device)
  File "/home/ubuntu/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/farm/modeling/adaptive_model.py", line 634, in __init__
    if str(device) == "cuda" and onnxruntime.get_device() != "GPU":
NameError: name 'onnxruntime' is not defined

Looks like ONNXAdaptiveModel.__init__ is perhaps just missing a local import onnxruntime, as is done in ONNXAdaptiveModel.load.

https://github.com/deepset-ai/FARM/blob/77530ab088bf634b1e0943fb95fd9b7bb04edb4d/farm/modeling/adaptive_model.py#L638-L643

I've manually worked around it by adding the import, after which everything else seems to work fine. Can create a PR with this change if you like...

Timoeller commented 3 years ago

It seems we havent really worked much with ONNX models... :smile: If you could create a PR fixing this that would be great!