When you hear “How does LLM work”, there are two things that you need to know. 1.model weights creation 2.Inference server
Model weights creation involve a big process where data is gathered, tokenized, trained. This video from Andrej Karpathy discusses this in much more bigger details.
Here we would be discussing about how inference servers work.
When you chat with LLM application, the chat comes to inference server. Inference server hosts the model weights you have in memory. based on what you send it performs the computation of next possible tokens in auto regressive way until you get an end token from the server.
But LLMs generate one token at a time, so generating a 100 token response is equal 100 forward passes through the server. so this is computation intensive process.
Also Model weights of better accurance models tend to be > 80Billion parameters, so if you have 16 bit default, it is 160GB of memory. In addition to this, we also need KV cache for each requent to send to user. A 128k context, will have 40GB KV Cache. This is lot of memory that too GPU memory. So this is memory intensive too.
To make it cost effective, we have only two choices
- Reduce model size
- Reduce the cost to host this infra.
Reduce model size (Model Optimization)
since LLMS are huge (>80GB as we discussed before), we can use 2 techiniques to compress it.
- Quantization
- Sparisification
Quantization
LLMs are released by default BF16 format which is 16 bit for each tensor values. Quantization converts this into lower bit format like FP8, INT8, INT4. this is applied to both weights and internal activations of the model.
BF16 -> INT8/FP8 saves around 50% in params size (GBs) BF16 to INT4/FP4 saves ~75%.
Sparsification
Model weights have lot of unnecessary values. This technique remoes typically 2 out of 4 values within model weight tensor which is set to 0.
Both these techniques reduce the memory we needed to save the models, thus improving latench and throughput of the servers.
But what about accuracy of the servers, are they not affected. Naive quantization, which is rounding every number down, does hurt the accuracy model. we can use Calibrated techniques like GPTQ, AWQ, helps to keep the accuracy to almost the same level as unquantized versions according to research.
Reduce cost to host the infra (Inference Optimization)
there are three techniques for this
- Continuous batching
- Paged Attention
- Prefix Caching
Continuous batching
Generation is iterative, so it is better to batch multiple request together, to reduce the latency.
But each request will have different context lenght. Having a static batching technique still leaves lot of unfilled GPU slots and thus wasting memory. Continuous batching fixes this. this schedules at token generation iteration level. when one finishes it does not wait for others in the batch to finish, a new request is replaced in the batch.
Paged Attention:
KV cache is dynamic and unknown lengths. This leads to lot of fragmentation in the memory. From our Operating Systems 101 class, we know that fragmentation is bad. So we used Pagination to fix that. Similar concept is ported inference servers.
Instead of storing KV cache as large fixed size block, it is stored as fixed-size blocks called pages. now for large requests, we can have multiple pages thus removing the fragmentation problem.
Prefix Caching:
typically we have same system prompt sent for multiple users. Multi turn conversations also have same entry passed again during conversation. By caching the KV cache of an existing query, a new query that shares the same prefix can skip recomputing that part of the prompt.