{"id":15008424,"url":"https://github.com/playboy-player/pytorch_dart","last_synced_at":"2025-04-09T16:04:53.614Z","repository":{"id":227767128,"uuid":"772347172","full_name":"Playboy-Player/pytorch_dart","owner":"Playboy-Player","description":"Pytorch_Dart is a dart wrapper for Libtorch,striving to provide an experience identical to PyTorch. You can use it as an alternative to Numpy in your Dart/Flutter projects.","archived":false,"fork":false,"pushed_at":"2024-12-24T16:25:14.000Z","size":127048,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T16:04:39.334Z","etag":null,"topics":["android","computer-vision","cpp","cross-platform","dart","dart-package","dartlang","deep-learning","deep-neural-networks","deeplearning","flutter","flutter-package","libtorch","linux","machine-learning","numpy","pytorch","tensor","torch","windows"],"latest_commit_sha":null,"homepage":"","language":"C++","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/Playboy-Player.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2024-03-15T02:29:37.000Z","updated_at":"2025-04-05T09:42:36.000Z","dependencies_parsed_at":"2024-06-09T21:56:25.010Z","dependency_job_id":"7c5e32b7-4db8-4ef2-b058-0b9d40af436f","html_url":"https://github.com/Playboy-Player/pytorch_dart","commit_stats":null,"previous_names":["playboy-player/pytorch_dart"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Playboy-Player%2Fpytorch_dart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Playboy-Player%2Fpytorch_dart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Playboy-Player%2Fpytorch_dart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Playboy-Player%2Fpytorch_dart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Playboy-Player","download_url":"https://codeload.github.com/Playboy-Player/pytorch_dart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065289,"owners_count":21041871,"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":["android","computer-vision","cpp","cross-platform","dart","dart-package","dartlang","deep-learning","deep-neural-networks","deeplearning","flutter","flutter-package","libtorch","linux","machine-learning","numpy","pytorch","tensor","torch","windows"],"created_at":"2024-09-24T19:18:41.827Z","updated_at":"2025-04-09T16:04:53.588Z","avatar_url":"https://github.com/Playboy-Player.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pytorch_Dart\n\nPytorch_Dart is a Dart wrapper for Libtorch, designed to provide a seamless experience akin to [PyTorch](https://github.com/pytorch/pytorch).\n\nIt serves as an alternative to Pytorch for Dart/Flutter projects.\n\n**This package is experimental and APIs may change in the future**.\n\n| Platform | Status | Prebuilt binaries                               |\n| -------- | ------ | ----------------------------------------------- |\n| Windows  | ✅     | x64(without CUDA)                               |\n| Android  | ✅     | arm64-v8a\u003cbr /\u003earmeabi-v7a\u003cbr /\u003ex86_64\u003cbr /\u003ex86 |\n| Linux    | ✅     | x64(without CUDA)                               |\n| iOS      | ❌     | coming soon                                     |\n| MacOS    | ❌     | coming soon                                     |\n\nNote: To run Pytorch_Dart on MacOS, replace `/libtorch-linux/libtorch` with libtorch for MacOS.\n\n- [Pytorch_Dart](#pytorch_dart)\n  - [Getting Started](#getting-started)\n    - [Add pytorch_dart to your pubspec.yaml](#add-pytorch_dart-to-your-pubspec.yaml)\n    - [Setup](#setup)\n    - [Enjoy it!](#enjoy-it!)\n      - [For Android developers](#for-android-developers)\n      - [Troubleshooting](#troubleshooting)\n         - [Windows](#windows)\n  - [Usage](#usage)\n    - [Brief Introduction](#brief-introduction)\n    - [Operator overloading](#operator-overloading)\n    - [Model Inferencing](#model-inferencing)\n      - [Example](#example)\n  - [Functions/APIs](#functions/apis)\n    - [torch](#torch)\n      - [Supported Functions](#supported-functions)\n    - [torch.tensor](#torch.tensor)\n    - [torch.jit](#torch.jit)\n  - [Roadmap](#roadmap)\n  - [Acknowledgement](#acknowledgement)\n\n## Getting Started\n\n### Add pytorch_dart to your pubspec.yaml\n\nTo include Pytorch_Dart in your Dart/Flutter project, add the following to your `pubspec.yaml` and then save `pubspec.yaml` :\n\n```dart\n    pytorch_dart:^0.2.3\n```\n\n### Setup\n\nRun the setup command below:\n\n```powershell\nflutter pub get\ndart run pytorch_dart:setup --platform \u003cyour_platform\u003e\n```\n\n`\u003cyour_platform\u003e` only support `linux` , `android` and `windows` now.(iOS coming soon)\n\nFor windows developers,if you use debug version of libtorch,the program works well in debug mode but throw some exceptions when you build in release mode and vice versa.\n\nIf you need to build in release mode,you have to install the release version of libtorch.\n\nThe setup process will install the debug version by default.If you want to get release version of libtorch,run:\n\n```\ndart run pytorch_dart:setup --platform windows release\n```\n\n### Enjoy it!\n\nNow you can import Pytorch_Dart in your Dart/Flutter project:\n\n```dart\n    import 'package:pytorch_dart/pytorch_dart.dart' as torch;\n\n```\n\n#### For Android developers\n\nLibtorch for Android requires a specific version of the NDK. Install NDK version 21.4.7075529 as instructed [here](https://developer.android.com/studio/projects/install-ndk?hl=zh-cn).\n\nAdd the NDK path to your project's `android/local.properties`:\n\n```dart\nndk.dir=\u003cpath_to_your_ndk\u003e/21.4.7075529\n```\n\nEnsure that your `local.properties` file looks similar to:\n\n```gradle\nflutter.sdk=/home/pc/flutter\nsdk.dir=/home/pc/Android/Sdk\nflutter.buildMode=debug\nndk.dir=/home/pc/Android/Sdk/ndk/21.4.7075529\n```\n\nAlso,`torch.load()` and `torch.save()` are not available on Android.\n\n#### Troubleshooting\n\n##### Windows\n\n```\nLaunching lib\\main.dart on Windows in debug mode...\n√  Built build\\windows\\x64\\runner\\Debug\\example.exe.\nError waiting for a debug connection: The log reader stopped unexpectedly, or never started.\nError launching application on Windows.\n```\n\nSolutions:\n\n1. Download libtorch from [here](https://download.pytorch.org/libtorch/cpu/)(Download `libtorch-win-shared-with-deps-2.2.2+cpu.zip` if you want to run in release mode,and download `libtorch-win-shared-with-deps-debug-2.2.2+cpu.zip` if you want to run in debug mode.)\n2. Unzip it\n3. copy all the files from `libtorch\\lib\\` to `build\\windows\\x64\\runner\\Debug\\` (debug mode) or `build\\windows\\x64\\runner\\Release`(release mode)\n\n## Usage\n\n### Brief Introduction\n\n1. It include some basic functions in [torch](https://pytorch.org/docs/stable/torch.html) now.\n2. Support for inferencing TorchScript models.\n3. **Almost all function usages remain consistent with PyTorch.**\n4. **Broadcasting also works for pytorch_dart.**\n5. Support for [torch.nn](https://pytorch.org/docs/stable/nn.html) is coming soon.\n6. Example\n\n```dart\nvar d=torch.eye(3,2);\nprint(d);\n```\n\nResult:\n\n```\nflutter:\n 1  0\n 0  1\n 0  0\n[ CPUFloatType{3,2} ]\n```\n\n### Operator overloading\n\nAttention:Dart has no magic functions(like `_radd_` in python).Therefore, among all binary operators that involve a Tensor and another data type, the Tensor must be the left operand.And there is no such restriction when both operands are Tensors.\n\nExample\n\n```dart\nimport 'package:pytorch_dart/pytorch_dart.dart' as torch;\n...\n\nvar c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]);\nvar d=c+10;// no exception\nvar e=10+c;//throw exception\n```\n\nOther binary operators (`-`,`*`,`/`)are just like `+`\n\nFor operator `[]` ,you can use it just like in Pytorch.\n\nHowever,in current version,slicing is not supported.Therefore,you cant't use `[a:b]` to select sub tensor.\n\nExample\n\n```dart\nimport 'package:pytorch_dart/pytorch_dart.dart' as torch;\n...\n\nvar c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]);\nprint(c[0][0]);\n```\n\nResult\n\n```dart\nflutter: 1\n[ CPUDoubleType{} ]\n```\n\n### Model Inferencing\n\nAbout how to get a TorchScript Model,see [here](https://h-huang.github.io/tutorials/recipes/torchscript_inference.html).\n\nIn Pytorch,we use `torch.jit.load()` to load TorchScript Models and `module.forward()` to inference.\n\nIn Pytorch_Dart,we have equivalent functions:`torch.jit_load()` and `module.forward()`.They have some small differnece with their Pytorch version.\n\n`torch.jit_load()` is just like `torch.jit.load()` in Pytorch,but it is an asynchronous function because we use `rootBundle`.\n\nTo load a model,see example below:\n\n```\n\ntorch.JITModule? module;\nvoid _loadModel() async{\n  module=await torch.jit_load('assets/traced_resnet_model.pt');\n}\n```\n\nHowever,`forward()` has some differences with the original Pytorch version.\n\nIn Dart,it receives  `List \u003cDynamic\u003e`  which means the input of the function `forward()` can be `List\u003cTensor\u003e`,`List\u003cScalar\u003e` or etc.\n\nIf the input of your model is a single tensor:\n\nIn Python, the following code is written:\n\n```\noutputTensor = module.forward(inputTensor)\n```\n\nBut in Dart,you have to put `inputTensor` into a list:\n\n```\nvar outputTensor = module!.forward([inputTensor]);   //! is a null-check opeator\n```\n\nAttention:Half tensors are not supported yet.\n\n#### Example\n\nWe provide an image classigfication example in [/example](https://github.com/Playboy-Player/pytorch_dart/tree/main/example).\n\n![1721127375812](image/README/1721127375812.png)\n\nTo run the example,see [pytorch_dart_standalone_examples](https://github.com/KernelInterrupt/pytorch_dart_standalone_examples) or run the code below:\n\n```shell\ngit clone https://github.com/Playboy-Player/pytorch_dart\ncd pytorch_dart\ngit submodule init\ngit submodule update --remote\ndart run pytorch_dart:setup --platform \u003cyour_platform\u003e\ncd example\nflutter run --debug //or \"flutter run --release\"\n\n```\n\n## Functions/APIs\n\nJust like Pytorch,functions in Pytorch_Dart are divided into multiple parts.\n\nIn current version,APIs are dividied into 3 parts:\n\n* **torch**\n* **torch.tensor**\n* **torch.jit**\n\n### torch\n\n#### Supported Functions\n\n1. `torch.tensor()` is not supported in pytorch_dart,use `torch.IntTensor()`,`torch.FloatTensor()` or `torch.DoubleTensor()` to create tensors.\n2. Functions avaliable now:\n   Attention: parameters wrapped by `{}` are optional parameters.\n\n   ```\n\n   torch.ones(List\u003cint\u003e size,{bool requiresGrad = false, int dtype = float32, Device? device_used})\n   torch.full(List\u003cint\u003e size, num values,{int dtype = float32, bool requiresGrad = false, Device? device_used}))\n   torch.eye(int n, int m,{bool requiresGrad = false, int dtype = float32, Device? device_used})\n   torch.IntTensor(List\u003cint\u003e list)\n   torch.FloatTensor(List\u003cdouble\u003e list)\n   torch.DoubleTensor(List\u003cdouble\u003e list)\n   torch.arange(double start, double end, double step,{bool requiresGrad = false})\n   torch.linspace(double start, double end, int steps,{bool requiresGrad = false})\n   torch.logspace(double start, double end, int steps, double base,{bool requiresGrad = false})\n   torch.equal(Tensor a,Tensor b)\n   torch.add(Tensor a, tensor b,{double alpha=1})\n   torch.sub(Tensor a, tensor b,{double alpha=1})\n   torch.mul(Tensor a, tensor b)\n   torch.div(Tensor a, tensor b)\n   torch.add_(Tensor a, tensor b,{double alpha=1})\n   torch.sub_(Tensor a, tensor b,{double alpha=1})\n   torch.mul_(Tensor a, tensor b)\n   torch.div_(Tensor a, tensor b)\n   torch.sum(Tensor a)\n   torch.mm(Tensor a, Tensor b)\n   torch.transpose(Tensor a,int dim0,int dim1)\n   torch.permute(Tensor a,List \u003cint\u003e permute_list)\n   torch.save(Tensor a,String path)\n   torch.load(String path)\n   torch.relu()\n   torch.leaky_relu()\n   torch.tanh()\n   torch.sigmoid()\n   torch.flatten(Tensor a, int startDim, int endDim)\n   torch.unsqueeze(Tensor tensor, int dim)\n   torch.clone(Tensor tensor)\n   torch.topk(Tensor a, int k,{int dim = -1, bool largest = true, bool sorted = true})\n   torch.allClose(Tensor left, Tensor right,{double rtol = 1e-08, double atol = 1e-05, bool equal_nan = false})\n   torch.empty(List\u003cint\u003e size,{bool requiresGrad = false, int dtype = float32, Device? device_used})\n   torch.ones(List\u003cint\u003e size,{bool requiresGrad = false, int dtype = float32, Device? device_used})\n   torch.full(List\u003cint\u003e size, num values,{int dtype = float32, bool requiresGrad = false, Device? device_used}))\n   torch.eye(int n, int m,{bool requiresGrad = false, int dtype = float32, Device? device_used})\n   ```\n3. Almost all function usages remain consistent with PyTorch.\n4. Some in-place operation are supported,such as `torch.add_()`\n5. Example Usage\n\n   ```dart\n   import 'package:pytorch_dart/pytorch_dart.dart' as torch;\n   ...\n\n   var c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]);\n   var d=torch.add(10,c)\n   print(d)\n   ```\n\n   Result:\n\n   ```dart\n   flutter:\n    11  12  13\n    14  15  16\n   [ CPUDoubleType{2,3} ]\n   ```\n\n### torch.tensor\n\n1. ##### `torch.tensor` Methods\n\n\n   * `.dim()`\n   * `.dtype()`\n   * `.shape()`\n   * `.size()`\n   * `.detach()`\n   * `.add_()`\n   * `.sub_()`\n   * `.mul_()`\n   * `.div_()`\n   * `.toList()`\n   * `.unsqueeze(int dim)`\n   * `.clone()`\n   * `.relu()`\n   * `.leaky_relu()`\n   * `.sigmoid()`\n   * `.tanh()`\n   * `.flatten()`\n   * `.equal(Tensor other)`\n   * `.sum()`\n   * `.mm(Tensor other)`\n   * `.view(List \u003cint\u003e size)`\n\n   **Note:** The `.dtype()` method in Pytorch_Dart differs from PyTorch. In PyTorch, `.dtype` returns an object representing the tensor's data type. In Pytorch_Dart, `.dtype()` returns a numerical representation of the data type. This may be updated in future versions.\n2. Example\n\n   ```dart\n   import 'package:pytorch_dart/pytorch_dart.dart' as torch;\n   ...\n\n   var c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]);\n   print(c.dtype())\n   ```\n\n   Result\n\n   ```dart\n   flutter: 7\n   ```\n\n   `7` represents `torch.float64.`\n\n   All the corresponding relations are in `lib/src/constants.dart`\n3. Other function usages remain consistent with PyTorch.\n\n### torch.jit\n\nSee [Model Inferencing](#Model-Inferencing).\n\n## Roadmap\n\n1. Add support for iOS and MacOS.\n2. Add support for other functions,such as `torch.nn`\n\n## Acknowledgement\n\nThis project leverages contributions from [pytorch-flutter-FFI-example](https://github.com/dvagala/pytorch-flutter-FFI-example) ,[gotorch](https://github.com/wangkuiyi/gotorch) and [TorchSharp](https://github.com/dotnet/TorchSharp)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplayboy-player%2Fpytorch_dart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplayboy-player%2Fpytorch_dart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplayboy-player%2Fpytorch_dart/lists"}