Blog

23 novembre 2021 · Go · WebAssembly · XGBoost

the future is now.

A pretrained XGBoost model, compiled from Go to WebAssembly, runs inside the browser and ships with its own web server in a 14 MB folder. Nothing to install, no server to rent.

Article en anglais.

VGAN generated picture by Erfurt Latrine Disaster @ErfurtLatrine

A machine-learning model can be delivered as a folder. In this prototype, a classifier trained in Python is scored by Go code compiled to WebAssembly, inside the user's browser. The folder holds a 7.1 MB WebAssembly module, a 6.7 MB web server and three small files: 14 MB in all, started with a double click.

The constraint was practical. A small interactive tool had to reach non-technical users and could not be deployed on a server. R Shiny or a Python equivalent would have meant installing and updating a language runtime on every machine; Docker on Windows, before WSL2, was its own ordeal. The question was how little the user could be asked to install. The answer here is nothing.

Two files carry the weight

The model scores in the browser, so the only things to deliver are the compiled module, a page that loads it and something to serve both. The module and the server account for 99.5% of the bytes. The page, the JavaScript glue that Go provides for WebAssembly and a 499-row test file together weigh 71 KB.

The module is large for what it does because a Go build carries its runtime and the standard-library packages the program imports, here net/http to fetch the model and the data, and fmt to log. The server is sheret, a single-file static server written in Go, under 7 MB. Go also cross-compiles to a Windows .exe without extra tooling, which is what makes the double-click delivery possible.

How it works

  1. An XGBoost binary classifier is trained in Python (scikit-learn split, binary:logistic objective) and saved with save_model("model.bst").
  2. Go reads the saved model with the leaves package, a pure-Go implementation of tree-ensemble prediction, so no XGBoost C library is needed.
  3. The Go program is compiled to WebAssembly and exposes a function to the page through syscall/js.

The core of the Go side is three calls: load the model, read the features into a dense matrix, predict.

model, _ := leaves.XGEnsembleFromReader(bufio.NewReader(bytes.NewReader(b)), true)
dense, _ := mat.DenseMatFromCsv(bufio.NewReader(bytes.NewReader(csv)), 0, false, ",", 0.0)
model.PredictDense(dense.Values, dense.Rows, dense.Cols, predictions, 0, 1)

The build is one line:

GOOS=js GOARCH=wasm go build -o demo/main.wasm

In the prototype, the module scores a test file of 499 customers with 38 features each, and logs every churn probability and the resulting retention rate to the browser console.

Why Go

The tooling was the first argument: the VS Code extension, debugger and linter work out of the box, and a small project compiles in a few seconds. Concurrency is built into the language through goroutines. And for a compiled language, Go is fast. In a 2017 comparison of 27 languages on ten benchmark problems, Go took 2.83 times as long as C; Python took 71.90 times as long, about 25 times slower than Go. Go also needed the second-smallest memory of all, 1.05 times the leanest language.

Those figures are for native code. WebAssembly runs in the browser's sandbox and is not measured in the paper; the prototype did not time it either.

What it is not

The page still loads jQuery, Bootstrap, Handsontable and Google Charts from public CDNs, so as committed it needs an internet connection; bundling those libraries into the folder would make it fully offline. The trained model file in the repository is empty, so a reader has to train and export their own before the demo returns predictions. And this is a prototype: the function exposed to the page is a stub, and the scoring runs once, at load.

Browser-side computation is no longer exotic. Google Earth, once a desktop application, now runs in the browser on WebAssembly, and JupyterLite runs a Python notebook the same way.

About the code

Code: webassembly_xgboost_demo on GitHub, adapted from Vanessa Sochat's regression-wasm (Lawrence Livermore National Laboratory). File sizes are measured on the repository's xgb-wasm/demo folder. Benchmark figures are from Pereira et al., Energy Efficiency across Programming Languages, SLE 2017, Table 4.