LLM Compression Techniques
Large language models (LLMs) without quantization demand significant GPU memory, greatly increasing deployment costs. This overview covers model compression techniques for LLMs and explores the LLM serving engines that support these quantized methods.
Pruning
Pruning is removing connection from the neural network. We can remove the connections in two ways:
Structured Pruning: All the incoming and outgoing connections for the selected neurons are removed.
Unstructured Pruning: Connection are removed based on the selection logic. Some neuron can have all the existing connections and some neuron might not have any connection and any number in between.
Selection logic can be as simple as checking the magnitude of the weights and removing the weights with magnitude close to zero.
Performance of unstructured pruning is generally better than structured pruning. We can also do more aggressive pruning with unstructured pruning while keeping the model performance.
Sparse models will require less memory to host and certain level of pruning does not impact the test performance.
We need to implement sparse inference kernels to provide both memory reduction and acceleration of sparse models. This is why pruning is not that much popular in LLMs compared to quantization.
Distillation
Distillation generally involves using a larger pre-trained model’s embedding vector as target to train a smaller model.
Distilled models most of the time performs better than if we just train the model directly on the train data.
Quantization
Model weights are generally stored in float16 format. Quantization is converting them from high precision format (float32, float16) to low precision format (like int8, int4).
LLMs are generally over parameterised and quantization acts as regularization, thus model performance does not degrade drastically.
Quantization also makes sense for real world edge application (personal assistant), where LLMs are more memory bound than compute bound.
All the quantization techniques can be grouped into two categories.
- Quantization aware training: The model is trained during quantization process, thus these methods produces more accurate models.
- Post training quantization: The model is directly quantized without further training. These models generally less accurate than the models produced by quantization aware training but it require very less compute to create the models.
Because LLMs have large number of parameters, doing quantization aware training is generally expensive. But because they are over-parameterized even with post training quantization the performance drop is very minimal.
Linear Quantization/ RTN
A simple form of quantization is linear quantization or round to nearest quantization.
Here, we convert the values of a more precise format (top) to a less precise format (below).
We can define scale (s) between the two format as:
s = (r_max — r_min)/(q_max-q_min)
We can compute the r_max as:
r_max = s * (q_max — z) => z = q_max — (r_max/s)
So any higher precision point (r)can be represented as:
q = int(round(r/s + z))
Linear quantization does not work well for int8 and does not work at all in int4 for majority of the LLM use-cases.
GPTQ
Get Souvik Mandal’s stories in your inbox
Join Medium for free to get updates from this writer.
GPTQ is the first post training quantization method that gives very good performance even with int4 quantization.
GPTQ quantizes the weights layer by layer such that after quantization one a layer’s weight the change in the output is minimum compared to before.
Within the same layer, it uses Optimal Brain Quantization (OBQ) to approximate layer wise optimization to row wise optimization. With this, we can approximate the above as:
sum of the squared errors, over each row of W. Then, OBQ handles each row w independently, quantizing one weight at a time while always updating all not-yet-quantized weights, in order to compensate for the error incurred by quantizing a single weight.
Which weights to quantize first?
weight which currently incurs the least additional quantization error. while this quite natural strategy does indeed seem to perform very well, its improvement over quantizing the weights in arbitrary order is generally small, in particular on large, heavily-parametrized layers.
One problem with arbitrary order can be if there is outliers in the weights and we quantize then first for a row remaining weights will have to be update by a large value. We can update those at end to make sure this does not happens.
Activation-aware Weights Quantization (AWQ)
When we quantize with LLM weights with RTN method the model perforrmance decreases (perplexity increase, lower PPL is better). But if we only keep 0.1% of the weights in fp16 and quantize rest of the weights, the quantize model retains its performance.
How to select which weights to keep?
We can consider the weights with high magnitude as important, but they give almost similar performance if we select the weights randomly. But if the weights are selected based on how much their contribution to the activation value, the selected weights seem to give the best performance.
Now, keeping the wights in mixed precision will make the inference complicated because we will require optimized kernals for matrix multiplication and so on.
This is solved by multiplying the important weights with a multiplier. The value of the multiplier is selected by applying a range of values and selecting the one with the less difference compared to the original.
In the Image above, we multiply W with the multiplier (s). We then devide the input by the same multiplier (X/s) such that the overall output is not changed.
AWQ is better than GPTQ. It required less number of samples and the quantized model is less prone to overfit on the calibration dataset.
Applying AWQ quantization is very simple with AutoAWQ repo. AWQ quantization images are from here.
There are other quantization methods like GGUF, EXL2 which are mainly used to run LLMs on CPU (offloading)and GPU.
Some observations on LLM quantization:
- The larger the model, the less the difference between the quantized and base models.
- performance smaller model without quantization < Large model with quantization running on same infra.
- AWQ is generally recommended for mass serving engines.
Quantized LLMs are supported in most of the popular LLM serving engines: vLLM, Aphrodite and so on.
Resources
- https://neuralmagic.com/wp-content/uploads/2020/10/Pruning-Ebook-2.pdf
- LLM Compression Survey
- TensorGPT paper
- Sparse GPT
- GPTQ Paper
- AWQ Quantization, Paper
- Quantization methods overview
Connect with me
Feel free to drop me a message or
Have a nice day ❤️









