Loading page…
Loading page…
Lab / Inference engine
I wrote a transformer forward pass in C++ and compiled it to WebAssembly.
Hugging Face trained HuggingFaceTB/SmolLM2-135M-Instruct. I fine-tuned it on retrieved-source answers and refusals, quantised its weights, and wrote the inference engine. The smaller tadiwa-nano model was trained on this site's prose.
Both run locally after their weights load. When available, the hosted model offers a comparison: selecting it sends your question to the server for retrieval and generation through an external API.
The test replays a fixed prompt through the C++ engine, including its KV cache, one token at a time. These saved results compare the final logits with PyTorch. They check the tested prompt and weights; they do not establish that every input will match.
| Weights | Max |diff| | RMS | Argmax | Verdict |
|---|---|---|---|---|
| fp32 | 5.25e-5 | 1.57e-5 | match | pass |
| int8 | 1.34e+0 | 2.06e-1 | match | pass |
| Weights | Max |diff| | RMS | Argmax | Verdict |
|---|---|---|---|---|
| fp32 | 7.63e-6 | 2.74e-6 | match | pass |
| int8 | 2.41e-1 | 8.87e-2 | match | pass |
Max |diff| is the largest logit difference; RMS describes the overall difference. Argmax checks whether both engines select the same next token. Quantisation can change the logits even when that token agrees.
Grounded mode retrieves passages from this site before generation. A LoRA fine-tune teaches the model to answer from those sources or decline when they do not contain an answer. These saved evaluations compare the base model with that fine-tune on held-out pages.
| Model | Correct refusals | Incorrect refusals | Source overlap |
|---|---|---|---|
| SmolLM2 stock | 0% | 0% | 6% |
| + grounded LoRA | 100% | 7% | 94% |
Correct refusals are measured on unanswerable questions; incorrect refusals on answerable ones. Source overlap measures generated five-word sequences found verbatim in the retrieved passages. It misses faithful paraphrases and can reward quotations that do not answer the question. It is not a factual-accuracy score.
10 of 10 matching their golden fixtures · tolerance 2e-4
NumPy produces the reference fixtures. The softmax fixture includes an extreme value to test numerical stability: a version without max-subtraction overflows and fails that case.
The matrix multiply uses a cache-aware triple loop, with SIMD left to the compiler. Generation speed depends on your device and appears alongside each run. Blocking, hand-written SIMD, and fused dequantisation remain future work.
The models can produce incorrect answers even with retrieved sources. This demo exercises the path from a weights file to a token: loading, tokenisation, attention, the KV cache, quantised kernels, and sampling. The SmolLM2 weights download is 129 MB.
The smaller model has 359K parameters and was trained on 64 KB of prose. Its saved training loss is 1.19 and validation loss is 1.94. It is included to test the engine with a small model trained on this site.