hl_tensor_ops.h 10.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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. */
H
hedaoyuan 已提交
14 15 16 17 18 19 20 21 22 23

#ifndef HL_TENSOR_OPS_H_
#define HL_TENSOR_OPS_H_

#include <cmath>
#include "hl_matrix_type.cuh"

namespace hppl {
namespace unary {

H
hedaoyuan 已提交
24 25
template <class T>
class add_scale {
H
hedaoyuan 已提交
26 27
private:
  const T p;
H
hedaoyuan 已提交
28

H
hedaoyuan 已提交
29 30 31 32 33
public:
  INLINE add_scale(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return a + p; }
};

H
hedaoyuan 已提交
34
template <class T>
H
hedaoyuan 已提交
35 36 37
class sub_scale {
private:
  const T p;
H
hedaoyuan 已提交
38

H
hedaoyuan 已提交
39 40 41 42 43
public:
  INLINE sub_scale(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return a - p; }
};

H
hedaoyuan 已提交
44
template <class T>
H
hedaoyuan 已提交
45 46 47
class mul_scale {
private:
  const T p;
H
hedaoyuan 已提交
48

H
hedaoyuan 已提交
49 50 51 52 53
public:
  INLINE mul_scale(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return a * p; }
};

H
hedaoyuan 已提交
54
template <class T>
H
hedaoyuan 已提交
55 56 57
class div_scale {
private:
  const T p;
H
hedaoyuan 已提交
58

H
hedaoyuan 已提交
59 60 61 62 63
public:
  INLINE div_scale(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return a / p; }
};

H
hedaoyuan 已提交
64
template <class T>
H
hedaoyuan 已提交
65 66 67 68 69
class neg {
public:
  INLINE T operator()(const T a) const { return -a; }
};

H
hedaoyuan 已提交
70
template <class T>
H
hedaoyuan 已提交
71 72 73 74 75
class exp_op {
public:
  INLINE T operator()(const T a) const { return std::exp(a); }
};

H
hedaoyuan 已提交
76
template <class T>
H
hedaoyuan 已提交
77 78 79 80 81
class log_op {
public:
  INLINE T operator()(const T a) const { return std::log(a); }
};

H
hedaoyuan 已提交
82
template <class T>
H
hedaoyuan 已提交
83 84 85 86 87
class sqrt_op {
public:
  INLINE T operator()(const T a) const { return std::sqrt(a); }
};

H
hedaoyuan 已提交
88
template <class T>
H
hedaoyuan 已提交
89 90 91 92 93
class square {
public:
  INLINE T operator()(const T a) const { return a * a; }
};

H
hedaoyuan 已提交
94
template <class T>
H
hedaoyuan 已提交
95 96 97 98 99
class reciprocal {
public:
  INLINE T operator()(const T a) const { return T(1) / a; }
};

H
hedaoyuan 已提交
100
template <class T>
H
hedaoyuan 已提交
101 102 103 104 105
class abs {
public:
  INLINE T operator()(const T a) const { return a > 0 ? a : -a; }
};

H
hedaoyuan 已提交
106
template <class T>
H
hedaoyuan 已提交
107 108 109 110 111
class sign {
public:
  INLINE T operator()(const T a) const { return (a > 0) - (a < 0); }
};

H
hedaoyuan 已提交
112
template <class T>
H
hedaoyuan 已提交
113 114 115
class min {
private:
  const T p;
H
hedaoyuan 已提交
116

H
hedaoyuan 已提交
117 118 119 120 121
public:
  INLINE min(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return a > p ? p : a; }
};

H
hedaoyuan 已提交
122
template <class T>
H
hedaoyuan 已提交
123 124 125
class max {
private:
  const T p;
H
hedaoyuan 已提交
126

H
hedaoyuan 已提交
127 128 129 130 131
public:
  INLINE max(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return a < p ? p : a; }
};

H
hedaoyuan 已提交
132
template <class T>
H
hedaoyuan 已提交
133 134 135
class pow_op {
private:
  const T p;
H
hedaoyuan 已提交
136

H
hedaoyuan 已提交
137 138 139 140 141
public:
  INLINE pow_op(const T s) : p(s) {}
  INLINE T operator()(const T a) const { return std::pow(a, p); }
};

H
hedaoyuan 已提交
142
template <class T>
H
hedaoyuan 已提交
143 144 145
class constant {
private:
  const T p;
H
hedaoyuan 已提交
146

H
hedaoyuan 已提交
147 148 149 150 151 152
public:
  INLINE constant(const T s) : p(s) {}
  INLINE T operator()(int i) const { return p; }
  INLINE T operator()(int i, int j) const { return p; }
};

H
hedaoyuan 已提交
153
template <class T>
H
hedaoyuan 已提交
154 155 156
class cmp_eq {
private:
  const T p;
H
hedaoyuan 已提交
157

H
hedaoyuan 已提交
158 159 160 161 162
public:
  INLINE cmp_eq(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a == p; }
};

H
hedaoyuan 已提交
163
template <class T>
H
hedaoyuan 已提交
164 165 166
class cmp_ne {
private:
  const T p;
H
hedaoyuan 已提交
167

H
hedaoyuan 已提交
168 169 170 171 172
public:
  INLINE cmp_ne(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a != p; }
};

H
hedaoyuan 已提交
173
template <class T>
H
hedaoyuan 已提交
174 175 176
class cmp_le {
private:
  const T p;
H
hedaoyuan 已提交
177

H
hedaoyuan 已提交
178 179 180 181 182
public:
  INLINE cmp_le(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a <= p; }
};

H
hedaoyuan 已提交
183
template <class T>
H
hedaoyuan 已提交
184 185 186
class cmp_lt {
private:
  const T p;
H
hedaoyuan 已提交
187

H
hedaoyuan 已提交
188 189 190 191 192
public:
  INLINE cmp_lt(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a < p; }
};

H
hedaoyuan 已提交
193
template <class T>
H
hedaoyuan 已提交
194 195 196
class cmp_ge {
private:
  const T p;
H
hedaoyuan 已提交
197

H
hedaoyuan 已提交
198 199 200 201 202
public:
  INLINE cmp_ge(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a >= p; }
};

H
hedaoyuan 已提交
203
template <class T>
H
hedaoyuan 已提交
204 205 206
class cmp_gt {
private:
  const T p;
H
hedaoyuan 已提交
207

H
hedaoyuan 已提交
208 209 210 211 212
public:
  INLINE cmp_gt(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a > p; }
};

H
hedaoyuan 已提交
213
template <class T>
H
hedaoyuan 已提交
214 215 216
class and_op {
private:
  const T p;
H
hedaoyuan 已提交
217

H
hedaoyuan 已提交
218 219 220 221 222
public:
  INLINE and_op(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a && p; }
};

H
hedaoyuan 已提交
223
template <class T>
H
hedaoyuan 已提交
224 225 226
class or_op {
private:
  const T p;
H
hedaoyuan 已提交
227

H
hedaoyuan 已提交
228 229 230 231 232 233 234 235
public:
  INLINE or_op(const T s) : p(s) {}
  INLINE bool operator()(const T a) const { return a || p; }
};

}  // namespace unary

namespace binary {
H
hedaoyuan 已提交
236
template <class T>
H
hedaoyuan 已提交
237 238 239 240 241
class add {
public:
  INLINE T operator()(const T a, const T b) const { return a + b; }
};

H
hedaoyuan 已提交
242
template <class T>
H
hedaoyuan 已提交
243 244 245 246
class add_scale {
private:
  const T p1;
  const T p2;
H
hedaoyuan 已提交
247

H
hedaoyuan 已提交
248 249
public:
  INLINE add_scale(const T s1, const T s2) : p1(s1), p2(s2) {}
H
hedaoyuan 已提交
250
  INLINE T operator()(const T a, const T b) const { return p1 * a + p2 * b; }
H
hedaoyuan 已提交
251 252
};

H
hedaoyuan 已提交
253
template <class T>
H
hedaoyuan 已提交
254 255 256 257 258
class sub {
public:
  INLINE T operator()(const T a, const T b) const { return a - b; }
};

H
hedaoyuan 已提交
259
template <class T>
H
hedaoyuan 已提交
260 261 262 263 264
class mul {
public:
  INLINE T operator()(const T a, const T b) const { return a * b; }
};

H
hedaoyuan 已提交
265
template <class T>
H
hedaoyuan 已提交
266 267
class div {
public:
H
hedaoyuan 已提交
268
  INLINE T operator()(const T a, const T b) const { return a / b; }
H
hedaoyuan 已提交
269 270
};

H
hedaoyuan 已提交
271
template <class T>
H
hedaoyuan 已提交
272 273 274 275 276
class cmp_eq {
public:
  INLINE bool operator()(const T a, const T b) const { return a == b; }
};

H
hedaoyuan 已提交
277
template <class T>
H
hedaoyuan 已提交
278 279 280 281 282
class cmp_ne {
public:
  INLINE bool operator()(const T a, const T b) const { return a != b; }
};

H
hedaoyuan 已提交
283
template <class T>
H
hedaoyuan 已提交
284 285 286 287 288
class cmp_le {
public:
  INLINE bool operator()(const T a, const T b) const { return a <= b; }
};

H
hedaoyuan 已提交
289
template <class T>
H
hedaoyuan 已提交
290 291 292 293 294
class cmp_lt {
public:
  INLINE bool operator()(const T a, const T b) const { return a < b; }
};

H
hedaoyuan 已提交
295
template <class T>
H
hedaoyuan 已提交
296 297 298 299 300
class cmp_ge {
public:
  INLINE bool operator()(const T a, const T b) const { return a >= b; }
};

H
hedaoyuan 已提交
301
template <class T>
H
hedaoyuan 已提交
302 303 304 305 306
class cmp_gt {
public:
  INLINE bool operator()(const T a, const T b) const { return a > b; }
};

H
hedaoyuan 已提交
307
template <class T>
H
hedaoyuan 已提交
308 309 310 311 312
class and_op {
public:
  INLINE bool operator()(const T a, const T b) const { return a && b; }
};

H
hedaoyuan 已提交
313
template <class T>
H
hedaoyuan 已提交
314 315 316 317 318
class or_op {
public:
  INLINE bool operator()(const T a, const T b) const { return a || b; }
};

H
hedaoyuan 已提交
319
template <class T>
H
hedaoyuan 已提交
320 321 322 323 324
class min {
public:
  INLINE T operator()(const T a, const T b) const { return a > b ? b : a; }
};

H
hedaoyuan 已提交
325
template <class T>
H
hedaoyuan 已提交
326 327 328 329 330
class max {
public:
  INLINE T operator()(const T a, const T b) const { return a < b ? b : a; }
};

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
#ifdef PADDLE_USE_SSE3
#ifndef PADDLE_TYPE_DOUBLE
template <>
class add<__m128> {
public:
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_add_ps(a, b);
  }
};

template <>
class add_scale<__m128> {
private:
  const __m128 p1;
  const __m128 p2;

public:
  INLINE add_scale(const __m128 s1, const __m128 s2) : p1(s1), p2(s2) {}
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_add_ps(_mm_mul_ps(p1, a), _mm_mul_ps(p2, b));
  }
};

template <>
class sub<__m128> {
public:
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_sub_ps(a, b);
  }
};

template <>
class mul<__m128> {
public:
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_mul_ps(a, b);
  }
};

template <>
class div<__m128> {
public:
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_div_ps(a, b);
  }
};

template <>
class min<__m128> {
public:
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_min_ps(a, b);
  }
};

template <>
class max<__m128> {
public:
  INLINE __m128 operator()(const __m128 a, const __m128 b) const {
    return _mm_max_ps(a, b);
  }
};
#else
template <>
class add<__m128d> {
public:
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_add_pd(a, b);
  }
};

template <>
class add_scale<__m128d> {
private:
  const __m128d p1;
  const __m128d p2;

public:
  INLINE add_scale(const __m128d s1, const __m128d s2) : p1(s1), p2(s2) {}
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_add_pd(_mm_mul_pd(p1, a), _mm_mul_pd(p2, b));
  }
};

template <>
class sub<__m128d> {
public:
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_sub_pd(a, b);
  }
};

template <>
class mul<__m128d> {
public:
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_mul_pd(a, b);
  }
};

template <>
class div<__m128d> {
public:
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_div_pd(a, b);
  }
};

template <>
class min<__m128d> {
public:
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_min_pd(a, b);
  }
};

template <>
class max<__m128d> {
public:
  INLINE __m128d operator()(const __m128d a, const __m128d b) const {
    return _mm_max_pd(a, b);
  }
};
#endif  // PADDLE_TYPE_DOUBLE
#endif  // PADDLE_USE_SSE3

#ifdef PADDLE_USE_NEON
#ifndef PADDLE_TYPE_DOUBLE
template <>
class add<float32x4_t> {
public:
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    return vmulq_f32(a, b);
  }
};

template <>
class add_scale<float32x4_t> {
private:
  const float32x4_t p1;
  const float32x4_t p2;

public:
  INLINE add_scale(const float32x4_t s1, const float32x4_t s2)
      : p1(s1), p2(s2) {}
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    return vaddq_f32(vmulq_f32(p1, a), vmulq_f32(p2, b));
  }
};

template <>
class sub<float32x4_t> {
public:
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    return vsubq_f32(a, b);
  }
};

template <>
class mul<float32x4_t> {
public:
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    return vmulq_f32(a, b);
  }
};

template <>
class div<float32x4_t> {
public:
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    float32x4_t tmp = vrecpeq_f32(b);
    return vmulq_f32(a, tmp);
  }
};

template <>
class min<float32x4_t> {
public:
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    return vminq_f32(a, b);
  }
};

template <>
class max<float32x4_t> {
public:
  INLINE float32x4_t operator()(const float32x4_t a,
                                const float32x4_t b) const {
    return vmaxq_f32(a, b);
  }
}
#else
#error To be implemented
#endif  // PADDLE_TYPE_DOUBLE
#endif  // PADDLE_USE_NEON

H
hedaoyuan 已提交
533 534 535 536
}  // namespace binary
}  // namespace hppl

#endif  // HL_TENSOR_OPS_H_