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 adapt a model to a specific domain, you have two fundamentally different options. RAG gives the model access to external information at inference time: the model itself doesn't change, but it can consult a knowledge base before responding. Fine-tuning takes a different approach: instead of changing what the model sees, it modifies the model's weights directly, reshaping its behavior. This can range from adjusting how a model responds, to introducing vocabulary and concepts from a domain it barely encountered 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