-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models #8074
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2d30945
added test infrastructure for frozen TF2 models
rohanmukh edb62ca
linting with black
rohanmukh 79a73ec
removing some comments
rohanmukh 689ac3b
change in comment in sequential test
rohanmukh 6637481
addressed the comments
rohanmukh 0b04d40
refactored to place vmobj_to_list in a common file
rohanmukh 4dd6167
Added helper function in python/tvm/relay/testing/tf.py
rohanmukh a14c386
Refactor tf according to CI error
rohanmukh 512ab4e
Added docstring
rohanmukh fd8f8e5
removing print
rohanmukh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,106 @@ | ||
# 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=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except | ||
# pylint: disable=import-outside-toplevel, redefined-builtin | ||
"""TF2 to relay converter test utilities""" | ||
|
||
import tvm | ||
from tvm import relay | ||
|
||
from tvm.runtime.vm import VirtualMachine | ||
import tvm.contrib.graph_executor as runtime | ||
from tvm.relay.frontend.tensorflow import from_tensorflow | ||
|
||
import tvm.testing | ||
from tvm.relay.testing.tf import vmobj_to_list as vmobj_to_list | ||
|
||
import tensorflow as tf | ||
from tensorflow.python.eager.def_function import Function | ||
|
||
|
||
def run_tf_code(func, input_): | ||
print(type(func)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove or change to log |
||
if type(func) is Function: | ||
out = func(input_) | ||
if isinstance(out, list): | ||
a = [x.numpy() for x in out] | ||
else: | ||
a = [out.numpy()] | ||
else: | ||
a = func(tf.constant(input_)) | ||
if type(a) is dict: | ||
a = [x.numpy() for x in a.values()] | ||
elif type(a) is list: | ||
a = [x.numpy() for x in a] | ||
else: | ||
a = a.numpy() | ||
return a | ||
|
||
|
||
def compile_graph_executor(mod, params, target="llvm", target_host="llvm", opt_level=3): | ||
with tvm.transform.PassContext(opt_level): | ||
lib = relay.build(mod, target=target, target_host=target_host, params=params) | ||
return lib | ||
|
||
|
||
def compile_vm(mod, params, target="llvm", target_host="llvm", opt_level=3, disabled_pass=None): | ||
with tvm.transform.PassContext(opt_level, disabled_pass=disabled_pass): | ||
vm_exec = relay.vm.compile(mod, target, target_host, params=params) | ||
return vm_exec | ||
|
||
|
||
def run_vm(vm_exec, input_, ctx=tvm.cpu(0)): | ||
vm = VirtualMachine(vm_exec, ctx) | ||
_out = vm.invoke("main", input_) | ||
return vmobj_to_list(_out) | ||
|
||
|
||
def run_graph_executor(lib, input_, ctx=tvm.cpu(0)): | ||
mod = runtime.GraphModule(lib["default"](ctx)) | ||
mod.set_input(0, input_) | ||
mod.run() | ||
return [mod.get_output(0).asnumpy()] | ||
|
||
|
||
def compare_tf_tvm(gdef, input_, output_, runtime="vm", output_tensors=None): | ||
"""compare tf and tvm execution for the same input. | ||
|
||
Parameters | ||
---------- | ||
gdef: TF2 graph def extracted to be fed into from_tensorflow parser. | ||
(https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto) | ||
|
||
input_: a single numpy array object | ||
|
||
output_: the expected output from TF to match TVM output with | ||
|
||
runtime: choose TVM runtime; either "vm" for VirtualMachine or "graph" for GraphExecutor | ||
|
||
output_tensors : List of output tensor names (Optional) | ||
if not specified then the last node is assumed as graph output. | ||
""" | ||
mod, params = from_tensorflow(gdef, outputs=output_tensors) | ||
if runtime == "vm": | ||
exec_ = compile_vm(mod, params) | ||
tvm_out = run_vm(exec_, input_) | ||
elif runtime == "graph": | ||
lib = compile_graph_executor(mod, params) | ||
tvm_out = run_graph_executor(lib, input_) | ||
else: | ||
raise RuntimeError("Runtime input not supported: %s" % runtime) | ||
|
||
tvm.testing.assert_allclose(output_, tvm_out, atol=1e-5) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this copied from somewhere? We can add this to testing to remove the other places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is copied from
tests/python/frontend/tensorflow/test_forward.py
Do you mean I should remove it from there and import its invocations from here (i.e.
relay.testing.tf
) in this PR? Or do you mean that should be done in another PR? Similar code exists in other places as well liketest_vm.py
,test_tensorrt.py
etc.