-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_crtp.hpp
More file actions
274 lines (248 loc) · 10.3 KB
/
Copy pathkernel_crtp.hpp
File metadata and controls
274 lines (248 loc) · 10.3 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#pragma once
#include "kernel_activation.hpp"
#include "tensor.hpp"
/*
* CRTP kernel facade — static dispatch to Derived::*Impl without virtual calls.
* Active backend is selected at compile time in active_kernel.hpp.
*/
template<typename Derived>
struct KernelBase
{
static void Mul(const Tensor& a, const Tensor& b, Tensor& c)
{
Derived::MulImpl(a, b, c);
}
static void MulScalar(const Tensor& a, float scalar, Tensor& c)
{
Derived::MulScalarImpl(a, scalar, c);
}
static void MatAdd(const Tensor& a, const Tensor& b, Tensor& c)
{
Derived::MatAddImpl(a, b, c);
}
static void MatAddND(const Tensor& a, const Tensor& b, Tensor& c)
{
Derived::MatAddNDImpl(a, b, c);
}
static void MatMul(const Tensor& a, const Tensor& b, Tensor& c)
{
Derived::MatMulImpl(a, b, c);
}
static void MulND(const Tensor& a, const Tensor& b, Tensor& c)
{
Derived::MulNDImpl(a, b, c);
}
static void ReLU(const Tensor& a, Tensor& c)
{
Derived::ReLUImpl(a, c);
}
static void Sigmoid(const Tensor& a, Tensor& c)
{
Derived::SigmoidImpl(a, c);
}
static void Tanh(const Tensor& a, Tensor& c)
{
Derived::TanhImpl(a, c);
}
static void LeakyReLU(const Tensor& a, Tensor& c, float alpha)
{
Derived::LeakyReLUImpl(a, c, alpha);
}
static void ReLU6(const Tensor& a, Tensor& c)
{
Derived::ReLU6Impl(a, c);
}
static void Softmax(const Tensor& a, Tensor& c)
{
Derived::SoftmaxImpl(a, c);
}
static void Gelu(const Tensor& a, Tensor& c)
{
Derived::GeluImpl(a, c);
}
static void Grn2dForward(const Tensor& input,
const float* gamma,
const float* beta,
int channels,
float eps,
float* channel_norm_scratch,
Tensor& output)
{
Derived::Grn2dForwardImpl(input, gamma, beta, channels, eps, channel_norm_scratch, output);
}
static bool Conv2dForward(const Tensor& input,
float* weights,
float* bias,
int kernel_size,
int stride,
int pad_h,
int pad_w,
int in_channels,
int out_channels,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
return Derived::Conv2dForwardImpl(input,
weights,
bias,
kernel_size,
stride,
pad_h,
pad_w,
pad_h,
pad_w,
in_channels,
out_channels,
fuse_activation,
output);
}
static bool DepthwiseConv2dForward(const Tensor& input,
float* weights,
float* bias,
int kernel_h,
int kernel_w,
int stride,
int pad_h,
int pad_w,
int pad_h_end,
int pad_w_end,
int channels,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
return Derived::DepthwiseConv2dForwardImpl(input,
weights,
bias,
kernel_h,
kernel_w,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
channels,
fuse_activation,
output);
}
static bool MaxPool2dForward(const Tensor& input,
int pool_size,
int stride,
int pad_h,
int pad_w,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
return Derived::MaxPool2dForwardImpl(input,
pool_size,
pool_size,
stride,
pad_h,
pad_w,
pad_h,
pad_w,
fuse_activation,
output);
}
// Backward-compatible overload (no fused activation).
static bool MaxPool2dForward(const Tensor& input,
int pool_size,
int stride,
int pad_h,
int pad_w,
Tensor& output)
{
return MaxPool2dForward(
input, pool_size, stride, pad_h, pad_w, NetkitKernelActivation::None, output);
}
static bool MaxPool2dForwardPadded(const Tensor& input,
int pool_h,
int pool_w,
int stride,
int pad_h,
int pad_w,
int pad_h_end,
int pad_w_end,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
return Derived::MaxPool2dForwardImpl(input,
pool_h,
pool_w,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
fuse_activation,
output);
}
static bool MaxPool2dForwardPadded(const Tensor& input,
int pool_h,
int pool_w,
int stride,
int pad_h,
int pad_w,
int pad_h_end,
int pad_w_end,
Tensor& output)
{
return MaxPool2dForwardPadded(input,
pool_h,
pool_w,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
NetkitKernelActivation::None,
output);
}
static void AvgPool2dForward(const Tensor& input,
int pool_size,
int stride,
int pad_h,
int pad_w,
Tensor& output)
{
Derived::AvgPool2dForwardImpl(
input, pool_size, pool_size, stride, pad_h, pad_w, pad_h, pad_w, output);
}
static void AvgPool2dForwardPadded(const Tensor& input,
int pool_h,
int pool_w,
int stride,
int pad_h,
int pad_w,
int pad_h_end,
int pad_w_end,
Tensor& output)
{
Derived::AvgPool2dForwardImpl(
input, pool_h, pool_w, stride, pad_h, pad_w, pad_h_end, pad_w_end, output);
}
static void BatchNorm2dForward(const Tensor& input,
const float* scale,
const float* bias,
int channels,
Tensor& output)
{
Derived::BatchNorm2dForwardImpl(input, scale, bias, channels, output);
}
static void LayerNorm2dForward(const Tensor& input,
const float* weight,
const float* bias,
int channels,
float eps,
Tensor& output)
{
Derived::LayerNorm2dForwardImpl(input, weight, bias, channels, eps, output);
}
static bool FullyConnectedWithBias(const Tensor& input,
const Tensor& weights,
const Tensor& bias,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
return Derived::FullyConnectedWithBiasImpl(input, weights, bias, fuse_activation, output);
}
};