First five minutes¶
The four Pion lines¶
On Apple Silicon, PionPromptCache sits where mlx-lm's own prompt cache does.
The first run warms it; every later run — this process or another — hits.
# Homebrew: brew install pavelhorak/tap/pion && brew services start pion (runs with these flags)
# Tarball or source build:
./pion-server --kvcache --metal-attention -w 1
from pion_vllm_mlx import PionPromptCache
pc = PionPromptCache(model, host="127.0.0.1", port=1974) # once per loaded model
cache = pc.get_or_prefill(prefix_ids, namespace="app|v1|llama-1b|system_v1") # MISS: prefill once + store · HIT: fetch
text = generate(model, tok, prompt=suffix_ids, prompt_cache=cache) # decode as usual, at native speed
get_or_prefill returns what mlx_lm.make_prompt_cache(model) would, with the
prefix's K/V already in it. prefix_ids is the part every request shares (the
system prompt, the few-shot block, the retrieved chunk); suffix_ids is the
part that changes. The first process to ask pays the prefill once and stores
it; every later call — this process, another one, or after a restart — fetches
it. That is the cross-process row in the table below. The same-process row
adds the Stage-2 attention patch, in which Pion computes the attention over
the prefix itself (pion-vllm-mlx/README.md).
Runnable end to end against a running Pion:
examples/prompt_cache_demo.py times vanilla
mlx-lm's cold prefill against five cached requests, each followed by a full
answer, and prints every one. Install with pip install 'pion-vllm-mlx[mlx]'.
The Redis wire, vector search, the semantic cache¶
KV — redis-cli talks to Pion unmodified:
Vector search. A vector is DIM * 4 raw float32 bytes — 6144 for the
default DIM 1536 — so this needs a real client rather than redis-cli. Three
things the API requires, in this order:
FT.CREATEdefines the index. Hashes written before it are indexed by it too (from 1.0.1), as in RediSearch;SKIPINITIALSCANturns that off.HSET <key> vec <blob> <field> <value>— at least two field/value pairs. The vector-ingest path is only taken forHSETwith 6 or more arguments.DISTANCE_METRICacceptsL2andCOSINE. Any other value —IPincluded — is refused atFT.CREATErather than silently answered with the wrong ordering. Omitting the keyword givesL2.TYPEmust beFLOAT32. AnFT.CREATEorFT.SEARCHoption Pion does not apply is refused, never ignored; the remaining differences from RediSearch are listed.
DIM is per-index in the RediSearch API but the blob must be DIM * 4 bytes of
float32 either way; the example below uses the server's default 1536.
pip install redis numpy # or: pixi run python -m pip install redis numpy
python3 - <<'PY'
import numpy as np, redis
from redis.commands.search.query import Query
r = redis.Redis(port=1974)
D = 1536 # blob is D*4 = 6144 bytes
r.execute_command("FT.CREATE", "idx", "SCHEMA", "vec", "VECTOR", "HNSW", "6",
"TYPE", "FLOAT32", "DIM", str(D), "DISTANCE_METRIC", "COSINE")
rng = np.random.default_rng(0)
vecs = {f"doc:{i}": rng.random(D).astype(np.float32) for i in range(20)}
for key, v in vecs.items():
r.execute_command("HSET", key, "vec", v.tobytes(), "title", key)
r.execute_command("FT.OPTIMIZE", "idx") # optional: build the graph now
q = (Query("*=>[KNN 3 @vec $B AS score]") # the 3 nearest, closest first
.sort_by("score").return_fields("title", "score").dialect(2))
res = r.ft("idx").search(q, {"B": vecs["doc:7"].tobytes()})
print(res.total, "hits; nearest =", res.docs[0].id) # 3 hits; nearest = doc:7
PY
Semantic cache. The cache's index is per worker, so it needs a
single-worker server (the default)
and an embedding backend. On macOS use Apple's NLEmbedding — no downloads, no
Python (the Homebrew service already runs with --nle-embed):
./pion-server --nle-embed # macOS: Apple's sentence embedding, 512-dim
# other platforms: pixi run install-inference # once; then ./pion-server
# # auto-embeds via MiniLM-L6-v2
redis-cli -p 1974 AI.SEMANTIC_CACHE SET "capital of France?" "Paris"
redis-cli -p 1974 AI.SEMANTIC_CACHE GET "What is the capital of France?"
# -> "Paris" — a different wording, matched by meaning
Server profiles¶
Run
./pion-server --helpfor the full flag reference (cluster / AI / inference / tuning groups), grouped and one-line-described.
./pion-server --profile kv # KV-only: ~50MB/worker, no HNSW
./pion-server --profile vector # KV + vector search
./pion-server --profile ai --flare # KV + vector + AI (semantic cache; FLARE is experimental)
./pion-server --kvcache -w 1 # + externalized attention (ATTEND.*)
./pion-server --metal-attention -w 1 # + native Metal SDPA on Mac (no Python; fp32, more accurate)
./pion-server --metal-attention-fp16 -w 1 # fp16 kernel — matches vanilla mlx-lm precision
./pion-server --metal-attention --fa-window 2048 -w 1 # sliding-window SDPA: scans only the last N tokens. Safe on Mistral SWA / Longformer; lossy on dense models (Llama, Gemma, GPT).
Vector quantization (--polarquant, --turboquant, --nanoquant) is in
doc/vector_engine.md; the Linux I/O backends
(--epoll, --iouring, --xdp) are in doc/networking.md.
Where next¶
- Where Pion does not help — read this before you build on it.
- Shared KV cache — namespaces, lanes, Stage 1 vs Stage 2.
- Command index and the CLI flags.