Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Batch Inference

In the previous chapter, we used the KV cache to speed up the generation of the GPT model. In this chapter, we explore another angle for improving generation efficiency: parallelism.

In real production environments, a GPT model often has to handle multiple generation requests at the same time. For example, a chatbot may have 100 users asking questions online at once. If they are processed one by one, users have to wait in line; if they can be processed in parallel as a batch, all requests can be computed at once, which greatly increases throughput.

Batch inference

The core idea of batch inference: pack multiple requests into one batch and complete the computation of all requests in a single forward pass.

How it works

  • Padding: requests have prompts of different lengths, so shorter sequences are filled with a special token (for example 0) up to the longest prompt length in the batch;

  • Masking: the padded positions have no real meaning, so a mask is needed to mark which positions are valid tokens and which are padding.

Model

GPT batch embedding layer

The GPT batch embedding layer (GPTEmbeddingWithPadding) needs to support custom position encoding: the positions of each sequence are encoded independently and are not affected by padding.

GPT batch attention layer

The GPT batch attention layer (GPTAttentionWithPadding) needs to handle the padding mask:

  • after computing the attention scores, set the scores at padded positions to a very large negative number (-109);

  • after the Softmax, the weights at padded positions are 0 and do not affect valid tokens.

GPT batch Transformer block

The GPT batch Transformer block (GPTTransformerWithPadding) passes the mask parameter along, making sure padded positions do not take part in the computation.

GPT batch model

The GPT batch model (GPTWithPadding) receives the mask and the position encodings and passes them on layer by layer.

GPT batch trainer

The generate function of the GPT batch trainer GPTBatchTrainer:

  • Batch input: process multiple prompts at the same time;

  • Dynamic padding: before each generation step, pad the current sequences to the longest length in the batch;

  • Independent sampling: sample the next token independently for each sequence.

Hyperparameters

Model parameters

The model parameters must stay consistent with the pre-training stage.

Modeling

Loading the model

Batch prompts

Serial inference

ROMEO:
When I pray.

CAMILLO:
In my liest, I have done drunk.

CAMILLO:
Ay, since the speak, my doubt, fainners!
I am too, as to a kind in honourity,
Can a very disentent till take from here other
out, strike than 'Which children choistones
That would how have m
JULIET:
Sir, I will contracted up by the Gaunt,
And provost: ha? the desires of her babel,
Lest that love there of whose back high and stain: he he,
Where he I cannot die. The misfaction,
What, how, have unide a private way
met my noble--Surpose, let, her but, le
HAMLET:
We have hoped fathis professed of sorrow.

CAPULET:
Repress too victrate affair honour dead!

First Citizen:
I pray thou art field at the misicians win,
Be gentlewomen, and perform and concealunt stood
ass to as thine princes; for as from there to
Shool. 
MACBETH:
Well, believe me; could never will good awake.

HASTINGS:
I'll cannot passlight the thanking of recovered
By Marcius nearer.

CLIFFORD:
What parter will us I more, if Hold answer;
Not we make of deserves dangerous are innocent
Whose times do your could tr
18.98765492 seconds

Batch inference

ROMEO:
Confess in to head; but I'll sea out
But all his otcher's eyes; it with their
flow; for I warrant then?

BRUTUS:
'IV Marcius, will I;'tis all-day:
Come, I part for me a sword,
But of it with hemies with my reting and prevail
If greet her laid, but well.


JULIET:
But I am lies bitter more and like a lost.

CLAUDIO:
O, my true, offence discretion.

JULIET:
Romeo, I'll canst upon. O, uncle, they do shall not
I cannot find upon; and seeing it them to me,
In to old unaviertain'd with clock'd.

ESCALUS:
I did thee, I g
HAMLET:
A blessing will steed tears, commands;
One would be so wonding of this acried:
So I bleem want full forget and that From
Offeriest! my cousin's hand be to my father,
And rebel, call me in this repeal'st;
For shake me Paduce consul,
Being being and with us
MACBETH:
Woo's committed for the life,
Where child belly to this knees man.

QUEEN MARGARET:
Let me King Henry, sir.

GREY:
With complimant your son is.

DUCHESS OF YORK:
And would not still the voices of supproof,
Minive I can a that death.

QUEEN ELIZABETH:
Your
19.44414520 seconds

In this chapter, we implemented batch inference: handle multiple generation requests at the same time, align the sequences with padding and masking, and complete all the computation in one forward pass.

But in the inference above we can see that batch inference did not show an efficiency gain (about 16.53 seconds serial versus about 16.50 seconds batched). There are two main reasons:

  • The total computation did not decrease: we did not use the KV cache, so every step recomputes the whole window. Batching only merges several requests into larger matrix multiplications, and the total computation is exactly the same as serial;

  • There is no idle compute to exploit: NumPy’s matrix multiplication is itself multi-threaded, and a single request can already saturate the CPU. On devices with a GPU, the benefit of batch inference would be much more significant.

With this, we have completed our discussion of model inference and tried to address the two core problems of controlling generation and improving efficiency:

  • Sampling strategies: control the quality and diversity of generation;

  • Stopping criteria: control when generation terminates;

  • KV cache: speeds up autoregressive generation;

  • Batch inference: raises the throughput of the model.

Exercises

  1. Complete a comprehensive exercise: integrate sampling strategies, stopping criteria, the KV cache, and batch inference together.