llama.cpp in a browser tab

February – May 2023 · archived · backend intern · internal R&D

Layered stack diagram: Brave and Node on top, a JavaScript adapter, the Emscripten module with generated glue and main.wasm, then the original C++ sources unchanged.
The stack, as reconstructed · drawn for this site

The problem

Final semester of college, 2023, on a backend internship at Tmotions. I wanted a language model to run where I already was, in a browser tab, without sending the weights out of the machine. llama.cpp already knew how to do inference. The browser did not look like Linux. This was internal R&D on internship time, not coursework.

What I did

I compiled ggml and llama.cpp with Emscripten, then wrote a JavaScript runtime adapter so the same .wasm could load a quantised model, tokenise, and run inference in Node and in Brave.

I did not port llama.cpp. I compiled main and called it from JS. The adapter is the host OS that WASM does not have: it creates the module, feeds it a fake filesystem, intercepts printf, and dumps the text into the DOM.

Method

Linear memory was the bottleneck, so the models stayed small. The test was crude and honest: does a token appear in Brave. The chat UI had an anime girl for an avatar, blue and pink hair, white cyberpunk clothes. She was decoration, not a character.

What I found

The interesting work was not the model. It was making a browser look enough like a host that an unmodified binary would start. Most of the difficulty was systems work, not machine learning, and that ratio has held for every local-inference project since.

Open

The source died with the laptop. This record is part memory, part reconstruction, and it does not claim that every compiler flag is remembered correctly. A rebuilt demo is still owed.