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

Split OnnxRuntime into high-level and low-level generated code #323

Closed

Conversation

mcimadamore
Copy link
Collaborator

@mcimadamore mcimadamore commented Feb 20, 2025

This PR reorganizes the code in the OnnxRuntime class. It now contains an higher-level implementation of the onnx C API, which is separate from the low-level bindings. These bindings are defined in the new foreign package, and have been mechanically generated with a tweaked version of jextract which:

  • omits some of the code that is not required (e.g. struct getters/setters)
  • make the generated code more usable for onnx use case -- meaning each struct field should have an "invoker" accessor, which fetches the struct pointer and then invokes it

The generated code is still quite big (OrtApi struct contains 10K LoC). I can make the bindings smaller if we only want to focus on the handful of functions we use today -- but in its current form, the low-level bindings are "complete" and support the full C API.

The main changes in OnnxRuntime are as follows:

  • calls to function pointers in OrtApi/OrtApiBase now are handled as calls to static wrappers generated by jextract.
  • there's no need to try/catch and wrap exceptions because the generated code does that
  • some of the explicit reinterpret calls with an explicit size have been replaced by generated reinterpret methods (which use the correct struct layout size)
  • some of the constants (logging level, API version) are now taken from the generated code

When doing this exercise, I uncovered an issue with the MHs for a couple of functions (ReleaseEnv and ReleaseSession) being incorrect -- the underlying function pointers for these are void (so no result) and they don't accept a trailing ret segment. Also calling checkStatus on them makes no sense (again, because these functions don't return a status).


Progress

  • Change must not contain extraneous whitespace

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/babylon.git pull/323/head:pull/323
$ git checkout pull/323

Update a local copy of the PR:
$ git checkout pull/323
$ git pull https://git.openjdk.org/babylon.git pull/323/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 323

View PR using the GUI difftool:
$ git pr show -t 323

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/323.diff

Using Webrev

Link to Webrev Comment

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@bridgekeeper
Copy link

bridgekeeper bot commented Feb 20, 2025

👋 Welcome back mcimadamore! A progress list of the required criteria for merging this PR into code-reflection will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Feb 20, 2025

@mcimadamore This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

Split OnnxRuntime into high-level and low-level generated code

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been no new commits pushed to the code-reflection branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the code-reflection branch, type /integrate in a new comment.

@openjdk openjdk bot added ready Pull request is ready to be integrated rfr Pull request is ready for review labels Feb 20, 2025
@mlbridge
Copy link

mlbridge bot commented Feb 20, 2025

Webrevs

@openjdk
Copy link

openjdk bot commented Feb 24, 2025

@mcimadamore this pull request can not be integrated into code-reflection due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout onnx_jextract
git fetch https://git.openjdk.org/babylon.git code-reflection
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge code-reflection"
git push

@openjdk openjdk bot added merge-conflict Pull request has merge conflict with target branch and removed ready Pull request is ready to be integrated labels Feb 24, 2025
@mcimadamore
Copy link
Collaborator Author

I've added some changes to make sure that the segments returned by the high-level API keep the automatic arena reachable. There are two places where I've added a reinterpret:

  • retAddr -- as the output parameter ret is accessed, a segment is read (which will have no arena)
  • run -- this also features output parameters (outputs) so we need to keep the segments read from these output parameters connected to the automatic arena

Fix lifetime of output address params
@openjdk
Copy link

openjdk bot commented Feb 24, 2025

@mcimadamore Please do not rebase or force-push to an active PR as it invalidates existing review comments. Note for future reference, the bots always squash all changes into a single commit automatically as part of the integration. See OpenJDK Developers’ Guide for more information.

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed merge-conflict Pull request has merge conflict with target branch labels Feb 24, 2025
@mcimadamore
Copy link
Collaborator Author

/integrate

@openjdk
Copy link

openjdk bot commented Feb 24, 2025

Going to push as commit 4752b37.

@openjdk openjdk bot added the integrated Pull request has been integrated label Feb 24, 2025
@openjdk openjdk bot closed this Feb 24, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Feb 24, 2025
@openjdk
Copy link

openjdk bot commented Feb 24, 2025

@mcimadamore Pushed as commit 4752b37.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

None yet

1 participant