Getting Started with Inferencing
Between hitting enter in an AI chat and getting the response streamed, a new type of tech economy is brewing. The whole journey from a user provided input message to multiplacation at hardware level can be termed as Inferencing
The following paragraph tries to peel the layers of what happens during inferencing.
So let’s start with the a very simple example of a text.
You send the text “The cow jumped over the moon” to the model. A model will try to predict the next token in the sequence. So the prediction will look something like this
- The cow jumped over the moon. -> and
- The cow jumped over the moon and -> the
- The cow jumped over the moon and the -> little
- The cow jumped over the moon and the little -> boy
- The cow jumped over the moon and the little boy -> laughed
- The cow jumped over the moon and the little boy laughed -> to
- The cow jumped over the moon and the little boy laughed to -> see
- The cow jumped over the moon and the little boy laughed to see -> such
And the final output is:
The cow jumped over the moon. and the little boy laughed to see such fun
So, the model predicted the next words(tokens) one by one and added to the sentence until the final output.
At each step, the tokens were processed by the model, computations perfomed. The whole process where the tokens (in this case a word) are predicted by a model running on a GPU is termed as Inferencing.
But, that’s just a high-level overview.
Technically, something like the below is going on behind the inferencing.
"What is the capital of France?"
│
▼
┌─────────────────┐
│ Tokenizer │ text ──► token IDs [3923, 374, 279, 6864, 315, 9822, 30]
└────────┬────────┘
▼
┌─────────────────┐
│ Embedding │ IDs ──► vectors (seq_len × d_model)
└────────┬────────┘
▼
┌─────────────────┐
│ N × Transformer│ self-attention + MLP, repeated N times
│ blocks │ (writes K/V for each token into the KV cache)
└────────┬────────┘
▼
┌─────────────────┐
│ LM head │ last hidden state ──► logits over vocabulary (~32k–256k)
└────────┬────────┘
▼
┌─────────────────┐
│ Sampler │ logits ──► probabilities ──► pick ONE token ("Paris")
└────────┬────────┘
│
└──► append token to the sequence, feed it back in, repeat
until <eos>, a stop string, or max_tokens
The modern inferencing architecture looks something like this
-
A client (can be a chat or an agent) sends a text (prompt)
-
The server accepts and does the necessary pre-checks like authentication, authorization, rate limits etc.
- The pre-checks or pre processing can contain few extra steps too like applying a chat template. Different models uses different chat templates
- Tokenize: This is the first step, where the input is converted into tokens. Again depending on model vendor the tokenizer can be different
- The request is added to the queue
- Scheduler admits this request, allocates KV-cache blocks
- PREFILL: KV Cache is filled after one pass over the prompt
-
DECODE Loop: In this step, tokens are predicted one at a time per step/sequence until we reach the stop word. A stop workd can be either EOS (End of String), a stop string, max_tokens limits reached or a cancel request.
- A detokenizer sends the stream of tokens back to the client
-
The tokens are rendered as they arrive and can be processed further for post processing like tool calls etc.
The above process shows the important steps happening during the inferencing request.
But, that’s not just it, Inferencing is much more than processing of tokens, it involves hardware, the inferencing stack etc.
In several next posts about inferencing, I will look into what Inferencing stack looks like.