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 
16 #if GOOGLE_CUDA
17 
18 #define EIGEN_USE_GPU
19 
20 #include "tensorflow/core/framework/bfloat16.h"
21 #define SPECIALIZE_FOR_GPUS
22 #include "tensorflow/core/kernels/cast_op.h"
23 #undef SPECIALIZE_FOR_GPUS
24 
25 namespace tensorflow {
26 namespace functor {
27 
28 typedef Eigen::GpuDevice GPUDevice;
29 
30 CAST_FUNCTORS(GPUDevice);
31 
32 #define DEFINE(O, I) template struct CastFunctor<GPUDevice, O, I>
33 
34 #define DEFINE_ALL_FROM(in_type)        \
35   DEFINE(in_type, bool);                \
36   DEFINE(in_type, uint8);               \
37   DEFINE(in_type, uint16);              \
38   DEFINE(in_type, uint32);              \
39   DEFINE(in_type, uint64);              \
40   DEFINE(in_type, int8);                \
41   DEFINE(in_type, int16);               \
42   DEFINE(in_type, int32);               \
43   DEFINE(in_type, int64);               \
44   DEFINE(in_type, Eigen::half);         \
45   DEFINE(in_type, float);               \
46   DEFINE(in_type, double);              \
47   DEFINE(in_type, std::complex<float>); \
48   DEFINE(in_type, std::complex<double>)
49 
50 DEFINE_ALL_FROM(bool);
51 DEFINE_ALL_FROM(uint8);
52 DEFINE_ALL_FROM(uint16);
53 DEFINE_ALL_FROM(uint32);
54 DEFINE_ALL_FROM(uint64);
55 DEFINE_ALL_FROM(int8);
56 DEFINE_ALL_FROM(int16);
57 DEFINE_ALL_FROM(int32);
58 DEFINE_ALL_FROM(int64);
59 DEFINE_ALL_FROM(double);
60 DEFINE_ALL_FROM(std::complex<double>);
61 DEFINE(float, bfloat16);
62 
63 #define DEFINE_ALL_TO_FLOAT(out_type) \
64   DEFINE(out_type, bool);             \
65   DEFINE(out_type, uint8);            \
66   DEFINE(out_type, uint16);           \
67   DEFINE(out_type, uint32);           \
68   DEFINE(out_type, uint64);           \
69   DEFINE(out_type, int8);             \
70   DEFINE(out_type, int16);            \
71   DEFINE(out_type, int32);            \
72   DEFINE(out_type, int64);            \
73   DEFINE(out_type, Eigen::half);      \
74   DEFINE(out_type, float);            \
75   DEFINE(out_type, std::complex<float>)
76 
77 #define DEFINE_ALL_TO_HALF(out_type) \
78   DEFINE(out_type, bool);            \
79   DEFINE(out_type, uint8);           \
80   DEFINE(out_type, uint16);          \
81   DEFINE(out_type, uint32);          \
82   DEFINE(out_type, uint64);          \
83   DEFINE(out_type, int8);            \
84   DEFINE(out_type, int16);           \
85   DEFINE(out_type, int32);           \
86   DEFINE(out_type, int64);           \
87   DEFINE(out_type, Eigen::half)
88 
89 DEFINE_ALL_TO_HALF(Eigen::half);
90 DEFINE_ALL_TO_HALF(bfloat16);
91 DEFINE_ALL_TO_FLOAT(float);
92 DEFINE_ALL_TO_FLOAT(std::complex<float>);
93 
94 #undef DEFINE_ALL_TO_FLOAT
95 #undef DEFINE_ALL_TO_HALF
96 #undef DEFINE_ALL_FROM
97 #undef DEFINE
98 
99 }  // end namespace functor
100 }  // end namespace tensorflow
101 
102 #endif  // GOOGLE_CUDA
103