Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Relay][VM] Relay VM serialization #3647

Merged
merged 5 commits into from
Jul 31, 2019
Merged

[Relay][VM] Relay VM serialization #3647

merged 5 commits into from
Jul 31, 2019

Conversation

zhiics
Copy link
Member

@zhiics zhiics commented Jul 28, 2019

This PR implements the serialization and deserialization of the Relay VM. For detailed information, please refer to #3594

An example could be like the following:

import numpy as np
import tvm
from tvm import relay

# define a simple network.
x = relay.var('x', shape=(10, 10))
f = relay.Function([x], x + x)
mod = relay.Module({"main": f})

# create a Relay VM.
ctx = tvm.cpu()
target = "llvm"
compiler = relay.vm.VMCompiler()
vm = compiler.compile(mod, target)
vm.init(ctx)

# serialize.
ser = relay.serializer.Serializer(vm)
code, lib = ser.serialize()

# save and load the code and lib file.
tmp = tvm.contrib.util.tempdir()
path_lib = tmp.relpath("lib.so")
lib.export_library(path_lib)
with open(tmp.relpath("code.bc"), "wb") as fo:
    fo.write(code)
loaded_lib = tvm.module.load(path_lib)
loaded_code = bytearray(open(tmp.relpath("code.bc"), "rb").read())

# deserialize.
deser = relay.deserializer.Deserializer(loaded_code, loaded_lib)
des_vm = deser.deserialize()

# execute the deserialized vm.
des_vm.init(ctx)
x_data = np.random.rand(10, 10).astype('float32')
res = des_vm.run(x_data)
print(res.asnumpy())

@jroesch @wweic @icemelon9 @tqchen @slyubomirsky @MarisaKirisame

@icemelon
Copy link
Member

Could we serialize the dst in the beginning since it's the most common field? Also could you add the format of every instruciton in the RFC?

@zhiics
Copy link
Member Author

zhiics commented Jul 29, 2019

@icemelon9 yes, we could, but I am not sure if we want to do that because some instructions don't have it. The current order is consistent to the order that they are constructed but the variable-length filed is rotated to the end.

tvm.testing.assert_allclose(res.asnumpy(), 3.0)


if __name__ == "__main__":
Copy link
Member

Choose a reason for hiding this comment

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

Can you write a loop over all the testing models load them into vm and serialize them and ensure they produce the same result after deserialization?

Copy link
Member Author

@zhiics zhiics Jul 30, 2019

Choose a reason for hiding this comment

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

May be just 1 or 2? It may make the testing too slow to have all them in the unit test.

Copy link
Member

@jroesch jroesch left a comment

Choose a reason for hiding this comment

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

Can you address my comments otherwise LGTM

Copy link
Contributor

@wweic wweic left a comment

Choose a reason for hiding this comment

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

lgtm.

src/relay/backend/vm/serialize_util.h Outdated Show resolved Hide resolved
@jroesch jroesch merged commit 9045512 into apache:master Jul 31, 2019
@zhiics zhiics deleted the serialize branch July 31, 2019 16:29
wweic pushed a commit to wweic/tvm that referenced this pull request Aug 9, 2019
* relay vm serialization

* fix lint

* load params, fix stream

* lint

* fix typo
wweic pushed a commit to neo-ai/tvm that referenced this pull request Sep 6, 2019
* relay vm serialization

* fix lint

* load params, fix stream

* lint

* fix typo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants