4 min read

vLLMs

5Ws

  1. open-source inference server/library and serving engine for llms
  2. developed by UC berkeley 2023
  3. its needed because KV cache memory blows up, if memory is handled naively, memory ends up being under-utilised, want high throughput but also low latency per request


Paged Attention

Problem: LLM performance is bottlenecked by memory, KV tensor cache is large, consumes 1.7GB for a single sequence and is dynamic, KV cache size depends on sequence length which is highly variable unpredictable. Existing systems waste 60-80% of memory due to fragmentation and over-reservation.

vLLM delivers up to 24x higher throughput than hugging face transformers. without model architecture changes. the idea is inspired by operating systems. we can store continuous keys and values in non-contiguous memory space (preventing fragmentation). paged attention partitions KV cache of each sequence into blocks.

because the blocks do not need to be in contiguous space, we can arrange the pages accordingly. Lets rename blocks and pages, tokens as bytes and sequences as processes.

since we add each token’s KV value into blocks of x size, we can allocate more memory at runtime

another advantage is: efficient memory sharing, many processes (sequences) can share the same physical blocks from their own logical blocks.

pagedAttention also reduces memory overhead of sampling algorithms cutting thier memory usage by 55%.

Whats copy on write mechanism

each physical block has a reference count showing how many logical blocks are pointing to it.

cow mechanism saves memory via shared prefixes, prompt parts.

  1. two sequences A & B share prompt “Hello world”. that prompts KV resides in physical block P0, refcount=2.
  2. A and B generate next token, A wants to generate “!” B generates something else.
  3. since A is about to write to P0 (which is shared) we cant let it write in place
  4. system performs cow, copy P0 → new block P1, A’s block table now points to P, A writes to P1, B still points to P0.


LLM engine

components:

  1. model executor: drives forward passes on the model, has configurations for n GPUs
  2. structured output manager
  3. scheduler, decides which requests go into the next engine step, contains:
     1. policy setting - can be either FCFS or priority
     2. waiting and running queries
     3. KV cache manager for paged attention


KV cache manager maintains a free_block_queue a pool of available KV cache blocks, during paged attention these blocks serve as indexing structure that map tokens to relevent KV cache blocks.


Initialization process

during model execution worker obj is created and three procedures are executed, these same procedures run independently on each worker process across different GPUs

  1. assign cuda device to a worker
  2. check if enough vram is available
  3. instantiate an internal model_runner object which holds sampler, KV cache, forward-pass buffers
  4. instantiate an inputbatch obj (holds CPU side forward-pass buffers, block tables for KV cache indexing, sampling metadata)
  5. instantiate model architecture
  6. load model weights
  7. call model.eval()
  8. initialize KV cache spec
  9. run a dummy/profiling forward pass and take a GPU memory snapshot to compute how many KV cache blocks fit in available VRAM
  10. allocate, reshape and bind KV cache tensors to attention layers


synchronous Generate function

  1. create a unique ID and capture its arrival time
  2. call an input preprocessor that tokenizes the prompt and returns a dict containing prompt, and embeddings
  3. pack this info into an engine core request, adding priority, sampling rate and other metadata
  4. pass the request into engine core, which wraps request into an object, sets its status to waiting. this request is then added to scheduler’s waiting queue
  5. as long as there are requests to process, engine repeatedly calls its step() function to process requests synchronously


Step function

each step function has three stages

  1. schedule: select which requests to decode, chunked prefill in this step
  2. run the model and sample tokens
  3. append sampled token ids to each request, detokenize and check stop conditions
  4. once a request is finished, return its KV cache blocks to free_block_queue


Scheduler

two main types of workloads an inference engine handles

  1. prefill requests - a forward pass over all prompt tokens, model computes internal representations and builds teh KV cache for all input tokens. since youre processing all tokens at once, you can leverage parallel computation heavily. this stage is compute bound, at the end of this stage the model samples or selects the first output tokens
  2. decode requests - after prefill, you generate subsequent tokens one at a time, for each new token, you feed in the latest token, not the entire prompt again, KV is cached, decode is constrained by memory bandwidth (read/write) decode is like it computes incremental updates.

V0 cant decode or prefill at the same time, but V1 engine can do

wfewf dsvsd