Optimize inference performance: add continuous-batching, NO_WARMUP support, improve thread/batch recommendations
Browse files
README.md
CHANGED
|
@@ -23,15 +23,17 @@ Set these in the Space settings:
|
|
| 23 |
|
| 24 |
Optional tuning variables:
|
| 25 |
|
| 26 |
-
- `CTX_SIZE`: context size, default `4096`
|
| 27 |
-
- `THREADS`: CPU thread count, default
|
| 28 |
-
- `THREADS_BATCH`: batch thread count, default matches
|
| 29 |
-
- `BATCH_SIZE`: prompt batch size, default
|
| 30 |
-
- `UBATCH_SIZE`: micro-batch size, default
|
| 31 |
-
- `CACHE_TYPE_K`: KV cache type for keys, default `
|
| 32 |
-
- `CACHE_TYPE_V`: KV cache type for values, default `
|
|
|
|
| 33 |
- `REASONING`: reasoning mode, accepts `True`, `False`, or `auto` and maps to llama.cpp `--reasoning`
|
| 34 |
- `LANGSEARCH_API_KEY`: optional API key for LangSearch web search tool (free tier: 1 req/sec, 60/min, 1000/day)
|
|
|
|
| 35 |
- `PARALLEL`: optional override for concurrent prompt decode slots
|
| 36 |
- `PORT`: listen port, default `7860`
|
| 37 |
|
|
@@ -51,14 +53,68 @@ The Docker image uses a multi-stage build on Ubuntu 24.04. The builder stage ins
|
|
| 51 |
|
| 52 |
Model downloads and HF cache live on the `/data` bucket so restarts do not redownload the model.
|
| 53 |
|
| 54 |
-
##
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
```bash
|
| 59 |
-
python3 /app/search_tool.py "
|
| 60 |
```
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
Examples:
|
| 63 |
```bash
|
| 64 |
python3 /app/search_tool.py "latest AI news" 5 true noLimit
|
|
|
|
| 23 |
|
| 24 |
Optional tuning variables:
|
| 25 |
|
| 26 |
+
- `CTX_SIZE`: context size, default `4096` (reduce to 2048-4096 for small models to improve speed)
|
| 27 |
+
- `THREADS`: CPU thread count, default auto-detect via nproc
|
| 28 |
+
- `THREADS_BATCH`: batch thread count, default matches THREADS
|
| 29 |
+
- `BATCH_SIZE`: prompt batch size, default 8 (increase to 16-32 for better throughput)
|
| 30 |
+
- `UBATCH_SIZE`: micro-batch size, default 8 (increase to 16-32 for better throughput)
|
| 31 |
+
- `CACHE_TYPE_K`: KV cache type for keys, default `f16` (use `q4_0` for memory savings)
|
| 32 |
+
- `CACHE_TYPE_V`: KV cache type for values, default `f16` (use `q4_0` for memory savings)
|
| 33 |
+
- `PERF_PROFILE`: `low_latency`, `balanced` (default), or `throughput`
|
| 34 |
- `REASONING`: reasoning mode, accepts `True`, `False`, or `auto` and maps to llama.cpp `--reasoning`
|
| 35 |
- `LANGSEARCH_API_KEY`: optional API key for LangSearch web search tool (free tier: 1 req/sec, 60/min, 1000/day)
|
| 36 |
+
- `NO_WARMUP`: set to `1` to skip model warmup and save startup time
|
| 37 |
- `PARALLEL`: optional override for concurrent prompt decode slots
|
| 38 |
- `PORT`: listen port, default `7860`
|
| 39 |
|
|
|
|
| 53 |
|
| 54 |
Model downloads and HF cache live on the `/data` bucket so restarts do not redownload the model.
|
| 55 |
|
| 56 |
+
## Performance Optimization
|
| 57 |
|
| 58 |
+
For small 2B models on CPU-only inference (~2-3 t/s baseline), optimize these settings:
|
| 59 |
+
|
| 60 |
+
**High Impact:**
|
| 61 |
+
- `CTX_SIZE`: Reduce from 32000 to **2048** or **4096** (KV cache memory dominates)
|
| 62 |
+
- `BATCH_SIZE` & `UBATCH_SIZE`: Increase to **16-32** for better CPU utilization
|
| 63 |
+
- `PERF_PROFILE`: Use **`throughput`** instead of balanced for batch mode
|
| 64 |
+
|
| 65 |
+
**Medium Impact:**
|
| 66 |
+
- `NO_WARMUP=1`: Skip model warmup to reduce startup latency by ~0.2s
|
| 67 |
+
- `CACHE_TYPE_K`, `CACHE_TYPE_V`: Use `q4_0` instead of `f16` to reduce KV cache memory by 75%
|
| 68 |
+
|
| 69 |
+
**Example for 2B model (CPU-only):**
|
| 70 |
+
```
|
| 71 |
+
CTX_SIZE=2048
|
| 72 |
+
BATCH_SIZE=16
|
| 73 |
+
UBATCH_SIZE=16
|
| 74 |
+
PERF_PROFILE=throughput
|
| 75 |
+
NO_WARMUP=1
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
Expected: 3-5 t/s generation speed (vs 2-3 t/s with default settings).
|
| 79 |
+
|
| 80 |
+
## Web Search Tool (Standalone)
|
| 81 |
+
|
| 82 |
+
The search tool is **not callable via model tool calling** (llama.cpp's `--tools` only supports built-in file/shell operations).
|
| 83 |
+
|
| 84 |
+
**Use cases:**
|
| 85 |
+
- Call from your application layer to augment model responses
|
| 86 |
+
- Pre-process queries before sending to model
|
| 87 |
+
- Post-process to fetch real-time data for specific topics
|
| 88 |
+
|
| 89 |
+
**API Usage (from application):**
|
| 90 |
+
|
| 91 |
+
```python
|
| 92 |
+
import requests
|
| 93 |
+
import json
|
| 94 |
+
|
| 95 |
+
response = requests.post(
|
| 96 |
+
"http://localhost:7860/v1/chat/completions",
|
| 97 |
+
headers={"Authorization": "Bearer YOUR_API_KEY"},
|
| 98 |
+
json={
|
| 99 |
+
"model": "model",
|
| 100 |
+
"messages": [{"role": "user", "content": "What's the latest news?"}],
|
| 101 |
+
"temperature": 0.7
|
| 102 |
+
}
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
latest_response = response.json()['choices'][0]['message']['content']
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
Then separately call search tool from your backend:
|
| 109 |
|
| 110 |
```bash
|
| 111 |
+
python3 /app/search_tool.py "latest AI news" 5 true noLimit
|
| 112 |
```
|
| 113 |
|
| 114 |
+
Combine results before returning to user.
|
| 115 |
+
|
| 116 |
+
**Command-line (Docker container):**
|
| 117 |
+
|
| 118 |
Examples:
|
| 119 |
```bash
|
| 120 |
python3 /app/search_tool.py "latest AI news" 5 true noLimit
|
start.sh
CHANGED
|
@@ -17,6 +17,7 @@ export CACHE_TYPE_K="${CACHE_TYPE_K:-f16}"
|
|
| 17 |
export CACHE_TYPE_V="${CACHE_TYPE_V:-f16}"
|
| 18 |
export REASONING="${REASONING:-auto}"
|
| 19 |
export TOOLS="${TOOLS:-}"
|
|
|
|
| 20 |
|
| 21 |
# Ensure data directories exist and are writable. Run as root in container startup.
|
| 22 |
echo "preparing storage: $HF_HOME and $MODEL_DIR"
|
|
@@ -115,8 +116,13 @@ server_args=(
|
|
| 115 |
--cache-type-k "${CACHE_TYPE_K}"
|
| 116 |
--cache-type-v "${CACHE_TYPE_V}"
|
| 117 |
--metrics
|
|
|
|
| 118 |
)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
case "${REASONING,,}" in
|
| 121 |
true|1|on|yes)
|
| 122 |
server_args+=(--reasoning on)
|
|
|
|
| 17 |
export CACHE_TYPE_V="${CACHE_TYPE_V:-f16}"
|
| 18 |
export REASONING="${REASONING:-auto}"
|
| 19 |
export TOOLS="${TOOLS:-}"
|
| 20 |
+
export NO_WARMUP="${NO_WARMUP:-0}"
|
| 21 |
|
| 22 |
# Ensure data directories exist and are writable. Run as root in container startup.
|
| 23 |
echo "preparing storage: $HF_HOME and $MODEL_DIR"
|
|
|
|
| 116 |
--cache-type-k "${CACHE_TYPE_K}"
|
| 117 |
--cache-type-v "${CACHE_TYPE_V}"
|
| 118 |
--metrics
|
| 119 |
+
--continuous-batching
|
| 120 |
)
|
| 121 |
|
| 122 |
+
if [ "${NO_WARMUP}" = "1" ]; then
|
| 123 |
+
server_args+=(--no-warmup)
|
| 124 |
+
fi
|
| 125 |
+
|
| 126 |
case "${REASONING,,}" in
|
| 127 |
true|1|on|yes)
|
| 128 |
server_args+=(--reasoning on)
|