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

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 331 332 333 334
class max {
public:
  INLINE T operator()(const T a, const T b) const { return a < b ? b : a; }
};

}  // namespace binary
}  // namespace hppl

#endif  // HL_TENSOR_OPS_H_