|
| 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 Conv2d operation test |
| 17 | +
|
| 18 | +""" |
| 19 | +import pytest |
| 20 | + |
| 21 | +import tensorflow as tf |
| 22 | +import numpy as np |
| 23 | +import os |
| 24 | +from tensorflow.python.ops import nn_ops |
| 25 | +import ngraph_bridge |
| 26 | + |
| 27 | +#Tests Ngraph Op: ConvolutionBackpropFilters with data format NCHW |
| 28 | +#TF Op: conv2d_backprop_filter |
| 29 | + |
| 30 | +np.random.seed(5) |
| 31 | +#Inputs |
| 32 | +N = 1 |
| 33 | +H = 7 |
| 34 | +W = 6 |
| 35 | +C = 2 |
| 36 | + |
| 37 | +I = C |
| 38 | +O = 2 |
| 39 | +filt_width = 3 |
| 40 | +filt_height = 3 |
| 41 | + |
| 42 | +input_sizes_nchw = [N, C, H, W] |
| 43 | +input_sizes_nhwc = [N, H, W, C] |
| 44 | +filter_size_hwio = [filt_height, filt_width, I, O] |
| 45 | +out_backprop_valid = [1, 2, 3, 2] |
| 46 | +out_backprop_same = [1, 2, 4, 3] |
| 47 | +out_backprop_in_sizes = {"VALID": out_backprop_valid, "SAME": out_backprop_same} |
| 48 | +stride_nhwc = [1, 2, 2, 1] |
| 49 | +stride_nchw = [1, 1, 2, 2] |
| 50 | + |
| 51 | + |
| 52 | +#TF graph |
| 53 | +def tf_model(padding): |
| 54 | + t1 = tf.placeholder(dtype=tf.float32, shape=input_sizes_nhwc, name='t1') |
| 55 | + t2 = tf.constant(filter_size_hwio, dtype=tf.int32, name='t2') |
| 56 | + t3 = tf.placeholder( |
| 57 | + dtype=tf.float32, shape=out_backprop_in_sizes[padding], name='t3') |
| 58 | + |
| 59 | + #reshaping the out_backprop to NHWC since TF does not support NCHW |
| 60 | + t3 = tf.transpose(t3, [0, 2, 3, 1]) |
| 61 | + |
| 62 | + #Cast dtype to bfloat16 for TF because NNP casts ng_model inputs |
| 63 | + t1 = tf.cast(t1, dtype=tf.bfloat16) |
| 64 | + t3 = tf.cast(t3, dtype=tf.bfloat16) |
| 65 | + |
| 66 | + filt = nn_ops.conv2d_backprop_filter( |
| 67 | + t1, t2, t3, stride_nhwc, padding=padding, data_format='NHWC') |
| 68 | + |
| 69 | + #Cast dtype back to float32 similar to NNP |
| 70 | + filt = tf.cast(filt, dtype=tf.float32) |
| 71 | + return filt, t1, t3 |
| 72 | + |
| 73 | + |
| 74 | +#Ngraph Graph |
| 75 | +def ng_model(padding): |
| 76 | + t1 = tf.placeholder(dtype=tf.float32, shape=input_sizes_nchw, name='t1') |
| 77 | + t2 = tf.constant(filter_size_hwio, dtype=tf.int32, name='t2') |
| 78 | + t3 = tf.placeholder( |
| 79 | + dtype=tf.float32, shape=out_backprop_in_sizes[padding], name='t3') |
| 80 | + |
| 81 | + filt = nn_ops.conv2d_backprop_filter( |
| 82 | + t1, t2, t3, stride_nchw, padding=padding, data_format='NCHW') |
| 83 | + return filt, t1, t3 |
| 84 | + |
| 85 | + |
| 86 | +config = tf.ConfigProto( |
| 87 | + allow_soft_placement=True, |
| 88 | + log_device_placement=False, |
| 89 | + inter_op_parallelism_threads=1) |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("padding", ("VALID", "SAME")) |
| 93 | +def test_conv2dbackpropfilter_nchw(padding): |
| 94 | + n_np_inp = np.random.rand(*input_sizes_nchw).astype('f') |
| 95 | + n_np_out = np.random.rand(*out_backprop_in_sizes[padding]).astype('f') |
| 96 | + |
| 97 | + #Reshape to NHWC for TF |
| 98 | + t_np_inp = np.transpose(n_np_inp, (0, 2, 3, 1)) |
| 99 | + t_np_out = np.transpose(n_np_out, (0, 2, 3, 1)) |
| 100 | + |
| 101 | + with tf.Session(config=config) as sess_tf: |
| 102 | + ngraph_bridge.disable() |
| 103 | + tf_out, input_data, out_backprop = tf_model(padding) |
| 104 | + feed_dict = {input_data: t_np_inp, out_backprop: t_np_out} |
| 105 | + tf_outval = sess_tf.run(tf_out, feed_dict=feed_dict) |
| 106 | + |
| 107 | + #Test 2: model2 with ngraph, NNP backend |
| 108 | + with tf.Session(config=config) as sess_ng: |
| 109 | + ngraph_bridge.enable() |
| 110 | + ngraph_bridge.update_config(config) |
| 111 | + os.environ['NGRAPH_TF_DISABLE_DEASSIGN_CLUSTERS'] = '1' |
| 112 | + ng_out, input_data, out_backprop = ng_model(padding) |
| 113 | + feed_dict = {input_data: n_np_inp, out_backprop: n_np_out} |
| 114 | + ng_outval = sess_ng.run(ng_out, feed_dict=feed_dict) |
| 115 | + |
| 116 | + assert np.allclose(tf_outval, ng_outval, rtol=0, atol=1e-02) |
0 commit comments