| --- |
| license: mit |
| language: |
| - en |
| - pt |
| tags: |
| - llama.cpp |
| - gguf |
| - text-diffusion |
| - block-diffusion |
| - diffusion-language-model |
| - gemma |
| - openai-api |
| - server |
| - cpu-inference |
| - offline |
| pipeline_tag: text-generation |
| --- |
| |
| <img src="https://huggingface.co/Brunobkr/REPO_NAME/resolve/main/capa.png" alt="ΩFFΣLLIα diffusion-gemma-http" width="100%"/> |
|
|
| # ΩFFΣLLIα — diffusion-gemma-http |
|
|
| **A native, single-file, OpenAI-compatible HTTP server for DiffusionGemma block text-diffusion models (GGUF / llama.cpp).** |
|
|
| `diffusion-gemma-http.cpp` makes a block text-diffusion model behave like a regular `llama-server`: load the GGUF once, listen on a port, answer `/v1/chat/completions`. Everything — tokenization, chat template, the block-diffusion denoising loop, and the HTTP API — runs in a single C++ process on top of the llama.cpp diffusion fork. No Python, no external tokenizer files, no per-step IPC. |
|
|
| Validated end-to-end on **CPU-only consumer hardware** (AMD Ryzen 5 5625U, 8 GB shared UMA, Kali Linux), serving a **DiffusionGemma 26B-A4B MoE (NVFP4 GGUF)** to a local chat UI over `:8080`. |
|
|
| --- |
|
|
| ## Why this exists |
|
|
| Diffusion language models cannot be served by the standard `llama-server`: generation is not autoregressive token-by-token decoding, but iterative **denoising of a fixed-length canvas** (`[prompt | canvas]` bidirectional forwards, region-aware masks, self-conditioning). Until now the options were a raw logits server driven by an external Python loop, or a one-shot CLI. This tool closes the gap: a persistent HTTP server speaking the OpenAI Chat Completions protocol, with the entire diffusion decode loop in-process. |
|
|
| ## Features |
|
|
| - **OpenAI-compatible API**: `POST /v1/chat/completions` (streaming via SSE and non-streaming), `GET /v1/models`, `GET /health`, `GET /` |
| - **Native tokenization** from the GGUF vocabulary (control tokens parsed atomically; no tokenizer.json needed) |
| - **Model chat template built in**: `<|turn>role\n … <turn|>` turns with `<|channel>thought … <channel|>` reasoning channels; thinking can be disabled per request (`"enable_thinking": false`, the default) by pre-filling an empty thought channel, or enabled globally with `--thinking` |
| - **Entropy-bound block-diffusion sampler** (see below), with all parameters read from GGUF metadata |
| - **Prompt-KV caching** (`DG_KVCACHE=1` / `--kvcache`): the prompt is prefilled once per block; each denoising step forwards only the canvas |
| - **Self-conditioning** across denoising steps (previous-step logits fed back from step 2 onward) |
| - **Multi-block generation**: when a block fills without an end-of-turn, it is appended to the prompt and a new canvas is denoised, until `max_tokens` or end of content |
| - **Streaming per block** with correct `finish_reason` (`stop` vs `length`) |
| - Memory-conscious: per-position statistics are computed in streaming passes over the logits — the probability matrix (`canvas × 262k vocab`) is never materialized |
|
|
| ## Requirements |
|
|
| 1. A **llama.cpp fork with the `diffusion-gemma` architecture** (the `DIFFUSION_GEMMA` model class providing `llama_diffusion_set_phase` / `llama_diffusion_set_sc`). |
| 2. A **DiffusionGemma GGUF** carrying the diffusion metadata: |
|
|
| | GGUF key | Meaning | |
| |---|---| |
| | `diffusion.canvas_length` | Canvas size per block (required; the C++ graph splits `[prompt \| canvas]` on it) | |
| | `diffusion.eb_max_steps` | Max denoising steps per block | |
| | `diffusion.eb_t_min` / `diffusion.eb_t_max` | Temperature schedule (linear, `t_max → t_min`) | |
| | `diffusion.eb_entropy_bound` | Per-position entropy bound for locking | |
| | `diffusion.eb_stability_threshold` | Consecutive stable-argmax steps required to lock | |
| | `diffusion.eb_confidence_threshold` | Reference-decoder parameter (read, reported, not used by this sampler — see Limitations) | |
|
|
| 3. The vendored headers already shipped with the llama.cpp tree: `vendor/cpp-httplib/httplib.h` (+ `httplib.cpp`) and `vendor/nlohmann/json.hpp`. |
|
|
| ## Build |
|
|
| Place the file at `tools/diffusion-gemma-http/diffusion-gemma-http.cpp` inside the fork, then: |
|
|
| ```cmake |
| # tools/diffusion-gemma-http/CMakeLists.txt |
| set(TARGET llama-diffusion-gemma-http) |
| add_executable(${TARGET} |
| diffusion-gemma-http.cpp |
| ${CMAKE_SOURCE_DIR}/vendor/cpp-httplib/httplib.cpp) |
| target_include_directories(${TARGET} PRIVATE |
| ${CMAKE_SOURCE_DIR}/vendor/cpp-httplib |
| ${CMAKE_SOURCE_DIR}/vendor) |
| target_link_libraries(${TARGET} PRIVATE llama Threads::Threads) |
| target_compile_features(${TARGET} PRIVATE cxx_std_17) |
| install(TARGETS ${TARGET} RUNTIME) |
| ``` |
|
|
| ```bash |
| echo 'add_subdirectory(diffusion-gemma-http)' >> tools/CMakeLists.txt |
| cmake -B build |
| cmake --build build --target llama-diffusion-gemma-http -j4 |
| ``` |
|
|
| ## Usage |
|
|
| ```bash |
| DG_KVCACHE=1 ./build/bin/llama-diffusion-gemma-http \ |
| -m model.gguf --port 8080 [--host 0.0.0.0] [-ngl N] [-c MAXTOK] [--thinking] |
| ``` |
|
|
| | Flag / env | Default | Description | |
| |---|---|---| |
| | `-m, --model` | — | GGUF path (positional also accepted) | |
| | `--port` / `--host` | `8080` / `0.0.0.0` | Bind address | |
| | `-c, --ctx` / `MAXTOK` | `2304` | Context budget = prompt + accumulated blocks. Non-causal forwards require the whole sequence in one ubatch, so the compute buffer scales with this — raise gradually on small-RAM machines | |
| | `-ngl` / `NGL` | `0` | GPU layers | |
| | `--kvcache` / `DG_KVCACHE=1` | off | Prompt-KV caching (strongly recommended on CPU) | |
| | `--thinking` / `DG_THINKING=1` | off | Enable the reasoning channel by default | |
| | `DG_MASK_ID` | auto (`<mask>`) | Override the canvas mask token id | |
| | `FA=1` | off | Flash attention | |
|
|
| ### API |
|
|
| ```bash |
| curl -s http://127.0.0.1:8080/v1/chat/completions -H 'Content-Type: application/json' -d '{ |
| "messages": [{"role": "user", "content": "Explique em duas frases o que é um número primo."}], |
| "max_tokens": 200, |
| "stream": false |
| }' |
| ``` |
|
|
| Any OpenAI-compatible client or front-end pointed at `http://host:8080` works unchanged. Per-request fields: `messages` (system merged into the first user turn), `max_tokens` / `max_completion_tokens`, `stream`, `enable_thinking`. |
|
|
| ## The sampler |
|
|
| The decode loop implements an **entropy-bound block-diffusion sampler**: |
|
|
| 1. The canvas starts fully masked. Each step runs one bidirectional forward and computes, per position, the best **real** token (mask excluded), its confidence, and the entropy of the mask-excluded distribution — in streaming passes, without materializing probabilities. |
| 2. A position **locks** when its argmax has been stable for `stability_threshold` consecutive steps **and** its entropy is below `entropy_bound`. |
| 3. A prediction of `<mask>` never locks: it means "not ready yet / end of content". Positions still masked when the model proposes nothing new for 2 consecutive steps signal **end of content** (the model's native length control). |
| 4. Minimum progress per step is guaranteed by confidence ranking; **adjacent positions never lock in the same step**, and an **anti-echo guard** defers locking a token identical to an already-locked neighbor (legitimate repetition persists and passes; denoising echoes dissolve). |
| 5. Temperature follows a linear `t_max → t_min` schedule; self-conditioning on the previous step's logits is active from step 2. |
|
|
| ### Honest limitations |
|
|
| - This sampler is a **validated approximation**, not a byte-exact port of the reference entropy-bound decoder: in particular, `diffusion.eb_confidence_threshold` is read and reported but plays no role in the locking rule, whose reference semantics differ from a naive confidence cutoff. Residual artifacts of parallel unmasking (rare token echoes) are mitigated by the guards above but not formally eliminated. |
| - Single-flight inference: concurrent requests are serialized by a mutex. |
| - Diffusion on CPU is compute-heavy: every denoising step is a dense forward over the canvas (plus the prompt without KV caching). Expect minutes, not seconds, for long answers on laptop-class CPUs. |
|
|
| ## Provenance |
|
|
| Developed iteratively against a live DiffusionGemma 26B-A4B (MoE, 30 layers, 262k vocab, canvas 256, Harmony-style `<|turn>`/`<|channel>` template) quantized to NVFP4 GGUF, debugged end-to-end from raw logits to a working chat UI. Part of the **ΩFFΣLLIα** local-first, zero-telemetry tooling line. |
|
|
| - Author: **Bruno Becker** — [huggingface.co/Brunobkr](https://huggingface.co/Brunobkr) |
| - Research: [doi.org/10.5281/zenodo.20026837](https://doi.org/10.5281/zenodo.20026837) |
| - Built on [llama.cpp](https://github.com/ggml-org/llama.cpp) (MIT) with a diffusion-gemma architecture fork; HTTP via [cpp-httplib](https://github.com/yhirose/cpp-httplib), JSON via [nlohmann/json](https://github.com/nlohmann/json). |
|
|
| ## License |
|
|
| MIT, following the llama.cpp ecosystem it extends. |
|
|