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 Extensions to Java and build with Gradle #18503

Open
HossamAmer12 opened this issue Nov 18, 2023 · 7 comments
Open

Add ORT Extensions to Java and build with Gradle #18503

HossamAmer12 opened this issue Nov 18, 2023 · 7 comments
Labels
api:Java issues related to the Java API platform:mobile issues related to ONNX Runtime mobile; typically submitted using template

Comments

@HossamAmer12
Copy link

Describe the documentation issue

I saw your tutorial here:
https://onnxruntime.ai/docs/extensions/

I added:
implementation("com.microsoft.onnxruntime:onnxruntime-extensions-android:latest.release")

I added:
import ai.onnxruntime.*

I added:
sessionOptions.registerCustomOpLibrary(OrtxPackage.getLibraryPath());

the last file gives me the error saying that the OrtxPackage does not exist. What is missing?

Page / URL

No response

@HossamAmer12 HossamAmer12 added the documentation improvements or additions to documentation; typically submitted using template label Nov 18, 2023
@github-actions github-actions bot added api:Java issues related to the Java API platform:mobile issues related to ONNX Runtime mobile; typically submitted using template labels Nov 18, 2023
@Craigacp
Copy link
Contributor

Did you import ai.onnxruntime.extensions.*?

@HossamAmer12
Copy link
Author

HossamAmer12 commented Nov 19, 2023

Thanks @Craigacp. Btw, I am adding the onnx code in a Kotlin file. I think that it should not matter much.

I have this now in App.kt:

import ai.onnxruntime.OnnxTensor
import ai.onnxruntime.OrtEnvironment
import ai.onnxruntime.OrtSession
import ai.onnxruntime.extensions.*
public class Predictor {

    val env = OrtEnvironment.getEnvironment()
    val sessionOptions = OrtSession.SessionOptions()
    /* Register the custom ops from onnxruntime-extensions */
    sessionOptions.registerCustomOpLibrary(OrtxPackage.getLibraryPath());
}

and in build.gradle.kts:

// onnx deps
implementation("com.microsoft.onnxruntime:onnxruntime-android:1.15.1")
implementation("com.microsoft.onnxruntime:onnxruntime:latest.release")
implementation("com.microsoft.onnxruntime:onnxruntime-extensions-android:latest.release")

The errors are:

Unresolved reference: extensions
Expecting member declaration

If I add:
implementation("com.microsoft.onnxruntime:onnxruntime-extensions:latest.release")
in build.gradle.kts. I get the following error:

Error while evaluating property 'filteredArgumentsMap' of task ':app:compileKotlin'.
Could not resolve all files for configuration ':app:compileClasspath'.
> Could not find any matches for com.microsoft.onnxruntime:onnxruntime-extensions:latest.release as no versions of com.microsoft.onnxruntime:onnxruntime-extensions are available.

Any clue?

@Craigacp
Copy link
Contributor

I don't think it's a good idea to have both onnxruntime-android and onnxruntime as dependencies. It's clearly not adding the dependency for onnxruntime-extensions-android correctly, possibly due to something in the aar packaging, but I don't understand Android packaging so I'm not sure what is going wrong.

@HossamAmer12
Copy link
Author

Please note that the code that I am writing is a simple Kotlin and Jave code and not android code.

I am wondering if this

implementation("com.microsoft.onnxruntime:onnxruntime-extensions-android:latest.release")

only works for Android and not Kotlin+Java. Any other alternatives to run this? Do I have to run in Android? Can I just run it on my laptop?

@Craigacp
Copy link
Contributor

You shouldn't mix the Android and the regular Java jars, they aren't compatible. The Java source code itself is (mostly) the same, but the native code underneath is compiled differently. Unfortunately there aren't any Java binaries available for onnxruntime-extensions yet (see microsoft/onnxruntime-extensions#597), so you'll need to compile it from source on the platforms you care about. This isn't too hard, and in practice you don't even need to build the extensions jar you can just compile the extensions native library and load it as a custom op library.

@HossamAmer12
Copy link
Author

This isn't too hard, and in practice you don't even need to build the extensions jar you can just compile the extensions native library and load it as a custom op library.

Is it simply adding these files and compile them via gradle?

@HossamAmer12
Copy link
Author

HossamAmer12 commented Nov 20, 2023

@Craigacp I did some work on the other issue. Let's try to get some help over there

@natke natke removed the documentation improvements or additions to documentation; typically submitted using template label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:Java issues related to the Java API platform:mobile issues related to ONNX Runtime mobile; typically submitted using template
Projects
None yet
Development

No branches or pull requests

3 participants