Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Guglielmo Iozzia

Bigger isn’t always better. Train and tune highly focused language models optimized for domain specific tasks. When you need a language model to respond accurately and quickly about a specific field of knowledge, the sprawling capacity of a LLM may hurt more than it helps. Domain-Specific Small Language Models teaches you to build generative AI models optimized for specific fields. In Domain-Specific Small Language Models you’ll discover: Model sizing best practices Open source libraries, frameworks, utilities and runtimes Fine-tuning techniques for custom datasets Hugging Face’s libraries for SLMs Running SLMs on commodity hardware Model optimization or quantization Perfect for cost- or hardware-constrained environments, Small Language Models (SLMs) train on domain specific data for high-quality results in specific tasks. In Domain-Specific Small Language Models you’ll develop SLMs that can generate everything from Python code to protein structures and antibody sequences—all on commodity hardware. about the reader For machine learning engineers familiar with Python. about the author Guglielmo Iozzia is a Director, ML/AI and Applied Mathematics at MSD. He studied Electronic and Biomedical Engineering at the University of Bologna, has an extensive background in Software and ML/AI Engineering applied to real-life use cases across different industries, such as Biotech Manufacturing, Healthcare, Cloud Operations, and Cyber Security.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical guide to building, fine-tuning, and deploying small language models that beat giant LLMs on focused tasks—without needing a data center. Best for Python-savvy ML engineers working under cost, latency, or hardware constraints. 【Book Arc】 - **Opening (~0%–10%)**: Frames why smaller, domain-tuned models often outperform sprawling LLMs, covering model sizing, sustainability, alignment, and the Python/PyTorch prerequisites you'll need. - **Early (~10%–30%)**: Core specialization toolkit—fine-tuning workflows, Hugging Face Transformers/Datasets, RAG with vector stores like FAISS, and a hands-on GPT-2 case study generating Manim code. - **Middle (~30%–50%)**: Optimization and serving—quantization (int8, GPTQ 4-bit), DeepSpeed inference, ONNX conversion, KV caching, and measuring real latency/throughput gains. - **Late (~50%–80%)**: Applied domain walkthroughs, including Python code generation with CodeGen models and evaluation via HumanEval-style correctness checks. - **Ending (~80%–100%)**: Broader domain applications (e.g., protein structures, antibody sequences) and consolidation of the build-tune-optimize-deploy pipeline. (Excerpts do not cover the final chapters in detail.) 【Key Takeaways】 - **Bigger isn't automatically better** (Opening): For narrow domains, a tuned sub-10B-parameter model can match or exceed a general LLM while cutting cost, latency, and CO2 footprint. - **Fine-tuning is the primary specialization lever** (Early): Hugging Face's Transformers API keeps code largely model-agnostic, so you can swap pre-trained backbones with minimal changes. - **RAG complements fine-tuning** (Early): When you need fresh or private knowledge, embed your knowledge base into a vector store (FAISS is the worked example) rather than retraining. - **Quantization unlocks commodity hardware** (Middle): int8 and GPTQ 4-bit methods shrink models dramatically with modest accuracy loss—but always benchmark perplexity across multiple runs, not one. - **Serving optimizations compound** (Middle): DeepSpeed kernel injection, ONNX export, and KV caching each reduce latency and variance; ONNX notably tightened run-to-run consistency in the CodeGen example. - **Evaluation must be task-specific** (Middle): Code generation is judged by executing outputs against test suites (HumanEval), not by eyeballing text—and safety guardrails matter when running generated code. - **Small models still hallucinate** (Early): The fine-tuned Manim model handled structure well but struggled with numeric arguments, showing domain tuning reduces—not eliminates—errors. - **Alignment is a design choice you control** (Opening): The author argues alignment should target model intent, not vendor-defined right/wrong—an area where domain narrowing gives you leverage. 【Reading Tips】 - **Deep-read the Early and Middle sections** if your goal is hands-on fine-tuning and deployment; these contain the most concrete, reusable code patterns. - **Skim the Opening** if you already know transformer basics—focus instead on the sizing and sustainability arguments that justify the SLM approach. - **Run the companion Colab notebooks** referenced throughout; the book is explicitly hands-on and GPU-accelerated examples are central to the learning. - **Treat quantization benchmarks as starting points**, not verdicts—replicate perplexity and latency tests on your own hardware and task. - **Watch the safety caveat** around executing generated code (HumanEval's guardrails are disabled in the notebook); sandbox any evaluation you run. 【Coverage Limits】 This guide is based on stratified excerpts covering roughly the first half of the book; later chapters on advanced domain applications (protein/antibody generation) and any concluding material are only lightly represented.
Page 9
odel can understand their interplay within a given context. Transformer architectures have been proposed also for other tasks where different types or data i...
View in text
Excerpt 2
source code presented in this chapter is provided. Hardware acceleration (GPU, free tier) is required. The dataset we are going to use is manim_python (https...
View in text
Excerpt 3
trained a model using a DL framework of choice, or received a model trained by someone else, you can convert it in the ONNX format first stored in a combinat...
View in text
Excerpt 4
. Please be careful when executing this kind of assessments. The results of the correctness phase are aggregated and saved in a JSON file, along with the gen...
View in text
Excerpt 5
is work important? Because it has demonstrated potential in accelerating materials discovery by producing candidate crystal structures os.environ["HF_TOKEN"]...
View in text
Excerpt 6
B], [Y]) Let’s rebuild the model and recheck it: from onnx.helper import make_opsetid onnx_optimized_model = make_model(graph, opset_imports=[ make_opsetid('...
View in text
Excerpt 7
It would be detailed in chapter 12. { "role": "user", "content": "why is the sky blue?" } ] So far, we have seen SLMs served through Ollama in action from a...
View in text
Excerpt 8
put in existing systems. To this end, frameworks like vLLM [4] powered by PagedAttention and research systems like Orca [5] have significantly improved the p...
View in text
Tags
AI categories
Artificial IntelligencePythonData
Publish Year: 2025
Language: English
Pages: 470
File Format: PDF
File Size: 9.4 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…