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

add ort session options #8433

Merged
merged 9 commits into from
May 19, 2023

Conversation

leqiao-1
Copy link
Contributor

Related Issues/PRs

#8413

What changes are proposed in this pull request?

Add onnxruntime session options when saving and loading onnx model with pyfunc

How is this patch tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests (describe details, including test results, below)

Does this PR change the documentation?

  • No. You can skip the rest of this section.
  • Yes. Make sure the changed pages / sections render correctly in the documentation preview.

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

User can provide onnxruntime session options when saving and loading onnx model with pyfunc.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/recipes: Recipes, Recipe APIs, Recipe configs, Recipe Templates
  • area/projects: MLproject format, project running backends
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/server-infra: MLflow Tracking server backend
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

@github-actions github-actions bot added area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs. labels May 15, 2023
@mlflow-automation
Copy link
Collaborator

mlflow-automation commented May 15, 2023

Documentation preview for 82d4667 will be available here when this CircleCI job completes successfully.

More info

@github-actions
Copy link

@leqiao-1 Thank you for the contribution! Could you fix the following issue(s)?

⚠ DCO check

The DCO check failed. Please sign off your commit(s) by following the instructions here. See https://github.com/mlflow/mlflow/blob/master/CONTRIBUTING.md#sign-your-work for more details.

@leqiao-1 leqiao-1 force-pushed the leqiao/ort_session_options branch 2 times, most recently from 99e9bfa to 8a75bc6 Compare May 15, 2023 09:31
mlflow/onnx.py Outdated
@@ -176,6 +178,7 @@ def save_model(
onnx_version=onnx.__version__,
data=model_data_subpath,
providers=onnx_execution_providers,
options=onnx_session_options,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add some validation here, validate the option keys and value types.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use onnx_session_options=onnx_session_options ?

sess_options.inter_op_num_threads = inter_op_num_threads
if intra_op_num_threads:
sess_options.intra_op_num_threads = intra_op_num_threads
if execution_mode:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not force execution_mode value to be one of "SEQUENTIAL" and "PARALLEL" ? and we should document it.

sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel(
graph_optimization_level
)
if extra_session_config:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be an arbitrary dict ?

Copy link
Collaborator

@WeichenXu123 WeichenXu123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall good, but I have some comments.

@@ -255,9 +255,9 @@ def __init__(self, path, providers=None):
if intra_op_num_threads:
sess_options.intra_op_num_threads = intra_op_num_threads
if execution_mode:
if execution_mode == 0:
if execution_mode.upper() == "SEQUENTIAL":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document that when logging model, we should use "SEQUENTIAL" or "PARALLEL" as session option value.

mlflow/onnx.py Outdated Show resolved Hide resolved
mlflow/onnx.py Outdated Show resolved Hide resolved
mlflow/onnx.py Outdated
Comment on lines 134 to 142
:param onnx_session_options: Dictionary of options to be passed to onnxruntime.InferenceSession.
For example:
``{
'graph_optimization_level': 99,
'intra_op_num_threads': 1,
'inter_op_num_threads': 1,
'execution_mode': 'sequential'
}``
'execution_mode' can be set ot 'sequential' or 'parallel'.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls also add the doc in "log_model" method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been added

@@ -176,6 +188,7 @@ def save_model(
onnx_version=onnx.__version__,
data=model_data_subpath,
providers=onnx_execution_providers,
onnx_session_options=onnx_session_options,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add simple verification:

  • check keys are valid
  • for execution_mode, check value are valid
  • for other option keys, check types are correct, e.g., inter_op_num_threads should be integer that is > 0

@leqiao-1 leqiao-1 force-pushed the leqiao/ort_session_options branch 2 times, most recently from a7ad12c to 39c3a21 Compare May 18, 2023 07:26
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
Signed-off-by: leqiao <leqiao@microsoft.com>
@leqiao-1 leqiao-1 force-pushed the leqiao/ort_session_options branch from 39c3a21 to 275ddca Compare May 18, 2023 07:27
if execution_mode.upper() == "SEQUENTIAL":
sess_options.execution_mode = onnxruntime.ExecutionMode.ORT_SEQUENTIAL
elif execution_mode.upper() == "PARALLEL":
sess_options.execution_mode = onnxruntime.ExecutionMode.ORT_PARALLEL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for other values, raises error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK we don't need this. We already do verification when saving model

Copy link
Collaborator

@WeichenXu123 WeichenXu123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@WeichenXu123 WeichenXu123 enabled auto-merge (squash) May 18, 2023 12:06
@WeichenXu123
Copy link
Collaborator

@leqiao-1 Could you fix the lint errors https://github.com/mlflow/mlflow/actions/runs/5011618497/jobs/8987189752?pr=8433

Once you fix them, I will merge the PR.

auto-merge was automatically disabled May 19, 2023 02:54

Head branch was pushed to by a user without write access

Signed-off-by: leqiao <leqiao@microsoft.com>
@leqiao-1 leqiao-1 force-pushed the leqiao/ort_session_options branch from 049735c to 82d4667 Compare May 19, 2023 02:55
@leqiao-1
Copy link
Contributor Author

Format fixed. Thanks

@WeichenXu123 WeichenXu123 enabled auto-merge (squash) May 19, 2023 04:22
@WeichenXu123 WeichenXu123 merged commit 4c91827 into mlflow:master May 19, 2023
33 checks passed
Comment on lines +171 to +174
raise ValueError(
f"Key {key} in onnx_session_options is not a valid \
ONNX Runtime session options key"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the actual error message look like:

key = 1
raise ValueError(
    f"Key {key} in onnx_session_options is not a valid \
                        ONNX Runtime session options key"
)
Traceback (most recent call last):
  File "a.py", line 2, in <module>
    raise ValueError(
ValueError: Key 1 in onnx_session_options is not a valid                         ONNX Runtime session options key

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Comment on lines +262 to +270
inter_op_num_threads = options.get("inter_op_num_threads")
intra_op_num_threads = options.get("intra_op_num_threads")
execution_mode = options.get("execution_mode")
graph_optimization_level = options.get("graph_optimization_level")
extra_session_config = options.get("extra_session_config")
if inter_op_num_threads:
sess_options.inter_op_num_threads = inter_op_num_threads
if intra_op_num_threads:
sess_options.intra_op_num_threads = intra_op_num_threads
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified by using assignment expressions:

if inter_op_num_threads := options.get("inter_op_num_threads"):
    # do something with inter_op_num_threads

@leqiao-1 leqiao-1 mentioned this pull request May 19, 2023
33 tasks
@leqiao-1
Copy link
Contributor Author

@harupy I created another PR to fix the comments. #8472

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants