cast_op_test.py revision 9a8c5ad18c61cb0695d31e2ce969008c82999c7c
1# Copyright 2015 Google Inc. 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
16"""Tests for tensorflow.ops.tf.cast."""
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import numpy as np
22import tensorflow as tf
23
24
25class CastOpTest(tf.test.TestCase):
26
27  def _toDataType(self, dtype):
28    """Returns TensorFlow data type for numpy type."""
29    if dtype == np.float32:
30      return tf.float32
31    elif dtype == np.float64:
32      return tf.float64
33    elif dtype == np.int32:
34      return tf.int32
35    elif dtype == np.int64:
36      return tf.int64
37    elif dtype == np.bool:
38      return tf.bool
39    else:
40      return None
41
42  def _cast(self, x, dtype, use_gpu=False):
43    with self.test_session(use_gpu=use_gpu):
44      val = tf.constant(x, self._toDataType(np.array([x]).dtype))
45      return tf.cast(val, self._toDataType(dtype), name="cast").eval()
46
47  def _test(self, x, dtype, use_gpu=False):
48    """Tests cast(x) to dtype behaves the same as numpy.astype."""
49    np_ans = x.astype(dtype)
50    tf_ans = self._cast(x, dtype, use_gpu)
51    self.assertAllEqual(np_ans, tf_ans)
52
53  def _testTypes(self, x, use_gpu=False):
54    """Tests cast(x) to different tf."""
55    if use_gpu:
56      type_list = [np.float32, np.float64, np.int64]
57    else:
58      type_list = [np.float32, np.float64, np.int32, np.int64]
59    for from_type in type_list:
60      for to_type in type_list:
61        self._test(x.astype(from_type), to_type, use_gpu)
62
63    self._test(x.astype(np.bool), np.float32, use_gpu)
64    self._test(x.astype(np.uint8), np.float32, use_gpu)
65    if not use_gpu:
66      self._test(x.astype(np.bool), np.int32, use_gpu)
67      self._test(x.astype(np.int32), np.int32, use_gpu)
68
69  def _testAll(self, x):
70    self._testTypes(x, use_gpu=False)
71    if x.dtype == np.float32 or x.dtype == np.float64:
72      self._testTypes(x, use_gpu=True)
73
74  def testBasic(self):
75    self._testAll(np.arange(-10, 10).reshape(2, 10))
76    self._testAll(np.linspace(-10, 10, 17))
77
78  def testSmallValues(self):
79    f4 = np.finfo(np.float32)
80    f8 = np.finfo(np.float64)
81    self._testAll(np.array([0, -1, 1, -f4.resolution, f4.resolution,
82                            f8.resolution, -f8.resolution]))
83
84  def testBfloat16(self):
85    a = np.random.uniform(-100, 100, 100).astype(np.float32)
86    with self.test_session(use_gpu=False):
87      b = tf.cast(tf.cast(a, tf.bfloat16), tf.float32)
88      self.assertAllClose(a, b.eval(), rtol=1/128.)
89    with self.test_session(use_gpu=True):
90      b = tf.cast(tf.cast(a, tf.bfloat16), tf.float32)
91      self.assertAllClose(a, b.eval(), rtol=1/128.)
92
93  def testRandom(self):
94    self._testAll(np.random.normal(0, 10, 210).reshape([2, 3, 5, 7]))
95    self._testAll(np.random.normal(0, 1e6, 210).reshape([2, 3, 5, 7]))
96
97  # Special values like int32max, int64min, inf, -inf, nan casted to
98  # integer values in somewhat unexpected ways. And they behave
99  # differently on CPU and GPU.
100  def _compare(self, x, dst_dtype, expected, use_gpu=False):
101    np.testing.assert_equal(self._cast(x, dst_dtype, use_gpu=use_gpu),
102                            dst_dtype(expected))
103
104  def testIntToFloatBoundary(self):
105    i4 = np.iinfo(np.int32)
106    i8 = np.iinfo(np.int64)
107
108    self._compare(i4.min, np.float32, i4.min, False)
109    self._compare(i4.max, np.float32, i4.max, False)
110    self._compare(i8.min, np.float32, i8.min, False)
111    self._compare(i8.max, np.float32, i8.max, False)
112    self._compare(i4.min, np.float64, i4.min, False)
113    self._compare(i4.max, np.float64, i4.max, False)
114    self._compare(i8.min, np.float64, i8.min, False)
115    self._compare(i8.max, np.float64, i8.max, False)
116    # NOTE: GPU does not support int32/int64 for casting.
117
118  def testInfNan(self):
119    i4 = np.iinfo(np.int32)
120    i8 = np.iinfo(np.int64)
121
122    self._compare(np.inf, np.float32, np.inf, False)
123    self._compare(np.inf, np.float64, np.inf, False)
124    self._compare(np.inf, np.int32, i4.min, False)
125    self._compare(np.inf, np.int64, i8.min, False)
126    self._compare(-np.inf, np.float32, -np.inf, False)
127    self._compare(-np.inf, np.float64, -np.inf, False)
128    self._compare(-np.inf, np.int32, i4.min, False)
129    self._compare(-np.inf, np.int64, i8.min, False)
130    self.assertAllEqual(np.isnan(self._cast(np.nan, np.float32, False)), True)
131    self.assertAllEqual(np.isnan(self._cast(np.nan, np.float64, False)), True)
132    self._compare(np.nan, np.int32, i4.min, False)
133    self._compare(np.nan, np.int64, i8.min, False)
134
135    self._compare(np.inf, np.float32, np.inf, True)
136    self._compare(np.inf, np.float64, np.inf, True)
137    self._compare(-np.inf, np.float32, -np.inf, True)
138    self._compare(-np.inf, np.float64, -np.inf, True)
139    self.assertAllEqual(np.isnan(self._cast(np.nan, np.float32, True)), True)
140    self.assertAllEqual(np.isnan(self._cast(np.nan, np.float64, True)), True)
141
142  def _OpError(self, x, dtype, err):
143    with self.test_session():
144      with self.assertRaisesOpError(err):
145        tf.cast(x, dtype).eval()
146
147  def testNotImplemented(self):
148    self._OpError(np.arange(0, 10), tf.string,
149                  "Cast.*int64.*string.*")
150
151  def testGradients(self):
152    t = [tf.float32, tf.float64]
153    for src_t in t:
154      for dst_t in t:
155        with self.test_session():
156          x = tf.constant(1.0, src_t)
157          z = tf.identity(x)
158          y = tf.cast(z, dst_t)
159          err = tf.test.compute_gradient_error(x, [], y, [])
160          self.assertLess(err, 1e-3)
161
162
163class SparseTensorCastTest(tf.test.TestCase):
164
165  def testCast(self):
166    indices = tf.constant([[0], [1], [2]], tf.int64)
167    values = tf.constant(np.array([1, 2, 3], np.int64))
168    shape = tf.constant([3], tf.int64)
169    st = tf.SparseTensor(indices, values, shape)
170    st_cast = tf.cast(st, tf.float32)
171    with self.test_session():
172      self.assertAllEqual(st_cast.indices.eval(), [[0], [1], [2]])
173      self.assertAllEqual(st_cast.values.eval(),
174                          np.array([1, 2, 3], np.float32))
175      self.assertAllEqual(st_cast.shape.eval(), [3])
176
177
178class SaturateCastTest(tf.test.TestCase):
179
180  def testSaturate(self):
181    in_types = tf.float32,
182    out_types = tf.int8, tf.uint8, tf.int16, tf.float32
183    with self.test_session() as sess:
184      for in_type in in_types:
185        for out_type in out_types:
186          lo, hi = in_type.min, in_type.max
187          x = tf.constant([lo, lo + 1, lo // 2, hi // 2, hi - 1, hi],
188                          dtype=in_type)
189          y = tf.saturate_cast(x, dtype=out_type)
190          self.assertEqual(y.dtype, out_type)
191          x, y = sess.run([x, y])
192          correct = np.maximum(out_type.min, np.minimum(out_type.max, x))
193          self.assertAllEqual(correct, y)
194
195
196if __name__ == "__main__":
197  tf.test.main()
198