pytorch-lite-multiplatform

所属分类:iPhone/iOS
开发工具:kotlin
文件大小:0KB
下载次数:0
上传日期:2023-08-11 08:50:53
上 传 者sh-1993
说明:  Android和iOS上PyTorch-Lite库的Kotlin多平台包装器。,
(A Kotlin multi-platform wrapper around the PyTorch Lite libraries on Android and iOS.,)

文件列表:
LICENCE (11357, 2023-08-11)
PLMLibTorchWrapper.podspec (998, 2023-08-11)
build.gradle.kts (5737, 2023-08-11)
build_dummy_model.py (1823, 2023-08-11)
convention-plugins/ (0, 2023-08-11)
convention-plugins/build.gradle.kts (244, 2023-08-11)
convention-plugins/src/ (0, 2023-08-11)
convention-plugins/src/main/ (0, 2023-08-11)
convention-plugins/src/main/kotlin/ (0, 2023-08-11)
convention-plugins/src/main/kotlin/convention.publication.gradle.kts (3106, 2023-08-11)
example/ (0, 2023-08-11)
example/androidApp/ (0, 2023-08-11)
example/androidApp/build.gradle.kts (626, 2023-08-11)
example/androidApp/src/ (0, 2023-08-11)
example/androidApp/src/main/ (0, 2023-08-11)
example/androidApp/src/main/AndroidManifest.xml (632, 2023-08-11)
example/androidApp/src/main/java/ (0, 2023-08-11)
example/androidApp/src/main/java/de/ (0, 2023-08-11)
example/androidApp/src/main/java/de/voize/ (0, 2023-08-11)
example/androidApp/src/main/java/de/voize/example/ (0, 2023-08-11)
example/androidApp/src/main/java/de/voize/example/plm/ (0, 2023-08-11)
example/androidApp/src/main/java/de/voize/example/plm/android/ (0, 2023-08-11)
example/androidApp/src/main/java/de/voize/example/plm/android/MainActivity.kt (746, 2023-08-11)
example/androidApp/src/main/res/ (0, 2023-08-11)
example/androidApp/src/main/res/layout/ (0, 2023-08-11)
example/androidApp/src/main/res/layout/activity_main.xml (812, 2023-08-11)
example/androidApp/src/main/res/values/ (0, 2023-08-11)
example/androidApp/src/main/res/values/colors.xml (207, 2023-08-11)
example/androidApp/src/main/res/values/styles.xml (299, 2023-08-11)
example/build.gradle.kts (420, 2023-08-11)
example/gradle.properties (231, 2023-08-11)
example/gradle/ (0, 2023-08-11)
... ...

# pytorch-lite-multiplatform ![CI](https://github.com/voize-gmbh/pytorch-lite-multiplatform/actions/workflows/test.yml/badge.svg) ![Maven Central](https://img.shields.io/maven-central/v/de.voize/pytorch-lite-multiplatform) ![Cocoapods](https://img.shields.io/cocoapods/v/PLMLibTorchWrapper) A Kotlin multi-platform wrapper around the PyTorch Lite libraries on [Android](https://pytorch.org/mobile/android/) and [iOS](https://pytorch.org/mobile/ios/). You can use this library in your Kotlin multi-platform project to write mobile inference code for PyTorch Lite models. The API is very close to the Android API of PyTorch Lite. ## Installation Add the following to your `shared/build.gradle.kts` as a `commonMain` dependency. ```kotlin implementation("de.voize:pytorch-lite-multiplatform:") ``` Add the `PLMLibTorchWrapper` pod to your `cocoapods` plugin block in `shared/build.gradle.kts` and add `useLibraries()` because the `PLMLibTorchWrapper` pod has a dependency on the `LibTorch-Lite` pod which contains static libraries. ```kotlin cocoapods { ... pod("PLMLibTorchWrapper") { version = "" headers = "LibTorchWrapper.h" } useLibraries() } ``` If you use Kotlin version < 1.8.0 the `headers` property is not available. Instead, you have to add the following to your `shared/build.gradle.kts` (see [this issue](https://youtrack.jetbrains.com/issue/KT-44155/Cocoapods-doesnt-support-pods-without-module-map-file-inside) for more information): ```kotlin tasks.named("generateDefPLMLibTorchWrapper").configure { doLast { outputFile.writeText(""" language = Objective-C headers = LibTorchWrapper.h """.trimIndent()) } } ``` Additional steps: - make sure bitcode is disabled in your iOS XCode project - make sure that your iOS app's Podfile does **not** include `use_frameworks!` - your `framework` block should probably declare `isStatic = true` ## Usage First, [export your PyTorch model for the lite interpreter](https://pytorch.org/tutorials/recipes/mobile_interpreter.html). Manage in your application how the exported model file is stored on device, e.g. bundled with your app, downloaded from a server during app initialization or something else. Then you can initialize the `TorchModule` with the path to the model file. ```kotlin import de.voize.pytorch_lite_multiplatform.TorchModule val module = TorchModule(path = "") ``` Once you initialized the model you are ready to run inference. Just like in the Android API of PyTorch Lite, you can use `IValue` and `Tensor` to pass input data into your model and to process the model output. To manage the memory allocated for your tensors you need to use `plmScoped` to specify up to which point you need to keep the memory allocated. ```kotlin import de.voize.pytorch_lite_multiplatform.* plmScoped { val inputTensor = Tensor.fromBlob( data = floatArrayOf(...), shape = longArrayOf(...), scope = this ) val inputIValue = IValue.fromTensor(inputTensor) val output = module.forward(inputIValue) // you could also use // module.runMethod("forward", inputIValue) val outputTensor = output.toTensor() val outputData = outputTensor.getDataAsFloatArray() ... } ``` `IValue`s are very flexible to construct the input you need for your model, e.g. tensors, scalars, flags, dicts, tuples etc. Refer to the [`IValue`]() interface for all available options and browse [PyTorch's Android Demo](https://github.com/pytorch/android-demo-app) for examples on inferences using `IValue`. ## Memory Management To make management of resources allocated for your inference across Android and iOS simpler we introduced the `PLMScope` and the `plmScoped` util. On Android, the JVM garbage collection and PyTorch Lite manage the allocated memory nicely so `plmScoped` is a noop. But on iOS, memory is allocated in Kotlin and exchanged with native Objective-C code and vice-versa without automatic deallocation of resources. This is where `plmScoped` comes in and frees the memory allocated for your inference. So it is important that you properly define the scope in which resources need to stay allocated to avoid memory leaks or memory being lost that is needed later. ## Running tests ### iOS To run the tests on iOS, execute the `iosSimulatorX64Test` gradle task: ``` ./gradlew iosSimulatorX64Test ``` This will automatically call `build_dummy_model.py` to create the dummy torchscript module for testing, copy it into the simulator files directory and execute the tests. Make sure to select a Python environment where the torch dependency is available.

近期下载者

相关文件


收藏者