liveBook · Manning

Chapter 17: Deploying to production

What you will learn in chapter 17 of Deep Learning with PyTorch, Second Edition

The introduction explains why deployment choices depend on how people need to access a model and where it needs to run.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why deployment choices depend on how people need to access a model and where it needs to run.

Key terms: Gradio, FastAPI, network service, ONNX, C++, mobile devices

17.1 Serving PyTorch models

The section introduces a hands-on path from a simple network server for sharing a model to examining its limitations, with a warning not to use an untrusted network.

17.1.1 Our model served by Gradio

This section shows how Gradio turns a Python function into a shareable model demo, using text-to-image generation as an example, while noting its production limits.

Key terms: Gradio, function signature, input and output types, diffusers, Stable Diffusion, text prompt, demo.launch(share=True), production, high-performance, scalable

17.1.2 Our model behind a FastAPI server

This section shows how to serve a text-generation model through a FastAPI endpoint and test it locally.

Key terms: FastAPI, POST route, /generate, Pydantic model, JSON response, pretrained weights, .to('cuda'), model.eval(), torch.no_grad(), inference

17.1.3 What we want from deployment

The section identifies the main requirements for serving a PyTorch model efficiently and safely: concurrent request handling, efficient inference, streaming output, and bounded inputs.

Key terms: FastAPI, async def, asynchronous endpoints, coroutines, model inference time, batch requests, GPU, second thread, global interpreter lock (GIL), streaming the response

17.1.4 Request batching and streaming responses

This section shows how a FastAPI server can batch incoming model requests while streaming generated tokens back to each client.

Key terms: request batching, queue, model worker, full batch, maximum time, lifespan event, model_worker, shutdown_event, inference_queue, results dictionary

17.1.5 How to make PyTorch models even faster

This section presents practical ways to speed up PyTorch inference or reduce model footprint, then points to existing serving tools.

Key terms: torch.compile, just-in-time (JIT) compilation, kernel fusion, operator reordering, distillation, quantization, 32-bit, 8-bit, quantization-aware training, post-training quantization

17.2 Exporting models

The section explains why production deployment may call for exporting a PyTorch model and what the export produces.

Key terms: export, GIL, embedded systems, standardized representation, exporting a model

17.2.1 Interoperability beyond PyTorch with ONNX

This section explains how ONNX lets PyTorch models run outside PyTorch and shows how to export a model for an ONNX-compatible runtime.

Key terms: ONNX, ONNX-compatible runtime, operations, dummy input, torch.onnx.export, trace, ONNX file

17.2.2 PyTorch’s own export: torch.export

This section shows how torch.export captures a PyTorch model as a graph and how that graph can be compiled, packaged, and run for deployment.

Key terms: ONNX, torch.export, interoperability, torch.nn.Module, example input, traced graph, TorchDynamo, parameters, torch.ops.aten, dtype

17.3 Expanding on torch.compile

This introduction connects the previously discussed model export and execution flow to torch.compile before the next section examines their differences.

Key terms: torch.compile, exported model

17.3.1 Full graph capture vs. disjoint graphs

This section contrasts torch.export’s full-graph requirement with torch.compile’s ability to optimize models despite graph breaks, then explains the compilation process and its predecessors.

Key terms: torch.export, full graph capture, static computation graph, dynamic control flow, torch.compile, TorchDynamo, Dynamo, graph breaks, disjoint graphs, regular Python

17.4 Understanding execution with torch.profiler

This section shows how torch.profiler reveals model performance and execution flow, including changes caused by compilation.

Key terms: torch.profiler, execution time, memory usage, bottlenecks, trace, CPU execution timeline, GPU execution timeline, record_function(), Compiled Region, fused GPU operations

17.5 Using PyTorch outside of Python

Introduces PyTorch ecosystem offerings as a way to run models outside Python, including in C++ applications and on mobile devices.

Key terms: PyTorch’s Ecosystems, PyTorch models

17.5.1 LibTorch: PyTorch in C++

This section introduces LibTorch as a way to use PyTorch from C++, then outlines how to build and run examples and load a model compiled ahead of time.

Key terms: LibTorch, C++ API, Python API, CMake, CMAKE_PREFIX_PATH, build directory, inference, torch::nn::Module, Net, ahead-of-time (AOT) inductor

17.6 Going mobile: ExecuTorch

This section explains why ExecuTorch is useful on resource-constrained devices and outlines how an exported PyTorch model is prepared and run in a mobile application.

Key terms: ExecuTorch, mobile phones, embedded systems, PyTorch operations, torch.export, computational graph, Edge IR, runtime environment, LibTorch, JNI

17.7 Conclusion

The section concludes that PyTorch offers deployment paths for varied environments, supported by an evolving ecosystem.

Key terms: Gradio, FastAPI, ONNX, LibTorch, ExecuTorch, torch.compile

17.8 Exercises

17.9 Summary

Concepts explained in this chapter

← Chapter 16: Training models on multiple GPUs

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.