Domain-Specific Small Language Models
{"all_nodes":"stitched from OCR output; frontier verification status per node"}local graph
Domain-Specific Small Language Models — Guglielmo Iozzia
Section titled “Domain-Specific Small Language Models — Guglielmo Iozzia”Efficient AI for local deployment — Manning. Foreword by Matthew R. Versaggi.
The book is organized as 15 chapters under 4 parts, followed by an index. Known scan anomalies, documented in the stitch spec: two mis-inserted re-shoot pages skipped, printed 8–9 missing from chapter 1, the foreword/preface/acknowledgments/about sections absent, and the index likely truncated.
chapter_01
Section titled “chapter_01”node-book_build-small-language-model_chapter-01-small-language-models — Chapter 1 — Small language models
chapter_02
Section titled “chapter_02”node-book_build-small-language-model_chapter-02-tuning-for-a-specific-domain — Chapter 2 — Tuning for a specific domain
chapter_03
Section titled “chapter_03”node-book_build-small-language-model_chapter-03-end-to-end-transformer-fine-tuning — Chapter 3 — End-to-end transformer fine-tuning
chapter_04
Section titled “chapter_04”node-book_build-small-language-model_chapter-04-running-inference — Chapter 4 — Running inference
chapter_05
Section titled “chapter_05”node-book_build-small-language-model_chapter-05-exploring-onnx — Chapter 5 — Exploring ONNX
chapter_06
Section titled “chapter_06”node-book_build-small-language-model_chapter-06-quantizing-for-your-production-environment — Chapter 6 — Quantizing for your production environment
chapter_07
Section titled “chapter_07”node-book_build-small-language-model_chapter-07-generating-python-code — Chapter 7 — Generating Python code
chapter_08
Section titled “chapter_08”node-book_build-small-language-model_chapter-08-generating-protein-structures — Chapter 8 — Generating protein structures
chapter_09
Section titled “chapter_09”node-book_build-small-language-model_chapter-09-advanced-quantization-techniques — Chapter 9 — Advanced quantization techniques
chapter_10
Section titled “chapter_10”node-book_build-small-language-model_chapter-10-profiling-insights — Chapter 10 — Profiling insights
chapter_11
Section titled “chapter_11”node-book_build-small-language-model_chapter-11-deployment-and-serving — Chapter 11 — Deployment and serving
chapter_12
Section titled “chapter_12”node-book_build-small-language-model_chapter-12-running-on-your-laptop — Chapter 12 — Running on your laptop
chapter_13
Section titled “chapter_13”node-book_build-small-language-model_chapter-13-creating-end-to-end-llm-applications — Chapter 13 — Creating end-to-end LLM applications
chapter_14
Section titled “chapter_14”node-book_build-small-language-model_chapter-14-advanced-components-for-llm-applications — Chapter 14 — Advanced components for LLM applications
chapter_15
Section titled “chapter_15”node-book_build-small-language-model_chapter-15-test-time-compute-and-small-language-models — Chapter 15 — Test-time compute and small language models
Title Page
Section titled “Title Page”DOMAIN-SPECIFIC Small Language Models Efficient AI for local deployment
Guglielmo Iozzia Foreword by Matthew R. Versaggi
MANNING
Copyright
Section titled “Copyright”For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected]
© 2026 Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein.
Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964
Development editor: Dustin Archibald Technical editor: Riccardo Mattivi Review editor: Angelina Lazukić Production editor: Aleksandar Dragosavljević Copy editor: Andy Carroll Proofreader: Melody Dolab Technical proofreader: Bin Hu Typesetter: Tamara Švelić Sabljić Cover designer: Marija Tudor
ISBN 9781633436701 Printed in the United States of America
Brief Contents
Section titled “Brief Contents”brief contents
PART 1 FIRST STEPS … 1 1 ■ Small language models 3
PART 2 CORE DOMAIN-SPECIFIC LLMs … 15 2 ■ Tuning for a specific domain 17 3 ■ End-to-end transformer fine-tuning 36 4 ■ Running inference 51 5 ■ Exploring ONNX 71 6 ■ Quantizing for your production environment 96
PART 3 REAL-WORLD USE CASES … 119 7 ■ Generating Python code 121 8 ■ Generating protein structures 145
PART 4 ADVANCED CONCEPTS … 163 9 ■ Advanced quantization techniques 165 10 ■ Profiling insights 184 11 ■ Deployment and serving 206 12 ■ Running on your laptop 235
BRIEF CONTENTS
13 ■ Creating end-to-end LLM applications 258 14 ■ Advanced components for LLM applications 292 15 ■ Test-time compute and small language models 326
Table of Contents
Section titled “Table of Contents”contents
foreword xii preface xiv acknowledgments xv about this book xvii about the author xix about the cover illustration xx
PART 1 FIRST STEPS…1
1 Small language models 3 1.1 What are small language models? 4 1.2 The Transformer architecture 5 1.3 Areas of application 6 1.4 The open source revolution 6 1.5 Risks and challenges with generalist LLMs 10 1.6 Domain-specific vs. generalist LLMs for business value 11
PART 2 CORE DOMAIN-SPECIFIC LLMs…15
2 Tuning for a specific domain 17 2.1 Data preparation 17 Data preparation for BERT fine-tuning 18 • Data preparation for GPT fine-tuning 20 • Data preparation for RAG 22
CONTENTS 2.2 Retrieval-augmented generation 24 2.3 Fine-tuning 25 2.4 LoRA 29 2.5 RAG or fine-tuning? 34 3 End-to-end transformer fine-tuning 36 3.1 Data preparation 36 3.2 Fine-tuning 38 3.3 Testing the fine-tuned model 41 3.4 Domain-specific evaluation 44 4 Running inference 51 4.1 How to generate content 51 Text completion 52 • Few-shot learning 55 • Code generation 56 • Evaluating the generated content 57 4.2 Calculating inference cost 59 4.3 Areas for improvement (cost savings and performance) 60 Getting the most from your GPU 60 • Batching 64 Estimating the generation time 65 • Optimizing GPU use with DeepSpeed 66 5 Exploring ONNX 71 5.1 The ONNX format 71 5.2 ONNX operators and types 74 5.3 The ONNX runtime 81 5.4 ONNX runtime providers 82 5.5 ONNX for LLMs on CPU 84 5.6 ONNX for LLMs on GPU 88 ONNX for GPT on GPU 89 • I/O binding 92 6 Quantizing for your production environment 96 6.1 Transformers precision formats 96 6.2 8-bit quantization 99 Hands-on 8-bit quantization 100 • LLM.int8() and quantization 104
CONTENTS
6.3 8-bit quantization with ONNX 107 6.4 4-bit quantization 112 4-bit quantization with GPTQ 112 • 4-bit quantization with ggml 114
PART 3 REAL-WORLD USE CASES … 119
7 Generating Python code 121 7.1 Using Transformers to generate code 121 7.2 Generating Python code with a Transformer architecture 123 Python code generation with CodeGen 123 • Using ONNX with models not supported by Optimum 133 • Model evaluation 134 Python code generation with better models 137
7.3 Coding assistance on commodity hardware 140
8 Generating protein structures 145 8.1 Applying Transformers in chemistry 145 8.2 From natural language to protein structures 147 8.3 Antibody generation with an SLM 149 8.4 From CIF files to crystal structures 153
PART 4 ADVANCED CONCEPTS … 163
9 Advanced quantization techniques 165 9.1 FlexGen 166 9.2 SmoothQuant 171 9.3 BitNet 176 9.4 BitNet and Python 179
10 Profiling insights 184 10.1 Profiling ONNX-ported LLMs 184 10.2 Transforming raw ONNX profiling data into insights 188 10.3 Optimization of ONNX graphs for LLMs 197
CONTENTS
11 Deployment and serving 206 11.1 vLLM 206 Offline serving 208 ▪ Online serving 212 11.2 FastAPI 215 Benchmarking various models 218 ▪ Deploying the best-performing model with FastAPI 222 11.3 MLC LLM 223 11.4 Deployment and inference on Android devices 229 MLC LLM framework 229 ▪ MLLM framework 229 Hugging Face Transformers 231
12 Running on your laptop 235 12.1 Why use a personal local assistant 236 12.2 Running an LLM locally with Ollama 236 Importing a custom model into Ollama 240 ▪ User privacy in Ollama 243 12.3 Running an LLM locally with LM Studio 244 12.4 The LM Studio Python SDK 248 12.5 Running an LLM locally with Jan 251 12.6 The Cortex local LLM engine 253
13 Creating end-to-end LLM applications 258 13.1 Why LLMs alone aren’t enough 259 13.2 Combining a domain-specific SLM with RAG 261 13.3 Using a vector database 275 13.4 Building an agent 279
14 Advanced components for LLM applications 292 14.1 GraphRAG 292 Microsoft’s open source GraphRAG capabilities 305 Evaluation metrics 306 14.2 RAG + Agentic AI 307 14.3 Long- and short-term memory management 320
CONTENTS
15 Test-time compute and small language models 326 15.1 Test-time compute 326 15.2 The OptiLLM inference proxy 328 15.3 SLMs with embedded test-time compute 337 15.4 Building a reasoning domain-specific SLM 338
index 347
About the Cover Illustration
Section titled “About the Cover Illustration”about the cover illustration
Section titled “about the cover illustration”The figure on the cover of Domain-Specific Small Language Models, titled “Il Guappo,” or “The tough guy,” is taken from a book by Francesco de Bourcard, published in 1853. Each illustration is finely drawn and colored by hand.
In those days, it was easy to identify where people lived and what their trade or station in life was just by their dress. Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional culture centuries ago, brought back to life by pictures from collections such as this one.