| 1 | <div align="center"> |
| 2 | |
| 3 | <img src="assets/logo.svg" alt="" width="72"> |
| 4 | |
| 5 | # Elffuss Engine |
| 6 | |
| 7 | **A modular WebGPU runtime for GGUF language models: a GGUF reader, a tokenizer and a sampler in plain ES modules, behind a small chat API. No dependencies, no build step.** |
| 8 | |
| 9 | [](#licence) |
| 10 | [](#layout) |
| 11 | [](#what-it-runs) |
| 12 | |
| 13 | [Elffuss](https://elffuss.com) |
| 14 | |
| 15 | </div> |
| 16 | |
| 17 | > **Provisional.** This is version 0.0.0, a snapshot of the engine published before its first release. The API below |
| 18 | > works and will change: read the [limits of this version](#limits-of-this-version) and the |
| 19 | > [changelog](CHANGELOG.md) before you depend on it. |
| 20 | |
| 21 | **In this README:** [quick start](#quick-start) · [what it runs](#what-it-runs) · [limits of this version](#limits-of-this-version) · [layout](#layout) · [how it is checked](#how-it-is-checked) · [licence](#licence) |
| 22 | |
| 23 | ## Quick start |
| 24 | |
| 25 | Inference runs in the page, on WebGPU, and the weights are read from Hugging Face with HTTP range requests. The page |
| 26 | needs a secure context (`https://`, or `http://localhost` and `http://127.0.0.1`) and a WebGPU adapter that offers the |
| 27 | `shader-f16` feature and a storage-buffer binding limit large enough for the model's largest tensor (see the |
| 28 | [limits of this version](#limits-of-this-version)). Serve this repository with any static file server and open a page |
| 29 | like this one from it. |
| 30 | |
| 31 | ```html |
| 32 | <!doctype html> |
| 33 | <meta charset="utf-8"> |
| 34 | <pre id="out"></pre> |
| 35 | <script type="module"> |
| 36 | import * as engine from './js/provider.js'; |
| 37 | |
| 38 | console.log(engine.models()); // [{ id, label }, …] |
| 39 | engine.configure('qwen35-0.8b'); // the small model from Hugging Face (already the default) |
| 40 | await engine.load((status) => console.log(status)); |
| 41 | |
| 42 | engine.setSampling({ temperature: 0.7, seed: 1, maxTokens: 120 }); |
| 43 | const out = document.getElementById('out'); |
| 44 | const reply = await engine.chat( |
| 45 | [{ role: 'user', content: 'Write one short sentence about the sea.' }], |
| 46 | 'You are a concise assistant.', |
| 47 | (piece) => { out.textContent += piece; }, |
| 48 | ); |
| 49 | console.log(reply); |
| 50 | |
| 51 | await engine.unload(); |
| 52 | </script> |
| 53 | ``` |
| 54 | |
| 55 | This exact page is the one we ran, in headless Chrome, against this tree served from `http://127.0.0.1`. |
| 56 | |
| 57 | - `models()` lists the registry: an `id` and a `label` per model. |
| 58 | - `configure(id)` selects a model. It returns `true` when the selection changes, and `false` otherwise, also for an |
| 59 | `id` that does not exist. |
| 60 | - `load(onProgress)` downloads and prepares the selected model; `onProgress` receives status text. |
| 61 | - `setSampling({ temperature, seed, p, k, minP, presence, repeatPenalty, maxTokens, razonar })` merges into the |
| 62 | current sampling settings, where `p` is top-p and `k` is top-k; other keys, `topP` and `topK` among them, are |
| 63 | ignored in this version. For a model that reasons, `razonar: true` or `false` forces reasoning on or off, and |
| 64 | `null` leaves it to the registry. `setSampling(null)` goes back to the model's defaults. |
| 65 | - `chat(history, system, onToken, options)` runs one turn. `history` is an array of `{ role, content }`, and any |
| 66 | role other than `assistant` is read as `user`; `system` is the system prompt; `onToken` receives each piece of |
| 67 | text as it is generated; the promise resolves to the reply. |
| 68 | - To stop a turn, pass `{ signal }` as `options` (a bare `AbortSignal` or an `AbortController` works too), or call |
| 69 | `abort()`, which stops the turn in progress and the ones waiting behind it. A stopped turn rejects with an error |
| 70 | whose `name` is `AbortError`; its `partial` holds the text emitted until then, and `porDescarga` is `true` when |
| 71 | `unload()` or a `configure()` to another model stopped it. |
| 72 | - `unload()` releases the model and the WebGPU device. |
| 73 | |
| 74 | ## What it runs |
| 75 | |
| 76 | - The model of the quick start: Qwen3.5 0.8B in `Q8_0`, from |
| 77 | [`ggml-org/Qwen3.5-0.8B-GGUF`](https://huggingface.co/ggml-org/Qwen3.5-0.8B-GGUF) on Hugging Face, Apache-2.0 as its |
| 78 | GGUF header declares. Its architecture is `qwen35`, a hybrid of state-space and attention layers. |
| 79 | - `models()` also lists two larger models that are hosted for the Elffuss apps. They are not meant to be loaded from |
| 80 | other sites. |
| 81 | - Weight types the engine reads: `F32`, `F16`, `BF16`, `Q8_0`, `Q2_K`, `Q4_K`, `Q5_K`, `Q6_K`, `IQ1_S`, `IQ2_XXS`, |
| 82 | and two third-party ternary formats, `PQ2_0` and `PTQ1_0`. |
| 83 | |
| 84 | Other GGUF files may fail to load, or load and answer worse without an error: the engine has two architecture |
| 85 | profiles (`qwen35` and `llama`), reads only the `gpt2` tokenizer family, and always writes a ChatML prompt instead of |
| 86 | reading the chat template of the file. |
| 87 | |
| 88 | ## Limits of this version |
| 89 | |
| 90 | - **WebGPU features.** The attention cache is stored in half precision, so the engine needs the `shader-f16` feature: |
| 91 | without it, `load()` finishes and the first `chat()` fails when it compiles its shaders. It also refuses a model |
| 92 | that has a tensor larger than the adapter's `maxStorageBufferBindingSize`, a limit it asks the adapter for. |
| 93 | - **No cache of its own.** Inside the Elffuss sites the host app keeps the model in the browser. Anywhere else, every |
| 94 | `load()` reads the model from the network again. Even so, `load()` first checks the browser's storage estimate as if |
| 95 | it were going to keep the model, and refuses with a message about disk space when the estimate is too small. |
| 96 | - **Host files.** `load()` and `chat()` look for optional files of the host app, described under Bundlers below. |
| 97 | Outside the Elffuss sites they answer 404, which the browser console shows, and the engine carries on without them |
| 98 | (for the missing store it also logs a warning). |
| 99 | - **History.** Outside the Elffuss apps the engine keeps the last 12 messages of `history`, then fits them to the |
| 100 | context. |
| 101 | - **One model per page.** The module holds a single engine; selecting another model unloads the current one. |
| 102 | - **Spanish messages.** Progress and error texts are in Spanish, and the console gets a few lines per turn. |
| 103 | - **Reasoning is streamed too.** With a model that reasons, `onToken` also receives the reasoning; the resolved reply |
| 104 | does not include it. |
| 105 | - **Bundlers.** The engine looks for three optional files of the host app (`../runtime/model-store.js`, |
| 106 | `../runtime/model-broker.js` and `../context.js`) with literal relative imports, and carries on without them. A |
| 107 | bundler may try to resolve those paths when it builds; serve the modules as they are. |
| 108 | |
| 109 | ## Layout |
| 110 | |
| 111 | ``` |
| 112 | js/ the runtime: 19 ES modules, served as they are written |
| 113 | provider.js the chat API of the quick start |
| 114 | registro.js the model registry; it imports nothing, so a page can list models without the engine |
| 115 | gguf.js the GGUF reader, which reads by byte ranges |
| 116 | tokenizer.js the tokenizer |
| 117 | sample.js the sampler |
| 118 | model.js a model session on WebGPU |
| 119 | device.js the WebGPU device |
| 120 | the rest the shaders (kernels.*.wgsl.js) and what runs them (ops.js, state.js, hadamard.js, |
| 121 | quant-tables.js) |
| 122 | assets/ the logo |
| 123 | ``` |
| 124 | |
| 125 | This repository receives curated snapshots of the engine. Development happens elsewhere, so the history here moves in |
| 126 | snapshots. |
| 127 | |
| 128 | ## How it is checked |
| 129 | |
| 130 | We check it internally against llama.cpp: the GGUF reader against llama.cpp's reader, the tokenizer token by token |
| 131 | against `llama-tokenize`, the weight types against llama.cpp's decoder (the two ternary formats, which llama.cpp |
| 132 | does not have, against their published definition), and forward passes, layer by layer, against llama.cpp's |
| 133 | activations for the model formats that llama.cpp reads. The ternary model is checked piece by piece and end to end. |
| 134 | Those tests and their reference data are not part of this repository. |
| 135 | |
| 136 | ## Licence |
| 137 | |
| 138 | Apache-2.0, see [LICENSE](LICENSE). `js/quant-tables.js` carries grid tables copied from llama.cpp under the MIT licence |
| 139 | of the ggml authors, reproduced in full at the top of that file; see [NOTICE](NOTICE). |