1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ============================================================================== 15 """Functional tests for reduction ops.""" 16 17 from __future__ import absolute_import 18 from __future__ import division 19 from __future__ import print_function 20 21 import numpy as np 22 23 from tensorflow.python.framework import ops 24 from tensorflow.python.ops import math_ops 25 from tensorflow.python.platform import test 26 27 28 class BaseReductionTest(test.TestCase): 29 30 def _tf_reduce(self, x, reduction_axes, keepdims): 31 raise NotImplementedError() 32 33 34 class BigReductionTest(BaseReductionTest): 35 """Test reductions for sum and boolean all over a wide range of shapes.""" 36 37 def _tf_reduce_max(self, x, reduction_axes, keepdims): 38 return math_ops.reduce_max(x, reduction_axes, keepdims) 39 40 def _tf_reduce_all(self, x, reduction_axes, keepdims): 41 return math_ops.reduce_all(x, reduction_axes, keepdims) 42 43 def _tf_reduce_mean(self, x, reduction_axes, keepdims): 44 return math_ops.reduce_mean(x, reduction_axes, keepdims) 45 46 def _tf_reduce_sum(self, x, reduction_axes, keepdims): 47 return math_ops.reduce_sum(x, reduction_axes, keepdims) 48 49 def testFloat32Sum(self): 50 # make sure we test all possible kernel invocations 51 # logic is the same for all ops, test just float32 for brevity 52 arr_ = np.ones([4097, 4097], dtype=np.float32) 53 for size_x in [ 54 1, 2, 3, 4, 16, 17, 32, 33, 64, 65, 128, 131, 256, 263, 1024, 1025, 55 4096, 4097 56 ]: 57 for size_y in [ 58 1, 2, 3, 4, 16, 17, 32, 33, 64, 65, 128, 131, 256, 263, 1024, 1025, 59 4096, 4097 60 ]: 61 arr = arr_[0:size_x, 0:size_y] 62 col_sum = np.ones([size_y], dtype=np.float32) * size_x 63 row_sum = np.ones([size_x], dtype=np.float32) * size_y 64 full_sum = np.ones([], dtype=np.float32) * size_x * size_y 65 66 with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: 67 tf_row_sum = self._tf_reduce_sum(arr, 1, False) 68 tf_col_sum = self._tf_reduce_sum(arr, 0, False) 69 tf_full_sum = self._tf_reduce_sum(arr, [0, 1], False) 70 tf_out_row, tf_out_col, tf_out_full = sess.run( 71 [tf_row_sum, tf_col_sum, tf_full_sum]) 72 self.assertAllClose(col_sum, tf_out_col) 73 self.assertAllClose(row_sum, tf_out_row) 74 self.assertAllClose(full_sum, tf_out_full) 75 76 arr_ = np.ones([130, 130, 130], dtype=np.float32) 77 for size_x in range(1, 130, 13): 78 for size_y in range(1, 130, 13): 79 for size_z in range(1, 130, 13): 80 arr = arr_[0:size_x, 0:size_y, 0:size_z] 81 sum_y = np.ones([size_x, size_z], dtype=np.float32) 82 sum_xz = np.ones([size_y], dtype=np.float32) 83 84 with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: 85 tf_sum_xz = self._tf_reduce_mean(arr, [0, 2], False) 86 tf_sum_y = self._tf_reduce_mean(arr, 1, False) 87 tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) 88 self.assertAllClose(sum_y, tf_out_sum_y) 89 self.assertAllClose(sum_xz, tf_out_sum_xz) 90 91 def testFloat32Max(self): 92 # make sure we test all possible kernel invocations 93 # logic is the same for all ops, test just float32 for brevity 94 arr_ = np.random.uniform( 95 low=-3, high=-1, size=[4105, 4105]).astype(np.float32) 96 for size_x in [ 97 1, 2, 3, 4, 16, 17, 32, 33, 64, 65, 128, 131, 256, 263, 1024, 1025, 98 4096, 4097 99 ]: 100 for size_y in [ 101 1, 2, 3, 4, 16, 17, 32, 33, 64, 65, 128, 131, 256, 263, 1024, 1025, 102 4096, 4097 103 ]: 104 arr = arr_[0:size_x, 0:size_y] 105 col_max = np.max(arr, axis=0) 106 row_max = np.max(arr, axis=1) 107 full_max = np.max(col_max) 108 109 with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: 110 tf_row_max = self._tf_reduce_max(arr, 1, False) 111 tf_col_max = self._tf_reduce_max(arr, 0, False) 112 tf_full_max = self._tf_reduce_max(arr, [0, 1], False) 113 tf_out_row, tf_out_col, tf_out_full = sess.run( 114 [tf_row_max, tf_col_max, tf_full_max]) 115 self.assertAllClose(col_max, tf_out_col) 116 self.assertAllClose(row_max, tf_out_row) 117 self.assertAllClose(full_max, tf_out_full) 118 119 arr_ = np.random.uniform( 120 low=-3, high=-1, size=[130, 130, 130]).astype(np.float32) 121 for size_x in range(1, 130, 13): 122 for size_y in range(1, 130, 13): 123 for size_z in range(1, 130, 13): 124 arr = arr_[0:size_x, 0:size_y, 0:size_z] 125 sum_y = np.max(arr, axis=1) 126 sum_xz = np.max(arr, axis=(0, 2)) 127 128 with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: 129 tf_sum_xz = self._tf_reduce_max(arr, [0, 2], False) 130 tf_sum_y = self._tf_reduce_max(arr, 1, False) 131 tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) 132 self.assertAllClose(sum_y, tf_out_sum_y) 133 self.assertAllClose(sum_xz, tf_out_sum_xz) 134 135 def testBooleanAll(self): 136 # make sure we test all possible kernel invocations 137 # test operation where T(0) is not the identity 138 arr_ = np.ones([4097, 4097], dtype=np.bool) 139 for size_x in [ 140 1, 2, 3, 4, 16, 17, 32, 33, 64, 65, 128, 131, 256, 263, 1024, 1025, 141 4096, 4097 142 ]: 143 for size_y in [ 144 1, 2, 3, 4, 16, 17, 32, 33, 64, 65, 128, 131, 256, 263, 1024, 1025, 145 4096, 4097 146 ]: 147 arr = arr_[0:size_x, 0:size_y] 148 col_sum = np.ones([size_y], dtype=np.bool) 149 row_sum = np.ones([size_x], dtype=np.bool) 150 full_sum = np.ones([1], dtype=np.bool).reshape([]) 151 152 with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: 153 tf_row_sum = self._tf_reduce_all(arr, 1, False) 154 tf_col_sum = self._tf_reduce_all(arr, 0, False) 155 tf_full_sum = self._tf_reduce_all(arr, [0, 1], False) 156 tf_out_row, tf_out_col, tf_out_full = sess.run( 157 [tf_row_sum, tf_col_sum, tf_full_sum]) 158 self.assertAllClose(col_sum, tf_out_col) 159 self.assertAllClose(row_sum, tf_out_row) 160 self.assertAllClose(full_sum, tf_out_full) 161 162 arr_ = np.ones([130, 130, 130], dtype=np.bool) 163 for size_x in range(1, 130, 13): 164 for size_y in range(1, 130, 13): 165 for size_z in range(1, 130, 13): 166 arr = arr_[0:size_x, 0:size_y, 0:size_z] 167 sum_y = np.ones([size_x, size_z], dtype=np.bool) 168 sum_xz = np.ones([size_y], dtype=np.bool) 169 170 with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: 171 tf_sum_xz = self._tf_reduce_all(arr, [0, 2], False) 172 tf_sum_y = self._tf_reduce_all(arr, 1, False) 173 tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) 174 self.assertAllClose(sum_y, tf_out_sum_y) 175 self.assertAllClose(sum_xz, tf_out_sum_xz) 176 177 178 if __name__ == "__main__": 179 test.main() 180