# diffuse-cpp High-performance C++ inference engine for Diffusion Language Models, built on GGML. [![License: AGPL-3.0](https://img.shields.io/badge/License-AGPL%203.0-blue.svg)](https://www.gnu.org/licenses/agpl-3.0) [![C++17](https://img.shields.io/badge/C%2B%2B-17-blue.svg)](https://en.cppreference.com/w/cpp/17) [![GGML](https://img.shields.io/badge/GGML-v0.9.8-green.svg)](https://github.com/ggerganov/ggml) ## Highlights - **15–28 tok/s on easy real prompts** with Q4_K_M + entropy_exit + inter-step cache + 256-token generation - **Up to 3.2x faster than llama.cpp** (8.51 tok/s) on the same hardware - **Inter-step KV cache**: 1.6x average speedup with no quality degradation - **6 of 8 real prompts outperform llama.cpp** (vs 3 of 8 without cache) - **~6x speedup** vs F16 baseline on easy prompts (factual, translation, arithmetic) - **Adaptive scheduling**: 2-4 steps for easy prompts, 16 for hard — the model decides - **5.1 GB quantized model** (vs 14.9 GB F16) - **Never worse**: entropy_exit + cache maintains quality across all prompt types ## What is diffuse-cpp? diffuse-cpp is to Diffusion Language Models what llama.cpp is to autoregressive LLMs: a fast, portable, quantization-enabled inference engine. Diffusion LLMs (dLLMs) like LLaDA generate all tokens in parallel through an iterative refinement process, rather than sequentially left-to-right. This shifts inference from memory-bound to compute-bound, making aggressive quantization and SIMD optimizations highly effective on CPU. Until now, dLLMs ran exclusively on GPU with PyTorch. diffuse-cpp brings them to CPU with near-interactive performance. ## Benchmark Results All benchmarks: LLaDA-8B-Instruct, AMD EPYC 4465P 12-Core (24 threads), 125GB RAM, steps=16, 3 reps + 1 warmup. ### Real Prompt Performance (Q4_K_M, entropy_exit, B=256, steps=16, threads=12) | Prompt | No-Cache tok/s | Cache tok/s | Steps | vs llama.cpp | |---|---|---|---|---| | Capital of France? | 17.5 | **24.4** | 3 | 2.9x | | Translate to French | 25.9 | **27.7** | 2 | 3.3x | | 15 × 23? | 12.8 | **15.7** | 4 | 1.8x | | Translate to Spanish | 7.6 | **22.9** | 7 | 2.7x | | Python is_prime() | 3.2 | **4.9** | 16 | 0.6x | | Poem about ocean | 3.2 | **5.3** | 16 | 0.6x | | Why is sky blue? | 3.3 | **12.0** | 16 | 1.4x | | List the planets | 3.3 | **9.4** | 15 | 1.1x | | **Average** | **9.6** | **15.3** | | **1.8x** | *llama.cpp baseline: 8.51 tok/s (Llama-3-8B Q4_K_M, same hardware). Cache enabled by default. 6 of 8 prompts outperform llama.cpp; 2 (code generation, creative writing) remain slower due to requiring all 16 steps.* ### Quantization Performance (steps=16, threads=12, B=64) | Model | Size | low_confidence | Speedup vs F16 | |-------|------|----------------|-----------------| | F16 | 14.9 GB | 1.64 tok/s | 1.00x | | Q8_0 | 8.4 GB | 1.84 | 1.12x | | Q4_K_M | 5.1 GB | 2.52 | **1.54x** | ### Thread Scaling (Q4_K_M, steps=16) | Threads | low_confidence | Scaling | |---------|---------------|---------| | 1 | 0.34 tok/s | 1.0x | | 4 | 1.18 | 3.5x | | 12 | 2.52 | 7.5x | | 24 | 2.21 | 6.6x | ## Quick Start ### Build ```bash git clone --recursive https://github.com/iafiscal1212/diffuse-cpp.git cd diffuse-cpp cmake -B build -DCMAKE_BUILD_TYPE=Release cmake --build build -j$(nproc) ``` ### Basic Usage ```bash # Pre-tokenize your prompt (diffuse-cpp does not include a tokenizer) # Use transformers or tiktoken to get token IDs as a comma-separated list # Generate text (example with token IDs) ./build/diffuse-cli \ -m model.gguf \ --tokens "128000,3923,374,279,6864,315,9822,30" \ -n 256 \ -s 16 \ -t 12 \ --remasking entropy_exit ``` **Note**: diffuse-cpp operates on token IDs, not raw text. Use the HuggingFace transformers library to tokenize your prompts: ```python from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("LLaDA-8B-Instruct") tokens = tokenizer.encode("What is the capital of France?") print(",".join(map(str, tokens))) ``` ## Quantization Convert a model from HuggingFace to GGUF F16: ```bash python tools/convert-llada.py \ --input /path/to/LLaDA-8B-Instruct \ --output llada-8b-f16.gguf ``` Quantize to Q4_K_M (recommended for best performance): ```bash ./build/diffuse-quantize \ llada-8b-f16.gguf \ llada-8b-q4km.gguf \ Q4_K_M ``` Available quantization formats: - `Q4_K_M`: 4-bit mixed precision (recommended, 5.1 GB) - `Q8_0`: 8-bit (8.4 GB, higher quality) - `F16`: 16-bit float (14.9 GB, reference quality) ## Supported Models | Model | Architecture | Status | |-------|-------------|--------| | LLaDA-8B | Masked diffusion (Llama backbone) | Production | | SEDD | Score-based diffusion | Planned | | MDLM | Masked discrete diffusion | Planned | ## How It Works Diffusion LLMs generate text through iterative refinement: 1. Start with all tokens masked 2. Forward pass: predict logits for all positions 3. Unmask a fraction of tokens (lowest entropy first) 4. Repeat until all tokens are unmasked Unlike autoregressive models (one token per forward pass), diffusion models generate all tokens in parallel, reading the model weights once per step instead of once per token. This makes quantization extremely effective: the reduced memory bandwidth directly translates to faster inference. ## entropy_exit: Semantic Scheduling Standard diffusion schedulers use a fixed number of steps regardless of prompt difficulty. entropy_exit is a semantic scheduler that lets the model decide when to stop: - **Easy prompts** (translation, factual, arithmetic): converges in 2-4 steps → **15–28 tok/s** (B=256, cache) - **Medium prompts** (code, complex translation): 7-15 steps → **5–23 tok/s** (B=256, cache) - **Hard prompts** (creative writing, open-ended reasoning): uses all configured steps → **5–12 tok/s** (B=256, cache) - **Never worse**: maintains quality across all prompt types Based on systematic benchmarking across 42 prompts in 12 categories: - **55% of prompts** achieve >20% speedup - **Translation**: 2.71x (language-dependent, 1.4x–4.2x) - **Simple code**: 2.21x (factorial, reverse string, is_prime) - **Creative writing**: 1.0x (no speedup, but no degradation either) Estimated speedup on mixed chatbot traffic: **~1.8x** (weighted by typical task distribution). ### How It Works After each forward pass, entropy_exit: 1. Computes entropy for each masked position 2. Unmasks all positions below threshold (default: 1.5) 3. Terminates early if all tokens are unmasked Zero overhead: entropy computation is negligible vs forward pass cost. ## Inter-Step KV Cache diffuse-cpp caches the Key and Value tensors from each transformer layer between denoising steps. Instead of recomputing all positions at every step, only the "active set" is recomputed: - **Masked positions**: tokens still being refined - **Recently unmasked positions**: tokens whose hidden states may have changed Positions that were unmasked in earlier steps and haven't changed reuse their cached K,V. This reduces the per-step cost from O(N²) to O(N_active × N) per layer, where N_active shrinks as tokens converge. **Average speedup: 1.6x** across 8 real prompts (from 9.6 to 15.3 tok/s). Speedup is highest on prompts that use many steps (up to 3.7x on 16-step prompts) and minimal on prompts that already converge in 2-3 steps. The cache acts as implicit regularization: reusing stable K,V from previous steps prevents attention from amplifying accumulated errors. In 3 of 8 benchmarked prompts, cache output quality is measurably better than no-cache. ```bash # Cache is ON by default. To disable: ./build/diffuse-cli -m model.gguf --tokens "..." -n 256 -s 16 -t 12 --no-cache # Keep recently-changed positions active for N extra steps (higher fidelity, slightly slower): ./build/diffuse-cli -m model.gguf --tokens "..." -n 256 -s 16 -t 12 --cache-keep-active 5 ``` ## Building From Source ### Requirements - C++17 compiler (GCC 8+, Clang 7+, MSVC 2019+) - CMake 3.14+ - Git (for GGML submodule) ### Full Build ```bash git clone --recursive https://github.com/iafiscal1212/diffuse-cpp.git cd diffuse-cpp # Release build (optimized) cmake -B build -DCMAKE_BUILD_TYPE=Release cmake --build build -j$(nproc) # Tools (CLI, bench, quantize) are built by default # To disable: -DDIFFUSE_BUILD_TOOLS=OFF ``` Binaries: - `build/diffuse-cli`: command-line inference - `build/diffuse-quantize`: model quantization - `build/diffuse-bench`: benchmarking ## API diffuse-cpp provides a C++ API for embedding in other applications: ```cpp #include "diffuse.h" // Load model diffuse_model* model = diffuse_model_load("model.gguf", 12); // Create context diffuse_context* ctx = diffuse_context_new(model, 128, 12); // Configure sampler diffuse_sampler_params params; params.n_steps = 16; params.remasking = diffuse_remasking::ENTROPY_EXIT; params.entropy_threshold = 1.5f; // Generate std::vector prompt_tokens = {128000, 3923, 374}; auto output = diffuse_generate(ctx, prompt_tokens, 256, params); // Cleanup diffuse_context_free(ctx); diffuse_model_free(model); ``` See `include/diffuse.h` for full API documentation. ## Performance Tips 1. **Use Q4_K_M quantization**: best throughput/quality tradeoff 2. **Enable entropy_exit**: free speedup on 55% of prompts 3. **Keep inter-step cache ON** (default): 1.6x average speedup with no quality loss 4. **Thread count**: optimal = physical cores (hyperthreading reduces performance) 5. **Steps**: start with 16, reduce to 8 if quality is acceptable 6. **Entropy threshold**: 1.5 is a good default; increase to 2.0 for more aggressive early exit ## Project Status diffuse-cpp is production-ready for LLaDA-8B models. Additional architectures (SEDD, MDLM) are planned. Current limitations: - No integrated tokenizer (use transformers) - Default 256 generated tokens per call (configurable via -n flag) - Single-model inference only (no batching) - CPU-only (GPU support via GGML is possible but not prioritized) ## Contributing Contributions welcome. Areas of interest: - Additional model architectures (SEDD, MDLM) - Improved scheduling heuristics - Tokenizer integration - Multi-query batching ## License AGPL-3.0. Commercial licenses available upon request. For commercial licensing inquiries, contact: caresment@gmail.com ## Citation If you use diffuse-cpp in your research, please cite: ```bibtex @software{diffuse_cpp_2026, title={diffuse-cpp: High-Performance Inference for Diffusion Language Models}, author={Carmen Estévez}, year={2026}, url={https://github.com/iafiscal1212/diffuse-cpp} } ``` ## Acknowledgments - Built on [GGML](https://github.com/ggerganov/ggml) by Georgi Gerganov - Inspired by [llama.cpp](https://github.com/ggerganov/llama.cpp) - Benchmarking methodology adapted from llama.cpp - entropy_exit scheduler informed by prior work on entropy-adaptive sampling (EAGS, Fast-dLLM)