|
| 1 | +package com.swmansion.rnexecutorch |
| 2 | + |
| 3 | +import com.facebook.react.bridge.Promise |
| 4 | +import com.facebook.react.bridge.ReactApplicationContext |
| 5 | +import com.facebook.react.bridge.ReadableArray |
| 6 | +import com.swmansion.rnexecutorch.utils.ArrayUtils |
| 7 | +import com.swmansion.rnexecutorch.utils.Fetcher |
| 8 | +import com.swmansion.rnexecutorch.utils.ProgressResponseBody |
| 9 | +import com.swmansion.rnexecutorch.utils.ResourceType |
| 10 | +import com.swmansion.rnexecutorch.utils.TensorUtils |
| 11 | +import okhttp3.OkHttpClient |
| 12 | +import org.pytorch.executorch.Module |
| 13 | +import org.pytorch.executorch.Tensor |
| 14 | +import java.net.URL |
| 15 | + |
| 16 | +class ETModule(reactContext: ReactApplicationContext) : NativeETModuleSpec(reactContext) { |
| 17 | + private lateinit var module: Module |
| 18 | + private val client = OkHttpClient() |
| 19 | + |
| 20 | + override fun getName(): String { |
| 21 | + return NAME |
| 22 | + } |
| 23 | + |
| 24 | + private fun downloadModel( |
| 25 | + url: URL, resourceType: ResourceType, callback: (path: String?, error: Exception?) -> Unit |
| 26 | + ) { |
| 27 | + Fetcher.downloadResource(reactApplicationContext, |
| 28 | + client, |
| 29 | + url, |
| 30 | + resourceType, |
| 31 | + { path, error -> callback(path, error) }, |
| 32 | + object : ProgressResponseBody.ProgressListener { |
| 33 | + override fun onProgress(bytesRead: Long, contentLength: Long, done: Boolean) { |
| 34 | + } |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + override fun loadModule(modelPath: String, promise: Promise) { |
| 39 | + try { |
| 40 | + downloadModel( |
| 41 | + URL(modelPath), ResourceType.MODEL |
| 42 | + ) { path, error -> |
| 43 | + if (error != null) { |
| 44 | + promise.reject(error.message!!, "-1") |
| 45 | + return@downloadModel |
| 46 | + } |
| 47 | + |
| 48 | + module = Module.load(path) |
| 49 | + promise.resolve(0) |
| 50 | + return@downloadModel |
| 51 | + } |
| 52 | + } catch (e: Exception) { |
| 53 | + promise.reject(e.message!!, "-1") |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + override fun loadMethod(methodName: String, promise: Promise) { |
| 58 | + val result = module.loadMethod(methodName) |
| 59 | + if (result != 0) { |
| 60 | + promise.reject("Method loading failed", result.toString()) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + promise.resolve(result) |
| 65 | + } |
| 66 | + |
| 67 | + override fun forward( |
| 68 | + input: ReadableArray, |
| 69 | + shape: ReadableArray, |
| 70 | + inputType: Double, |
| 71 | + promise: Promise |
| 72 | + ) { |
| 73 | + try { |
| 74 | + val executorchInput = |
| 75 | + TensorUtils.getExecutorchInput(input, ArrayUtils.createLongArray(shape), inputType.toInt()) |
| 76 | + |
| 77 | + lateinit var result: Tensor |
| 78 | + module.forward(executorchInput)[0].toTensor().also { result = it } |
| 79 | + |
| 80 | + promise.resolve(ArrayUtils.createReadableArray(result)) |
| 81 | + return |
| 82 | + } catch (e: IllegalArgumentException) { |
| 83 | + //The error is thrown when transformation to Tensor fails |
| 84 | + promise.reject("Forward Failed Execution", "18") |
| 85 | + return |
| 86 | + } catch (e: Exception) { |
| 87 | + //Executorch forward method throws an exception with a message: "Method forward failed with code XX" |
| 88 | + val exceptionCode = e.message!!.substring(e.message!!.length - 2) |
| 89 | + promise.reject("Forward Failed Execution", exceptionCode) |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + companion object { |
| 95 | + const val NAME = "ETModule" |
| 96 | + } |
| 97 | +} |
0 commit comments