Build a toy LLM server on CPU
CPU · your Macscript120 minrequiredGoal · Serve Qwen2.5-0.5B-Instruct behind a FastAPI /v1/chat/completions endpoint with token streaming, written by you.
- 1pip install torch transformers fastapi uvicorn sse-starlette; load Qwen/Qwen2.5-0.5B-Instruct in bfloat16 (or float32 on CPU).
- 2Implement a POST /v1/chat/completions handler that applies the chat template with tokenizer.apply_chat_template.
- 3Write a manual decode loop (no model.generate) that keeps past_key_values and yields one token per step.
- 4Stream tokens as server-sent events in the OpenAI chunk format; detokenize incrementally.
- 5Add timing logs for tokenization, prefill, each decode step and detokenization.
- 6Call it with the openai Python client pointed at base_url http://localhost:8000/v1 and confirm streaming works.
- 7Print a per-stage time breakdown for a 50-token prompt and 50-token answer.
- Deliverable
- server.py plus a markdown table of per-stage timings for one request.
- Check
- The openai client receives a streamed response; prefill time is a single number while decode shows about 50 roughly equal step times.