|
| 1 | +# ============================================================================== |
| 2 | +# Copyright 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 test for checking backend setting using rewriter config for grappler |
| 17 | +
|
| 18 | +""" |
| 19 | +from __future__ import absolute_import |
| 20 | +from __future__ import division |
| 21 | +from __future__ import print_function |
| 22 | + |
| 23 | +import pytest |
| 24 | +import os |
| 25 | +import numpy as np |
| 26 | +import shutil |
| 27 | +import tensorflow as tf |
| 28 | +from tensorflow.core.protobuf import rewriter_config_pb2 |
| 29 | +import ngraph_bridge |
| 30 | + |
| 31 | +from common import NgraphTest |
| 32 | + |
| 33 | + |
| 34 | +class TestRewriterConfigBackendSetting(NgraphTest): |
| 35 | + |
| 36 | + @pytest.mark.skipif( |
| 37 | + not ngraph_bridge.is_grappler_enabled(), |
| 38 | + reason='Rewriter config only works for grappler path') |
| 39 | + @pytest.mark.parametrize(("backend",), ( |
| 40 | + ('CPU',), |
| 41 | + ('INTERPRETER',), |
| 42 | + )) |
| 43 | + def test_config_updater_api(self, backend): |
| 44 | + dim1 = 3 |
| 45 | + dim2 = 4 |
| 46 | + a = tf.placeholder(tf.float32, shape=(dim1, dim2), name='a') |
| 47 | + x = tf.placeholder(tf.float32, shape=(dim1, dim2), name='x') |
| 48 | + b = tf.placeholder(tf.float32, shape=(dim1, dim2), name='y') |
| 49 | + axpy = (a * x) + b |
| 50 | + |
| 51 | + config = tf.ConfigProto() |
| 52 | + rewriter_options = rewriter_config_pb2.RewriterConfig() |
| 53 | + rewriter_options.meta_optimizer_iterations = ( |
| 54 | + rewriter_config_pb2.RewriterConfig.ONE) |
| 55 | + rewriter_options.min_graph_nodes = -1 |
| 56 | + ngraph_optimizer = rewriter_options.custom_optimizers.add() |
| 57 | + ngraph_optimizer.name = "ngraph-optimizer" |
| 58 | + ngraph_optimizer.parameter_map["ngraph_backend"].s = backend.encode() |
| 59 | + ngraph_optimizer.parameter_map["device_id"].s = b'0' |
| 60 | + # TODO: This test will pass if grappler fails silently. |
| 61 | + # Need to do something about that |
| 62 | + backend_extra_params_map = { |
| 63 | + 'CPU': { |
| 64 | + 'device_config': '' |
| 65 | + }, |
| 66 | + 'INTERPRETER': { |
| 67 | + 'test_echo': '42', |
| 68 | + 'hello': '3' |
| 69 | + } |
| 70 | + } |
| 71 | + extra_params = backend_extra_params_map[backend] |
| 72 | + for k in extra_params: |
| 73 | + ngraph_optimizer.parameter_map[k].s = extra_params[k].encode() |
| 74 | + config.MergeFrom( |
| 75 | + tf.ConfigProto( |
| 76 | + graph_options=tf.GraphOptions( |
| 77 | + rewrite_options=rewriter_options))) |
| 78 | + |
| 79 | + with tf.Session(config=config) as sess: |
| 80 | + outval = sess.run( |
| 81 | + axpy, |
| 82 | + feed_dict={ |
| 83 | + a: 1.5 * np.ones((dim1, dim2)), |
| 84 | + b: np.ones((dim1, dim2)), |
| 85 | + x: np.ones((dim1, dim2)) |
| 86 | + }) |
| 87 | + assert (outval == 2.5 * (np.ones((dim1, dim2)))).all() |
0 commit comments