Home | History | Annotate | Download | only in kernel_tests
      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 """Tests for tensorflow.ops.tf.matmul."""
     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 constant_op
     24 from tensorflow.python.framework import dtypes
     25 from tensorflow.python.ops import gradient_checker
     26 from tensorflow.python.ops import math_ops
     27 from tensorflow.python.platform import test
     28 
     29 
     30 def RandMatrix(rows, cols, tr, round_bfloat=False):
     31   if tr:
     32     rows, cols = cols, rows
     33   rand_func = np.random.randint if round_bfloat else np.random.uniform
     34   return (np.clip(
     35       rand_func(
     36           low=-256.0, high=256.0, size=rows * cols), -64,
     37       64) / 128.0).reshape([rows, cols]).astype(np.float32)
     38 
     39 
     40 class SparseMatMulTest(test.TestCase):
     41 
     42   def _testCpuMatmul(self,
     43                      x,
     44                      y,
     45                      tr_a=False,
     46                      tr_b=False,
     47                      sp_a=True,
     48                      sp_b=False,
     49                      x_dtype=dtypes.float32,
     50                      y_dtype=dtypes.float32):
     51     with self.test_session(use_gpu=False):
     52       tf_x = math_ops.cast(x, x_dtype)
     53       tf_y = math_ops.cast(y, y_dtype)
     54       tf_ans = math_ops.matmul(
     55           tf_x,
     56           tf_y,
     57           transpose_a=tr_a,
     58           transpose_b=tr_b,
     59           a_is_sparse=sp_a,
     60           b_is_sparse=sp_b)
     61       out = tf_ans.eval()
     62       np_x = math_ops.cast(tf_x, dtypes.float32).eval()
     63       np_y = math_ops.cast(tf_y, dtypes.float32).eval()
     64 
     65     if tr_a:
     66       np_x = np.transpose(np_x)
     67     if tr_b:
     68       np_y = np.transpose(np_y)
     69 
     70     np_ans = np.matrix(np_x) * np.matrix(np_y)
     71     self.assertShapeEqual(np_ans, tf_ans)
     72     self.assertAllCloseAccordingToType(np_ans, out, rtol=1e-4, atol=1e-4)
     73 
     74   def testBasic(self):
     75     x = np.arange(0., 4.).reshape([4, 1]).astype(np.float32)
     76     y = np.arange(-1., 1.).reshape([1, 2]).astype(np.float32)
     77     for x_dtype in (dtypes.float32, dtypes.bfloat16):
     78       for y_dtype in (dtypes.float32, dtypes.bfloat16):
     79         self._testCpuMatmul(x, y, x_dtype=x_dtype, y_dtype=y_dtype)
     80 
     81   def testZeroDim(self):
     82     x = np.ones((4, 0)).astype(np.float32)
     83     y = np.ones((0, 3)).astype(np.float32)
     84     for x_dtype in (dtypes.float32, dtypes.bfloat16):
     85       for y_dtype in (dtypes.float32, dtypes.bfloat16):
     86         self._testCpuMatmul(x, y, x_dtype=x_dtype, y_dtype=y_dtype)
     87 
     88   def testEmpty(self):
     89     x = np.ones((0, 0)).astype(np.float32)
     90     y = np.ones((0, 0)).astype(np.float32)
     91     for x_dtype in (dtypes.float32, dtypes.bfloat16):
     92       for y_dtype in (dtypes.float32, dtypes.bfloat16):
     93         self._testCpuMatmul(x, y, x_dtype=x_dtype, y_dtype=y_dtype)
     94 
     95   # Tests setting one dimension to be a high value.
     96   def testLarge(self):
     97     r1 = np.random.randint(6000, 20000)
     98     r2 = np.random.randint(1, 10)
     99     r3 = np.random.randint(1, 10)
    100     for m, k, n in [(r1, r2, r3), (r2, r1, r3), (r2, r3, r1)]:
    101       for x_dtype in (dtypes.float32, dtypes.bfloat16):
    102         for y_dtype in (dtypes.float32, dtypes.bfloat16):
    103           x = RandMatrix(m, k, False)
    104           y = RandMatrix(k, n, False)
    105           self._testCpuMatmul(x, y, x_dtype=x_dtype, y_dtype=y_dtype)
    106 
    107   # Tests random sized matrices.
    108   def testRandom(self):
    109     for tr_a in [True, False]:
    110       for tr_b in [True, False]:
    111         for sp_a in [True, False]:
    112           for sp_b in [True, False]:
    113             for x_dtype in (dtypes.float32, dtypes.bfloat16):
    114               for y_dtype in (dtypes.float32, dtypes.bfloat16):
    115                 n, k, m = np.random.randint(1, 100, size=3)
    116                 x = RandMatrix(n, k, tr_a)
    117                 y = RandMatrix(k, m, tr_b)
    118                 self._testCpuMatmul(
    119                     x,
    120                     y,
    121                     tr_a,
    122                     tr_b,
    123                     sp_a,
    124                     sp_b,
    125                     x_dtype=x_dtype,
    126                     y_dtype=y_dtype)
    127 
    128 
    129 class MatMulGradientTest(test.TestCase):
    130 
    131   def _testGradients(self, tr_a, tr_b, sp_a, sp_b, a_dtype, b_dtype, delta,
    132                      name):
    133     with self.test_session():
    134       a = constant_op.constant(
    135           RandMatrix(
    136               3, 2, tr_a, round_bfloat=True), dtype=dtypes.float32)
    137       b = constant_op.constant(
    138           RandMatrix(
    139               2, 4, tr_b, round_bfloat=True), dtype=dtypes.float32)
    140       tf_a = math_ops.cast(a, a_dtype) if a_dtype != dtypes.float32 else a
    141       tf_b = math_ops.cast(b, b_dtype) if b_dtype != dtypes.float32 else b
    142 
    143       m = math_ops.matmul(
    144           tf_a,
    145           tf_b,
    146           name=name,
    147           transpose_a=tr_a,
    148           transpose_b=tr_b,
    149           a_is_sparse=sp_a,
    150           b_is_sparse=sp_b)
    151       err = (gradient_checker.compute_gradient_error(
    152           a, [2, 3] if tr_a else [3, 2],
    153           m, [3, 4],
    154           x_init_value=a.eval(),
    155           delta=delta) + gradient_checker.compute_gradient_error(
    156               b, [4, 2] if tr_b else [2, 4],
    157               m, [3, 4],
    158               x_init_value=b.eval(),
    159               delta=delta))
    160     self.assertLess(err, delta / 2.)
    161 
    162   def testGradientInput(self):
    163     for tr_a in [True, False]:
    164       for tr_b in [True, False]:
    165         for sp_a in [True, False]:
    166           for sp_b in [True, False]:
    167             for a_dtype in (dtypes.float32, dtypes.bfloat16):
    168               for b_dtype in (dtypes.float32, dtypes.bfloat16):
    169                 # Note: bfloat16 only has 7 mantissa bits, versus float32 with
    170                 # 10. Hence, we shift by 2 bits to pass the test.
    171                 if a_dtype == dtypes.bfloat16 and b_dtype == dtypes.bfloat16:
    172                   delta = 1 / 16.
    173                 else:
    174                   delta = 1 / 64.
    175                 name = "sparse_matmul_%s_%s_%s_%s" % (tr_a, tr_b, sp_a, sp_b)
    176                 self._testGradients(tr_a, tr_b, sp_a, sp_b, a_dtype, b_dtype,
    177                                     delta, name)
    178 
    179 
    180 if __name__ == "__main__":
    181   test.main()
    182