forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumeric_types.h
More file actions
249 lines (208 loc) · 6.76 KB
/
Copy pathnumeric_types.h
File metadata and controls
249 lines (208 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_FRAMEWORK_NUMERIC_TYPES_H_
#define TENSORFLOW_FRAMEWORK_NUMERIC_TYPES_H_
#include <complex>
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
// Disable clang-format to prevent 'FixedPoint' header from being included
// before 'Tensor' header on which it depends.
// clang-format off
#include "third_party/eigen3/unsupported/Eigen/CXX11/FixedPoint"
// clang-format on
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
// Single precision complex.
typedef std::complex<float> complex64;
// Double precision complex.
typedef std::complex<double> complex128;
// We use Eigen's QInt implementations for our quantized int types.
typedef Eigen::QInt8 qint8;
typedef Eigen::QUInt8 quint8;
typedef Eigen::QInt32 qint32;
typedef Eigen::QInt16 qint16;
typedef Eigen::QUInt16 quint16;
// see framework/bfloat16.h for description.
struct bfloat16 {
EIGEN_DEVICE_FUNC bfloat16() {}
EIGEN_DEVICE_FUNC explicit bfloat16(const float v) {
if (Eigen::numext::isnan(v)) {
value = NAN_VALUE;
return;
}
const uint16_t* p = reinterpret_cast<const uint16_t*>(&v);
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
value = p[0];
#else
value = p[1];
#endif
}
// Following the convention of numpy, converting between complex and
// float will lead to loss of imag value.
explicit EIGEN_DEVICE_FUNC bfloat16(const complex64& val)
: bfloat16(val.real()) {}
explicit EIGEN_DEVICE_FUNC bfloat16(const complex128& val)
: bfloat16(static_cast<float>(val.real())) {}
template <class T>
explicit EIGEN_DEVICE_FUNC bfloat16(const T& val)
: bfloat16(static_cast<float>(val)) {}
EIGEN_DEVICE_FUNC explicit operator float() const {
float result;
uint16_t* q = reinterpret_cast<uint16_t*>(&result);
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
q[0] = value;
q[1] = 0;
#else
q[0] = 0;
q[1] = value;
#endif
return result;
}
EIGEN_DEVICE_FUNC explicit operator bool() const {
return static_cast<bool>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator Eigen::half() const {
return static_cast<Eigen::half>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator short() const {
return static_cast<short>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator int() const {
return static_cast<int>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator long() const {
return static_cast<long>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator char() const {
return static_cast<char>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator signed char() const {
return static_cast<signed char>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator unsigned char() const {
return static_cast<unsigned char>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator unsigned int() const {
return static_cast<unsigned int>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator unsigned long() const {
return static_cast<unsigned long>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator unsigned long long() const {
return static_cast<unsigned long long>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator long long() const {
return static_cast<long long>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator double() const {
return static_cast<double>(float(*this));
}
EIGEN_DEVICE_FUNC explicit operator complex64() const {
return complex64(float(*this), float(0.0));
}
EIGEN_DEVICE_FUNC explicit operator complex128() const {
return complex128(double(*this), double(0.0));
}
static bfloat16 epsilon() {
bfloat16 x;
x.value = 0x3c00; // 0x1.0p-7
return x;
}
uint16_t value;
// A value that represents "not a number".
static const uint16_t NAN_VALUE = 0x7FC0;
};
inline bfloat16 operator+(bfloat16 a, bfloat16 b) {
return bfloat16(static_cast<float>(a) + static_cast<float>(b));
}
inline bfloat16 operator-(bfloat16 a, bfloat16 b) {
return bfloat16(static_cast<float>(a) - static_cast<float>(b));
}
inline bfloat16 operator*(bfloat16 a, bfloat16 b) {
return bfloat16(static_cast<float>(a) * static_cast<float>(b));
}
inline bfloat16 operator/(bfloat16 a, bfloat16 b) {
return bfloat16(static_cast<float>(a) / static_cast<float>(b));
}
inline bfloat16 operator-(bfloat16 a) {
a.value ^= 0x8000;
return a;
}
inline bool operator<(bfloat16 a, bfloat16 b) {
return static_cast<float>(a) < static_cast<float>(b);
}
inline bool operator<=(bfloat16 a, bfloat16 b) {
return static_cast<float>(a) <= static_cast<float>(b);
}
inline bool operator==(bfloat16 a, bfloat16 b) {
return static_cast<float>(a) == static_cast<float>(b);
}
inline bool operator!=(bfloat16 a, bfloat16 b) {
return static_cast<float>(a) != static_cast<float>(b);
}
inline bool operator>(bfloat16 a, bfloat16 b) {
return static_cast<float>(a) > static_cast<float>(b);
}
inline bool operator>=(bfloat16 a, bfloat16 b) {
return static_cast<float>(a) >= static_cast<float>(b);
}
inline bfloat16& operator+=(bfloat16& a, bfloat16 b) {
a = a + b;
return a;
}
inline bfloat16& operator-=(bfloat16& a, bfloat16 b) {
a = a - b;
return a;
}
inline bfloat16 operator++(bfloat16& a) {
a += bfloat16(1);
return a;
}
inline bfloat16 operator--(bfloat16& a) {
a -= bfloat16(1);
return a;
}
inline bfloat16 operator++(bfloat16& a, int) {
bfloat16 original_value = a;
++a;
return original_value;
}
inline bfloat16 operator--(bfloat16& a, int) {
bfloat16 original_value = a;
--a;
return original_value;
}
inline bfloat16& operator*=(bfloat16& a, bfloat16 b) {
a = a * b;
return a;
}
inline bfloat16& operator/=(bfloat16& a, bfloat16 b) {
a = a / b;
return a;
}
} // end namespace tensorflow
namespace Eigen {
template <>
struct NumTraits<tensorflow::bfloat16> : GenericNumTraits<uint16_t> {};
using ::tensorflow::operator==;
using ::tensorflow::operator!=;
} // namespace Eigen
#if defined(COMPILER_MSVC) && !defined(__clang__)
namespace std {
template <>
struct hash<Eigen::half> {
std::size_t operator()(const Eigen::half& a) const {
return static_cast<std::size_t>(a.x);
}
};
} // namespace std
#endif // COMPILER_MSVC
#endif // TENSORFLOW_FRAMEWORK_NUMERIC_TYPES_H_