ENع
→Deployment
الدرس 24 · النماذج اللغوية الكبيرة عمليًا

Running models in production

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

A trained model is only useful once it answers real requests, quickly and cheaply. Follow one prompt through an inference server: the weights waiting in GPU memory, prefill and decode, the KV cache that grows with every token, and why memory, not maths, sets the speed. Then see the three tricks every serving system uses: batching, paging the cache, and quantization.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 24 · تفاعلي
01 / 10

A request arrives

Running a model in production means serving it. An inference server waits for requests. Your app sends a prompt over HTTP, "Write a haiku about the sea", and the server queues it until the GPU, the chip that runs the model, is free.

POST /v1/completions        (HTTP request)
  prompt      "Write a haiku about the sea"
  max_tokens  64

the server queues it until the GPU can take it
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A request arrives

    Running a model in production means serving it. An inference server waits for requests. Your app sends a prompt over HTTP, "Write a haiku about the sea", and the server queues it until the GPU, the chip that runs the model, is free.

    POST /v1/completions        (HTTP request)
      prompt      "Write a haiku about the sea"
      max_tokens  64
    
    the server queues it until the GPU can take it
  2. Weights live on the GPU

    Before any request, the server loads the weights, the model's learned numbers, into GPU memory. Our example: 13 billion weights at 2 bytes each (16-bit numbers) is 26 GB, about two thirds of a 40 GB GPU.

    model   OPT-13B: 13 billion weights
    GPU     A100 40GB
    w16 = 13e9 * 2 / GB
          -> 26.0 GB   (65% of 40 GB)
    loaded once at start-up, shared by every request
  3. Prefill: read the prompt

    First comes prefill. All the prompt's tokens go through the model in one pass, side by side. Every layer saves each token's key and value vectors, what attention looks up later, in the KV cache. Out comes the first new token.

    prompt  6 tokens, processed together
    each layer, each token -> one key + one value
    stored in the KV cache (blocks of 4 tokens here)
      block 0  A:4     block 1  A:2
    -> first new token "Grey"
  4. Decode: one token per step

    Then decode: one new token per step. The newest token goes in, its key and value join the cache, and attention reads all the cached ones instead of recomputing them. The cache grows by one token every step.

    step 1  feed "Grey", add its key+value   A:3
            attend over 7 cached tokens -> "waves"
    step 2  feed "waves", add its key+value  A:4
            attend over 8 cached tokens -> "fold"
    without the cache, every step would redo the prompt
  5. The cache is big

    Each token stores a key and a value in each of 40 layers, 5,120 numbers each, 2 bytes per number: 819,200 bytes, about 0.8 MB. A full 2,048-token request needs about 1.7 GB of cache.

    kv = 2 * 40 * 5120 * 2
         key+value x layers x numbers x bytes
       -> 819,200 bytes per token
    req = kv * 2048 / GB
       -> 1.68 GB for one full-length request
  6. Decode waits on memory

    Each decode step reads all 26 GB of weights from memory. This GPU reads 1,555 GB per second, so a step takes at least 17 ms: at most about 60 tokens a second for one request. Memory, not arithmetic, is the limit.

    bandwidth   1,555 GB/s   (A100 40GB datasheet)
    ms = w16 / 1555 * 1000
       -> 16.7 ms per step, at least
       -> at most ~60 tokens/s for a single request
    the GPU could do far more maths in that time
  7. Batching shares the read

    So servers batch: many requests run through the same step, and one read of the weights serves them all. With continuous batching, a new request joins at the very next step instead of waiting for the whole batch to finish.

    request B arrives: 3-token prompt   block 2  B:3
    next step serves A and B with one weight read
      B -> block 2  B:4
      A -> new block 3  A:1
    continuous batching: join or leave at any step
  8. Paging the cache

    Older servers reserved cache for the longest possible answer up front, and only 20 to 38% of it held real tokens. vLLM's PagedAttention hands out small blocks only when needed, anywhere in memory, and raises throughput 2 to 4 times.

    request A  blocks 0, 1, 3      (not side by side)
    request B  blocks 2, 4
    a block is taken only when the last one is full
    waste: at most part of one block per request
    more requests fit -> bigger batches
  9. Smaller weights

    Quantization stores each weight in fewer bits. At 4 bits our 26 GB of weights shrinks to 6.5 GB, leaving far more room for cache, and so for bigger batches. Methods such as GPTQ keep the accuracy loss small; always measure it.

    bits per weight   16      8      4
    weights (GB)      26.0    13.0    6.5
    w4 = 13e9 * 0.5 / GB
       -> 6.5 GB  (plus a little for scale factors)
    fewer bytes to read also speeds up each step
  10. Stream the answer

    Each new token is sent to your app the moment it is made, so text appears while the model is still writing. Time to first token is the queue plus prefill; after that, each token takes one decode step.

    data: {"token": "Grey"}      (server-sent events)
    data: {"token": "waves"}
    data: {"token": "fold"}
    time to first token = queue + prefill
    time per token      = one decode step

اختبار يتاح بعد الخطوة الأخيرة.