|
| 1 | +--- |
| 2 | +title: Serve high throughput inference with vLLM |
| 3 | +weight: 4 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## About batch sizing in vLLM |
| 10 | + |
| 11 | +vLLM enforces two limits to balance memory use and throughput: a per‑sequence length (`max_model_len`) and a per‑batch token limit (`max_num_batched_tokens`). No single request can exceed the sequence limit, and the sum of tokens in a batch must stay within the batch limit. |
| 12 | + |
| 13 | +## Serve an OpenAI‑compatible API |
| 14 | + |
| 15 | +Start the server with sensible CPU default parameters and a quantized model: |
| 16 | + |
| 17 | +```bash |
| 18 | +export VLLM_TARGET_DEVICE=cpu |
| 19 | +export VLLM_CPU_KVCACHE_SPACE=32 |
| 20 | +export VLLM_CPU_OMP_THREADS_BIND="0-$(($(nproc)-1))" |
| 21 | +export VLLM_MLA_DISABLE=1 |
| 22 | +export ONEDNN_DEFAULT_FPMATH_MODE=BF16 |
| 23 | +export OMP_NUM_THREADS="$(nproc)" |
| 24 | +export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libtcmalloc_minimal.so.4 |
| 25 | + |
| 26 | +vllm serve DeepSeek-V2-Lite-w4a8dyn-mse-channelwise \ |
| 27 | + --dtype float32 --max-model-len 4096 --max-num-batched-tokens 4096 |
| 28 | +``` |
| 29 | + |
| 30 | +## Run multi‑request batch |
| 31 | + |
| 32 | +After confirming a single request works explained in previous example, simulate concurrent load with a small OpenAI API compatible client. Save this as `batch_test.py`: |
| 33 | + |
| 34 | +```python |
| 35 | +import asyncio |
| 36 | +import time |
| 37 | +from openai import AsyncOpenAI |
| 38 | + |
| 39 | +# vLLM's OpenAI-compatible server |
| 40 | +client = AsyncOpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY") |
| 41 | + |
| 42 | +model = "DeepSeek-V2-Lite-w4a8dyn-mse-channelwise" # vllm server model |
| 43 | + |
| 44 | +# Batch of 8 prompts |
| 45 | +messages_list = [ |
| 46 | + [{"role": "user", "content": "Explain Big O notation with two examples."}], |
| 47 | + [{"role": "user", "content": "Show a simple recursive function and explain how it works."}], |
| 48 | + [{"role": "user", "content": "Draft a polite email requesting a project deadline extension."}], |
| 49 | + [{"role": "user", "content": "Explain what a hash function is and common uses."}], |
| 50 | + [{"role": "user", "content": "Explain binary search and its time complexity."}], |
| 51 | + [{"role": "user", "content": "Write a Python function that checks if a string is a palindrome."}], |
| 52 | + [{"role": "user", "content": "Explain how caching improves performance with a simple analogy."}], |
| 53 | + [{"role": "user", "content": "Explain the difference between supervised and unsupervised learning."}], |
| 54 | +] |
| 55 | + |
| 56 | +CONCURRENCY = 8 |
| 57 | + |
| 58 | +async def run_one(i: int, messages): |
| 59 | + resp = await client.chat.completions.create( |
| 60 | + model=model, |
| 61 | + messages=messages, |
| 62 | + max_tokens=128, # Change as per comfiguration |
| 63 | + ) |
| 64 | + return i, resp.choices[0].message.content |
| 65 | + |
| 66 | +async def main(): |
| 67 | + t0 = time.time() |
| 68 | + sem = asyncio.Semaphore(CONCURRENCY) |
| 69 | + |
| 70 | + async def guarded_run(i, msgs): |
| 71 | + async with sem: |
| 72 | + try: |
| 73 | + return await run_one(i, msgs) |
| 74 | + except Exception as e: |
| 75 | + return i, f"[ERROR] {type(e).__name__}: {e}" |
| 76 | + |
| 77 | + tasks = [asyncio.create_task(guarded_run(i, msgs)) for i, msgs in enumerate(messages_list, start=1)] |
| 78 | + results = await asyncio.gather(*tasks) # order corresponds to tasks list |
| 79 | + |
| 80 | + # Print outputs in input order |
| 81 | + results.sort(key=lambda x: x[0]) |
| 82 | + for idx, out in results: |
| 83 | + print(f"\n=== Output {idx} ===\n{out}\n") |
| 84 | + |
| 85 | + print(f"Batch completed in : {time.time() - t0:.2f}s") |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + asyncio.run(main()) |
| 89 | +``` |
| 90 | + |
| 91 | +Run 8 concurrent requests against your server: |
| 92 | + |
| 93 | +```bash |
| 94 | +python3 batch_test.py |
| 95 | +``` |
| 96 | + |
| 97 | +This validates multi‑request behavior and shows aggregate throughput in the server logs. |
| 98 | + |
| 99 | +```output |
| 100 | +(APIServer pid=4474) INFO 11-10 01:00:56 [loggers.py:221] Engine 000: Avg prompt throughput: 19.7 tokens/s, Avg generation throughput: 187.2 tokens/s, Running: 8 reqs, Waiting: 0 reqs, GPU KV cache usage: 1.6%, Prefix cache hit rate: 0.0% |
| 101 | +(APIServer pid=4474) INFO: 127.0.0.1:44060 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 102 | +(APIServer pid=4474) INFO: 127.0.0.1:44134 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 103 | +(APIServer pid=4474) INFO: 127.0.0.1:44076 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 104 | +(APIServer pid=4474) INFO: 127.0.0.1:44068 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 105 | +(APIServer pid=4474) INFO: 127.0.0.1:44100 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 106 | +(APIServer pid=4474) INFO: 127.0.0.1:44112 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 107 | +(APIServer pid=4474) INFO: 127.0.0.1:44090 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 108 | +(APIServer pid=4474) INFO: 127.0.0.1:44120 - "POST /v1/chat/completions HTTP/1.1" 200 OK |
| 109 | +(APIServer pid=4474) INFO 11-10 01:01:06 [loggers.py:221] Engine 000: Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 57.5 tokens/s, Running: 0 reqs, Waiting: 0 reqs, GPU KV cache usage: 0.0%, Prefix cache hit rate: 0.0% |
| 110 | +``` |
| 111 | +## Optional: Serving BF16 non-quantized model |
| 112 | + |
| 113 | +For a BF16 path on Arm, vLLM is acclerated by direct oneDNN integration in vLLM which allows aarch64 model to be hyperoptimized. |
| 114 | + |
| 115 | +```bash |
| 116 | +vllm serve deepseek-ai/DeepSeek-V2-Lite \ |
| 117 | + --dtype bfloat16 --max-model-len 4096 \ |
| 118 | + --max-num-batched-tokens 4096 |
| 119 | +``` |
| 120 | + |
| 121 | +## Go Beyond: Power Up Your vLLM Workflow |
| 122 | +Now that you’ve successfully quantized and served a model using vLLM on Arm, here are some further ways to explore: |
| 123 | + |
| 124 | +* **Try different models:** Apply the same steps to other [Hugging Face models](https://huggingface.co/models) like Llama, Qwen or Gemma. |
| 125 | + |
| 126 | +* **Connect a chat client:** Link your server with OpenAI-compatible UIs like [Open WebUI](https://github.com/open-webui/open-webui) |
0 commit comments