Deploying your ML Model with TorchServe

PyTorch · Beginner ·📰 AI News & Updates ·6y ago

Key Takeaways

The video demonstrates how to deploy PyTorch models using TorchServe, a flexible and easy-to-use tool for serving PyTorch models, and covers topics such as model optimization, performance improvement, and inference using the management API and inference API.

Full Transcript

welcome to the pie torch summer hackathon 2020 I'm Brad Hinds and what partner engineer were the pie tours team if you're not sure what a partner engineer is it's my day job to make sure that developers like you get the absolute most they can out of pie torch and related tools in this video I'm going to show you how to deploy your machine learning model with torch serve a trained machine learning model alone does not make a full fledged application you'll need to integrate it into a larger system in this video I'll be using the term model serving to refer to this integration and subsequent use of the model recently the PI torch team released torch serve which is the preferred model serving solution for pi torch it's a performant and scalable tool for wrapping your PI torch model in an HTTP or HTTPS API in this short video I won't be going into the advanced features of torch serve instead I'll be focusing on the stuff you'll need for a hackathon namely how to stand up your first torch serve installation and start serving inferences from your model in this video I'll be showing you how to package your model and any supporting files it needs in a model archive and place it in the model store where torch sir can see it how to apply the default model handlers the torch cert provides for certain use cases and how to create a custom model handler if you need one for a new use case starting torch serve and configuring it for serving your model by a torch serves inference API examining a manipulating server state with the management API and where to go to find log files so you can see what's happening under the hood for this walkthrough I'm going to show you how to setup a simple image classifier and torch serve everything I'm about to show you is documented more thoroughly at PI torch org slash Docs and at github comm / pi torch slash serve first make sure you're in an environment with PI torch 1.5 or higher you'll also need open JDK 11 that's version 11 of the Java development kit next install torch sir and the torch serve model archiver you can use pip or anaconda for this I'll be using anaconda when installing with anaconda don't forget to use the PI torch channel that's - si PI torch on the command line now I'll show you some optional steps to optimize your model for use with torch serve they aren't strictly necessary but will often improve performance and memory footprint so I encourage you to try them and profile the results with your own model so first things first we'll import PI torch and torch vision and this is a good place to point out that you should be using pi torch 1.5 or higher for this example in the next cell we're going to create a PI torch model object now for the actual app I have a pre trained optimized model ready to go but for the optimization process I wanted to show you this on a custom model so that you could duplicate the process with your own models this model happens to contain some very common layer types A to D convolutional layer a to D batch norming layer and a rectified linear unit for activation and the forward function just strings those three operations together so now that we have our model how do we optimize it first thing let's get an instance of the model in my get model helper you'll notice that besides just instantiating the model I call m dot eval so eval turns off things in the model that you don't want on during inference time training holding layers like dropout automated gradient tracking all this training related stuff meets up CPU cycles and we don't need it for inference so we're gonna make sure the model is an eval mode the second thing we're going to do is some layer fusion fusing layers means taking multiple operations and combining them together into a single operation this improves performance in memory footprint now with diffuse modules method that I'm going to show you there are only certain combinations of layers that you can fuse together I'm going to refer you to the documentation for the latest information on that but here we're going to try to accuse together convolution bachelor-man Darrell you once modules are fused the next one we're going to do is quantize the model PI torch tensors default to using 32-bit floating-point numbers as their underlying type when we quantize we're going to change that to an 8-bit integer this will perform faster and reduce the models footprint both on disk and in marine the final thing we're going to do is save the model as torch script torch script is an optimized format for your model including both your computation graph and you're learning weights it's meant to be consumed by the PI torch just-in-time compiler or JIT so once it's exported we'll save now there are subtleties to layer fusion into quantizing your model that you'll want to be familiar with all of this is covered in the PI torch documentation for quantization which I encourage you to check out once you have your model ready you'll need to create a model archive and place it in the model store the model store is just a folder that holds your model archives a model archive is a single file bundle that includes your model and any supporting files you need for a natural language model that might be a vocabulary or an embedding or if you're deploying an on-court script model you'll also need to add the Python file that defines your model class so let's make the model store and we'll create a model archive for our model I'll be using my own model with the learning weight saved in a dot PT file and the model code in the dot py file I'm also going to pull down a single file from the torch sir github repo it's just a JSON file that has all the human readable labels for the categories my model is trained against let's take these three files and make a model archive out of them the important things I'm specifying on this command line are I specify the name of my model endpoint with the model name flag the version of the model tortured can simultaneously serve multiple versions of the same model for things like benchmarking and maybe testing with the serialized file flag we specify the file that contains our models learning weights the model file flag specifies the Python file that contains your models class definition the model handler flag lets you specify code that pre processes and post processes data for your model here I'm using the default image classifier model handler but if I were using a custom Handler I'd specify a path to a Python file with the model handling code for more details see the PI torch documentation and the github repo for torch servic github.com / PI torch / serve and finally the extra files flag lets you specify a list of files to include in your model archive here I've included the index that contains the human readable names of my categories so let's look at what we have and recap briefly we have created the model store folder gathered the files we need the model it's Python file and a supporting file with the human readable labels for our categories and finally we combine these files into a model archive file the final preparatory step is to move the model to the model store now we have everything we need to start torch serve and serve our model this is how we'll start towards surf the important arguments here are the start flag because we're starting torch serve the NCS flag stands for no configuration snapshots by default torch surf stays its state it will restore it the next time you restart it to avoid confusion for your first time using torch serve I've turned that feature off we specify the model store because that's where our model archives are and finally with the models flag we can specify a list of model archives that we'd like to serve you must specify the model archive file name the piece to the left of the equal sign is the name of the API endpoint and is optional so now I'll actually start torch serve and you can see a lot of log info scrolling by worker threads are being started models are being loaded into memory and those worker threads just for the sake of tidiness I'm going to flip over to another terminal window if you want to check the status torch serve and your endpoint you can use the management API this call tells us which models were serving we want to drill down on detail for a particular model we can specify it in the URL and now we can see that we have four separate worker threads all serving version 1.0 of my cv model you can also use the management API for adding and removing models and model versions and changing the number of worker threads assigned to a model all this is documented the docks at PI torch org and with example code at github comm / PI torch / serve so let's do some actual inference I happen to have an image file already and so let's see when model thinks this is for that we'll use the inference API on this command line we're going to specify our server and port by default torch serve uses port 8080 we're going to call the predictions endpoint and we're going to specify which model we want to serve the prediction and path to a file for input and sure enough my model thinks the picture of a cat is a picture of a cat so that's success the last thing I need to show you is how to stop torch serve you just call torch serve from the command line with the stop flag and that is torch serve in a nutshell there's a lot more to learn about torch serve multiple methods for configuration configurations snapshots for setup convenience and custom handlers for new use cases again I strongly recommend the documentation at PI torch org slash Docs and the examples at github comm / pi torch slash serve thank you for watching and for participating in a PI torch summer hackathon 2020

Original Description

Learn more: https://pytorch.org/serve/ Deploying and managing models in production is often the most difficult part of the machine learning process. TorchServe is a flexible and easy to use tool for serving PyTorch models. In this talk, Brad Heintz walks through how to use TorchServe to deploy trained models at scale without writing custom code. Subscribe to this page to get the latest news, updates, and weekly tutorials planned for the full duration of the Global PyTorch Summer Hackathon 2020. Haven't signed up yet? Learn more and get involved here: https://bit.ly/3iILgDs
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from PyTorch · PyTorch · 0 of 60

← Previous Next →
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
29 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

This video teaches how to deploy PyTorch models using TorchServe, including model optimization, performance improvement, and inference using the management API and inference API. By following the steps outlined in the video, viewers can learn how to serve PyTorch models in a production-ready environment.

Key Takeaways
  1. Install TorchServe and the TorchServe model archiver using pip or Anaconda
  2. Create a PyTorch model object
  3. Optimize the model for use with TorchServe
  4. Package the model and supporting files in a model archive
  5. Apply default model handlers or create a custom model handler
  6. Fuse layers together
  7. Quantize the model
  8. Save the model as a Torch Script
  9. Create a model archive and place it in the model store
  10. Start TorchServe with the start flag and important arguments
💡 TorchServe provides a flexible and easy-to-use way to serve PyTorch models, allowing for model optimization, performance improvement, and inference using the management API and inference API.

Related Reads

📰
The top AI fear for 6,000 tech pros isn't losing their jobs - it's more work for the same pay
Tech pros fear increased workload due to AI, not job loss, and are hesitant to recommend their role to newcomers
ZDNet
📰
Only10 Vol.26.01 | 10 AI Projects Worth Watching This Week
Discover 10 noteworthy AI projects using the CRP framework to stay updated on the latest developments in the field
Medium · ChatGPT
📰
Top 10 AI Development Companies in the USA (2026 Edition)
Discover the top 10 AI development companies in the USA to learn about industry leaders and potential partners or employers
Medium · AI
📰
Kimi K3 is so popular Moonshot ran out of GPUs and paused new sign-ups
Moonshot AI's Kimi K3 model is so popular it exceeded computing power, prompting a pause on new sign-ups, highlighting the importance of scaling infrastructure for AI startups
The Next Web AI
Up next
The Real Reason Most Good Podcasts Hit Podfade
Melanie Benson
Watch →