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

Delay import onnxruntime to avoid ImportError when onnxruntime is missing and not needed #235

Merged
merged 5 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions .azure-pipelines/linux-conda-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
python.version: '3.9'
ONNX_PATH: onnx==1.12.0

Python310_112:
python.version: '3.10'
ONNX_PATH: onnx==1.12.0


maxParallel: 4

Expand All @@ -56,12 +60,22 @@ jobs:
conda install -c conda-forge protobuf
conda install -c conda-forge numpy
pip install -r requirements.txt
pip install pytest
displayName: 'Install dependencies'

- script: |
python -c "import onnxconverter_common"
displayName: 'Check import without onnxruntime'

- script: |
pip install onnxruntime
pip install onnxmltools
pip install $(ONNX_PATH)
pip install pytest
displayName: 'Install onnxruntime, onnxmltools'

- script: |
pip install -e .
displayName: 'Install dependencies'
displayName: 'Install onnxconverter_common'

- script: |
pip install flake8
Expand Down
3 changes: 2 additions & 1 deletion onnxconverter_common/auto_mixed_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def validate(res1, res2):

"""

import onnxruntime as ort
import onnx
import numpy as np
from onnxconverter_common import float16
Expand Down Expand Up @@ -127,6 +126,8 @@ def add_missing_dtypes_using_ort(model, feed_dict, outputs_per_iter=100):


def get_tensor_values_using_ort(model, input_feed, output_names=None, sess_options=None):
# delayed import to avoid taking a strong dependancy on onnxruntime
import onnxruntime as ort
if output_names is None:
sess = ort.InferenceSession(model.SerializeToString(), sess_options, providers=['CUDAExecutionProvider'])
return sess.run(None, input_feed)
Expand Down
6 changes: 4 additions & 2 deletions onnxconverter_common/auto_mixed_precision_model_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import copy
import numpy as np
import onnxruntime as ort
import onnx
import os
import uuid
Expand Down Expand Up @@ -115,7 +114,8 @@ def validate(res1, res2):
kwargs["is_final_model"] = False
result = _convert_and_check_inference_result(**kwargs)
if not result:
raise ValueError("Validation failed for model with nothing converted to fp16.")
raise ValueError("Validation failed for model with nothing converted to fp16. "
"Given parameters %r." % kwargs)

final_block_list = _find_nodes_blocking_fp16(**kwargs)

Expand Down Expand Up @@ -245,6 +245,8 @@ def _convert_and_check_inference_result(**kwargs):


def inference(model_path, input_feed, providers=None):
# delayed import to avoid taking a strong dependancy on onnxruntime
import onnxruntime as ort
sess = ort.InferenceSession(model_path, None, providers=providers)
output = sess.run(None, input_feed)
return output
Expand Down