Git Product home page Git Product logo

pytorch-flutter-ffi-example's Introduction

PyTorch / LibTorch in Flutter through C++ FFI

FFI is faster the method channels, and I couldn't find a good example for this so I've made one.

This example app is working as it is on Android and iOS. No further dependency needs to be installed. Tested on Flutter 3.3.8

Steps I took when making this example app:

  1. Create FFI plugin from this guide
  2. Android: copied native_pytorch/libtorch-android and setup native_pytorch/android/CMakeLists.txt (If anyone wondering, I got the .so and header files from the original .aar pytorch package, just unzipep and taken from the jni/ and headers/ )
  3. iOS: setup native_pytorch/ios/native_pytorch.podspec (note that s.static_framework = true is mandatory)
  4. iOS: drag native_pytorch/ios/Classes/native_pytorch.cpp to xCode to projects files (on the left side under the runner). Otherwise it say symbols not found

Some benchmarks - FFI vs Current version of the flutter_pytorch_mobile package

The inference is significantly faster through FFI on Android. Moreover, with bigger input tensors, I was getting an OutOfMemoryError before, and now with the FFI it's totally fine.

The current version of the package:
Input shape (1, 3, 350, 350) - inference 886ms
Input shape (1, 3, 700, 700) -  inference 3050ms
Input shape (1, 3, 2000, 2000) -  inference `OutOfMemoryError`
Input shape (1, 3, 8750, 8750) -  inference `OutOfMemoryError`

FFI:
Input shape (1, 3, 350, 350) - inference 250ms
Input shape (1, 3, 700, 700) -  inference 314ms
Input shape (1, 3, 2000, 2000) -  inference 580ms
Input shape (1, 3, 8750, 8750) -  inference 12114ms

(I measured the time to make the model inference from Dart List, and get the output to Dart List, so the conversions to/from C++ data structures are already included in the measures. Testing ML model was LDC)

Linux Support

  1. Ensure you have installed PyTorch
  2.  import torch
     print(torch.utils.cmake_prefix_path)
  3. Copy the output and open native_pytorch/linux/CMakeLists.txt
  4. Replace list(APPEND CMAKE_PREFIX_PATH "/home/pc/anaconda3/envs/torch-gpu/lib/python3.8/site-packages/torch/share/cmake") with list(APPEND CMAKE_PREFIX_PATH "your_output")
  5. Run scripts/init_linux.sh

pytorch-flutter-ffi-example's People

Contributors

dvagala avatar kernelinterrupt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.