extensions.h 11.7 KB
Newer Older
1
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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.

#pragma once

17 18 19 20
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/complex.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/core/hostdevice.h"
21 22 23 24 25

#include "unsupported/Eigen/CXX11/Tensor"

namespace Eigen {

26
using float16 = phi::dtype::float16;
27
template <typename T>
28
using complex = phi::dtype::complex<T>;
29 30 31 32 33

template <typename T>
struct NumTraits;

template <>
34 35
struct NumTraits<phi::dtype::bfloat16>
    : GenericNumTraits<phi::dtype::bfloat16> {
36 37 38 39 40 41 42
  enum {
    IsSigned = true,
    IsInteger = false,
    IsComplex = false,
    RequireInitialization = false
  };

43 44
  HOSTDEVICE static inline phi::dtype::bfloat16 epsilon() {
    return phi::dtype::raw_uint16_to_bfloat16(0x3400);
45
  }
46 47
  HOSTDEVICE static inline phi::dtype::bfloat16 dummy_precision() {
    return phi::dtype::bfloat16(1e-5f);
48
  }
49 50
  HOSTDEVICE static inline phi::dtype::bfloat16 highest() {
    return phi::dtype::raw_uint16_to_bfloat16(0x7f7f);
51
  }
52 53
  HOSTDEVICE static inline phi::dtype::bfloat16 lowest() {
    return phi::dtype::raw_uint16_to_bfloat16(0xff7f);
54
  }
55 56
  HOSTDEVICE static inline phi::dtype::bfloat16 infinity() {
    return phi::dtype::raw_uint16_to_bfloat16(0x7f80);
57
  }
58 59
  HOSTDEVICE static inline phi::dtype::bfloat16 quiet_NaN() {
    return phi::dtype::raw_uint16_to_bfloat16(0xffc1);
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
template <>
struct NumTraits<complex<float>> : GenericNumTraits<std::complex<float>> {
  typedef float Real;
  typedef typename NumTraits<float>::Literal Literal;
  enum {
    IsComplex = 1,
    RequireInitialization = NumTraits<float>::RequireInitialization,
    ReadCost = 2 * NumTraits<float>::ReadCost,
    AddCost = 2 * NumTraits<Real>::AddCost,
    MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
  };

  EIGEN_DEVICE_FUNC
  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
  EIGEN_DEVICE_FUNC
  static inline Real dummy_precision() {
    return NumTraits<Real>::dummy_precision();
  }
  EIGEN_DEVICE_FUNC
  static inline int digits10() { return NumTraits<Real>::digits10(); }
};

template <>
struct NumTraits<complex<double>> : GenericNumTraits<std::complex<double>> {
  typedef double Real;
  typedef typename NumTraits<double>::Literal Literal;
  enum {
    IsComplex = 1,
    RequireInitialization = NumTraits<double>::RequireInitialization,
    ReadCost = 2 * NumTraits<double>::ReadCost,
    AddCost = 2 * NumTraits<Real>::AddCost,
    MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
  };

  EIGEN_DEVICE_FUNC
  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
  EIGEN_DEVICE_FUNC
  static inline Real dummy_precision() {
    return NumTraits<Real>::dummy_precision();
  }
  EIGEN_DEVICE_FUNC
  static inline int digits10() { return NumTraits<Real>::digits10(); }
};

107 108 109 110 111 112 113 114 115 116
template <>
struct NumTraits<float16> : GenericNumTraits<float16> {
  enum {
    IsSigned = true,
    IsInteger = false,
    IsComplex = false,
    RequireInitialization = false
  };

  HOSTDEVICE static inline float16 epsilon() {
117
    return phi::dtype::raw_uint16_to_float16(0x0800);
118 119 120
  }
  HOSTDEVICE static inline float16 dummy_precision() { return float16(1e-2f); }
  HOSTDEVICE static inline float16 highest() {
121
    return phi::dtype::raw_uint16_to_float16(0x7bff);
122 123
  }
  HOSTDEVICE static inline float16 lowest() {
124
    return phi::dtype::raw_uint16_to_float16(0xfbff);
125 126
  }
  HOSTDEVICE static inline float16 infinity() {
127
    return phi::dtype::raw_uint16_to_float16(0x7c00);
128 129
  }
  HOSTDEVICE static inline float16 quiet_NaN() {
130
    return phi::dtype::raw_uint16_to_float16(0x7c01);
131 132 133
  }
};

134 135 136 137 138
namespace numext {

//////////// bfloat methods /////////////

template <>
139 140
HOSTDEVICE inline bool(isnan)(const phi::dtype::bfloat16& a) {
  return (phi::dtype::isnan)(a);
141 142 143
}

template <>
144 145
HOSTDEVICE inline bool(isinf)(const phi::dtype::bfloat16& a) {
  return (phi::dtype::isinf)(a);
146 147 148
}

template <>
149 150
HOSTDEVICE inline bool(isfinite)(const phi::dtype::bfloat16& a) {
  return (phi::dtype::isfinite)(a);
151 152 153
}

template <>
154 155
HOSTDEVICE inline phi::dtype::bfloat16 exp(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::expf(static_cast<float>(a)));
156 157
}

R
ronnywang 已提交
158
template <>
159 160
HOSTDEVICE inline phi::dtype::bfloat16 expm1(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::expm1f(static_cast<float>(a)));
R
ronnywang 已提交
161 162
}

163
template <>
164 165
HOSTDEVICE inline phi::dtype::bfloat16 erf(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::erff(static_cast<float>(a)));
166 167 168
}

template <>
169 170
HOSTDEVICE inline phi::dtype::bfloat16 log(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::logf(static_cast<float>(a)));
171 172 173
}

template <>
174 175
HOSTDEVICE inline phi::dtype::bfloat16 tanh(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::tanhf(static_cast<float>(a)));
176 177 178
}

template <>
179 180
HOSTDEVICE inline phi::dtype::bfloat16 sqrt(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::sqrtf(static_cast<float>(a)));
181 182 183
}

template <>
184 185
HOSTDEVICE inline phi::dtype::bfloat16 ceil(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::ceilf(static_cast<float>(a)));
186 187 188
}

template <>
189 190
HOSTDEVICE inline phi::dtype::bfloat16 floor(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::floorf(static_cast<float>(a)));
191 192 193
}

template <>
194 195
HOSTDEVICE inline phi::dtype::bfloat16 round(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::roundf(static_cast<float>(a)));
196 197 198
}

template <>
199 200 201
HOSTDEVICE inline phi::dtype::bfloat16 pow(const phi::dtype::bfloat16& a,
                                           const phi::dtype::bfloat16& b) {
  return phi::dtype::bfloat16(
202
      ::powf(static_cast<float>(a), static_cast<float>(b)));
203 204 205
}

template <>
206 207
HOSTDEVICE inline phi::dtype::bfloat16 abs(const phi::dtype::bfloat16& a) {
  return phi::dtype::bfloat16(::fabs(static_cast<float>(a)));
208 209 210
}

template <>
211 212
HOSTDEVICE inline phi::dtype::bfloat16 mini(const phi::dtype::bfloat16& a,
                                            const phi::dtype::bfloat16& b) {
213 214 215 216
  return b < a ? b : a;
}

template <>
217 218
HOSTDEVICE inline phi::dtype::bfloat16 maxi(const phi::dtype::bfloat16& a,
                                            const phi::dtype::bfloat16& b) {
219
  return a < b ? b : a;
220 221
}

222 223 224 225
//////////// complex<float> methods /////////////

template <>
HOSTDEVICE inline bool(isnan)(const complex<float>& a) {
226
  return (phi::dtype::isnan)(a);
227 228 229 230
}

template <>
HOSTDEVICE inline bool(isinf)(const complex<float>& a) {
231
  return (phi::dtype::isinf)(a);
232 233 234 235
}

template <>
HOSTDEVICE inline bool(isfinite)(const complex<float>& a) {
236
  return (phi::dtype::isfinite)(a);
237 238 239 240 241 242 243 244 245 246 247 248
}

template <>
HOSTDEVICE inline complex<float> exp(const complex<float>& a) {
  float com = ::expf(a.real);
  float res_real = com * ::cosf(a.imag);
  float res_imag = com * ::sinf(a.imag);
  return complex<float>(res_real, res_imag);
}

template <>
HOSTDEVICE inline complex<float> log(const complex<float>& a) {
249
  return phi::dtype::log(a);
250 251 252 253
}

template <>
HOSTDEVICE inline complex<float> tanh(const complex<float>& a) {
254
  return phi::dtype::tanh(a);
255 256 257 258
}

template <>
HOSTDEVICE inline complex<float> sqrt(const complex<float>& a) {
259
  return phi::dtype::sqrt(a);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

template <>
HOSTDEVICE inline complex<float> ceil(const complex<float>& a) {
  return complex<float>(::ceilf(a.real), ::ceilf(a.imag));
}

template <>
HOSTDEVICE inline complex<float> floor(const complex<float>& a) {
  return complex<float>(::floorf(a.real), ::floor(a.imag));
}

template <>
HOSTDEVICE inline complex<float> round(const complex<float>& a) {
  return complex<float>(::roundf(a.real), ::roundf(a.imag));
}

template <>
HOSTDEVICE inline complex<float> pow(const complex<float>& a,
                                     const complex<float>& b) {
280
  return phi::dtype::pow(a, b);
281 282 283 284
}

template <>
HOSTDEVICE inline float abs(const complex<float>& a) {
285
  return phi::dtype::abs(a);
286 287 288 289 290 291
}

//////////// complex<double> methods /////////////

template <>
HOSTDEVICE inline bool(isnan)(const complex<double>& a) {
292
  return (phi::dtype::isnan)(a);
293 294 295 296
}

template <>
HOSTDEVICE inline bool(isinf)(const complex<double>& a) {
297
  return (phi::dtype::isinf)(a);
298 299 300 301
}

template <>
HOSTDEVICE inline bool(isfinite)(const complex<double>& a) {
302
  return (phi::dtype::isfinite)(a);
303 304 305 306 307 308 309 310 311 312 313 314
}

template <>
HOSTDEVICE inline complex<double> exp(const complex<double>& a) {
  double com = ::expf(a.real);
  double res_real = com * ::cosf(a.imag);
  double res_imag = com * ::sinf(a.imag);
  return complex<double>(res_real, res_imag);
}

template <>
HOSTDEVICE inline complex<double> log(const complex<double>& a) {
315
  return phi::dtype::log(a);
316 317 318 319
}

template <>
HOSTDEVICE inline complex<double> tanh(const complex<double>& a) {
320
  return phi::dtype::tanh(a);
321 322 323 324
}

template <>
HOSTDEVICE inline complex<double> sqrt(const complex<double>& a) {
325
  return phi::dtype::sqrt(a);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

template <>
HOSTDEVICE inline complex<double> ceil(const complex<double>& a) {
  return complex<double>(::ceilf(a.real), ::ceilf(a.imag));
}

template <>
HOSTDEVICE inline complex<double> floor(const complex<double>& a) {
  return complex<double>(::floorf(a.real), ::floor(a.imag));
}

template <>
HOSTDEVICE inline complex<double> round(const complex<double>& a) {
  return complex<double>(::roundf(a.real), ::roundf(a.imag));
}

template <>
HOSTDEVICE inline complex<double> pow(const complex<double>& a,
                                      const complex<double>& b) {
346
  return phi::dtype::pow(a, b);
347 348 349 350
}

template <>
HOSTDEVICE inline double abs(const complex<double>& a) {
351
  return phi::dtype::abs(a);
352 353
}

354 355 356 357
//////////// float16 methods /////////////

template <>
HOSTDEVICE inline bool(isnan)(const float16& a) {
358
  return (phi::dtype::isnan)(a);
359 360 361 362
}

template <>
HOSTDEVICE inline bool(isinf)(const float16& a) {
363
  return (phi::dtype::isinf)(a);
364 365 366 367
}

template <>
HOSTDEVICE inline bool(isfinite)(const float16& a) {
368
  return (phi::dtype::isfinite)(a);
369 370 371 372 373 374 375
}

template <>
HOSTDEVICE inline float16 exp(const float16& a) {
  return float16(::expf(static_cast<float>(a)));
}

R
ronnywang 已提交
376 377 378 379 380
template <>
HOSTDEVICE inline float16 expm1(const float16& a) {
  return float16(::expm1f(static_cast<float>(a)));
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
template <>
HOSTDEVICE inline float16 erf(const float16& a) {
  return float16(::erff(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 log(const float16& a) {
  return float16(::logf(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 tanh(const float16& a) {
  return float16(::tanhf(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 sqrt(const float16& a) {
  return float16(::sqrtf(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 ceil(const float16& a) {
  return float16(::ceilf(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 floor(const float16& a) {
  return float16(::floorf(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 round(const float16& a) {
  return float16(::roundf(static_cast<float>(a)));
}

template <>
HOSTDEVICE inline float16 pow(const float16& a, const float16& b) {
  return float16(::powf(static_cast<float>(a), static_cast<float>(b)));
}

template <>
HOSTDEVICE inline float16 abs(const float16& a) {
  return float16(::fabs(static_cast<float>(a)));
}

426 427 428 429 430 431 432 433 434 435
template <>
HOSTDEVICE inline float16 mini(const float16& a, const float16& b) {
  return b < a ? b : a;
}

template <>
HOSTDEVICE inline float16 maxi(const float16& a, const float16& b) {
  return a < b ? b : a;
}

436 437
}  // namespace numext
}  // namespace Eigen