Install Wasm Build Environment

This page describes the steps to setup build environment for WebAssembly and WebGPU builds.

Step 1: Install EMSDK

Emscripten is an LLVM-based compiler that compiles C/C++ source code to WebAssembly. We need to install emscripten for webgpu build.

  • Please follow the installation instruction here to install the latest emsdk.

  • Source path/to/emsdk_env.sh so emcc is reachable from PATH and the command emcc works.

Validate that emcc is accessible in shell

emcc --version

Step 2: Set TVM_HOME and MLC_LLM_HOME

We need to set a path to a tvm source in order to build tvm runtime. Note that you do not need to build tvm unity from the source. The source here is only used to build the web runtime component. Set environment variable in your shell startup profile in to point to 3rdparty/tvm (if preferred, you could also point to your own TVM address if you installed TVM from source).

Besides, we also need to set MLC_LLM_HOME so that we can locate mlc_wasm_runtime.bc when compiling a model library wasm.

export TVM_HOME=/path/to/3rdparty/tvm
export MLC_LLM_HOME=/path/to/mlc-llm

Step 3: Prepare Wasm Runtime

First, we need to obtain a copy of the mlc-llm source code for the setup script

git clone https://github.com/mlc-ai/mlc-llm.git --recursive
cd mlc-llm

Now we can prepare wasm runtime using the script in mlc-llm repo

./web/prep_emcc_deps.sh

We can then validate the outcome

>>> echo ${TVM_HOME}

/path/set/in/step2

>>> ls -l ${TVM_HOME}/web/dist/wasm/*.bc

tvmjs_support.bc
wasm_runtime.bc
webgpu_runtime.bc