pytorch_dart 0.2.0
pytorch_dart: ^0.2.0 copied to clipboard
A dart wrapper for Libtorch,provides some fundametal bindings.
Pytorch_Dart #
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.
This package is experimental and APIs may change in the future.
| Platform | Status | Prebuilt binaries |
|---|---|---|
| Windows | ✅ | x64(without CUDA) |
| Android | ✅ | arm64-v8a armeabi-v7a x86_64 x86 |
| Linux | ✅ | x64(without CUDA) |
| iOS | ❌ | coming soon |
| MacOS | ❌ | coming soon |
Theoretically you can run pytorch_dart on MacOS by simply replace /libtorch-linux/libtorch with libtorch for MacOS.
Attention #
Pytorch_dart will be refactored based on the C wrapper of TorchSharp since v0.1.1,the last version using the C wrapper of gotorch is 0.1.0
Getting Started #
Add pytorch_dart to your pubspec.yaml #
pytorch_dart:^0.1.0
Setup #
dart run pytorch_dart:setup --platform <your_platform>
<your_platform> only support linux , android and windows now.
For windows,if you use debug version of libtorch,the program throw some exceptions when you build in release mode.
In this situation,you have to install the release version of libtorch.
Here we install the debug version by default.If you want to get release version of libtorch,run:
dart run pytorch_dart:setup --platform windows release
Enjoy it! #
import 'package:pytorch_dart/pytorch_dart.dart' as torch;
For Android developers #
libtorch for android needs a specific version of NDK,so install NDK and choose version 21.4.7075529
Then add a row in android/local.properties in your project
ndk.dir=<path_to_your_ndk>/21.4.7075529
After adding a row,your local.properties should look like this:
flutter.sdk=/home/pc/flutter
sdk.dir=/home/pc/Android/Sdk
flutter.buildMode=debug
ndk.dir=/home/pc/Android/Sdk/ndk/21.4.7075529
Also,'torch.load()' and 'torch.save()' are not available on Android.
For windows developers #
Troubleshooting
Launching lib\main.dart on Windows in debug mode...
√ Built build\windows\x64\runner\Debug\example.exe.
Error waiting for a debug connection: The log reader stopped unexpectedly, or never started.
Error launching application on Windows.
Solutions:
- Download libtorch from here(Download
libtorch-win-shared-with-deps-2.2.2+cpu.zipif you want to run in release mode,and downloadlibtorch-win-shared-with-deps-debug-2.2.2+cpu.zipif you want to run in debug mode.) - Unzip it
- copy all the files from
libtorch\lib\tobuild\windows\x64\runner\Debug\(debug mode) orbuild\windows\x64\runner\Release(release mode) - This problem is about copying library to the correct place, maybe I'll find a better solution later.
Usage #
Brief Introduction #
- It include some basic functions in torch now.
- Support for torch.nn is coming soon.
- Almost all function usages remain consistent with PyTorch.
- Broadcasting also works for pytorch_dart.
- Example
var d=torch.eye(3,2);
print(d);
Result:
flutter:
1 0
0 1
0 0
[ CPUFloatType{3,2} ]
Operator overloading #
Attention:Dart has no magic functions(like _radd_ in python).Therefore, in binary operators, tensor can only be on the left side.
Example
import 'package:pytorch_dart/pytorch_dart.dart' as torch;
...
var c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]);
var d=c+10;// no errors
var e=10+c;//cause errors
Other binary operators (-,*,/)are just like +
For operator [] ,you can use it just like in Pytorch.
However,in current version,slicing is not supported.Therefore,you cant't use [a:b] to select sub tensor.
Example
import 'package:pytorch_dart/pytorch_dart.dart' as torch;
...
var c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]);
print(c[0][0]);
Result
flutter: 1
[ CPUDoubleType{} ]
torch #
-
torch.tensor()is not supported in pytorch_dart,usetorch.IntTensor(),torch.FloatTensorortorch.DoubleTensorto create tensors. -
Functions avaliable now:
torch.empty()torch.eye()torch.ones()torch.IntTensor(List<int> list)torch.FloatTensor(List<double> list)torch.DoubleTensor(List<double> list)torch.arange(double start, double end, double step,{bool requiresGrad = false})torch.linspace(double start, double end, int steps,{bool requiresGrad = false})torch.logspace(double start, double end, int steps, double base,{bool requiresGrad = false})torch.equal(Tensor a,Tensor b)torch.add(Tensor a, tensor b,{double alpha=1})torch.sub(Tensor a, tensor b,{double alpha=1})torch.mul(Tensor a, tensor b)torch.div(Tensor a, tensor b)torch.add_(Tensor a, tensor b,{double alpha=1})torch.sub_(Tensor a, tensor b,{double alpha=1})torch.mul_(Tensor a, tensor b)torch.div_(Tensor a, tensor b)torch.sum(Tensor a)torch.mm(Tensor a, Tensor b)torch.transpose(Tensor a,int dim0,int dim1)torch.permute(Tensor a,List <int> permute_list)torch.save(Tensor a,String path)torch.load(String path) -
Almost all function usages remain consistent with PyTorch.
-
Some in-place operation are supported,such as
torch.add_() -
Example
import 'package:pytorch_dart/pytorch_dart.dart' as torch; ... var c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]); var d=torch.add(10,c) print(d)Result:
flutter: 11 12 13 14 15 16 [ CPUDoubleType{2,3} ]
torch.tensor #
-
Functions avaliable now:
.dim().dtype().shape().size().detach().add_().sub_().mul_().div_().toList() -
.dtype()is different from its implementation in Pytorch.In Pytorch,
.dtypereturns an object represents the data type of a tensorBut in pytorch_dart,
.dtype()returns a number represents the data type of a tensor.(maybe I will rewrite it later)Example
import 'package:pytorch_dart/pytorch_dart.dart' as torch; ... var c=torch.DoubleTensor([[1.0,2.0,3.0],[4.0,5.0,6.0]]); print(c.dtype())Result
flutter: 77representstorch.float64.All the corresponding relations are in
lib/src/constants.dart -
Other function usages remain consistent with PyTorch.
Roadmap #
- Add support for iOS and MacOS.
- Add support for other functions,such as
torch.nn
Acknowledgement #
This project uses pytorch-flutter-FFI-example and gotorch