Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Generative AI on Kubernetes

  • The most common use case for running AI on kubernetes is inference.
  • HuggingFace is the largest community where you can find models, datasets, and libraries.

Transformer Architecture

  • The transformer architecture describes an encoding phase and decoding phase.This has been used to create three different classes of encoder models: encoder only models, encoder-decoder models, and decoder-only models.
  • Encoder models are popular for learning embeddings used in classification tasks
    • Examples includes Google’s BERT and Meta’s RoBERTa
  • Encoder-decoder models were originally designed for generative tasks like translation or summarization, where input and output are strongly connected.
    • Google’s Flat-T5 is one example of a encoder-decoder model
  • Decoder-only models are used for generative tasks, particurly conversational AI and text generation
    • GPT-x are all examples of decoder-only models
  • Modern LLMs are predominantly based on Transformer Architecture or a derivitave
  • The main advantage of Transformer Architecture (over others like recurrrent neural networks (RNNs)), is that it doesn’t have recurring units. This means it doesn’t use the output of one neuron as the input to another. This makes it highly parallelizable during training.

Tokenizer Implementation

  • A tokenizer is a tool that takes a sentence as input and returns a list of tokens as output.
  • A token is usually a subword and is language specific
  • Each token has an integer representation, so it is possible to convert the full sentence to a sequence of numbers
  • The most commonly used tokenizer is the HuggingFace tokenizer library
  • The general rule of thumb is to consider four characters in English as one token

Embedding

  • After we break the input down into tokens, we are ready for the embedding phase
  • Embedding is a process that generates a vector representation of the input, capturing its semantic meaning
    • This means the distance between two vectors is smaller if they represent semantically similar inputs, and larger if the inputs are not strongly related
    • As an example, consider the words dog and puppy. They are related to each other, so their embedding representations produce vectors with a smaller distance compared to the embeddings dog and car.

The Two Phases of Inference

  • From a high level, the end-to-end inference pipeline has two steps: prefill and decode.
  • The prefill phase tokenizes the input, applies embedding, and generates the first token.
  • The decode phase generates the tokens one by one and computes the output text.
  • Both phases use the model in the same way to produce a token. However, the decoding phase produces on token at a time, while the prefill phase is done in parallel. This makes the prefill workload compute-bound, while the decode phase is memory-bound.

Prefill

  • The prefill phase works as an initializatoin phase where the input prompt is processed and the first token is produced
  • The prefil hpase has two operational characteristics worth noting: it is compute-bound and the tokenizer runs entirely on CPU.
  • The prefil phase directly affects the time to first token (TTFT)

Decode

  • After prefill, the user’s prompt is parsed, loaded, and the first token is produced with a signle forward pass of the neural net.
  • The decode phase is in charge of the generation of the rest of the tokens until the end-of-stream is encountered.
  • The decode phase cannot be parallelized and it has to proceed one token at a time because of the autoregressive nature of generation. Autoregressive means that each generated token is based on the previous sequence and becomes part of th eprevious state used to generate the next one.

Model Servers

  • In general, running a model requires two components: the model and a model server
  • A model server is a component that includes one or more runtimes
    • vLLM and HuggingFace TGI are two examples
  • Model Server Controllers are typically used when hosting models in Kubernetes to manage the CRDs associated with the model server and its resources