0xarchit commited on
Commit
e22f356
·
1 Parent(s): 1420bd1

remove parallel support for faster inference

Browse files
Files changed (2) hide show
  1. README.md +14 -13
  2. start.sh +88 -37
README.md CHANGED
@@ -24,17 +24,16 @@ Set these in the Space settings:
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
 
40
  ## Endpoints
@@ -55,27 +54,29 @@ Model downloads and HF cache live on the `/data` bucket so restarts do not redow
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
 
 
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 cgroup CPU quota, then `nproc`
28
  - `THREADS_BATCH`: batch thread count, default matches THREADS
29
+ - `BATCH_SIZE`: prompt batch size, default 128 for balanced mode
30
+ - `UBATCH_SIZE`: micro-batch size, default 128 for balanced mode
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
  - `PORT`: listen port, default `7860`
38
 
39
  ## Endpoints
 
54
 
55
  ## Performance Optimization
56
 
57
+ For small 2B models on CPU-only inference (~2-4 t/s baseline), optimize these settings:
58
 
59
  **High Impact:**
60
+ - The server forces `--parallel 1`, so only one request runs at a time and all CPU is focused on the active query
61
+ - `BATCH_SIZE` & `UBATCH_SIZE`: Use **64-256** for faster prompt ingestion; tiny values like 8 cause many small prompt batches
62
+ - `CTX_SIZE`: Use **2048** unless you need long conversations; larger context increases KV/cache work
63
+ - `PERF_PROFILE`: Use **`balanced`** for normal personal use; `throughput` only changes prompt batch sizing now
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
+ - `TOOLS`: Leave unset unless you need tool calling. Built-in shell tools add prompt/tool overhead and should not be exposed publicly.
69
 
70
  **Example for 2B model (CPU-only):**
71
  ```
72
  CTX_SIZE=2048
73
+ BATCH_SIZE=128
74
+ UBATCH_SIZE=128
75
+ PERF_PROFILE=balanced
76
+ REASONING=False
77
  ```
78
 
79
+ Expected: faster first-token latency from prompt ingestion. Generation speed is still CPU-bound on the free tier, so a 2B Q4 model will usually remain in the low single-digit tokens/sec range.
80
 
81
  ## Web Search Tool (Standalone)
82
 
start.sh CHANGED
@@ -1,7 +1,65 @@
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
 
4
- THREADS="${THREADS:-$(nproc)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  export OMP_NUM_THREADS="$THREADS"
6
  export OPENBLAS_NUM_THREADS="$THREADS"
7
  export MKL_NUM_THREADS="$THREADS"
@@ -10,11 +68,11 @@ export NUMEXPR_NUM_THREADS="$THREADS"
10
  export HF_HOME="${HF_HOME:-/data/hf-cache}"
11
  export MODEL_DIR="${MODEL_DIR:-/data/models}"
12
  export MODEL_PATH="${MODEL_PATH:-/data/models/model.gguf}"
13
- export CTX_SIZE="${CONTEXT_LENGTH:-${CTX_SIZE:-4096}}"
14
  export QUANT_PREFERENCE="${QUANT_PREFERENCE:-q4_k_m,q4_k_s,q4_k,q4_0,q4_1}"
15
  export PERF_PROFILE="${PERF_PROFILE:-balanced}"
16
- 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
  export NO_WARMUP="${NO_WARMUP:-0}"
@@ -55,13 +113,13 @@ LLAMA_SERVER_BIN="${LLAMA_SERVER_BIN:-/usr/local/bin/llama-server}"
55
  # set server profile defaults
56
  case "$PERF_PROFILE" in
57
  low_latency)
58
- DEFAULT_BATCH=1
59
- DEFAULT_UBATCH=1
60
- DEFAULT_THREADS_BATCH=1
61
  ;;
62
  balanced)
63
- DEFAULT_BATCH=8
64
- DEFAULT_UBATCH=8
65
  DEFAULT_THREADS_BATCH=${THREADS}
66
  ;;
67
  throughput)
@@ -70,37 +128,32 @@ case "$PERF_PROFILE" in
70
  DEFAULT_THREADS_BATCH=${THREADS}
71
  ;;
72
  *)
73
- DEFAULT_BATCH=8
74
- DEFAULT_UBATCH=8
75
  DEFAULT_THREADS_BATCH=${THREADS}
76
  ;;
77
  esac
78
 
79
- # Adjust defaults for low-memory environments (Spaces free tier: ~16GB)
80
- if [ -r /proc/meminfo ]; then
81
- MEM_KB=$(awk '/MemTotal/ {print $2}' /proc/meminfo || echo 0)
82
- else
83
- MEM_KB=0
84
- fi
85
- echo "detected memory (KB): $MEM_KB"
86
  if [ "$MEM_KB" -gt 0 ] && [ "$MEM_KB" -le 18000000 ]; then
87
- echo "low-memory profile detected: adjusting ctx-size and cache types"
88
- # reduce default context to save KV cache memory
89
- CTX_SIZE="${CTX_SIZE:-2048}"
90
- # use compact cache formats
91
- CACHE_TYPE_K="${CACHE_TYPE_K:-f16}"
92
- CACHE_TYPE_V="${CACHE_TYPE_V:-f16}"
93
- # further reduce batch sizes if balanced/throughput
94
- if [ "$PERF_PROFILE" = "balanced" ]; then
95
- DEFAULT_BATCH=4
96
- DEFAULT_UBATCH=4
97
- elif [ "$PERF_PROFILE" = "throughput" ]; then
98
- DEFAULT_BATCH=64
99
- DEFAULT_UBATCH=64
100
- fi
101
  fi
102
 
103
- export CACHE_TYPE_K CACHE_TYPE_V CTX_SIZE
 
 
 
 
 
 
 
 
104
 
105
  server_args=(
106
  --model "$MODEL_PATH"
@@ -148,11 +201,9 @@ if [[ -n "${TOOLS}" ]]; then
148
  echo "enabled tools: $TOOLS"
149
  fi
150
 
151
- if [[ -n "${PARALLEL:-}" ]]; then
152
- server_args+=(--parallel "$PARALLEL")
153
- fi
154
 
155
  server_cmd=("$LLAMA_SERVER_BIN" "${server_args[@]}")
156
  echo "server command: $LLAMA_SERVER_BIN ${server_args[*]}"
157
  echo "starting llama-server"
158
- exec su -s /bin/sh appuser -c "$(printf '%q ' "${server_cmd[@]}")"
 
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
 
4
+ detect_cpu_quota_threads() {
5
+ local quota period threads
6
+
7
+ if [ -r /sys/fs/cgroup/cpu.max ]; then
8
+ read -r quota period < /sys/fs/cgroup/cpu.max || true
9
+ if [ "${quota:-max}" != "max" ] && [ "${period:-0}" -gt 0 ] 2>/dev/null; then
10
+ threads=$(( (quota + period - 1) / period ))
11
+ [ "$threads" -gt 0 ] && echo "$threads" && return
12
+ fi
13
+ fi
14
+
15
+ if [ -r /sys/fs/cgroup/cpu/cpu.cfs_quota_us ] && [ -r /sys/fs/cgroup/cpu/cpu.cfs_period_us ]; then
16
+ quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
17
+ period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)
18
+ if [ "${quota:-0}" -gt 0 ] && [ "${period:-0}" -gt 0 ] 2>/dev/null; then
19
+ threads=$(( (quota + period - 1) / period ))
20
+ [ "$threads" -gt 0 ] && echo "$threads" && return
21
+ fi
22
+ fi
23
+
24
+ echo 0
25
+ }
26
+
27
+ detect_memory_kb() {
28
+ local value
29
+
30
+ if [ -r /sys/fs/cgroup/memory.max ]; then
31
+ value=$(cat /sys/fs/cgroup/memory.max)
32
+ if [ "$value" != "max" ] && [ "${value:-0}" -gt 0 ] 2>/dev/null; then
33
+ echo $(( value / 1024 ))
34
+ return
35
+ fi
36
+ fi
37
+
38
+ if [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
39
+ value=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
40
+ if [ "${value:-0}" -gt 0 ] && [ "$value" -lt 9223372036854771712 ] 2>/dev/null; then
41
+ echo $(( value / 1024 ))
42
+ return
43
+ fi
44
+ fi
45
+
46
+ if [ -r /proc/meminfo ]; then
47
+ awk '/MemTotal/ {print $2}' /proc/meminfo || echo 0
48
+ else
49
+ echo 0
50
+ fi
51
+ }
52
+
53
+ HOST_THREADS="$(nproc)"
54
+ QUOTA_THREADS="$(detect_cpu_quota_threads)"
55
+ if [ -z "${THREADS:-}" ]; then
56
+ if [ "$QUOTA_THREADS" -gt 0 ] && [ "$QUOTA_THREADS" -lt "$HOST_THREADS" ]; then
57
+ THREADS="$QUOTA_THREADS"
58
+ else
59
+ THREADS="$HOST_THREADS"
60
+ fi
61
+ fi
62
+
63
  export OMP_NUM_THREADS="$THREADS"
64
  export OPENBLAS_NUM_THREADS="$THREADS"
65
  export MKL_NUM_THREADS="$THREADS"
 
68
  export HF_HOME="${HF_HOME:-/data/hf-cache}"
69
  export MODEL_DIR="${MODEL_DIR:-/data/models}"
70
  export MODEL_PATH="${MODEL_PATH:-/data/models/model.gguf}"
71
+ REQUESTED_CTX_SIZE="${CONTEXT_LENGTH:-${CTX_SIZE:-}}"
72
  export QUANT_PREFERENCE="${QUANT_PREFERENCE:-q4_k_m,q4_k_s,q4_k,q4_0,q4_1}"
73
  export PERF_PROFILE="${PERF_PROFILE:-balanced}"
74
+ REQUESTED_CACHE_TYPE_K="${CACHE_TYPE_K:-}"
75
+ REQUESTED_CACHE_TYPE_V="${CACHE_TYPE_V:-}"
76
  export REASONING="${REASONING:-auto}"
77
  export TOOLS="${TOOLS:-}"
78
  export NO_WARMUP="${NO_WARMUP:-0}"
 
113
  # set server profile defaults
114
  case "$PERF_PROFILE" in
115
  low_latency)
116
+ DEFAULT_BATCH=64
117
+ DEFAULT_UBATCH=64
118
+ DEFAULT_THREADS_BATCH=${THREADS}
119
  ;;
120
  balanced)
121
+ DEFAULT_BATCH=128
122
+ DEFAULT_UBATCH=128
123
  DEFAULT_THREADS_BATCH=${THREADS}
124
  ;;
125
  throughput)
 
128
  DEFAULT_THREADS_BATCH=${THREADS}
129
  ;;
130
  *)
131
+ DEFAULT_BATCH=128
132
+ DEFAULT_UBATCH=128
133
  DEFAULT_THREADS_BATCH=${THREADS}
134
  ;;
135
  esac
136
 
137
+ # Adjust defaults for low-memory environments (Spaces free tier: ~16GB).
138
+ # Use cgroup limits first because /proc/meminfo can report the host machine.
139
+ MEM_KB="$(detect_memory_kb)"
140
+ echo "detected effective memory (KB): $MEM_KB"
 
 
 
141
  if [ "$MEM_KB" -gt 0 ] && [ "$MEM_KB" -le 18000000 ]; then
142
+ echo "low-memory profile detected: using compact defaults"
143
+ DEFAULT_CTX_SIZE=2048
144
+ else
145
+ DEFAULT_CTX_SIZE=4096
 
 
 
 
 
 
 
 
 
 
146
  fi
147
 
148
+ CTX_SIZE="${REQUESTED_CTX_SIZE:-$DEFAULT_CTX_SIZE}"
149
+ CACHE_TYPE_K="${REQUESTED_CACHE_TYPE_K:-f16}"
150
+ CACHE_TYPE_V="${REQUESTED_CACHE_TYPE_V:-f16}"
151
+ PARALLEL=1
152
+
153
+ echo "effective threads: $THREADS (host=$HOST_THREADS, quota=$QUOTA_THREADS)"
154
+ echo "effective llama profile: perf=$PERF_PROFILE single_request=1 ctx=$CTX_SIZE batch=${BATCH_SIZE:-$DEFAULT_BATCH} ubatch=${UBATCH_SIZE:-$DEFAULT_UBATCH}"
155
+
156
+ export CACHE_TYPE_K CACHE_TYPE_V CTX_SIZE PARALLEL
157
 
158
  server_args=(
159
  --model "$MODEL_PATH"
 
201
  echo "enabled tools: $TOOLS"
202
  fi
203
 
204
+ server_args+=(--parallel "$PARALLEL")
 
 
205
 
206
  server_cmd=("$LLAMA_SERVER_BIN" "${server_args[@]}")
207
  echo "server command: $LLAMA_SERVER_BIN ${server_args[*]}"
208
  echo "starting llama-server"
209
+ exec su -s /bin/sh appuser -c "$(printf '%q ' "${server_cmd[@]}")"