-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_dispatch.hpp
More file actions
582 lines (543 loc) · 23.8 KB
/
Copy pathkernel_dispatch.hpp
File metadata and controls
582 lines (543 loc) · 23.8 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#pragma once
#include "kernel_crtp.hpp"
#include "kernel_activation.hpp"
#include "reference_kernel.hpp"
#include "tensor.hpp"
#include <cfloat>
#include <type_traits>
namespace detail
{
template<typename T>
concept NkAcceleratedKernel = !std::same_as<T, ReferenceKernel>;
constexpr void ApplyReferenceActivation(const Tensor& a,
Tensor& c,
NetkitKernelActivation activation,
float alpha)
{
switch (activation)
{
case NetkitKernelActivation::ReLU:
ReferenceKernel::ReLUImpl(a, c);
break;
case NetkitKernelActivation::Sigmoid:
ReferenceKernel::SigmoidImpl(a, c);
break;
case NetkitKernelActivation::Tanh:
ReferenceKernel::TanhImpl(a, c);
break;
case NetkitKernelActivation::LeakyReLU:
ReferenceKernel::LeakyReLUImpl(a, c, alpha);
break;
case NetkitKernelActivation::ReLU6:
ReferenceKernel::ReLU6Impl(a, c);
break;
default:
break;
}
}
template<NkAcceleratedKernel Fast, typename Reference = ReferenceKernel>
void TryVectorMul(const Tensor& a, const Tensor& b, Tensor& c)
{
if (!Fast::TryMul(a, b, c))
Reference::MulImpl(a, b, c);
}
template<typename Reference = ReferenceKernel>
void TryVectorMul(const Tensor& a, const Tensor& b, Tensor& c)
requires std::same_as<Reference, ReferenceKernel>
{
Reference::MulImpl(a, b, c);
}
template<NkAcceleratedKernel Fast, typename Reference = ReferenceKernel>
void TryVectorMulScalar(const Tensor& a, float scalar, Tensor& c)
{
if (!Fast::TryMulScalar(a, scalar, c))
Reference::MulScalarImpl(a, scalar, c);
}
template<typename Reference = ReferenceKernel>
void TryVectorMulScalar(const Tensor& a, float scalar, Tensor& c)
requires std::same_as<Reference, ReferenceKernel>
{
Reference::MulScalarImpl(a, scalar, c);
}
template<NkAcceleratedKernel Fast, typename Reference = ReferenceKernel>
void TryVectorMatMul(const Tensor& a, const Tensor& b, Tensor& c)
{
if (!Fast::TryMatMul(a, b, c))
Reference::MatMulImpl(a, b, c);
}
template<typename Reference = ReferenceKernel>
void TryVectorMatMul(const Tensor& a, const Tensor& b, Tensor& c)
requires std::same_as<Reference, ReferenceKernel>
{
Reference::MatMulImpl(a, b, c);
}
template<typename LayerFast, typename VectorFast>
void TryMatAdd2D(const Tensor& a, const Tensor& b, Tensor& c)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (!LayerFast::TryMatAdd(a, b, c))
ReferenceKernel::MatAddImpl(a, b, c);
}
else if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (!VectorFast::TryMatAdd(a, b, c))
ReferenceKernel::MatAddImpl(a, b, c);
}
else
ReferenceKernel::MatAddImpl(a, b, c);
}
template<typename LayerFast, typename VectorFast>
void TryMatAddND(const Tensor& a, const Tensor& b, Tensor& c)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (!LayerFast::TryMatAdd(a, b, c))
ReferenceKernel::MatAddNDImpl(a, b, c);
}
else if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (!VectorFast::TryMatAdd(a, b, c))
ReferenceKernel::MatAddNDImpl(a, b, c);
}
else
ReferenceKernel::MatAddNDImpl(a, b, c);
}
template<typename LayerFast, typename VectorFast>
void TryNnActivation(const Tensor& a,
Tensor& c,
NetkitKernelActivation activation,
float alpha)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (!LayerFast::TryActivationForward(a, c, activation, alpha))
ApplyReferenceActivation(a, c, activation, alpha);
}
else if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (activation == NetkitKernelActivation::ReLU)
{
if (!VectorFast::TryClip(a, c, 0.0f, FLT_MAX))
ReferenceKernel::ReLUImpl(a, c);
}
else if (activation == NetkitKernelActivation::ReLU6)
{
if (!VectorFast::TryClip(a, c, 0.0f, 6.0f))
ReferenceKernel::ReLU6Impl(a, c);
}
else
ApplyReferenceActivation(a, c, activation, alpha);
}
else
ApplyReferenceActivation(a, c, activation, alpha);
}
template<typename LayerFast>
bool TryLayerConv(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)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (LayerFast::TryConv2dForward(input,
weights,
bias,
kernel_size,
stride,
pad_h,
pad_w,
in_channels,
out_channels,
fuse_activation,
output))
return true;
}
return ReferenceKernel::Conv2dForwardImpl(input,
weights,
bias,
kernel_size,
stride,
pad_h,
pad_w,
pad_h,
pad_w,
in_channels,
out_channels,
fuse_activation,
output,
nullptr);
}
template<typename LayerFast>
bool TryLayerDepthwiseConv(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)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (pad_h_end == pad_h && pad_w_end == pad_w)
{
if (LayerFast::TryDepthwiseConv2dForward(input,
weights,
bias,
kernel_h,
kernel_w,
stride,
pad_h,
pad_w,
channels,
fuse_activation,
output))
return true;
}
}
return ReferenceKernel::DepthwiseConv2dForwardImpl(input,
weights,
bias,
kernel_h,
kernel_w,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
channels,
fuse_activation,
output);
}
template<typename LayerFast, typename VectorFast>
bool TryFullyConnected(const Tensor& input,
const Tensor& weights,
const Tensor& bias,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (LayerFast::TryFullyConnectedWithBias(input, weights, bias, fuse_activation, output))
return true;
}
else if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (VectorFast::TryFullyConnectedWithBias(input, weights, bias, output))
{
if (kernel_activation_is_fused(fuse_activation))
{
if (fuse_activation == NetkitKernelActivation::ReLU)
{
if (VectorFast::TryClip(output, output, 0.0f, FLT_MAX))
return true;
}
else if (fuse_activation == NetkitKernelActivation::ReLU6)
{
if (VectorFast::TryClip(output, output, 0.0f, 6.0f))
return true;
}
}
return false;
}
}
return ReferenceKernel::FullyConnectedWithBiasImpl(input, weights, bias, fuse_activation, output);
}
template<typename LayerFast, typename VectorFast>
void TryGelu(const Tensor& a, Tensor& c)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (LayerFast::TryGeluForward(a, c))
return;
}
if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (VectorFast::TryGeluForward(a, c))
return;
}
ReferenceKernel::GeluImpl(a, c);
}
template<typename VectorFast>
void TryGrn2d(const Tensor& input,
const float* gamma,
const float* beta,
int channels,
float eps,
float* channel_norm_scratch,
Tensor& output)
{
if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (VectorFast::TryGrn2dForward(
input, gamma, beta, channels, eps, channel_norm_scratch, output))
return;
}
ReferenceKernel::Grn2dForwardImpl(
input, gamma, beta, channels, eps, channel_norm_scratch, output);
}
/*
* ComposedKernel<VectorFast, LayerFast> — single CRTP implementation for all backend mixes.
* ReferenceKernel as a template argument means "use reference for that role".
*/
template<typename VectorFast, typename LayerFast>
struct ComposedKernel : KernelBase<ComposedKernel<VectorFast, LayerFast>>
{
static void MulImpl(const Tensor& a, const Tensor& b, Tensor& c)
{
TryVectorMul<VectorFast>(a, b, c);
}
static void MulScalarImpl(const Tensor& a, float scalar, Tensor& c)
{
TryVectorMulScalar<VectorFast>(a, scalar, c);
}
static void MatAddImpl(const Tensor& a, const Tensor& b, Tensor& c)
{
TryMatAdd2D<LayerFast, VectorFast>(a, b, c);
}
static void MatAddNDImpl(const Tensor& a, const Tensor& b, Tensor& c)
{
TryMatAddND<LayerFast, VectorFast>(a, b, c);
}
static void MatMulImpl(const Tensor& a, const Tensor& b, Tensor& c)
{
TryVectorMatMul<VectorFast>(a, b, c);
}
static void MulNDImpl(const Tensor& a, const Tensor& b, Tensor& c)
{
TryVectorMul<VectorFast>(a, b, c);
}
static void ReLUImpl(const Tensor& a, Tensor& c)
{
TryNnActivation<LayerFast, VectorFast>(a, c, NetkitKernelActivation::ReLU, 0.0f);
}
static void SigmoidImpl(const Tensor& a, Tensor& c)
{
TryNnActivation<LayerFast, VectorFast>(a, c, NetkitKernelActivation::Sigmoid, 0.0f);
}
static void TanhImpl(const Tensor& a, Tensor& c)
{
TryNnActivation<LayerFast, VectorFast>(a, c, NetkitKernelActivation::Tanh, 0.0f);
}
static void LeakyReLUImpl(const Tensor& a, Tensor& c, float alpha)
{
TryNnActivation<LayerFast, VectorFast>(a, c, NetkitKernelActivation::LeakyReLU, alpha);
}
static void ReLU6Impl(const Tensor& a, Tensor& c)
{
TryNnActivation<LayerFast, VectorFast>(a, c, NetkitKernelActivation::ReLU6, 0.0f);
}
static void SoftmaxImpl(const Tensor& a, Tensor& c)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (!LayerFast::TrySoftmaxForward(a, c))
ReferenceKernel::SoftmaxImpl(a, c);
}
else
ReferenceKernel::SoftmaxImpl(a, c);
}
static void GeluImpl(const Tensor& a, Tensor& c)
{
TryGelu<LayerFast, VectorFast>(a, c);
}
static void Grn2dForwardImpl(const Tensor& input,
const float* gamma,
const float* beta,
int channels,
float eps,
float* channel_norm_scratch,
Tensor& output)
{
TryGrn2d<VectorFast>(input, gamma, beta, channels, eps, channel_norm_scratch, output);
}
static bool Conv2dForwardImpl(const Tensor& input,
float* weights,
float* bias,
int kernel_size,
int stride,
int pad_h,
int pad_w,
int pad_h_end,
int pad_w_end,
int in_channels,
int out_channels,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (pad_h_end == pad_h && pad_w_end == pad_w)
{
if (LayerFast::TryConv2dForward(input,
weights,
bias,
kernel_size,
stride,
pad_h,
pad_w,
in_channels,
out_channels,
fuse_activation,
output))
return true;
}
}
return ReferenceKernel::Conv2dForwardImpl(input,
weights,
bias,
kernel_size,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
in_channels,
out_channels,
fuse_activation,
output,
nullptr);
}
static bool DepthwiseConv2dForwardImpl(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 TryLayerDepthwiseConv<LayerFast>(input,
weights,
bias,
kernel_h,
kernel_w,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
channels,
fuse_activation,
output);
}
static bool MaxPool2dForwardImpl(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)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (pad_h_end == pad_h && pad_w_end == pad_w && pool_h == pool_w)
{
if (LayerFast::TryMaxPool2dForward(input,
pool_h,
stride,
pad_h,
pad_w,
fuse_activation,
output))
return kernel_activation_is_fused(fuse_activation);
}
}
ReferenceKernel::MaxPool2dForwardImpl(input,
pool_h,
pool_w,
stride,
pad_h,
pad_w,
pad_h_end,
pad_w_end,
fuse_activation,
output);
return kernel_activation_is_fused(fuse_activation);
}
static void AvgPool2dForwardImpl(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)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (pad_h_end == pad_h && pad_w_end == pad_w && pool_h == pool_w)
{
if (LayerFast::TryAvgPool2dForward(
input, pool_h, stride, pad_h, pad_w, output))
return;
}
}
ReferenceKernel::AvgPool2dForwardImpl(
input, pool_h, pool_w, stride, pad_h, pad_w, pad_h_end, pad_w_end, output);
}
static void BatchNorm2dForwardImpl(const Tensor& input,
const float* scale,
const float* bias,
int channels,
Tensor& output)
{
if constexpr (NkAcceleratedKernel<LayerFast>)
{
if (!LayerFast::TryBatchNorm2dForward(input, scale, bias, channels, output))
ReferenceKernel::BatchNorm2dForwardImpl(input, scale, bias, channels, output);
}
else if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (!VectorFast::TryBatchNorm2dForward(input, scale, bias, channels, output))
ReferenceKernel::BatchNorm2dForwardImpl(input, scale, bias, channels, output);
}
else
ReferenceKernel::BatchNorm2dForwardImpl(input, scale, bias, channels, output);
}
static void LayerNorm2dForwardImpl(const Tensor& input,
const float* weight,
const float* bias,
int channels,
float eps,
Tensor& output)
{
if constexpr (NkAcceleratedKernel<VectorFast>)
{
if (VectorFast::TryLayerNorm2dForward(input, weight, bias, channels, eps, output))
return;
}
ReferenceKernel::LayerNorm2dForwardImpl(input, weight, bias, channels, eps, output);
}
static bool FullyConnectedWithBiasImpl(const Tensor& input,
const Tensor& weights,
const Tensor& bias,
NetkitKernelActivation fuse_activation,
Tensor& output)
{
return TryFullyConnected<LayerFast, VectorFast>(input, weights, bias, fuse_activation, output);
}
};
} // namespace detail