Skip to content

[RUNTIME] Introduce MetadataModule to separate code compilation/interpretation and weight initialization - #5770

Merged
tqchen merged 13 commits into
apache:masterfrom
zhiics:byoc_metadata
Jun 18, 2020
Merged

[RUNTIME] Introduce MetadataModule to separate code compilation/interpretation and weight initialization#5770
tqchen merged 13 commits into
apache:masterfrom
zhiics:byoc_metadata

Conversation

@zhiics

@zhiics zhiics commented Jun 10, 2020

Copy link
Copy Markdown
Member

This PR attempts to separate metadata initialization and source code compilation (when necessary). The discussion can be found here: https://discuss.tvm.ai/t/byoc-runtime-json-runtime-for-byoc/6579/15

The goal is to have a standalone wrapper (MetadataModule) to take care of module initialization for different runtimes. In the BYOC case, we save constants and code separately. The constant would be loaded by the wrapper while the code is handled by csourcemodule.

@zhiics
zhiics marked this pull request as draft June 10, 2020 23:51
@zhiics
zhiics force-pushed the byoc_metadata branch 2 times, most recently from 22a3bf6 to e7a137c Compare June 12, 2020 04:28
Comment thread src/relay/backend/contrib/dnnl/codegen.cc Outdated
@zhiics
zhiics marked this pull request as ready for review June 12, 2020 15:54
@zhiics

zhiics commented Jun 12, 2020

Copy link
Copy Markdown
Member Author

Comment thread python/tvm/runtime/module.py Outdated
Comment thread src/runtime/module_init_wrapper.cc Outdated
Comment thread python/tvm/target/source_module.py Outdated
Comment thread src/target/source/source_module.cc Outdated
Comment thread src/runtime/module_init_wrapper.cc Outdated
Comment thread python/tvm/runtime/module.py Outdated
Comment thread python/tvm/runtime/module.py Outdated
@zhiics

zhiics commented Jun 16, 2020

Copy link
Copy Markdown
Member Author

@tqchen I removed all wrappers, made MetadataModule internal, and dispatched the initialization of each submodule per function/symbol base. There is now no user API change. But I had to add a field the CSourceModule to let it know what metadata it needs. This, however, doesn't affect the compilation and the original use/semantic of CSourceModule because we only need this information to help build up the MetadataModule.

@tqchen

tqchen commented Jun 16, 2020

Copy link
Copy Markdown
Member

cc @mbaret @FrozenGene @comaniac it would be great if you can help to take a look as well

Comment thread python/tvm/contrib/graph_runtime.py Outdated
@FrozenGene

Copy link
Copy Markdown
Member

Maybe I miss something. so maybe this is considered. That is I don't see unwrap_modules. I ask this because I think it is related with our next module based interface runtime. The usage like this:

with relay.build_config(opt_level=3):
    complied_graph_lib = relay.build_module.build(
        mod, "llvm", params=params)

from tvm.contrib import util
temp = util.tempdir()
file_name = "deploy_lib.so"
path_lib = temp.relpath(file_name)
complied_graph_lib.export_library(path_lib)
loaded_lib = tvm.runtime.load_module(path_lib)
ctx = tvm.cpu(0)
gmod = loaded_lib['default'](ctx)
set_input = gmod["set_input"]
run = gmod["run"]
get_output = gmod["get_output"]
data = np.random.uniform(-1, 1, size=(1, 3, 224, 224)).astype("float32")
set_input("data", tvm.nd.array(data))
run()
out = get_output(0).asnumpy()

As you could see we will wrap params / lib / json into one lib. So I want to see the logic of unwrap_modules and to see how to connect with it.

THANKS!

@zhiics

zhiics commented Jun 17, 2020

Copy link
Copy Markdown
Member Author

@FrozenGene The current implementation doesn't actually change any user/frontend APIs. We added the MetadataModule to take care of weight initialization and separate it from code compilation. So you would have:

MetadataModule:
    - DSO module
    - Accelerator CSourceModule0
    - Accelerator CSourceModule1

As you can see, MetadataModule manages the params, but we will still have code in the submodules. The serialization and deserialization make sure that the module structure is always maintained correctly. Therefore, we can still have params/lib/json in one module.

In other words, it should not affect the way we package the final artifacts, but it changes the way we compile certain modules (e.g. CSourceModule) and the way we initialize modules that need constant data.

@comaniac comaniac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Only miner comments. Thanks!

Comment thread src/relay/backend/compile_engine.cc Outdated
Comment thread src/relay/backend/contrib/codegen_c/codegen.cc Outdated
Comment thread src/relay/backend/contrib/codegen_c/codegen_c.h Outdated
Comment thread src/relay/backend/contrib/codegen_c/codegen.cc Outdated
Comment thread src/runtime/metadata_module.cc
Comment thread src/runtime/metadata_module.cc Outdated
Comment thread src/runtime/metadata_module.cc Outdated
Comment thread src/runtime/metadata_module.cc Outdated
Comment thread src/runtime/metadata_module.cc

@comaniac comaniac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/relay/backend/contrib/codegen_c/codegen_c.h Outdated
Comment thread python/tvm/contrib/graph_runtime.py Outdated
@zhiics zhiics changed the title [BYOC][runtime] Separate code and metadata for CSourceModule [RUNTIME] Introduce MetadataModule to separate code compilation/interpretation and weight initialization Jun 18, 2020
Comment thread python/tvm/contrib/graph_runtime.py
@tqchen
tqchen merged commit eacfe89 into apache:master Jun 18, 2020
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Jun 30, 2020
zhiics added a commit to neo-ai/tvm that referenced this pull request Jul 2, 2020
@zhiics
zhiics deleted the byoc_metadata branch July 8, 2020 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants