This experimental sample demonstrates one way to target the web platform with IREE. The output artifact is a web page containing an interactive MNIST digits classifier.
The MNIST ML model is compiled statically together with the IREE runtime into a single .js + .wasm bundle.
install
target with CMake)emsdk_env.bat
)bash ./build_sample.sh [path to install] && bash ./serve_sample.sh
To rebuild most parts of the sample (C runtime, sample HTML, CMake config, etc.), just control + C
to stop the local webserver and rerun the script.
This MNIST model, also used in the Vision sample, is compiled using the “static library” output setting of IREE's compiler (see the Static library sample). The resulting .h
and .o
files are compiled together with main.c
, while the .vmfb
is embedded into a C file that is similarly linked in.
Emscripten is used (via the emcmake
CMake wrapper) to compile the output binary into WebAssembly and JavaScript files.
The provided index.html
file can be served together with the output .js
and .wasm
files.
iree_api.js
exposes a Promise-based API to the hosting application in index.html
iree_api.js
creates a worker running iree_worker.js, which includes Emscripten's JS code and instantiates the WebAssembly moduleiree_api.js
and iree_worker.js
internallyThe sample supports running both single-threaded using device_sync.c
(backed by the local HAL‘s ‘sync’ device) and multi-threaded using device_multithreaded.c
(backed by the local HAL’s ‘task’ device).
Each configuration is offered as a CMake target, then iree_worker.js
specifies which script URL to load.
Multithreading requires Web Workers and SharedArrayBuffer: