-
Notifications
You must be signed in to change notification settings - Fork 3
/
THCTensorMathPairwise.cu
238 lines (191 loc) · 5.2 KB
/
THCTensorMathPairwise.cu
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
#include <THC/THCTensorMath.h>
#include <THC/THCGeneral.h>
#include <TH/THHalf.h>
#include <THC/THCTensorCopy.h>
#include <THC/THCApply.cuh>
#include <THC/THCNumerics.cuh>
#include <THC/THCTensorMathCompareT.cuh>
#include <THC/THCTensor.hpp>
template <typename T>
struct TensorAddConstantOp {
TensorAddConstantOp(T v) : val(v) {}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = *in + val;
}
__device__ __forceinline__ void operator()(T* v) {
*v += val;
}
const T val;
};
template <typename T>
struct TensorSubConstantOp {
TensorSubConstantOp(T v) : val(v) {}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = *in - val;
}
__device__ __forceinline__ void operator()(T* v) {
*v -= val;
}
const T val;
};
template <typename T>
struct TensorMulConstantOp {
TensorMulConstantOp(T v) : val(v) {}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = *in * val;
}
__device__ __forceinline__ void operator()(T* v) {
*v *= val;
}
const T val;
};
template <typename T>
struct TensorDivConstantOp {
TensorDivConstantOp(T v) : val(v) {}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = *in / val;
}
__device__ __forceinline__ void operator()(T* v) {
*v /= val;
}
const T val;
};
template <>
struct TensorDivConstantOp<float> {
TensorDivConstantOp(float v) : val(1.f / v) {}
__device__ __forceinline__ void operator()(float* out, float* in) {
*out = *in * val;
}
__device__ __forceinline__ void operator()(float* v) {
*v *= val;
}
const float val;
};
template <>
struct TensorDivConstantOp<double> {
TensorDivConstantOp(double v) : val(1. / v) {}
__device__ __forceinline__ void operator()(double* out, double* in) {
*out = *in * val;
}
__device__ __forceinline__ void operator()(double* v) {
*v *= val;
}
const double val;
};
template<typename T>
static __device__ __forceinline__
typename std::enable_if<std::is_signed<T>::value, bool>::type
modulo_wrap(T a, T b) {
return (a != 0) && (a < 0) != (b < 0);
}
template<typename T>
static __device__ __forceinline__
typename std::enable_if<std::is_unsigned<T>::value, bool>::type
modulo_wrap(T a, T b) {
return false;
}
template <typename T>
struct TensorRemainderOp {
TensorRemainderOp(T v) : val(v) {}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = *in % val;
if (modulo_wrap<T>(*out, val)) {
*out += val;
}
}
__device__ __forceinline__ void operator()(T* v) {
*v = *v % val;
if (modulo_wrap<T>(*v, val)) {
*v += val;
}
}
const T val;
};
template <>
struct TensorRemainderOp<float> {
TensorRemainderOp(float v) : val(v) {}
__device__ __forceinline__ void operator()(float* out, float* in) {
*out = *in - val * floorf(*in / val);
}
__device__ __forceinline__ void operator()(float* v) {
*v = *v - val * floorf(*v / val);
}
const float val;
};
template <>
struct TensorRemainderOp<double> {
TensorRemainderOp(double v) : val(v) {}
__device__ __forceinline__ void operator()(double* out, double* in) {
*out = *in - val * floor(*in / val);
}
__device__ __forceinline__ void operator()(double* v) {
*v = *v - val * floor(*v / val);
}
const double val;
};
template <>
struct TensorRemainderOp<at::Half> {
TensorRemainderOp(at::Half v): val(v) {}
__device__ __forceinline__ void operator()(at::Half* out, at::Half* in) {
*out = *in - val * floorf(*in / val);
}
__device__ __forceinline__ void operator()(at::Half* v) {
*v = *v - val * floorf(*v / val);
}
const at::Half val;
};
template <typename T>
struct TensorFmodOp {
TensorFmodOp(T v) : val((float)v) {}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = (T) fmodf((float) *in, val);
}
__device__ __forceinline__ void operator()(T* v) {
*v = (T) fmodf((float) *v, val);
}
const float val;
};
template <>
struct TensorFmodOp<double> {
TensorFmodOp(double v) : val(v) {}
__device__ __forceinline__ void operator()(double* out, double* in) {
*out = fmod(*in, val);
}
__device__ __forceinline__ void operator()(double* v) {
*v = fmod(*v, val);
}
const double val;
};
template <typename T, int Upper>
struct TensorTriOp {
TensorTriOp(T *start_, int64_t stride0_, int64_t stride1_, int64_t k_)
: start(start_), stride0(stride0_), stride1(stride1_), k(k_) {}
__device__ __forceinline__ int mask(T *out) {
ptrdiff_t n = out - start;
int64_t row, col;
if (stride0 > stride1)
{
row = (int64_t) (n / stride0);
col = (int64_t) ((n % stride0) / stride1);
}
else
{
row = (int64_t) ((n % stride1) / stride0);
col = (int64_t) (n / stride1);
}
return Upper ? (col - row >= k) : (col - row <= k);
}
__device__ __forceinline__ void operator()(T* out, T* in) {
*out = mask(out) ? *in : ScalarConvert<int, T>::to(0);
}
__device__ __forceinline__ void operator()(T* v) {
if (!mask(v))
*v = ScalarConvert<int, T>::to(0);
}
const T *start;
const int64_t stride0, stride1, k;
};
#include <THC/generic/THCTensorMathPairwise.cu>
#include <THC/THCGenerateAllTypes.h>
#include <THC/generic/THCTensorMathPairwise.cu>
#include <THC/THCGenerateBoolType.h>