liveBook · Manning

Chapter 11: Deployment and serving

What you will learn in chapter 11 of Domain-Specific Small Language Models

The chapter introduces a selective survey of popular tools and environments for deploying and serving small, customized language models.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces a selective survey of popular tools and environments for deploying and serving small, customized language models.

11.1 vLLM

Introduces vLLM as an LLM-serving library, explains the memory-management problems behind PagedAttention, and surveys its serving capabilities.

Key terms: PagedAttention, KV cache, memory fragmentation, throughput, batch size, contiguous memory allocation, internal fragmentation, external fragmentation, preallocation, memory sharing

11.1.1 Offline serving

This subsection demonstrates offline inference with vLLM through text completion, chat, and a quantized GGUF model.

Key terms: LLM class, generate, KV cache, generate method, SamplingParams, temperature, top_p, top_k, chat method, conversation prompt

11.1.2 Online serving

This section shows how to serve an SLM through vLLM’s OpenAI-compatible HTTP API and benchmark its online performance.

Key terms: vLLM, serve CLI command, Hugging Face Hub, OpenAI API, OpenAI Python client API, completions, chat completions, embeddings, benchmark, ShareGPT

11.2 FastAPI

This section introduces FastAPI’s API-building features and shows how a text-generation model can be served through a POST route and packaged in Docker.

Key terms: FastAPI, Starlette, Pydantic, Python type hints, Swagger UI, ReDoc, route, HTTP GET, HTTP POST, dev

11.2.1 Benchmarking various models

This subsection benchmarks four versions of a language model to choose a FastAPI deployment candidate based on inference latency.

Key terms: FastAPI, model inference, benchmarking, FP32, ONNX, FP16, warmup, average latency, prompt lengths, ONNX optimized model

11.2.2 Deploying the best-performing model with FastAPI

The section shows how to serve a selected ONNX-optimized GPT-2 small model through a FastAPI route.

Key terms: generate function, ONNX model, ORT inference session, async, LLM latency, FastAPI, GenerateRequest, Pydantic’s BaseModel, route

11.3 MLC LLM

This section introduces MLC LLM and walks through preparing an SLM for a target device, then using its Python API or REST server for inference.

Key terms: MLC LLM, MLCEngine, OpenAI-compatible API, Hugging Face Hub, mlc_llm_convert_weights, mlc_llm_gen_config, mlc-chat-config.json, mlc_llm_compile, AsyncMLCEngine, streaming responses

11.4 Deployment and inference on Android devices

The section introduces private, offline SLM deployment on Android and contrasts common TensorFlow-based examples with the PyTorch alternatives that follow.

Key terms: TensorFlow Lite, MediaPipe, PyTorch, ONNX

11.4.1 MLC LLM framework

This section summarizes how to package and deploy an SLM to Android with MLC LLM, and notes the device-specific checks and development dependencies involved.

Key terms: MLC LLM, runtime, model libraries, template Android app, model weights, APK, target device, hardware specs, benchmark performance, optimize the model

11.4.2 MLLM framework

This subsection explains how to build mllm, run converted models on desktop or Android, and prepare custom models for resource-constrained devices.

Key terms: mllm, build.sh, Android NDK, Hugging Face Hub, bin directory, convert.py, PyTorch, safetensors, mllm format, INT4 quantization

11.4.3 Hugging Face Transformers

This section shows how to run a Hugging Face Transformers model on an Android device using Python in Termux, then choose and optimize a model for the device’s resources.

Key terms: developer options, Termux, virtual environment, PyTorch, Transformers, on-device inference, bert-base-uncased, classification, Python inference script, on-device performance

11.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 10: Profiling insights Chapter 12: Running on your laptop →

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.