Skip to content

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

llama component example

llama.cpp, running in the browser as a WebAssembly component, driving the GPU through wasi:webgpu.

How it works

llama.cpp has no knowledge of the web platform. It is compiled against WASI interfaces (wasi:webgpu, wasi:filesystem, …) and packaged as a component behind a small WIT API. jco then transpiles that component into an ES module:

import { api } from './generated/llama.js';
const { Model, Context, Sampler } = api;

const model = await new Model(bytes);
const tokens = await model.tokenize(prompt, true);
const token = await sampler.sample(ctx);

Jco derives classes, promises, typed arrays, etc. from the WIT types, so the API is idiomatic JavaScript rather than a C FFI. The JavaScript side never touches C++, the C++ side never touches JavaScript, and the entire contract between them is a few lines of WIT.

Build & run

npm install
npm run fetch-llama-wit   # pull the compiled component from ghcr.io
npm run build             # generate types + transpile to src/generated
npx http-server .         # any static file server works

The app depends on WebGPU and JSPI.

Firefox is slow

Firefox polls GPU devices on a 100ms timer, so every GPU sync costs ~96ms. Fix in progress upstream (bugzilla#1870699).

WIP

Releases

Packages

Contributors

Languages