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
#ifndef __xpu__

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

#include "unsupported/Eigen/CXX11/Tensor"

namespace Eigen {

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

template <typename T>
struct NumTraits;

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

45 46
  HOSTDEVICE static inline phi::dtype::bfloat16 epsilon() {
    return phi::dtype::raw_uint16_to_bfloat16(0x3400);
47
  }
48 49
  HOSTDEVICE static inline phi::dtype::bfloat16 dummy_precision() {
    return phi::dtype::bfloat16(1e-5f);
50
  }
51 52
  HOSTDEVICE static inline phi::dtype::bfloat16 highest() {
    return phi::dtype::raw_uint16_to_bfloat16(0x7f7f);
53
  }
54 55
  HOSTDEVICE static inline phi::dtype::bfloat16 lowest() {
    return phi::dtype::raw_uint16_to_bfloat16(0xff7f);
56
  }
57 58
  HOSTDEVICE static inline phi::dtype::bfloat16 infinity() {
    return phi::dtype::raw_uint16_to_bfloat16(0x7f80);
59
  }
60 61
  HOSTDEVICE static inline phi::dtype::bfloat16 quiet_NaN() {
    return phi::dtype::raw_uint16_to_bfloat16(0xffc1);
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
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(); }
};

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

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

136 137 138 139 140
namespace numext {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

224 225 226 227
//////////// complex<float> methods /////////////

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

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

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

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) {
251
  return phi::dtype::log(a);
252 253 254 255
}

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

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

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) {
282
  return phi::dtype::pow(a, b);
283 284 285 286
}

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

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

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

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

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

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) {
317
  return phi::dtype::log(a);
318 319 320 321
}

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

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

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) {
348
  return phi::dtype::pow(a, b);
349 350 351 352
}

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

356 357 358 359
//////////// float16 methods /////////////

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

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

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

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

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

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 426 427
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)));
}

428 429 430 431 432 433 434 435 436 437
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;
}

438 439
}  // namespace numext
}  // namespace Eigen
440 441

#endif  // __xpu__