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

Provider parameter needs to be set when instantiating InferenceSession in ONNXAdaptiveModel #1973

Closed
cjb06776 opened this issue Jan 6, 2022 · 1 comment · Fixed by #1976
Assignees

Comments

@cjb06776
Copy link
Contributor

cjb06776 commented Jan 6, 2022

Describe the bug
More recent versions of Onnx require the provider parameter to be explicitly set when creating a onnxruntime.InferenceSession, which results in errors currently when loading an ONNXAdaptiveModel.

Error message

  File "/usr/local/lib/python3.6/dist-packages/farm/modeling/adaptive_model.py", line 653, in load
    onnx_session = onnxruntime.InferenceSession(str(load_dir / "model.onnx"), sess_options)
  File "/usr/local/lib/python3.6/dist-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 335, in __init__
    self._create_inference_session(providers, provider_options, disabled_optimizers)
  File "/usr/local/lib/python3.6/dist-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 364, in _create_inference_session
    "onnxruntime.InferenceSession(..., providers={}, ...)".format(available_providers))

ValueError: This ORT build has ['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider'] enabled. Since ORT 1.9, you 
are required to explicitly set the providers parameter when instantiating InferenceSession. For example, onnxruntime.InferenceSession(..., providers=['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider'], ...)

I've made the below change to ONNXAdaptiveModel.load in my local environment to workaround the problem, the change:

  • Sets a default CUDAExecutionProvider or CPUExecutionProvider provider depending on the device type passed in, if no provider(s) are specified.
  • Optionally allow passing in of a custom provider list

Can create a PR with this if you like...

   @classmethod
    def load(cls, load_dir: Union[str, Path], device: str, **kwargs):  # type: ignore
        """
        Loads an ONNXAdaptiveModel from a directory.

        :param load_dir: Location where the ONNXAdaptiveModel is stored.
        :param device: The device on which this model will operate. Either "cpu" or "cuda".
        """
        load_dir = Path(load_dir)
        import onnxruntime
        sess_options = onnxruntime.SessionOptions()
        # Set graph optimization level to ORT_ENABLE_EXTENDED to enable bert optimization.
        sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_EXTENDED
        # Use OpenMP optimizations. Only useful for CPU, has little impact for GPUs.
        sess_options.intra_op_num_threads = multiprocessing.cpu_count()
-       onnx_session = onnxruntime.InferenceSession(str(load_dir / "model.onnx"), sess_options)
+       providers = kwargs.get("providers", ["CPUExecutionProvider"] if device.type == "cpu" else ["CUDAExecutionProvider"])
+       onnx_session = onnxruntime.InferenceSession(str(load_dir / "model.onnx"), sess_options, providers=providers)
@julian-risch
Copy link
Member

Hi @cjb06776 thanks for pointing out that issue and for suggesting a fix already. We'd really appreciate it if you could create a PR, yes please. 👍 Would these changes be backwards compatible in the sense that users with an older ONXX version could also work with the changed code? Could you please check that in your PR? Thank you!

cjb06776 pushed a commit to cjb06776/haystack that referenced this issue Jan 7, 2022
ZanSara added a commit that referenced this issue Mar 23, 2022
#1976)

* Set provider parameter when instantiating onnxruntime.InferenceSession
fixes #1973

* Change device type to torch.device

* set type annotation of device to torch.device everywhere

* Apply Black

* Change types of device and devices params across the codebase

* Update Documentation & Code Style

* Add type: ignore in the right location

* Update Documentation & Code Style

* Add type: ignore

* feedback

* Update Documentation & Code Style

* feedback 2

* Fix convert_to_transformers

* Fix syntax error

* Update Documentation & Code Style

* Consider augment and load_glove user-facing as well

* Update Documentation & Code Style

* Fix mypy

* Update Documentation & Code Style

Co-authored-by: Julian Risch <julian.risch@deepset.ai>
Co-authored-by: Sara Zan <sara.zanzottera@deepset.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants