chapter seven

7 Model specialization

 

This chapter covers

  • Adapting models to structured outputs
  • Replacing long prompts through fine-tuning
  • Efficient fine-tuning with LoRA and DoRA
  • Specializing LLMs without losing capabilities

Throughout the previous chapters we've worked on the model's structure through pruning and distillation to gain speed without losing general capability. Now the focus shifts: we're going to adapt the LLM's behavior to our specific data and formats.

To specialize a model, you can use RAG (retrieval-Augmented Generation) to inject external knowledge, or fine-tuning to modify its responses and instincts. Fine-tuning solves a wide spectrum of problems: from changing how a model that already has the necessary knowledge responds, to injecting vocabulary and concepts from a specialized domain it barely saw during pretraining.

One clear example is adapting the LLM’s response to structured formats. A generalist LLM tends to converse and add unnecessary explanations—like "Here's the JSON..."—that break production pipelines. Fine-tuning corrects this at the root by transferring the instructions from the prompt directly into the model's weights. The result is robust, predictable outputs that eliminate the need for lengthy prompts.

7.1 Efficient fine-tuning

7.1.1 The foundations of LoRA

7.1.2 Extending LoRA with weight decomposition

7.2 Quantization and the memory bottleneck

7.3 Specializing with QLoRa and QDoRA

7.3.1 Base model and clinical dataset

7.3.2 Establishing the baseline: reliability and capabilities

7.3.3 QLoRA training on the clinical dataset

7.3.4 QDoRA training on the clinical dataset

7.3.5 Evaluating the results against our goals

7.3.6 Merging weights and dynamic adapter routing

7.4 From paper to practice

7.5 Hands-on lab

7.6 Summary