Skip to content

Commit 6863520

Browse files
tqchenyongwww
authored andcommitted
[WEB] Update web runtime to support latest emcc (apache#14046)
[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.
1 parent de61933 commit 6863520

File tree

7 files changed

+28
-21
lines changed

7 files changed

+28
-21
lines changed

python/tvm/contrib/emcc.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"):
4040
"""
4141
cmd = [cc]
4242
cmd += ["-O3"]
43-
4443
cmd += ["-std=c++17"]
4544
cmd += ["--no-entry"]
45+
cmd += ["-s", "WASM_BIGINT=1"]
4646
cmd += ["-s", "ERROR_ON_UNDEFINED_SYMBOLS=0"]
4747
cmd += ["-s", "STANDALONE_WASM=1"]
4848
cmd += ["-s", "ALLOW_MEMORY_GROWTH=1"]
@@ -54,14 +54,17 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"):
5454
if obj.find("wasm_runtime.bc") != -1:
5555
with_runtime = True
5656

57+
libs = []
5758
if not with_runtime:
58-
objects += [find_lib_path("wasm_runtime.bc")[0]]
59+
libs += [find_lib_path("wasm_runtime.bc")[0]]
5960

60-
objects += [find_lib_path("tvmjs_support.bc")[0]]
61-
objects += [find_lib_path("webgpu_runtime.bc")[0]]
61+
libs += [find_lib_path("tvmjs_support.bc")[0]]
62+
libs += [find_lib_path("webgpu_runtime.bc")[0]]
6263

6364
cmd += ["-o", output]
64-
cmd += objects
65+
66+
# let libraries go before normal object
67+
cmd += libs + objects
6568

6669
if options:
6770
cmd += options

src/runtime/dso_library.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,10 @@ ObjectPtr<Library> CreateDSOLibraryObject(std::string library_path) {
148148
n->Init(library_path);
149149
return n;
150150
}
151+
152+
TVM_REGISTER_GLOBAL("runtime.module.loadfile_so").set_body([](TVMArgs args, TVMRetValue* rv) {
153+
ObjectPtr<Library> n = CreateDSOLibraryObject(args[0]);
154+
*rv = CreateModuleFromLibrary(n);
155+
});
151156
} // namespace runtime
152157
} // namespace tvm

src/runtime/library_module.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,5 @@ Module CreateModuleFromLibrary(ObjectPtr<Library> lib, PackedFuncWrapper packed_
224224

225225
return root_mod;
226226
}
227-
228-
TVM_REGISTER_GLOBAL("runtime.module.loadfile_so").set_body([](TVMArgs args, TVMRetValue* rv) {
229-
ObjectPtr<Library> n = CreateDSOLibraryObject(args[0]);
230-
*rv = CreateModuleFromLibrary(n);
231-
});
232227
} // namespace runtime
233228
} // namespace tvm

web/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
out
44
node_modules
55
build
6+
debug

web/Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,20 @@ all: dist/wasm/tvmjs_runtime.wasm dist/wasm/tvmjs_runtime.wasi.js
2626

2727
EMCC = emcc
2828

29-
EMCC_CFLAGS = $(INCLUDE_FLAGS) -O3 -std=c++17 -Wno-ignored-attributes --no-entry \
30-
-s ALLOW_MEMORY_GROWTH=1 -s STANDALONE_WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0
29+
EMCC_CFLAGS = $(INCLUDE_FLAGS) -O3 -std=c++17 -Wno-ignored-attributes
3130

32-
EMCC_LDFLAGS = --pre-js emcc/preload.js
31+
EMCC_LDFLAGS = --no-entry -s WASM_BIGINT=1 -s ALLOW_MEMORY_GROWTH=1 -s STANDALONE_WASM=1\
32+
-s ERROR_ON_UNDEFINED_SYMBOLS=0 --pre-js emcc/preload.js
3333

3434
dist/wasm/%.bc: emcc/%.cc
3535
@mkdir -p $(@D)
3636
$(EMCC) $(EMCC_CFLAGS) -c -MM -MT dist/wasm/$*.bc $< >dist/wasm/$*.d
37-
$(EMCC) $(EMCC_CFLAGS) -c -o dist/wasm/$*.bc $<
37+
$(EMCC) $(EMCC_CFLAGS) -emit-llvm -c -o dist/wasm/$*.bc $<
3838

3939

4040
dist/wasm/tvmjs_runtime.wasm: dist/wasm/wasm_runtime.bc dist/wasm/tvmjs_support.bc dist/wasm/webgpu_runtime.bc
4141
@mkdir -p $(@D)
42-
$(EMCC) $(EMCC_CFLAGS) -o dist/wasm/tvmjs_runtime.js $+ $(EMCC_LDFLAGS)
43-
42+
$(EMCC) $(EMCC_CFLAGS) -o dist/wasm/tvmjs_runtime.js $+ $(EMCC_LDFLAGS)
4443

4544
dist/wasm/tvmjs_runtime.wasi.js: dist/wasm/tvmjs_runtime.wasm emcc/decorate_as_wasi.py
4645
python3 emcc/decorate_as_wasi.py dist/wasm/tvmjs_runtime.js $@

web/emcc/tvmjs_support.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class AsyncLocalSession : public LocalSession {
214214
local_to.dtype = remote_from->dtype;
215215
local_to.strides = nullptr;
216216
local_to.byte_offset = 0;
217-
this->GetDeviceAPI(remote_from->device)->CopyDataFromTo(&local_to, remote_from, nullptr);
217+
this->GetDeviceAPI(remote_from->device)->CopyDataFromTo(remote_from, &local_to, nullptr);
218218
this->AsyncStreamWait(remote_from->device, nullptr, on_complete);
219219
} catch (const std::runtime_error& e) {
220220
this->SendException(on_complete, e.what());

web/src/webgpu.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export class WebGPUContext {
8989
if (dtype == "handle") {
9090
layoutEntries.push({
9191
binding: i,
92-
visibility: GPUShaderStage.COMPUTE
92+
visibility: GPUShaderStage.COMPUTE,
93+
buffer : {
94+
type: "storage"
95+
}
9396
});
9497
} else {
9598
throw new Error("Cannot handle argument type " + dtype + " in WebGPU shader");
@@ -99,7 +102,7 @@ export class WebGPUContext {
99102
entries: layoutEntries
100103
});
101104

102-
const textDecoder = new TextDecoder('utf-8')
105+
const textDecoder = new TextDecoder("utf-8")
103106
const codeString = textDecoder.decode(data.buffer)
104107

105108
const pipeline = this.device.createComputePipeline({
@@ -287,8 +290,9 @@ export class WebGPUContext {
287290

288291
this.numPendingReads += 1;
289292

290-
const readEvent = gpuTemp.mapAsync(GPUMapMode.READ).then((data: unknown) => {
291-
this.memory.storeRawBytes(to, new Uint8Array(data as ArrayBuffer));
293+
const readEvent = gpuTemp.mapAsync(GPUMapMode.READ).then(() => {
294+
const data = gpuTemp.getMappedRange();
295+
this.memory.storeRawBytes(to, new Uint8Array(data));
292296
this.numPendingReads -= 1;
293297
gpuTemp.destroy();
294298
});

0 commit comments

Comments
 (0)