In this post I will deep dive into two things that sit behind almost every AI model, as in, first - why do we use gpus to run AI models and second - how do we fit these huge models into GPU's limited memory.
Why do we need GPU instead of CPU to run AI models?
Its latency vs. High throughput. CPU can finish few jobs as fast as possible with its few number of cores with low latency while GPUs are mean to finish a huge number of similar jobs at the same time which is called high throughput with thousands of tiny cores.

Neural networks need matrix multiplication which is what GPUs are good at because GPU's have thousands of cores which work at the same time computing millions of multiply and add steps.

GPU has its own memory called VRAM
CPU uses system RAM while GPU uses Video RAM which sits with the graphics card and capacity of VRAM, like, 8 or 24 or 80 GB decides whether the model fits.
CPU and GPU work together
During training CPU keeps reading each batch of data and sends the processed data to GPU, and GPU does matrix arithmetic.

If you look at step 3 above, CPU is usually slow in getting the data ready then GPU finishes and waits for the next batch of data which means that the expensive GPU is actually sitting idle because the batch of data did not arrive in time, which is a common problem.
Training also needs a lot of memory, much more than inference because the network has to hold weights, gradients, optimized states, and activations compared to Inference which just need to hold weights and KV cache(in case of LLMs).

During inference, we have the weights and KV cache(in case of LLM). KV Cache can help to avoid dedoing repetitive computations at every step. This cache can grow longer over time.

Quantization:
Every weight inside a neural network is just a number, something like 2.4 or -4.8. By default, these numbers are stored as FP32, which is a 32-bit floating-point number. These 32 bits are not random. They are split into three parts, and each part has its own job.

The computation is stored using below formula
Where
- S = Tells is a number is positive or negative
- Exponent = Size, as in, how big or small it is
- Matissa = Holds actual digits of the number
The point of this is - While FP32 is very precise and flexible, precision computation is epensive because in the neural network every weight has to carry the full 32 bits of this sign-exponent-mantissa.
But why does this precision become a problem?
FP32 needs 4 bytes of memory for every single number, which is a parameter. When we multiply that by billions, it increases the memory required to hold the parameters.
We have not even added the optimizer, the activations, or anything else the model needs while running. So fitting a big model on a small, cheap GPU becomes really hard, which is why so many strong models need expensive data-centre.
FP32 to INT8
The main idea of quantization is simple. We take each 4-byte FP32 weight and turn it into a 1-byte INT8 number. So here, basically we swap a complex number for a simple one.

An INT8 has no sign, no exponent, and no mantissa. It is just a plain whole number from −128 to 127.
So for example, for a 7B model, we reduced the size of the parameters from 28 GB to 7 GB.
Affine quantization is the most common way to turn FP32 numbers into smaller INT8 numbers.
Here we use two simple tools called
- Scale: How much real value each tiny integer step is worth - basially the exchange rate between your real number and the small integer.
- Offset: Shift that divides where real zero lands on the integer ruler, so numbers dont waste half the range.
Scale:
Offset:
How to Dequantize the Quantized Number:
Basiaclly, to quantize a number, we divide the real number by the scale, add the zero-point, round it to the nearest whole number, and then clamp it so that it stays inside the INT8 range. To de-quantize it later, which means to get the number back, we just do the reverse, which is subtract the zero-point and multiply by the scale.
Example:
Consider this example tensor:
Compute the scale:
- Max absolute value. |x|max = 4.8.
- Float range. 4.8 − (−4.8) = 9.6.
- Integer range. 127 − (−128) = 255.
Scale:
Quantize:
De Quantize:
Notice that we did not get 2.4 back exactly, we got 2.4094. That small gap is called the quantization error, and it is basically the price we pay for making things smaller.
We can use the below numpy code to calculate the scale, quantized value, and dequantized value:
import numpy as np
def quantize(x, qmin=-128, qmax=127):
x = np.array(x, dtype=np.float32)
max_abs = np.max(np.abs(x))
scale = max_abs / max(abs(qmin), abs(qmax))
zero_point = 0
q = np.clip(
np.round(x / scale + zero_point),
qmin,
qmax
).astype(np.int8)
deq = scale * (q.astype(np.float32) - zero_point)
err = deq - x
return scale, q, deq, err
x = [2.4, -4.8, 1.5, 0.6, -2.7, 4.1]
scale, q, deq, err = quantize(x)
print("Original FP32:", x)
print("Scale:", scale)
print("Quantized INT8:", q)
print("Dequantized FP32:", deq)
print("Quantization Error:", err)
On the Fly - De Quantization
We saved our weights as INT8 to save space, but the inputs flowing through the model are usually FP16. So the GPU is asked to multiply something like this:
So we use on the fly de quantization where VRAM keeps the small INT8 weights along with their scale, and finally we can convert back to float at the foma; step inside the GPU core itself, just before do the multiply operation, using below formula:

Outlier Problem
Outliers are a common problem.
Example:
When we have an outlier, it impacts our scale, offset, quantized and dequantized value. For example:
| Original | ÷ scale (62.75) | round → INT8 | De-quantized |
|---|---|---|---|
| 2.4 | 0.038 | 0 | 0.00 |
| −4.8 | −0.076 | 0 | 0.00 |
| 1.5 | 0.024 | 0 | 0.00 |
| 0.6 | 0.010 | 0 | 0.00 |
| −2.7 | −0.043 | 0 | 0.00 |
| 8000.0 | 127.5 | 127 | 7968.6 |
As you can see in the above table, most of the non outlier values turned to 0 when we quantized and dequantized.
Granularity
To solve this outlier problem, we use granularity. There are three types in granularity based quantization.
- Per Tensor Quantization:
Here we use one scale for the whole matrix but even a single outlier can spoil quantization and dequantization.
- Per Channel quantization
Instead of one scale for the whole matrix, we use a separate scale for each row, which is each output neuron. Now an outlier only harms the row it is present in and every other row calculates its own scale and stays accurate.

- Group wise quantization
This is the standard for 4-bit models. We take each row and further split it into even smaller blocks — usually 64 or 128 weights — and each block computes its own scale. In this case, since the outlier can be limited to a tiny block, the damage it does is limited to that block.
Model Size with various Quantization
| Model | FP32 (4 B) | FP16 (2 B) | INT8 (1 B) | INT4 (0.5 B) |
|---|---|---|---|---|
| 7B | 28 GB | 14 GB | 7 GB | 3.5 GB |
| 13B | 52 GB | 26 GB | 13 GB | 6.5 GB |
| 70B | 280 GB | 140 GB | 70 GB | 35 GB |
What happens to a 7B parameter model size with various quantization?
