Model Training and Optimization: Pre-training, Fine-tuning, LoRA, Quantization
Model training and optimization describes the path from a model acquiring general knowledge, to adapting it to a specific task, to shrinking it enough to run on constrained hardware. Pre-training, fine-tuning, LoRA and quantization are the four core steps.
Pre-training
The initial stage where a model gains its basic capabilities and general knowledge from a very large and diverse dataset (for language models, sources such as books, web text and code). It is the most expensive stage, and most companies do not do it themselves; they start from an existing pre-trained model.
Fine-tuning
The process of taking a pre-trained model and training it further on a smaller, specific dataset to improve performance in a specific task (e.g., classifying fault reports or generating code). The image-model equivalent is transfer learning.
LoRA (Low-Rank Adaptation)
A technique that makes fine-tuning possible with much less compute and memory. The original weight matrix W is frozen; two small matrices (B and A) are added next to it and only these are trained:
W′ = W + B·A (B: d×r, A: r×d, r ≪ d)
Example: for a layer with d = 4096, the full weight matrix has 4096 × 4096 ≈ 16.8 million parameters. With rank r = 8, LoRA trains:
2 × 4096 × 8 = 65,536 parameters, about 0.4%.
After training, the small LoRA weights can be merged into the base model or kept as a separate file and swapped per task.
Quantization
The process of compressing a model by representing its weights with fewer bits. For example, reducing 16-bit values to 8 or 4 bits makes the model use less RAM and, on most hardware, run faster.
Example: memory needed for the weights alone of a 7-billion-parameter model:
| Format | Per parameter | Total |
|---|---|---|
| FP16 | 2 bytes | ≈ 14 GB |
| INT8 | 1 byte | ≈ 7 GB |
| 4-bit | 0.5 bytes | ≈ 3.5 GB |
Extra memory is needed at runtime for the context cache and intermediate values.
Common Mistakes
- Full fine-tuning on little data: updating the whole model with a few hundred examples leads to overfitting; LoRA or partial fine-tuning is safer.
- Not testing after quantization: the accuracy loss from fewer bits depends on the task; the compressed model must be re-measured on the same test set.
- Fine-tuning to add knowledge: for frequently changing document knowledge, RAG is usually a better fit than fine-tuning.
Industrial Relevance
Techniques like Quantization and LoRA are critical for Edge AI. They make it possible to run powerful models on constrained hardware. Small models that detect anomalies in sensor signals can run even on microcontrollers once reduced to 8-bit integers (see TinyML).
- Potential Application: In the future, advanced devices similar to the ZMA Data Acquisition could use quantized models to perform local anomaly detection without needing a constant cloud connection.
More: Industrial AI
Related Terms
- What are Transfer Learning and Foundation Models?What is transfer learning? Adapting a pre-trained model to your own problem with little data: backbone, head, fine-tuning options and foundation models.
- LLM Architecture: Transformer, Attention, ParametersHow LLM architecture works: the Transformer, the attention mechanism, parameter count, and how the context window limits sensor data analysis.
- What are Overfitting and Generalisation?What is overfitting and how to spot it? A training vs validation loss example, data leakage, class imbalance and test splits for predictive maintenance.
- What is AI Inference?What AI inference is: how it differs from training, latency and throughput, INT8 quantization, edge versus cloud inference and the most common mistakes.
- What are Embedded Systems?What is an embedded system? MCU vs MPU, bare-metal vs RTOS vs embedded Linux, a real-time timing budget example and the role in industrial devices.