|
| 1 | +# ============================================================================== |
| 2 | +# Copyright 2018-2019 Intel Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | +"""nGraph TensorFlow bridge Const operation test |
| 17 | +
|
| 18 | +""" |
| 19 | +from __future__ import absolute_import |
| 20 | +from __future__ import division |
| 21 | +from __future__ import print_function |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +import tensorflow as tf |
| 26 | +import os |
| 27 | + |
| 28 | +from common import NgraphTest |
| 29 | + |
| 30 | +# Uncomment for debugging; Also add -s in command like, e.g. |
| 31 | +# (venv-tf-py3) [build_cmake]$ |
| 32 | +# NGRAPH_TF_LOG_PLACEMENT=1 NGRAPH_TF_VLOG_LEVEL=6 pytest -s -k test_const_scalarval ../test/python/test_const.py |
| 33 | +import logging |
| 34 | +logging.basicConfig(level=logging.DEBUG) |
| 35 | + |
| 36 | + |
| 37 | +class TestConstOperations(NgraphTest): |
| 38 | + |
| 39 | + def test_const_listvals(self): |
| 40 | + zz = tf.constant([1, 2, 3, 4, 5, 6], dtype=float, shape=[2, 3]) |
| 41 | + |
| 42 | + def run_test(sess): |
| 43 | + return sess.run(zz) |
| 44 | + |
| 45 | + assert ( |
| 46 | + self.with_ngraph(run_test) == self.without_ngraph(run_test)).all() |
| 47 | + |
| 48 | + def test_const_listvals_2(self): |
| 49 | + zz = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=float, shape=[2, 3]) |
| 50 | + |
| 51 | + def run_test(sess): |
| 52 | + return sess.run(zz) |
| 53 | + |
| 54 | + assert ( |
| 55 | + self.with_ngraph(run_test) == self.without_ngraph(run_test)).all() |
| 56 | + |
| 57 | + def test_const_scalarval(self): |
| 58 | + zz = tf.constant(-3, dtype=float, shape=[2, 3]) |
| 59 | + |
| 60 | + def run_test(sess): |
| 61 | + return sess.run(zz) |
| 62 | + |
| 63 | + assert ( |
| 64 | + self.with_ngraph(run_test) == self.without_ngraph(run_test)).all() |
| 65 | + |
| 66 | + def test_const_lastfill(self): |
| 67 | + zz = tf.constant([1, 2], dtype=float, shape=[2, 3]) |
| 68 | + |
| 69 | + def run_test(sess): |
| 70 | + return sess.run(zz) |
| 71 | + |
| 72 | + assert ( |
| 73 | + self.with_ngraph(run_test) == self.without_ngraph(run_test)).all() |
| 74 | + |
| 75 | + def test_const_empty(self): |
| 76 | + log = logging.getLogger('test_const_empty') |
| 77 | + zz = tf.constant([], dtype=float, shape=[2, 3]) |
| 78 | + |
| 79 | + def run_test(sess): |
| 80 | + log.debug('Invoking sess.run(zz)') |
| 81 | + return sess.run(zz) |
| 82 | + |
| 83 | + # Ideally we want same behavior for both TF & NG, but for now we are deviating, |
| 84 | + # NGraph will throw error, but TF will fill in zeros |
| 85 | + # assert ( |
| 86 | + # self.with_ngraph(run_test) == self.without_ngraph(run_test)).all() |
| 87 | + |
| 88 | + # Test to see that exception is raised in NG |
| 89 | + try: |
| 90 | + # This test is expected to fail currently |
| 91 | + res = self.with_ngraph(run_test) |
| 92 | + assert False, 'Failed, expected test to raise error' |
| 93 | + except: |
| 94 | + log.debug('Passed, expected NG to raise error...') |
| 95 | + assert True |
0 commit comments