forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relay->Relax translator (ResNet example) (apache#75)
* Relay translator; build static bert forward/backward. * rebase * Add ops. * resnet demo * cleanup code. * Rebase. * Address comments. * leverage FTVMCompute for most op translation; reuse relay.Constant. * lint.
- Loading branch information
Showing
12 changed files
with
410 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Example ResNet workload by translating the Relay program to Relax""" | ||
|
||
import tvm | ||
import tvm.testing | ||
from tvm.relay import testing | ||
from tvm import relax, relay | ||
from tvm.relax.testing import relay_translator, nn | ||
from tvm.runtime import vm as vm_rt | ||
from tvm.script import relax as R | ||
import numpy as np | ||
|
||
if __name__ == "__main__": | ||
relay_mod, _ = testing.resnet.get_workload(num_layers=50, batch_size=1, dtype="float32") | ||
|
||
# translate the ResNet model from Relay to Relax | ||
relax_mod = relay_translator.from_relay(relay_mod["main"]) | ||
|
||
# print the ResNet IRmodule got translated | ||
print(R.parser.astext(relax_mod)) | ||
|
||
# build the IRModule and create relax vm | ||
target = tvm.target.Target("llvm", host="llvm") | ||
ex, lib = relax.vm.build(relax_mod, target) | ||
vm = relax.VirtualMachine(ex, tvm.cpu(), mod=lib) | ||
|
||
# init weights and run the model on relax vm | ||
shape = (1, 3, 224, 224) | ||
data = tvm.nd.array(np.random.rand(*shape).astype(np.float32)) | ||
params = nn.init_params(relax_mod) | ||
res = vm["main"](data, *params) | ||
|
||
# check correctness by comparing with relay result | ||
exe = relay.vm.compile(relay_mod, target) | ||
relay_vm = vm_rt.VirtualMachine(exe, tvm.cpu()) | ||
inputs = [data] + params | ||
expected_output = relay_vm.run(*inputs) | ||
tvm.testing.assert_allclose(res.numpy(), expected_output.numpy(), rtol=1e-4, atol=1e-4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=wildcard-import, redefined-builtin | ||
"""The Relax testing namespace containing nn and translator.""" | ||
|
||
from .nn import * | ||
from .relay_translator import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
"""FFI API for for Relax.""" | ||
import tvm._ffi | ||
|
||
tvm._ffi._init_api("relax", __name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.