Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

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

@cjb06776

Description

@cjb06776
Contributor

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.

"""
def __init__(self, onnx_session, language_model_class, language, prediction_heads, device):
if str(device) == "cuda" and onnxruntime.get_device() != "GPU":
raise Exception(f"Device {device} not available for Inference. For CPU, run pip install onnxruntime and"
f"for GPU run pip install onnxruntime-gpu")
self.onnx_session = onnx_session

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...

Activity

Timoeller

Timoeller commented on Nov 12, 2021

@Timoeller
Contributor

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

added a commit that references this issue on Nov 12, 2021
76640a5
added a commit that references this issue on Nov 12, 2021
267c08e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @Timoeller@cjb06776

      Issue actions

        ONNXAdaptiveModel causes NameError: name 'onnxruntime' is not defined · Issue #847 · deepset-ai/FARM