Sitemap
ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

LLM Compression Techniques

6 min readJun 2, 2024

--

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.

Press enter or click to view image in full size

Sparse models will require less memory to host and certain level of pruning does not impact the test performance.

Press enter or click to view image in full size
Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size
NVIDIA A100 GPU 80GB has 312TFLOPS. With low batch size we don’t use much of that compute. Even with 128 batch size we are not using all the compute. But for each token generation we will need to access 120 gb of memory.

All the quantization techniques can be grouped into two categories.

  1. Quantization aware training: The model is trained during quantization process, thus these methods produces more accurate models.
  2. 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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size
Source: GPTQ paper

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.

W is the full precision weight and W^ is the quantized weight. We find the W^ such a way that l2 difference of the output is minimum.

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

Resources

Connect with me

Feel free to drop me a message or

  1. Connect and reach out on LinkedIn
  2. Follow me on Medium or GitHub

Have a nice day ❤️

--

--

Souvik Mandal
Souvik Mandal

Written by Souvik Mandal

Senior AI Scientist @ Qure.ai, Ex Fractal, CSE IIT Indore, 20. LinkedIn: https://www.linkedin.com/in/mandalsouvik/

ITNEXT
ITNEXT

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.