Skip to content
ImagineWorld

Domain-Specific Small Language Models

node_idnode-book_build-small-language-model
node_typenode-book
schema_profileworld.node_book.v1
statusactive
orthogonal_scopemanifold, entropy, authority, state
manifoldworld
entropymedium
authorityinforms
stateexternal
domainimagineworld.net
node_interiorrosetta_contract
unisonone evidence claim read five ways: seated in the llms field, the work builds a small language model — the full pipeline at a scale one machine can hold — its framework, regime, and stance agreeing on what it is
governed_byWORLD-MANIFOLD
constitutional_citationCONSTITUTION
evidence_bundleworld-bundle-publication
compared_byworld-comparison-pillar-f-compiler-trinity-and-training-infrastructure
pillar_membershipF — training_infrastructure
book_titleDomain-Specific Small Language Models
book_subtitleEfficient AI for local deployment
book_authorGuglielmo Iozzia
book_publisherManning
total_chapters15
total_source_pages361
total_nodes16
source_pdfbuild-small-language-model_author_guglielmo-iozzia.pdf
source_pdf_sha256507d793c25bf98e278b60077af800a8f0396cfd25a96c11ab591c46b80ba3ec2
source_text_output_slugbuild-small-language-model
ocr_sourceQwen3-VL-8B-Instruct via llama-server (scaffold-ocr-batch-v2; pdftoppm 150dpi grayscale)
ocr_quality_classdraft
verification_statusfrontier-cleaned-pass-1
verification_progress{"all_nodes":"stitched from OCR output; frontier verification status per node"}
stitched_bykimi-k3-frontier-transposition-pass
pillar_normalizedF-training_infrastructure
updated_at2026-10-14T05:45:17Z
verification_noteThree cleanup passes (frontier). Pass 1: 424 unfenced code blocks fenced and tagged, 227 running headers removed, HTML converted, figure inventory opened. Pass 2: prose/logic read, 113 hyphenation breaks rejoined, 16 output transcripts fenced, 3 fence-parity inversions repaired. Pass 3: verbatim audit - dropped sentence and 3 section headings restored from scans, 16 printed captions restored, ch14 re-shoot duplicate (scan pp. 308-309) removed, 15 further figures converted losslessly. Pass 4: all four unresolved figures closed, nine more rasters replaced by verbatim text, every remaining crop re-set by hand and verified, assets 5.63 MB -> 2.93 MB. Pass 5: rendering repair - 116 fence/text collisions resolved, comment-headings and swallowed prose fixed, transcripts rebuilt, ~870 print-width line breaks joined, crops re-exported as plain grayscale, one-line captions. Pass 6: 46 Python blocks repaired against the scans, ch08 record re-transcribed, ch15 raw transcripts re-transcribed, all listing annotations restored. See CLEANED.md and CLEANUP_NOTES.md.
visual_conversion_pages19, 22, 35, 40, 72, 82, 83, 84, 91, 92, 107, 108, 134, 141, 142, 143, 160, 187, 189, 197, 236, 250, 259, 274, 294, 306, 307, 313, 322, 325, 329, 333, 334, 342, 346, 352
asset_count36
figures_converted41
figures_embedded36
figures_flagged0
python_blocks_parse_clean482/496
not_stitchedIndex
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.

node-book_build-small-language-model_chapter-01-small-language-models — Chapter 1 — Small language models

node-book_build-small-language-model_chapter-02-tuning-for-a-specific-domain — Chapter 2 — Tuning for a specific domain

node-book_build-small-language-model_chapter-03-end-to-end-transformer-fine-tuning — Chapter 3 — End-to-end transformer fine-tuning

node-book_build-small-language-model_chapter-04-running-inference — Chapter 4 — Running inference

node-book_build-small-language-model_chapter-05-exploring-onnx — Chapter 5 — Exploring ONNX

node-book_build-small-language-model_chapter-06-quantizing-for-your-production-environment — Chapter 6 — Quantizing for your production environment

node-book_build-small-language-model_chapter-07-generating-python-code — Chapter 7 — Generating Python code

node-book_build-small-language-model_chapter-08-generating-protein-structures — Chapter 8 — Generating protein structures

node-book_build-small-language-model_chapter-09-advanced-quantization-techniques — Chapter 9 — Advanced quantization techniques

node-book_build-small-language-model_chapter-10-profiling-insights — Chapter 10 — Profiling insights

node-book_build-small-language-model_chapter-11-deployment-and-serving — Chapter 11 — Deployment and serving

node-book_build-small-language-model_chapter-12-running-on-your-laptop — Chapter 12 — Running on your laptop

node-book_build-small-language-model_chapter-13-creating-end-to-end-llm-applications — Chapter 13 — Creating end-to-end LLM applications

node-book_build-small-language-model_chapter-14-advanced-components-for-llm-applications — Chapter 14 — Advanced components for LLM applications

node-book_build-small-language-model_chapter-15-test-time-compute-and-small-language-models — Chapter 15 — Test-time compute and small language models

DOMAIN-SPECIFIC Small Language Models Efficient AI for local deployment

Guglielmo Iozzia Foreword by Matthew R. Versaggi

MANNING

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

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

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

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.