From 2d70b6fdfc3db8c6b9606e588cde43eb00dca7e8 Mon Sep 17 00:00:00 2001 From: tqchen Date: Sun, 19 Feb 2023 10:01:42 -0500 Subject: [PATCH] [WEB] Update web runtime to latest emcc This PR provide fixes to re-enable web build for latest emcc. WASM runtime now works. WebGPU still need some catch up due to freq updates of the API spec. Tested locally. --- python/tvm/contrib/emcc.py | 13 ++++++++----- src/runtime/dso_library.cc | 5 +++++ src/runtime/library_module.cc | 5 ----- web/.gitignore | 1 + web/Makefile | 11 +++++------ web/emcc/tvmjs_support.cc | 2 +- web/src/webgpu.ts | 12 ++++++++---- 7 files changed, 28 insertions(+), 21 deletions(-) diff --git a/python/tvm/contrib/emcc.py b/python/tvm/contrib/emcc.py index 671e573b1c39..8be48651262f 100644 --- a/python/tvm/contrib/emcc.py +++ b/python/tvm/contrib/emcc.py @@ -40,9 +40,9 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"): """ cmd = [cc] cmd += ["-O3"] - cmd += ["-std=c++17"] cmd += ["--no-entry"] + cmd += ["-s", "WASM_BIGINT=1"] cmd += ["-s", "ERROR_ON_UNDEFINED_SYMBOLS=0"] cmd += ["-s", "STANDALONE_WASM=1"] cmd += ["-s", "ALLOW_MEMORY_GROWTH=1"] @@ -54,14 +54,17 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"): if obj.find("wasm_runtime.bc") != -1: with_runtime = True + libs = [] if not with_runtime: - objects += [find_lib_path("wasm_runtime.bc")[0]] + libs += [find_lib_path("wasm_runtime.bc")[0]] - objects += [find_lib_path("tvmjs_support.bc")[0]] - objects += [find_lib_path("webgpu_runtime.bc")[0]] + libs += [find_lib_path("tvmjs_support.bc")[0]] + libs += [find_lib_path("webgpu_runtime.bc")[0]] cmd += ["-o", output] - cmd += objects + + # let libraries go before normal object + cmd += libs + objects if options: cmd += options diff --git a/src/runtime/dso_library.cc b/src/runtime/dso_library.cc index a0c6c48b5e44..e4f4937a8a9f 100644 --- a/src/runtime/dso_library.cc +++ b/src/runtime/dso_library.cc @@ -148,5 +148,10 @@ ObjectPtr CreateDSOLibraryObject(std::string library_path) { n->Init(library_path); return n; } + +TVM_REGISTER_GLOBAL("runtime.module.loadfile_so").set_body([](TVMArgs args, TVMRetValue* rv) { + ObjectPtr n = CreateDSOLibraryObject(args[0]); + *rv = CreateModuleFromLibrary(n); +}); } // namespace runtime } // namespace tvm diff --git a/src/runtime/library_module.cc b/src/runtime/library_module.cc index 54fd362387c5..d6c2f791deb9 100644 --- a/src/runtime/library_module.cc +++ b/src/runtime/library_module.cc @@ -221,10 +221,5 @@ Module CreateModuleFromLibrary(ObjectPtr lib, PackedFuncWrapper packed_ return root_mod; } - -TVM_REGISTER_GLOBAL("runtime.module.loadfile_so").set_body([](TVMArgs args, TVMRetValue* rv) { - ObjectPtr n = CreateDSOLibraryObject(args[0]); - *rv = CreateModuleFromLibrary(n); -}); } // namespace runtime } // namespace tvm diff --git a/web/.gitignore b/web/.gitignore index 082c5a26770b..1f7cc0916a5f 100644 --- a/web/.gitignore +++ b/web/.gitignore @@ -3,3 +3,4 @@ out node_modules build +debug diff --git a/web/Makefile b/web/Makefile index 1153990a1880..026cb7ebc07c 100644 --- a/web/Makefile +++ b/web/Makefile @@ -26,21 +26,20 @@ all: dist/wasm/tvmjs_runtime.wasm dist/wasm/tvmjs_runtime.wasi.js EMCC = emcc -EMCC_CFLAGS = $(INCLUDE_FLAGS) -O3 -std=c++17 -Wno-ignored-attributes --no-entry \ - -s ALLOW_MEMORY_GROWTH=1 -s STANDALONE_WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 +EMCC_CFLAGS = $(INCLUDE_FLAGS) -O3 -std=c++17 -Wno-ignored-attributes -EMCC_LDFLAGS = --pre-js emcc/preload.js +EMCC_LDFLAGS = --no-entry -s WASM_BIGINT=1 -s ALLOW_MEMORY_GROWTH=1 -s STANDALONE_WASM=1\ + -s ERROR_ON_UNDEFINED_SYMBOLS=0 --pre-js emcc/preload.js dist/wasm/%.bc: emcc/%.cc @mkdir -p $(@D) $(EMCC) $(EMCC_CFLAGS) -c -MM -MT dist/wasm/$*.bc $< >dist/wasm/$*.d - $(EMCC) $(EMCC_CFLAGS) -c -o dist/wasm/$*.bc $< + $(EMCC) $(EMCC_CFLAGS) -emit-llvm -c -o dist/wasm/$*.bc $< dist/wasm/tvmjs_runtime.wasm: dist/wasm/wasm_runtime.bc dist/wasm/tvmjs_support.bc dist/wasm/webgpu_runtime.bc @mkdir -p $(@D) - $(EMCC) $(EMCC_CFLAGS) -o dist/wasm/tvmjs_runtime.js $+ $(EMCC_LDFLAGS) - + $(EMCC) $(EMCC_CFLAGS) -o dist/wasm/tvmjs_runtime.js $+ $(EMCC_LDFLAGS) dist/wasm/tvmjs_runtime.wasi.js: dist/wasm/tvmjs_runtime.wasm emcc/decorate_as_wasi.py python3 emcc/decorate_as_wasi.py dist/wasm/tvmjs_runtime.js $@ diff --git a/web/emcc/tvmjs_support.cc b/web/emcc/tvmjs_support.cc index 6395bfbb08f4..5bc1c32382ce 100644 --- a/web/emcc/tvmjs_support.cc +++ b/web/emcc/tvmjs_support.cc @@ -214,7 +214,7 @@ class AsyncLocalSession : public LocalSession { local_to.dtype = remote_from->dtype; local_to.strides = nullptr; local_to.byte_offset = 0; - this->GetDeviceAPI(remote_from->device)->CopyDataFromTo(&local_to, remote_from, nullptr); + this->GetDeviceAPI(remote_from->device)->CopyDataFromTo(remote_from, &local_to, nullptr); this->AsyncStreamWait(remote_from->device, nullptr, on_complete); } catch (const std::runtime_error& e) { this->SendException(on_complete, e.what()); diff --git a/web/src/webgpu.ts b/web/src/webgpu.ts index daaa122682e3..5de47c200dcc 100644 --- a/web/src/webgpu.ts +++ b/web/src/webgpu.ts @@ -89,7 +89,10 @@ export class WebGPUContext { if (dtype == "handle") { layoutEntries.push({ binding: i, - visibility: GPUShaderStage.COMPUTE + visibility: GPUShaderStage.COMPUTE, + buffer : { + type: "storage" + } }); } else { throw new Error("Cannot handle argument type " + dtype + " in WebGPU shader"); @@ -99,7 +102,7 @@ export class WebGPUContext { entries: layoutEntries }); - const textDecoder = new TextDecoder('utf-8') + const textDecoder = new TextDecoder("utf-8") const codeString = textDecoder.decode(data.buffer) const pipeline = this.device.createComputePipeline({ @@ -287,8 +290,9 @@ export class WebGPUContext { this.numPendingReads += 1; - const readEvent = gpuTemp.mapAsync(GPUMapMode.READ).then((data: unknown) => { - this.memory.storeRawBytes(to, new Uint8Array(data as ArrayBuffer)); + const readEvent = gpuTemp.mapAsync(GPUMapMode.READ).then(() => { + const data = gpuTemp.getMappedRange(); + this.memory.storeRawBytes(to, new Uint8Array(data)); this.numPendingReads -= 1; gpuTemp.destroy(); });