Skip to content

Commit

Permalink
[WEB] Update web runtime to support latest emcc (apache#14046)
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
tqchen authored and yongwww committed Feb 27, 2023
1 parent de61933 commit 6863520
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
13 changes: 8 additions & 5 deletions python/tvm/contrib/emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/dso_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,10 @@ ObjectPtr<Library> 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<Library> n = CreateDSOLibraryObject(args[0]);
*rv = CreateModuleFromLibrary(n);
});
} // namespace runtime
} // namespace tvm
5 changes: 0 additions & 5 deletions src/runtime/library_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,5 @@ Module CreateModuleFromLibrary(ObjectPtr<Library> lib, PackedFuncWrapper packed_

return root_mod;
}

TVM_REGISTER_GLOBAL("runtime.module.loadfile_so").set_body([](TVMArgs args, TVMRetValue* rv) {
ObjectPtr<Library> n = CreateDSOLibraryObject(args[0]);
*rv = CreateModuleFromLibrary(n);
});
} // namespace runtime
} // namespace tvm
1 change: 1 addition & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
out
node_modules
build
debug
11 changes: 5 additions & 6 deletions web/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
Expand Down
2 changes: 1 addition & 1 deletion web/emcc/tvmjs_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
12 changes: 8 additions & 4 deletions web/src/webgpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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({
Expand Down Expand Up @@ -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();
});
Expand Down

0 comments on commit 6863520

Please sign in to comment.