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

[Mobile] Android/Kotlin/JAVA Multi Threading for Multi models in android app #21289

Open
molo6379 opened this issue Jul 9, 2024 · 3 comments
Labels
api:Java issues related to the Java API platform:mobile issues related to ONNX Runtime mobile; typically submitted using template stale issues that have not been addressed in a while; categorized by a bot

Comments

@molo6379
Copy link

molo6379 commented Jul 9, 2024

Describe the issue

There are no example about Multi-threading on android device regarding of using multiple models

To reproduce

N/A

Urgency

I really need it ASAP please help :(((

Platform

Android

OS Version

12

ONNX Runtime Installation

Built from Source

Compiler Version (if 'Built from Source')

No response

Package Name (if 'Released Package')

onnxruntime-android

ONNX Runtime Version or Commit ID

com.microsoft.onnxruntime:onnxruntime-android:latest.release

ONNX Runtime API

Java/Kotlin

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

I am trying to use two models on android for inference but wanna make them run parallel.

private suspend fun createOrtSession(): OrtSession? {
    val so = OrtSession.SessionOptions()
    so.use {
        return ortEnv?.createSession(readModel(), so)
    }
}
private suspend fun createOrtSession2(): OrtSession? {
    val so = OrtSession.SessionOptions()
    so.use {
        return ortEnv?.createSession(readModel2(), so)
    }
}


val env = OrtEnvironment.getEnvironment()

env.use {
    val tensor = OnnxTensor.createTensor(env, imgData, shape)
    val tensor2 = OnnxTensor.createTensor(env, imgData2, shape2)

    val startTime = SystemClock.uptimeMillis()

    tensor2.use {
        val timeStart2 = System.currentTimeMillis()
        val output2 = ortSession2?.run(Collections.singletonMap(inputName2, tensor2))
//                Log.i(TAG, "Depth 모델 시간: " + (System.currentTimeMillis() - timeStart2).toString())
        result2.log.add("Depth Model 결과 : " + " " + (System.currentTimeMillis() - timeStart2))
        output2.use {

            val depthArray = output2?.get(0)?.value as Array<Array<FloatArray>>
//                    Log.i(TAG, Arrays.deepToString(depthArray))
        }
    }


    tensor.use {

        val timeStart3 = System.currentTimeMillis()
        val output = ortSession?.run(Collections.singletonMap(inputName, tensor))
//                Log.i(TAG, "Detection 모델 시간: " + (System.currentTimeMillis() - timeStart3).toString())
        result2.log.add("Detection Model 결과 : " + " " + (System.currentTimeMillis() - timeStart3))
        output.use {

Above code is that I made 2 seperate sessions for two different models but now Im running them in serial which is very inefficient.

Can I get an example of how to do Multi-thread on android and run two models in parallel?

@molo6379 molo6379 added the platform:mobile issues related to ONNX Runtime mobile; typically submitted using template label Jul 9, 2024
@github-actions github-actions bot added the api:Java issues related to the Java API label Jul 9, 2024
@tianleiwu
Copy link
Contributor

tianleiwu commented Jul 9, 2024

Since you have two independent sessions, you can have one thread to call one session to run inference.

For kotlin multiple threading, you can look at examples like https://medium.com/@korhanbircan/multithreading-and-kotlin-ac28eed57fea. For example, you can have two threads.

class DepthModelThread: Thread() {
    public override fun run() {
        ... // depth model inference code here.
    }
}
class DetectionModelThread: Thread() {
    public override fun run() {
        ... // detection model inference code here.
    }
}

For onnxruntime Kotlin/Java example, you can look at this:
https://github.com/microsoft/onnxruntime-inference-examples/blob/main/mobile/examples/super_resolution/android/app/src/main/java/ai/onnxruntime/example/superresolution/SuperResPerformer.kt

@Craigacp
Copy link
Contributor

Craigacp commented Jul 9, 2024

Copy link
Contributor

github-actions bot commented Aug 8, 2024

This issue has been automatically marked as stale due to inactivity and will be closed in 30 days if no further activity occurs. If further support is needed, please provide an update and/or more details.

@github-actions github-actions bot added the stale issues that have not been addressed in a while; categorized by a bot label Aug 8, 2024
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 stale issues that have not been addressed in a while; categorized by a bot
Projects
None yet
Development

No branches or pull requests

3 participants