├── .gitignore ├── README.md ├── build.py ├── index.html └── publish_gh_pages.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | dist 3 | *.bak 4 | .gitignore 5 | .pkl* 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TVM WebGPU Example 2 | ================== 3 | 4 | NOTE: This example is out dated, checkout https://github.com/mlc-ai/web-stable-diffusion instead 5 | 6 | This is an example project for building a tvm webgpu-backed module and deploy it to the web. 7 | 8 | ## Try it out 9 | 10 | - You can try it out at https://tqchen.com/tvm-webgpu-example/ 11 | - You will need a WebGPU enabled browser 12 | 13 | 14 | ## Build from Source 15 | 16 | You will need the latest version of TVM on the master. 17 | Please install the TVM via source build(by setting the PYTHONPATH). 18 | Checkout the steps under `tvm/web` to enable webgpu support. 19 | 20 | Build the dependent files in the `tvm/web` 21 | ```bash 22 | cd /path/to/tvm/web 23 | make 24 | npm run bundle 25 | ``` 26 | 27 | Then run the build script to build the model specific data. 28 | 29 | ```bash 30 | cd /path/to/tvm-webgpu-examples 31 | python3 build.py 32 | ``` 33 | 34 | You can now serve the content under `dist` 35 | ```bash 36 | cd /path/to/tvm-webgpu-examples/dist 37 | python3 http.server 38 | ``` 39 | 40 | You can also publish to github using the `publish_gh_pages` script. 41 | -------------------------------------------------------------------------------- /build.py: -------------------------------------------------------------------------------- 1 | """Example script to create tvm wasm modules and deploy it.""" 2 | 3 | import argparse 4 | import os 5 | from tvm import relay 6 | from tvm.contrib import util, emcc 7 | from tvm import rpc 8 | from tvm.contrib.download import download_testdata 9 | import tvm 10 | from tvm._ffi import libinfo 11 | import shutil 12 | import logging 13 | import json 14 | 15 | curr_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__))) 16 | 17 | 18 | def build_module(opts): 19 | network = opts.network 20 | build_dir = opts.out_dir 21 | 22 | dshape = (1, 3, 224, 224) 23 | from mxnet.gluon.model_zoo.vision import get_model 24 | block = get_model(network, pretrained=True) 25 | 26 | shape_dict = {"data": dshape} 27 | mod, params = relay.frontend.from_mxnet(block, shape_dict) 28 | func = mod["main"] 29 | func = relay.Function(func.params, 30 | relay.nn.softmax(func.body), None, 31 | func.type_params, func.attrs) 32 | 33 | target_device = "webgpu -modle=1080ti" 34 | target_host = "llvm -target=wasm32-unknown-unknown-wasm -system-lib" 35 | 36 | with relay.build_config(opt_level=3): 37 | graph, lib, params = relay.build( 38 | func, target=target_device, target_host=target_host, params=params) 39 | 40 | wasm_path = os.path.join(build_dir, f"{network}.wasm") 41 | lib.export_library(wasm_path, emcc.create_tvmjs_wasm) 42 | print(f"Exporting library to {build_dir}...") 43 | 44 | with open(os.path.join(build_dir, f"{network}.json"), "w") as f_graph_json: 45 | f_graph_json.write(graph) 46 | with open(os.path.join(build_dir, f"{network}.params"), "wb") as f_params: 47 | f_params.write(relay.save_param_dict(params)) 48 | 49 | 50 | def prepare_data(opts): 51 | """Build auxiliary data file.""" 52 | # imagenet synset 53 | synset_url = ''.join(['https://gist.githubusercontent.com/zhreshold/', 54 | '4d0b62f3d01426887599d4f7ede23ee5/raw/', 55 | '596b27d23537e5a1b5751d2b0481ef172f58b539/', 56 | 'imagenet1000_clsid_to_human.txt']) 57 | synset_name = 'imagenet1000_clsid_to_human.txt' 58 | synset_path = download_testdata(synset_url, synset_name, module='data') 59 | with open(synset_path) as f: 60 | synset = eval(f.read()) 61 | build_dir = opts.out_dir 62 | with open(os.path.join(build_dir, "imagenet1k_synset.json"), "w") as f_synset: 63 | f_synset.write(json.dumps(synset)) 64 | # Test image 65 | image_url = "https://homes.cs.washington.edu/~moreau/media/vta/cat.jpg" 66 | image_fn = os.path.join(build_dir, "cat.png") 67 | download_testdata(image_url, image_fn) 68 | # copy runtime js files. 69 | shutil.copyfile(libinfo.find_lib_path("tvmjs.bundle.js")[0], 70 | os.path.join(build_dir, "tvmjs.bundle.js")) 71 | shutil.copyfile(libinfo.find_lib_path("tvmjs_runtime.wasi.js")[0], 72 | os.path.join(build_dir, "tvmjs_runtime.wasi.js")) 73 | shutil.copyfile(os.path.join(curr_dir, "index.html"), 74 | os.path.join(build_dir, "index.html")) 75 | 76 | 77 | if __name__ == "__main__": 78 | parser = argparse.ArgumentParser() 79 | parser.add_argument("-o", "--out-dir", default=os.path.join(curr_dir, "dist")) 80 | parser.add_argument("--network", default="mobilenet1.0") 81 | parser.add_argument('-p', '--prepare', action='store_true') 82 | 83 | opts = parser.parse_args() 84 | 85 | build_dir = os.path.abspath(opts.out_dir) 86 | if not os.path.isdir(build_dir): 87 | os.makedirs(build_dir) 88 | opts.out_dir = build_dir 89 | 90 | if opts.prepare: 91 | prepare_data(opts) 92 | else: 93 | prepare_data(opts) 94 | build_module(opts) 95 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | TVM WebGPU MobileNet Demo 4 | 5 | 6 | 7 | 176 | 177 |

TVM WebGPU Demo

178 | 194 |

Options

195 | Image URL

201 | 202 |
203 | 204 |
205 |
206 | 207 | 208 | -------------------------------------------------------------------------------- /publish_gh_pages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Publish to gh-pages branch. 3 | set -e 4 | set -u 5 | 6 | cleanup() 7 | { 8 | git checkout master 9 | } 10 | trap cleanup 0 11 | 12 | git fetch 13 | cp .gitignore .gitignore.bak 14 | git checkout -B gh-pages origin/gh-pages 15 | git ls-files | xargs rm -f 16 | 17 | mv .gitignore.bak .gitignore 18 | cp -rf dist/* . 19 | 20 | DATE=`date` 21 | git add --all && git commit -am "Build at ${DATE}" 22 | git push origin gh-pages 23 | git checkout master 24 | echo "Finish deployment at ${DATE}" 25 | --------------------------------------------------------------------------------