|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Unit tests for converting TensorFlow debugging ops to Relay.""" |
| 18 | +import tensorflow as tf |
| 19 | +import numpy as np |
| 20 | +from tvm import relay |
| 21 | +from tvm.relay.frontend.tensorflow import from_tensorflow |
| 22 | + |
| 23 | +def run_relay(graph, *vars): |
| 24 | + mod, params = from_tensorflow(graph.as_graph_def(add_shapes=True)) |
| 25 | + ex = relay.create_executor('debug', mod=mod) |
| 26 | + return ex.evaluate()(*vars) |
| 27 | + |
| 28 | +def test_assert_true(): |
| 29 | + g = tf.Graph() |
| 30 | + with g.as_default(): |
| 31 | + x = tf.placeholder(tf.float32, shape=()) |
| 32 | + assert_op = tf.Assert(tf.less_equal(x, x), ["it failed"]) |
| 33 | + |
| 34 | + with tf.Session() as sess: |
| 35 | + x_value = np.random.rand() |
| 36 | + assert sess.run(assert_op, feed_dict={x: x_value}) is None |
| 37 | + |
| 38 | + # In TVM, tf.assert is converted to a no-op which is actually a 0, |
| 39 | + # though it should probably be none or an empty tuple. |
| 40 | + # |
| 41 | + # ToDo: It appears that the frontend converter gets confused here and |
| 42 | + # entirely eliminates all operands from main(). Likely because x <= x |
| 43 | + # is always true, so the placeholder can be eliminated. But TF doesn't |
| 44 | + # do that, it's happening in Relay, and that optimization shouldn't |
| 45 | + # affect the arity of the main function. We should have to pass in |
| 46 | + # x_value here. |
| 47 | + np.testing.assert_allclose(0, run_relay(g).asnumpy()) |
| 48 | + |
| 49 | +def test_assert_true_var_capture(): |
| 50 | + g = tf.Graph() |
| 51 | + with g.as_default(): |
| 52 | + x = tf.placeholder(tf.float32, shape=()) |
| 53 | + |
| 54 | + # It turns out that tf.assert() creates a large and complex subgraph if |
| 55 | + # you capture a variable as part of the error message. So we need to |
| 56 | + # test that, too. |
| 57 | + assert_op = tf.Assert(tf.less_equal(x, x), ["it failed", x]) |
| 58 | + |
| 59 | + with tf.Session() as sess: |
| 60 | + x_value = np.random.rand() |
| 61 | + assert sess.run(assert_op, feed_dict={x: x_value}) is None |
| 62 | + |
| 63 | + # ToDo: The frontend converter gets confused here as well, thinking |
| 64 | + # that it needs to be told what x is twice. It also notes the output of |
| 65 | + # the graph as a boolean, which is not correct - as you can see above, |
| 66 | + # TF believes that the value of this graph is None. In addition, the |
| 67 | + # arity of the translated function should be 1, not 2. |
| 68 | + np.testing.assert_allclose(True, run_relay(g, x_value, x_value).asnumpy()) |
| 69 | + |
| 70 | +def test_assert_false(): |
| 71 | + g = tf.Graph() |
| 72 | + with g.as_default(): |
| 73 | + assert_op = tf.Assert(tf.constant(False), ["it failed"]) |
| 74 | + |
| 75 | + with tf.Session() as sess: |
| 76 | + try: |
| 77 | + print(sess.run(assert_op)) |
| 78 | + assert False # TF should have thrown an exception |
| 79 | + except tf.errors.InvalidArgumentError as e: |
| 80 | + assert "it failed" in e.message |
| 81 | + |
| 82 | + # In TVM, tf.assert is converted to a no-op which is actually a 0, |
| 83 | + # though it should probably be none or an empty tuple. For the same |
| 84 | + # reason, there should not be an error here, even though the assertion |
| 85 | + # argument is false. |
| 86 | + np.testing.assert_allclose(0, run_relay(g).asnumpy()) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + test_assert_true() |
| 91 | + test_assert_true_var_capture() |
| 92 | + test_assert_false() |
| 93 | + |
0 commit comments