Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ci/test_ci_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
(["server/tools/export_ds4v_mmproj.py"], PY),
(["server/tools/ds4_mix_converter/ds4_mix_converter.cpp"], CPP),
(["server/examples/chat.py"], PY),
(["optimizations/pflash/tests/test_pflash.py"], PY),
(["optimizations/kvflash/tests/gate/scripts/kvlib.py"], PY),
(["optimizations/megakernel/setup.py"], PY_MK),
(["uv.lock"], PY_MK),
# Both models run all C++/GPU/serve jobs.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ jobs:
- name: Populate venv with cu128 torch + setuptools
# First pass: install the workspace's default deps. luce declares
# torch (which uv pulls from the pytorch-cu128 index per
# [tool.uv.sources]) and pflash drags in setuptools. Megakernel is
# [tool.uv.sources], along with its setuptools dependency). Megakernel is
# in the optional `megakernel` extra so its build does NOT run yet.
run: uv sync --frozen

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ After setup, run these commands from the repository root:
```bash
git submodule update --init --recursive

# Python deps (workspace at the repo root; one .venv shared by dflash, pflash,
# Python deps (workspace at the repo root; one .venv shared by luce
# and optionally megakernel). `uv` is the canonical installer; the legacy
# per-subproject `python -m venv .venv && pip install …` flow still works.
uv sync # dflash + pflash deps
uv sync # luce workspace deps
uv sync --extra megakernel # also compile the megakernel CUDA extension
bash scripts/check_uv_workspace.sh # lockfile + frozen-sync import smoke

Expand Down
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ RUN cd /src/server/build \
COPY pyproject.toml uv.lock README.md /src/
COPY server/pyproject.toml server/README.md /src/server/
COPY server/scripts /src/server/scripts
COPY optimizations/pflash /src/optimizations/pflash
COPY optimizations/megakernel /src/optimizations/megakernel

# ─── Stage 2: runtime ───────────────────────────────────────────────────────
Expand Down Expand Up @@ -176,7 +175,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

# uv manages Python 3.12 (required by the workspace) and resolves the
# luce + pflash members declared in pyproject.toml.
# luce member declared in pyproject.toml.
# uv (pinned) copied from the official image rather than `curl | sh`, so the
# version is fixed and no remote installer script runs at build time.
COPY --from=ghcr.io/astral-sh/uv:0.11.2 /uv /uvx /usr/local/bin/
Expand All @@ -198,7 +197,6 @@ WORKDIR /opt/lucebox-hub
# benchmark. The host-side `lucebox.sh` is the supported way to drive this
# image; the Python CLI inside owns all orchestration logic.
COPY --from=builder /src/pyproject.toml /src/uv.lock /src/README.md /opt/lucebox-hub/
COPY --from=builder /src/optimizations/pflash /opt/lucebox-hub/optimizations/pflash
COPY --from=builder /src/optimizations/megakernel/pyproject.toml \
/src/optimizations/megakernel/README.md \
/opt/lucebox-hub/optimizations/megakernel/
Expand Down Expand Up @@ -266,7 +264,7 @@ RUN printf '%s\n%s\n' \
# cache is gone by the time the layer commits, so we don't double-pay.
ENV UV_LINK_MODE=hardlink \
UV_NO_CACHE=1
# --no-editable: install workspace members (pflash, lucebox-hub, and the
# --no-editable: install workspace members (lucebox-hub and the
# luce server binding) as proper wheels rather than
# source-linked editable installs. Without this, hatch-vcs's build hook
# re-fires at runtime when `uv run` re-checks env consistency and tries
Expand Down
4 changes: 1 addition & 3 deletions Dockerfile.rocm
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ RUN cd /src/server/build \
COPY pyproject.toml uv.lock README.md /src/
COPY server/pyproject.toml server/README.md /src/server/
COPY server/scripts /src/server/scripts
COPY optimizations/pflash /src/optimizations/pflash
COPY optimizations/megakernel /src/optimizations/megakernel

# ─── Stage 2: runtime ───────────────────────────────────────────────────────
Expand Down Expand Up @@ -171,7 +170,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
rocblas \
&& rm -rf /var/lib/apt/lists/*

# uv (pinned) manages Python 3.12 + resolves the luce + pflash
# uv (pinned) manages Python 3.12 + resolves the luce
# workspace. Copied from the official image rather than `curl | sh`, so the
# version is fixed and no remote installer script runs at build time.
COPY --from=ghcr.io/astral-sh/uv:0.11.2 /uv /uvx /usr/local/bin/
Expand All @@ -185,7 +184,6 @@ ENV UV_PYTHON_INSTALL_DIR=/opt/uv/python \
WORKDIR /opt/lucebox-hub

COPY --from=builder /src/pyproject.toml /src/uv.lock /src/README.md /opt/lucebox-hub/
COPY --from=builder /src/optimizations/pflash /opt/lucebox-hub/optimizations/pflash
COPY --from=builder /src/optimizations/megakernel/pyproject.toml \
/src/optimizations/megakernel/README.md \
/opt/lucebox-hub/optimizations/megakernel/
Expand Down
14 changes: 14 additions & 0 deletions optimizations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Optimizations

This directory holds write-ups, benchmark results, and standalone experiments for Lucebox's engine optimizations.

**Production implementations live in [`../server/src/`](../server/src/)** (C++/CUDA/HIP inside `luce_server`) — not here.

| Directory | Status | What's here |
|---|---|---|
| [`pflash/`](pflash/README.md) | Docs | Algorithm overview, results, and `luce_server --pflash` flags. Runtime lives in [`server/src/pflash/`](../server/src/pflash/) and [`server/src/server/pflash_unified.cpp`](../server/src/server/pflash_unified.cpp). |
| [`spark/`](spark/README.md) | Docs | Hot/cold MoE expert residency write-up, results, and `luce_server --spark` flags. Runtime lives in [`server/src/common/`](../server/src/common/), [`server/src/server/`](../server/src/server/), [`server/src/laguna/`](../server/src/laguna/), and [`server/src/qwen35moe/`](../server/src/qwen35moe/). |
| [`paged_attention/`](paged_attention/README.md) | Docs | Fixed-block K/V paging design and experiments for `luce_server --paged-attention`. |
| [`kvflash/`](kvflash/README.md) | Docs + live gate scripts | Bounded KV residency design, results, and manual `luce_server --kvflash` gate scripts. Runtime lives in `server/src/`. |
| [`image_gen/`](image_gen/README.md) | Docs + fetch/bench scripts | MOSS-Image docs, model fetcher (`fetch_models.sh`), and bench harness for `luce_server`. Runtime lives in [`server/src/moss_image/`](../server/src/moss_image/). |
| [`megakernel/`](megakernel/README.md) | Standalone CUDA extension | Fused single-kernel Qwen 3.5-0.8B PyTorch CUDA extension (built separately via `uv sync --extra megakernel`; not linked into `luce_server`). |
69 changes: 25 additions & 44 deletions optimizations/pflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,20 @@ Decode after prefill: ~74 tok/s (dflash spec decode + DDTree). The pipeline is t

## Quick start

PFlash is the algorithm. The implementation lives in [`../../server/`](../../server/) as part of the luce daemon. The `optimizations/pflash/` directory in this repo only contains the Python tooling for **benchmarking** (NIAH case generation, bench harness around the daemon stdin protocol). Production deploys hit the luce daemon directly.
PFlash is the algorithm. The implementation lives in [`../../server/`](../../server/) as part of `luce_server`; production deploys run `luce_server` directly with no Python in the inference path.

```bash
# 1. from the repo root, install Python deps and build the server
uv sync
# 1. from the repo root, build luce_server
git submodule update --init --recursive
cmake -B server/build -S server -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CUDA_ARCHITECTURES=86
cmake --build server/build --target test_dflash -j
cmake --build server/build --target luce_server -j

# 2. fetch weights (target + spec-decode draft + drafter scorer)
# 2. fetch weights (target + spec-decode draft + drafter scorer + PFlash assets)
uv run hf download unsloth/Qwen3.6-27B-GGUF Qwen3.6-27B-Q4_K_M.gguf --local-dir server/models/
uv run hf download Qwen/Qwen3.5-0.8B model.safetensors tokenizer.json --local-dir server/models/drafter/
uv run hf download z-lab/Qwen3.6-27B-DFlash model.safetensors --local-dir server/models/draft/

# 2b. convert the drafter (Qwen3.5-0.8B HF) to a BF16 GGUF for the C++ scorer.
# The submodule already vendors llama.cpp at deps/llama.cpp.
uv run python server/deps/llama.cpp/convert_hf_to_gguf.py server/models/drafter \
--outtype bf16 --outfile server/models/Qwen3.5-0.8B-BF16.gguf

# 3. generate NIAH cases + run head-to-head bench against the C++ daemon
uv run --directory optimizations/pflash python tests/niah_gen.py --n 1 --ctx 131072 --out /tmp/niah_128k.jsonl
uv run --directory optimizations/pflash python tests/bench_niah_cpp.py \
--bin ../../server/build/test_dflash \
--target ../../server/models/Qwen3.6-27B-Q4_K_M.gguf \
--draft-spec ../../server/models/draft/model.safetensors \
--drafter-gguf ../../server/models/Qwen3.5-0.8B-BF16.gguf \
--cases /tmp/niah_128k.jsonl --keep-ratio 0.05 --n-gen 256
uv run hf download unsloth/Qwen3.5-0.8B-GGUF Qwen3.5-0.8B-BF16.gguf --local-dir server/models/
uv run hf download z-lab/Qwen3.6-27B-DFlash model.safetensors config.json --local-dir server/models/draft/
uv run hf download Lucebox/pflash --local-dir server/models/pflash/
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
```

## OpenAI server flags
Expand All @@ -92,50 +78,46 @@ For an OpenAI-compatible server with transparent compression on long prompts, ru

| Flag | Choices / type | Default | Effect |
|---|---|:---:|---|
| `--pflash` | `auto` / `on` / `off` | `auto` | Page long messages of at least `--prefill-threshold` droppable tokens. `auto` is on when `--prefill-drafter` and all four PFlash files (below) are given; `on` refuses to start without them. |
| `--prefill-threshold` | int (tokens) | `8192` | Droppable tokens a message needs to be paged; `0` pages every message. |
| `--pflash` | `auto` / `on` / `off` | `auto` | Page eligible regions once their combined droppable tokens reach `--prefill-threshold`. `auto` is on when `--prefill-drafter` and all four PFlash files (below) are given; `on` refuses to start without them. |
| `--prefill-threshold` | int (tokens) | `8192` | Aggregate droppable-token threshold across eligible regions (each region with at least `min(threshold, 2048)` droppable tokens); `0` pages every eligible region. |
| `--prefill-keep-ratio` | float `(0, 1]` | release curve | Fraction of source tokens to keep. Without a ratio flag the release curve `8192:0.450 16384:0.350 32768:0.270 65536:0.260 120000:0.240` with the adaptive keep ratio applies. |
| `--prefill-drafter` | path to `.gguf` | required for PFlash | Drafter weights (Qwen3.5-0.8B BF16 GGUF). |
| `--prefill-drafter` | path to `.gguf` | required for PFlash | Drafter weights (published `Qwen3.5-0.8B-BF16.gguf`, checked against the PFlash asset SHA-256 contract). |

PFlash needs four files, all from [`Lucebox/pflash`](https://huggingface.co/Lucebox/pflash): the scoring head (`PFLASH_SCORING_HEAD_GGUF`), the segment and request probes (`PFLASH_SEGMENT_PROBE_GGUF`, `PFLASH_REQUEST_PROBE_GGUF`) and the segment reranker (`PFLASH_SEG_RERANKER_GGUF`); an unset probe or reranker path is looked up beside the head. Every unset PFlash setting takes the measured release profile; see [PFlash configuration](../../server/docs/PFLASH_CONFIGURATION.md#quick-launch).
PFlash needs four files, all from [`Lucebox/pflash`](https://huggingface.co/Lucebox/pflash): the scoring head (`PFLASH_SCORING_HEAD_GGUF`, `seg-scoring-head.gguf`), the segment and request probes (`PFLASH_SEGMENT_PROBE_GGUF`, `PFLASH_REQUEST_PROBE_GGUF`) and the segment reranker (`PFLASH_SEG_RERANKER_GGUF`); an unset probe or reranker path is looked up beside the head. Every unset PFlash setting takes the measured release profile; see [PFlash configuration](../../server/docs/PFLASH_CONFIGURATION.md#quick-launch).

```bash
PFLASH_SCORING_HEAD_GGUF=server/models/pflash/head.gguf \
PFLASH_SEGMENT_PROBE_GGUF=server/models/pflash/segment-probe.gguf \
PFLASH_REQUEST_PROBE_GGUF=server/models/pflash/request-probe.gguf \
PFLASH_SEG_RERANKER_GGUF=server/models/pflash/seg-reranker.gguf \
PFLASH_SCORING_HEAD_GGUF=server/models/pflash/seg-scoring-head.gguf \
./server/build/luce_server server/models/Qwen3.6-27B-Q4_K_M.gguf \
--draft server/models/draft/model.safetensors \
--prefill-drafter server/models/Qwen3.5-0.8B-BF16.gguf
--draft server/models/draft/model.safetensors \
--prefill-drafter server/models/Qwen3.5-0.8B-BF16.gguf \
--pflash on
```

PFlash is on because the drafter and the four files are given. A user message or tool output with fewer droppable tokens than the threshold is served as is; a longer one is paged, and the system prompt and tool definitions are always served whole. See the [server README](../../server/README.md#pflash) for how the view pages, recalls and continues a conversation.
`--pflash on` fails fast at startup if any of the four assets or drafter contracts is missing (with `--pflash auto`, the default, PFlash turns on automatically once they are present). Eligible regions with at least `min(--prefill-threshold, 2048)` droppable tokens are admitted individually once they reach `--prefill-threshold`, or in aggregate once the combined droppable tokens cross `--prefill-threshold` (for current-turn tool outputs, only the current turn's eligible tool outputs count toward that aggregate, so cold history does not force compression on a small active tool output). Shorter prompts are served as is, and the system prompt and tool definitions are always served whole. See the [server README](../../server/README.md#pflash) for how the view pages, recalls and continues a conversation.

Validated end-to-end at 64K and 128K source on RTX 3090 (Qwen3.6-27B Q4_K_M target + Qwen3.5-DFlash draft + Qwen3-0.6B BF16 drafter).

## Daemon stdin protocol
## `test_dflash --daemon` stdin protocol

The dflash daemon runs persistently and accepts these commands on stdin (newline-delimited):
For low-level benchmarking, build the test daemon with `cmake --build server/build --target test_dflash -j` and launch `test_dflash <target.gguf> <draft> --daemon --stream-fd=<fd>` (`luce_server` does not read stdin commands). It runs persistently, accepts newline-delimited commands on stdin, and writes int32 token-id streams (terminated by `-1`) to `--stream-fd`:

| Command | Effect |
|---|---|
| `compress <ids.bin> <keep_x1000> <drafter.gguf>` | Drafter scores the prompt and emits the compressed token-id stream (terminated by `-1`). |
| `generate <prompt_ids.bin> <n_gen> <out_ids.bin>` | Target spec-decode on the (already compressed) prompt. Streams committed token ids on stdout. |
| `compress <ids.bin> <keep_x1000> <drafter.gguf>` | Drafter scores the prompt and emits the compressed token-id stream to `--stream-fd` (terminated by `-1`). |
| `<prompt_ids.bin> <n_gen>` | Target spec-decode on the (already compressed) prompt. Streams committed token ids to `--stream-fd` (terminated by `-1`). |
| `park draft` / `park target` / `park` | Free draft / target / both weights from VRAM. |
| `unpark draft` / `unpark target` / `unpark` | Restore weights from disk to VRAM. |
| `free drafter` | Release the spec-prefill drafter context (drafter weights + KV). |

Typical flow at 128K on a 24 GB card: `park target` → `compress` → `free drafter` → `unpark target` → `unpark draft` → `generate` → `park draft`.

`pflash.dflash_client.DflashClient` is the Python wrapper around this protocol used by `tests/bench_niah_cpp.py`.

## Runtime tunables

PFlash's required assets and operational settings are listed in [PFlash configuration](../../server/docs/PFLASH_CONFIGURATION.md). On the daemon process:
PFlash's required assets and operational settings are listed in [PFlash configuration](../../server/docs/PFLASH_CONFIGURATION.md). For direct `luce_server` launches, attention windowing is controlled by `--fa-window <N>` (default `0`, full attention on the compressed prompt). On `test_dflash` (and the container entrypoint, which translates `LUCE_FA_WINDOW` to `--fa-window`):

| Env var | Default | Purpose |
|---|:---:|---|
| `LUCE_FA_WINDOW` | `0` (set by the bench client) | Sliding window of the target's full-attention layers; `0` keeps full attention on the compressed prompt. Unset, `test_dflash` uses 2048. |
| `LUCE_FA_WINDOW` | `2048` (`test_dflash`) | Sliding window of the target's full-attention layers; set `0` for full attention on the compressed prompt. Ignored by direct `luce_server` launches (use `--fa-window`). |
| `LUCE_KV_K` / `LUCE_KV_V` | (auto) | KV-cache quant types. `q4_0` / `q4_0` is the bench setting. `tq3_0` saves another ~4 GB at 128K. |

The `LUCE_FP_*` FlashPrefill switches do not affect PFlash: the scorer does not run the FlashPrefill kernels.
Expand Down Expand Up @@ -205,7 +187,7 @@ What we built:

- **Single 24 GB GPU** target (RTX 3090 reference). On 32+ GB cards, drafter + target can coexist and the park/unpark dance disappears.
- **Qwen3.6-27B Q4_K_M target + Qwen3.5-0.8B drafter** is the validated pair. Other targets/drafters need keep-ratio re-calibration.
- **Any target model.** The drafter scores the prompt on its own, so what it keeps does not depend on the model that serves it. A backend only says how it frees memory for the drafter and gets it back (`server/src/pflash/pflash_window.h`); loading, scoring and releasing the drafter are shared. Today: Qwen3.5/3.6 (`qwen35`, also layer-split), Qwen3 (`qwen3`), Qwen3.8-Flash (`qwen4exp`) and DeepSeek V4/V4.1 (`deepseek4`, `deepseek41`). Qwen3.8-Flash sizes its context to its GPUs, so with PFlash on it keeps the drafter's room free (its weights plus 56 KiB per context token) on whichever of its GPUs (target or expert device) the drafter shares, and never parks.
- **Supported target backends.** The in-process drafter scores the prompt on its own, and the server enables PFlash on backends that expose per-candidate scores (`pflash_gives_candidate_scores()`, plus a `server/src/pflash/pflash_window.h` memory window): Qwen3.5/3.6 (`qwen35`, also layer-split), Qwen3 (`qwen3`), Qwen3.8-Flash (`qwen4exp`), DeepSeek V4/V4.1 (`deepseek4`, `deepseek41`), and Inkling 2 (`inkling2`). Other backends (such as `laguna`) do not expose candidate scores and reject `--pflash on`. Qwen3.8-Flash sizes its context to its GPUs, so with PFlash on it keeps the drafter's room free (its weights plus 56 KiB per context token) on whichever of its GPUs (target or expert device) the drafter shares, and never parks.
- **Concurrent serving** (`--max-concurrency > 1`) with PFlash is dense Qwen3.5 only: its scheduler scores prompts on a thread of its own while the other slots decode. Other models compress one request at a time, and the engine refuses PFlash with more than one slot (`--pflash auto` turns PFlash off instead).
- **NIAH single-needle** is the only retrieval task validated end-to-end. Multi-doc QA, long-form code retrieval, etc. still TBD.

Expand Down Expand Up @@ -238,8 +220,7 @@ defaults.
### Drafter selection: BF16 Qwen3.5-0.8B for compress

PFlash compress benefits from a small, fast drafter. The validated
choice is **Qwen3.5-0.8B** in **BF16 safetensors** with ~5 attention
layers. The DFlash drafter for the same target works correctly
choice is **Qwen3.5-0.8B** in **BF16 GGUF** (`Qwen3.5-0.8B-BF16.gguf`). The DFlash drafter for the same target works correctly
during decode-after-unpark but is heavier than ideal for compress.

Practical guidance:
Expand Down Expand Up @@ -285,7 +266,7 @@ caching artefacts.
@software{luce_pflash_2026,
title = {Luce PFlash: speculative prefill compression for long-context spec decode on consumer GPUs},
author = {Lucebox},
url = {https://github.com/Luce-Org/lucebox-hub/tree/main/pflash},
url = {https://github.com/Luce-Org/lucebox/tree/main/optimizations/pflash},
year = {2026}
}

Expand Down
Loading
Loading