Skip to main content

1. Introduction

Model compression is the process of reducing the size, memory usage, and computational cost of a machine learning model while maintaining acceptable prediction performance. The two important model compression techniques are:

Main goals

  • Reduce model size
  • Reduce memory usage
  • Improve inference speed
  • Reduce deployment cost
  • Make models suitable for edge/mobile devices
  • Maintain acceptable accuracy

2. Quantization

Quantization converts model parameters and/or computations from a higher numerical precision to a lower precision. A common conversion is:
Where:
  • FP32 = 32-bit floating point
  • INT8 = 8-bit integer

3. FP32

Most PyTorch models initially use float32 values. For example:
Each FP32 value requires:
Therefore, a model with many FP32 parameters requires more memory.

4. INT8

INT8 uses 8 bits to represent values.
Therefore:
The theoretical storage requirement for the quantized weights can therefore be approximately one-quarter of FP32 weight storage. Actual model size depends on the model and quantization implementation.

5. Why Quantization?

Consider a model with:
FP32 storage:
INT8 storage:
Conceptually:
This can significantly reduce memory requirements.

6. Post-Training Quantization

Post-training quantization (PTQ) applies quantization after the model has already been trained.
The basic advantage is that the original model does not need to be trained again for the simplest PTQ approaches.

7. Dynamic Quantization

Dynamic quantization is a simple form of post-training quantization. In PyTorch:
can be used to quantize supported layers. For example:
This is especially useful for learning and for CPU inference scenarios involving supported layers.

8. Simple Quantization Example

quantization_demo.py

Output:

9. Understanding the Example

Step 1 - Create model

This creates a simple fully connected layer.

Step 2 - Set evaluation mode

The model is placed in evaluation mode because the model is being used for inference rather than training.

Step 3 - Quantize the model

The important part is:
This specifies 8-bit quantization for the supported layer. The model changes conceptually from:
to:

Step 4 - Create input

This creates one sample containing 10 input values. Example:

Step 5 - Generate original prediction

The FP32 model produces an output.

Step 6 - Generate quantized prediction

The quantized model processes the same input. The outputs may be close but not necessarily identical because quantization introduces numerical approximation.

10. Important Observation

Quantization does not mean:
Actual quantization uses a mapping between floating-point values and a lower-precision representation. A simplified concept is:
The quantized model keeps additional information such as scaling parameters to approximately reconstruct the required numerical range.

11. Quantization Trade-off

Quantization provides benefits, but there can be a trade-off.
Whereas:
Therefore:
The objective is to find an acceptable balance.

12. Pruning

Pruning is another model compression technique. Instead of reducing the precision of parameters, pruning removes or disables parameters that contribute relatively little to the model. Example:
The small weights have been removed or set to zero.

13. Types of Pruning

Unstructured Pruning

Individual weights are removed.
This creates sparse weights.

Structured Pruning

Entire structures are removed. Examples:
  • Neurons
  • Channels
  • Filters
  • Attention heads
Example:
Structured pruning can produce a physically smaller architecture and can therefore be easier to exploit for actual inference speedups.

14. Quantization vs Pruning


15. Quantization + Pruning

Both techniques can be combined.
This can provide greater compression than using only one technique.

16. Model Compression Workflow

The important point is that compression should be measured, not assumed to be beneficial.

17. Size Comparison

For a compression experiment:
Example: The exact values depend on the model, hardware, runtime, and quantization method.

18. Measuring Model Size

A simple way to measure a saved model:
For a fair comparison:
can be saved and their file sizes compared.

19. Measuring Inference Latency

A simple latency measurement:
Running multiple iterations gives a more useful average than measuring a single inference.

20. Simple Coding Challenge

File

Objective

Create a small PyTorch model and compare:
Measure:
  1. Model size
  2. Inference latency
  3. Output values

Expected workflow


21. What Should Be Learned From This Challenge?

The important concepts are:

Quantization

Reduce numerical precision.

Pruning

Reduce model complexity.

Compression objective


22. MLOps Connection

Model compression fits into the deployment part of an MLOps pipeline:
For example:

23. Key Terms


24. Final Summary

One-line learning

Quantization reduces the precision of model parameters, while pruning removes unnecessary parameters or structures to make ML models smaller and more efficient.

For the coding

Start with:
then:
and compare the original FP32 model and quantized INT8 model in terms of size, latency, and output.