PyTorch Mobile - David Reiss

PyTorch · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

PyTorch Mobile is a version of PyTorch built for Android and iOS, supporting an end-to-end workflow from Python to mobile devices, with features like Torch Script and LibTorch C++ API. The framework enables efficient on-device ML, allowing for lower latency and privacy-preserving techniques like federated learning.

Full Transcript

break was canceled in exchange you get me hi everyone I'm David and I am here to talk about the PI torch mobile experimental release so what is PI torch mobile well it's pi torch for mobile I know that sounds a little bit silly but a lot of the questions we get when we talk about pi torch mobile are did you build a brand new framework are you going to have to or are we gonna have to export our model to a new format are we going to have to port a bunch of operators and the answer to all of those is no so no new framework no model conversion no operator porting this is just PI torch built for Android and iOS and we've made a lot of internal changes to make it work better on mobile but it's still the PI torch that you know and love and we believe that having a unified framework is the best way to enable you all to bring your work from research to production now one decision we did make is not to put a full Python runtime into our mobile build that would have been a little bit crazy so that means that eager mode execution is out of the question on mobile but what we do support is any torch script model so you learned about torch script earlier it's a large and growing subset of Python that can run most Pi torch models and that's the technology that PI torch mobile is built on so if your model is compatible with Lib torch using either tracing or scripting then it can run on mobile and that means that we support all of the control flow constructs and data types that are supported in pipe in torch script so oops yes functions yes tuples yes even name tuples also yes so that all sounds great let's talk about how to use it in case you weren't paying attention to Michael's talk this morning this is how you write a model with torch script and if we zoom in on this section down here it's really just these two lines of code that you have to write and that's it that's going to save your model to a file you can hand that off to a mobile engineer now mobile engineers you want to know how to run this so let's talk about that first you need to add a dependency on PI torch that's a one line configuration on both Android and iOS and that's gonna bring in a pre-built binary that's configured for development now you need to write your code to run the model so let's look at that on Android we have a Java wrapper for Lib torch and that means that you won't have to use the Android NDK you don't even need to install the NDK and I know the NDK is a lot easier to use than it used to be but it can still be intimidating for app developers who are used to programming in Java so we've taken care of all of that for you I just write Java code and this is what it looks like you have your feature vector you wrap it in a tensor you pass that to your model and you extract your data from the output so pretty simple alright now on iOS the API that we expose is the Lib torch C++ API and this gives you the maximum of flexibility and functionality but it means that you're going to have to write a small amount of objective C++ code to bridge between your app which is probably written in objective-c or Swift and Lib torch which is written in C++ but we think this isn't too significant of an overhead because it uses the same tool chain and the same build process as the rest of your iOS app so I'll go through the code if you've used Lib torch before it should look very familiar again feature vector wrap it in a tensor run it through your model and collect your output so it's basically the same four lines of code on both platforms to run your model and I want to show you what it looks like for real so we have some demo apps that we're going to be showing in the demo area this evening there's one on Android one on iOS the first model in them is a text model so you type in some text and it tries to predict which section of reddit that text came from the second model is a vision model that's trained on imagenet so it takes the live image feed from your phone camera and it tries to classify what it thinks it's looking at based on the 1000 class is an image net and in terms of model architecture the text model is based on a multi class classifier using an L TM that we've been testing in one of our production apps at Facebook the image model is based on a quantized mobile net B to Architecture and this really emphasizes the benefit of having a unified framework you can design your model train your model test your model with quantization on the server side and then you can use that same model on mobile because we have the exact same quantization framework same interface same semantics so though there are and there are other PI torch features that will be getting the same treatment so for example you heard about the deep type conversions this morning type promotion this morning and that works as well on mobile and there's also a name tensor which is going to be coming to torch script soon and when that's in torch script it will also be available on mobile so the code for both of the demo apps is available on github right now you can check it out you can use it as a starting point for your own integrations and just as a data point the Android app is a hundred percent Java the iOS app is about 85% Swift so just to recap what we're releasing today full torch script support I want to emphasize that it is the real torch script interpreter that's running on the mobile device there are pre-built binary releases already out in J Center and cocoa pods one line of configuration to install the Java bindings are bundled with the Android version and these releases contain all of the forward operators that have CPU implementations so it should be able to run any of your models because it's basically everything in the kitchen sink type of release now some of these models have some of these operators have already been optimized for mobile the one we paid the most attention to is convolution and the float version has some optimizations that we brought over from cafe to go which is our existing production deep learning framework for mobile and the quantized versions are based on Q&N pack which is a library of highly optimized quantized numerical kernels that we released about a year ago now not all the operators have gotten this optimization treatment so that brings us to what's coming up first and foremost we want to make things faster we think we can squeeze a little more water from the stone of these high-value operators but also find some of the operators that haven't got enough attention and make sure they're running up to speed as well we want to make the release smaller so that the current sort of kitchen sink build weighs in at about five megabytes per architecture and the biggest tool that we have for making that smaller is giving you all the ability to build a customized release of Pi torch mobile that is tailored for your own operators so for example if you're releasing an app that has a bunch of vision models you don't want to be bundling a bunch of code that has text operators in it that's just a waste we're experimenting with a wrapper for Objective C and Swift that we think can lower the barrier to entry for iOS developers even further and we've talked about doing a Kotlin wrapper but that I think is gonna be movin more by community demand because the Java wrapper actually does work pretty well either from Colin there are some more long-term projects as well when I add GPU support so if you've ever programmed a mobile GPU you probably know it's not quite the same it's just running some CUDA kernels but most mobile chips do have a powerful GPU and we want to be able to take advantage of this to accelerate your models and then even even further down that spectrum is neural network accelerators which are starting to show up on some of the newer mobile SOC sees and they can provide even a greater level of performance but often what comes with that is an even greater level of challenge when you're trying to program program them and so if we can sort of hide some of that complexity for you and make it easier for you to run your models on on those parts of a chip that would be fantastic so that's a quick look at what we're planning in terms of what you can do today please visit the mobile page it's linked right from the top of pite org try out the code if you're feeling brave you can ship a production app with it this is an experimental release but we do expect it to work but more importantly tell us how you're using it you know come to the PI torch discussion forum under the mobile topic post about your app let us know what's going well for you and even more importantly what you would like to see improved because we have a lot more work to do and your feedback will help guide our designs and also our priorities thank you very much [Applause]

Original Description

Running ML on edge devices is growing in importance as applications continue to demand lower latency. It is also a foundational element for privacy-preserving techniques such as federated learning. To enable more efficient on-device ML, PyTorch 1.3 now supports an end-to-end workflow from Python to deployment on iOS and Android. Learn more about this early, experimental release.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from PyTorch · PyTorch · 29 of 60

1 What is PyTorch?
What is PyTorch?
PyTorch
2 PyTorch Tutorial: A Quick Preview
PyTorch Tutorial: A Quick Preview
PyTorch
3 PyTorch Summer Hackathon 2019
PyTorch Summer Hackathon 2019
PyTorch
4 Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
PyTorch
5 PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch
6 Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
PyTorch
7 Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
PyTorch
8 Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
PyTorch
9 Introduction to Machine Learning for Developers at F8 2019
Introduction to Machine Learning for Developers at F8 2019
PyTorch
10 Powered by PyTorch at F8 2019
Powered by PyTorch at F8 2019
PyTorch
11 Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
PyTorch
12 New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
PyTorch
13 PyTorch Developer Conference 2018: Recap
PyTorch Developer Conference 2018: Recap
PyTorch
14 PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch
15 PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch
16 PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch
17 PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch
18 PyTorch Developer Conference 2019 | Full Livestream
PyTorch Developer Conference 2019 | Full Livestream
PyTorch
19 PyTorch Developer Conference 2019: Recap
PyTorch Developer Conference 2019: Recap
PyTorch
20 PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch
21 What’s new in PyTorch 1.3 - Lin Qiao
What’s new in PyTorch 1.3 - Lin Qiao
PyTorch
22 PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch
23 Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
PyTorch
24 Quantization - Dmytro Dzhulgakov
Quantization - Dmytro Dzhulgakov
PyTorch
25 PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch
26 Apex -  Michael Carilli, NVIDIA
Apex - Michael Carilli, NVIDIA
PyTorch
27 Dataloader Design for PyTorch - Tongzhou Wang, MIT
Dataloader Design for PyTorch - Tongzhou Wang, MIT
PyTorch
28 Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
PyTorch
PyTorch Mobile - David Reiss
PyTorch Mobile - David Reiss
PyTorch
30 Model Interpretability with Captum - Narine Kokhilkyan
Model Interpretability with Captum - Narine Kokhilkyan
PyTorch
31 Detectron2 - Next Gen Object Detection Library - Yuxin Wu
Detectron2 - Next Gen Object Detection Library - Yuxin Wu
PyTorch
32 Speech Extensions to Fairseq - Dmytro Okhonko
Speech Extensions to Fairseq - Dmytro Okhonko
PyTorch
33 PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch
34 PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch
35 PyTorch in Robotics - Yisong Yue, Caltech
PyTorch in Robotics - Yisong Yue, Caltech
PyTorch
36 StanfordNLP - Yuhao Zhang, Stanford
StanfordNLP - Yuhao Zhang, Stanford
PyTorch
37 Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
PyTorch
38 Collaborative Natural Language Inference - Sasha Rush, Cornell
Collaborative Natural Language Inference - Sasha Rush, Cornell
PyTorch
39 Privacy Preserving AI - Andrew Trask, OpenMined
Privacy Preserving AI - Andrew Trask, OpenMined
PyTorch
40 CrypTen - Laurens van der Maaten
CrypTen - Laurens van der Maaten
PyTorch
41 PyTorch at Uber - Sidney Zhang, Uber
PyTorch at Uber - Sidney Zhang, Uber
PyTorch
42 PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch
43 PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch
44 PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch
45 PyTorch Developer Conference 2019 - Panel Discussion
PyTorch Developer Conference 2019 - Panel Discussion
PyTorch
46 Using deep learning and PyTorch to power next gen aircraft at Caltech
Using deep learning and PyTorch to power next gen aircraft at Caltech
PyTorch
47 Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
PyTorch
48 TorchScript and PyTorch JIT | Deep Dive
TorchScript and PyTorch JIT | Deep Dive
PyTorch
49 Announcing the PyTorch Global Summer Hackathon 2020
Announcing the PyTorch Global Summer Hackathon 2020
PyTorch
50 Opening Up the Black Box: Model Understanding with Captum and PyTorch
Opening Up the Black Box: Model Understanding with Captum and PyTorch
PyTorch
51 PyTorch Mobile Runtime for Android
PyTorch Mobile Runtime for Android
PyTorch
52 Torchvision in 5 minutes
Torchvision in 5 minutes
PyTorch
53 3D Deep Learning with PyTorch3D
3D Deep Learning with PyTorch3D
PyTorch
54 What is Torchtext?
What is Torchtext?
PyTorch
55 TorchAudio: A Quick Intro
TorchAudio: A Quick Intro
PyTorch
56 PyTorch Mobile Runtime for iOS
PyTorch Mobile Runtime for iOS
PyTorch
57 PySlowFast: Deep learning with Video
PySlowFast: Deep learning with Video
PyTorch
58 PyTorch Pruning | How it's Made by Michela Paganini
PyTorch Pruning | How it's Made by Michela Paganini
PyTorch
59 Measuring Fairness in Machine Learning Systems
Measuring Fairness in Machine Learning Systems
PyTorch
60 PyTorch for Hackathons
PyTorch for Hackathons
PyTorch

PyTorch Mobile enables efficient on-device ML, allowing for lower latency and privacy-preserving techniques like federated learning. The framework supports an end-to-end workflow from Python to mobile devices, with features like Torch Script and LibTorch C++ API. By using PyTorch Mobile, developers can build and deploy ML models on edge devices, optimizing for low latency and improving overall performance.

Key Takeaways
  1. Add a dependency on PyTorch
  2. Write code to run the model
  3. Use a Java wrapper on Android
  4. Use the LibTorch C++ API on iOS
  5. Design and train models with quantization
  6. Test and deploy models on mobile devices
💡 PyTorch Mobile's support for Torch Script and LibTorch C++ API enables efficient on-device ML, making it an ideal choice for developers who want to build and deploy ML models on edge devices.

Related AI Lessons

I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →