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