{"id":15066242,"url":"https://github.com/voize-gmbh/pytorch-lite-multiplatform","last_synced_at":"2025-04-10T13:42:51.209Z","repository":{"id":38300835,"uuid":"474316962","full_name":"voize-gmbh/pytorch-lite-multiplatform","owner":"voize-gmbh","description":"A Kotlin multi-platform wrapper around the PyTorch Lite libraries on Android and iOS.","archived":false,"fork":false,"pushed_at":"2024-07-23T19:45:50.000Z","size":400,"stargazers_count":41,"open_issues_count":3,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T12:21:45.814Z","etag":null,"topics":["kotlin","kotlin-multiplatform-mobile","pytorch","pytorch-lite","pytorch-mobile"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/voize-gmbh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-26T10:42:37.000Z","updated_at":"2025-03-18T21:03:52.000Z","dependencies_parsed_at":"2024-06-28T00:50:08.563Z","dependency_job_id":"ee00a6fc-a3a0-40a7-aae5-6907bbe46db2","html_url":"https://github.com/voize-gmbh/pytorch-lite-multiplatform","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voize-gmbh%2Fpytorch-lite-multiplatform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voize-gmbh%2Fpytorch-lite-multiplatform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voize-gmbh%2Fpytorch-lite-multiplatform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voize-gmbh%2Fpytorch-lite-multiplatform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/voize-gmbh","download_url":"https://codeload.github.com/voize-gmbh/pytorch-lite-multiplatform/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248226264,"owners_count":21068170,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kotlin","kotlin-multiplatform-mobile","pytorch","pytorch-lite","pytorch-mobile"],"created_at":"2024-09-25T01:04:11.165Z","updated_at":"2025-04-10T13:42:51.191Z","avatar_url":"https://github.com/voize-gmbh.png","language":"Kotlin","funding_links":[],"categories":["人工智能"],"sub_categories":["机器学习"],"readme":"# pytorch-lite-multiplatform\n\n![CI](https://github.com/voize-gmbh/pytorch-lite-multiplatform/actions/workflows/test.yml/badge.svg)\n![Maven Central](https://img.shields.io/maven-central/v/de.voize/pytorch-lite-multiplatform)\n![Cocoapods](https://img.shields.io/cocoapods/v/PLMLibTorchWrapper)\n\nA Kotlin multi-platform wrapper around the PyTorch Lite libraries on [Android](https://pytorch.org/mobile/android/) and [iOS](https://pytorch.org/mobile/ios/).\nYou 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. A high level introduction is available [in our blog post](https://tech.voize.de/posts/pytorch-lite-multiplatform).\n\n## Installation\n\nAdd the following to your `shared/build.gradle.kts` as a `commonMain` dependency.\n\n```kotlin\nimplementation(\"de.voize:pytorch-lite-multiplatform:\u003cversion\u003e\")\n```\n\nAdd 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.\n\n```kotlin\ncocoapods {\n    ...\n\n    pod(\"PLMLibTorchWrapper\") {\n        version = \"\u003cversion\u003e\"\n        headers = \"LibTorchWrapper.h\"\n    }\n\n    useLibraries()\n}\n```\n\nIf you use Kotlin version \u003c 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):\n\n```kotlin\ntasks.named\u003corg.jetbrains.kotlin.gradle.tasks.DefFileTask\u003e(\"generateDefPLMLibTorchWrapper\").configure {\n    doLast {\n        outputFile.writeText(\"\"\"\n            language = Objective-C\n            headers = LibTorchWrapper.h\n        \"\"\".trimIndent())\n    }\n}\n```\n\nAdditional steps:\n\n- make sure bitcode is disabled in your iOS XCode project\n- make sure that your iOS app's Podfile does **not** include `use_frameworks!`\n- your `framework` block should probably declare `isStatic = true`\n\n## Usage\n\nFirst, [export your PyTorch model for the lite interpreter](https://pytorch.org/tutorials/recipes/mobile_interpreter.html).\nManage 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.\nThen you can initialize the `TorchModule` with the path to the model file.\n\n```kotlin\nimport de.voize.pytorch_lite_multiplatform.TorchModule\n\nval module = TorchModule(path = \"\u003cpath/to/model.ptl\u003e\")\n```\n\nOnce you initialized the model you are ready to run inference.\n\nJust 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.\n\n```kotlin\nimport de.voize.pytorch_lite_multiplatform.*\n\nplmScoped {\n    val inputTensor = Tensor.fromBlob(\n        data = floatArrayOf(...),\n        shape = longArrayOf(...),\n        scope = this\n    )\n\n    val inputIValue = IValue.fromTensor(inputTensor)\n\n    val output = module.forward(inputIValue)\n    // you could also use\n    // module.runMethod(\"forward\", inputIValue)\n\n    val outputTensor = output.toTensor()\n    val outputData = outputTensor.getDataAsFloatArray()\n\n    ...\n}\n```\n\n`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`.\n\n## Memory Management\n\nTo 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.\n\n## Running tests\n\n### iOS\n\nTo run the tests on iOS, execute the `iosSimulatorX64Test` gradle task:\n\n```\n./gradlew iosSimulatorX64Test\n```\n\nThis 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.\nMake sure to select a Python environment where the torch dependency is available.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoize-gmbh%2Fpytorch-lite-multiplatform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvoize-gmbh%2Fpytorch-lite-multiplatform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoize-gmbh%2Fpytorch-lite-multiplatform/lists"}