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

[java] Changes OrtEnvironment so it can't be closed by users #10670

Merged
merged 2 commits into from
Mar 1, 2022

Conversation

Craigacp
Copy link
Contributor

Description:
The changes in #10200 enforce that OrtEnv can't be created more than once in a single process. The Java API used to allow this to ensure that users could configure thread pools and logging levels in the environment. This PR changes OrtEnvironment so there can only be a single instance of it for the lifetime of the JVM (well, as much as it can be enforced in Java, it may be possible to unload the OrtEnvironment.class entirely and reload it, resetting the state of its statics and allowing another environment to be created). The environment now registers a JVM shutdown hook which releases the environment as the JVM shuts down. This could cause the environment to be released while a daemon thread which uses ORT is running, as daemon threads concurrently execute with the shutdown hooks, however it's not possible to prevent this from within Java.

This caused a few refactorings in the tests to ensure that the thread pool tests can still run. These refactorings slow down the Java tests slightly as they now need to fork a new JVM for each test class. There might be some implication for the android build system here if it runs the Java tests as part of the build, but I'm not familiar with how those are used.

OrtEnvironment still implements AutoCloseable though the close method is now a no-op. Removing this interface would be a breaking change on users, and we can consider if we want to do that or not.

Much of this PR is visual noise in the tests from the indentation change of moving the environment creation out of a try-with-resources block.

Motivation and Context

@Craigacp
Copy link
Contributor Author

Note this PR will conflict with the other two PRs I have open (#10653 and #10318), and so if they are merged before this then it'll need some fixes and vice versa.

env.close();
assertTrue(env.isClosed());
}
private static final OrtEnvironment env = OrtEnvironment.getEnvironment();
Copy link
Member

Choose a reason for hiding this comment

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

env = OrtEnvironment.getEnvironment();

Is there a test that checks that repeated calls return the same instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I've not added an explicit test for that. I can do.

@yuslepukhin
Copy link
Member

/azp run Linux CPU CI Pipeline, Linux CPU Minimal Build E2E CI Pipeline, Linux CPU x64 NoContribops CI Pipeline, Linux GPU CI Pipeline, Linux GPU TensorRT CI Pipeline, Linux Nuphar CI Pipeline, Linux OpenVINO CI Pipeline, MacOS CI Pipeline

@yuslepukhin
Copy link
Member

/azp run MacOS NoContribops CI Pipeline, Windows CPU CI Pipeline, Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, Windows WebAssembly CI Pipeline, orttraining-amd-gpu-ci-pipeline, orttraining-linux-ci-pipeline, orttraining-linux-gpu-ci-pipeline, orttraining-ortmodule-distributed

@azure-pipelines
Copy link

Azure Pipelines successfully started running 7 pipeline(s).

1 similar comment
@azure-pipelines
Copy link

Azure Pipelines successfully started running 7 pipeline(s).

@pranavsharma
Copy link
Contributor

FYI see my comment here #10200 (comment). Could close() offer the same functionality that the C API offers? I suppose that would help with the tests of creating the env with different options?

@Craigacp
Copy link
Contributor Author

Craigacp commented Feb 25, 2022

The close is the problem though, my understanding is that closing the OrtEnv such that its destructor is called causes issues with the changes in #10200 which means that creating a fresh OrtEnv in the same process fails.

We could refcount it (as it was before) but the try-with-resources idiom that is encouraged in Java would mean that it's being closed and reopened multiple times. There's no good way for a user to signal that they are completely done with an object such that it can't be rebuilt ever again in Java, because most of the time that's not a useful thing to do.

@yuslepukhin
Copy link
Member

/azp run onnxruntime-binary-size-checks-ci-pipeline, onnxruntime-python-checks-ci-pipeline, ONNX Runtime Web CI Pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Craigacp Craigacp force-pushed the java-environment-refactor branch from 7c8f09b to b37aee6 Compare February 28, 2022 18:30
@Craigacp
Copy link
Contributor Author

Rebased after the merge of #10318.

@edgchen1
Copy link
Contributor

/azp run Windows CPU CI Pipeline, Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, Windows WebAssembly CI Pipeline, orttraining-amd-gpu-ci-pipeline, orttraining-linux-ci-pipeline, orttraining-linux-gpu-ci-pipeline, orttraining-ortmodule-distributed, onnxruntime-python-checks-ci-pipeline

@edgchen1
Copy link
Contributor

/azp run Linux CPU CI Pipeline, Linux CPU Minimal Build E2E CI Pipeline, Linux GPU CI Pipeline, Linux GPU TensorRT CI Pipeline, Linux Nuphar CI Pipeline, Linux OpenVINO CI Pipeline, MacOS CI Pipeline, ONNX Runtime Web CI Pipeline, onnxruntime-binary-size-checks-ci-pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 8 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 9 pipeline(s).

pranavsharma
pranavsharma previously approved these changes Feb 28, 2022
Copy link
Contributor

@pranavsharma pranavsharma left a comment

Choose a reason for hiding this comment

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

nit: unrelated to the objective of this PR - the check for INSTANCE == null can be avoided in the getEnvironment variants keeping it in only once place.

@Craigacp
Copy link
Contributor Author

nit: unrelated to the objective of this PR - the check for INSTANCE == null can be avoided in the getEnvironment variants keeping it in only once place.

The three checks do different things. The one in getEnvironment() is used to avoid the warning message which is usually printed if you try to change the logging name or level which comes from getEnvironment(OrtLoggingLevel, String). The one in getEnvironment(OrtLoggingLevel, String, ThreadingOptions) is to make it throw an exception because you can't change the thread pool and setting the thread pool incorrectly is (in my judgement, which is open to suggestion) more of a programming error than having the wrong string prepended to all the log messages.

@edgchen1
Copy link
Contributor

/azp run Windows CPU CI Pipeline, Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, Windows WebAssembly CI Pipeline, orttraining-amd-gpu-ci-pipeline, orttraining-linux-ci-pipeline, orttraining-linux-gpu-ci-pipeline, orttraining-ortmodule-distributed, onnxruntime-python-checks-ci-pipeline

@edgchen1
Copy link
Contributor

/azp run Linux CPU CI Pipeline, Linux CPU Minimal Build E2E CI Pipeline, Linux GPU CI Pipeline, Linux GPU TensorRT CI Pipeline, Linux Nuphar CI Pipeline, Linux OpenVINO CI Pipeline, MacOS CI Pipeline, ONNX Runtime Web CI Pipeline, onnxruntime-binary-size-checks-ci-pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 8 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 9 pipeline(s).

@pranavsharma
Copy link
Contributor

LGTM 👍

Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

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

:shipit:

@pranavsharma pranavsharma merged commit f856608 into microsoft:master Mar 1, 2022
@edgchen1
Copy link
Contributor

edgchen1 commented Mar 1, 2022

Thanks for the change @Craigacp!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants