Model Training and Optimization: Pre-training, Fine-tuning, LoRA, Quantization

AIModel TrainingFine-tuningLoRAQuantizationEdge AI

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:

FormatPer parameterTotal
FP162 bytes≈ 14 GB
INT81 byte≈ 7 GB
4-bit0.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