forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudnnBatchNormEx.go
771 lines (724 loc) · 21.7 KB
/
cudnnBatchNormEx.go
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
package gocudnn
/*
#include <cudnn.h>
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
"github.com/dereklstinson/cutil"
)
//BatchNormDEx is a gocudnn original. This is to make the batchnorm operation similar to the majority cudnn.
type BatchNormDEx struct {
mode C.cudnnBatchNormMode_t
op C.cudnnBatchNormOps_t
set bool
gogc bool
}
//CreateBatchNormDescriptorEx creates a new BatchNormDEx
func CreateBatchNormDescriptorEx() *BatchNormDEx {
return new(BatchNormDEx)
}
//Set sets the BatchNormMode and BatchNormOps held in the descriptor
func (b *BatchNormDEx) Set(mode BatchNormMode, op BatchNormOps) error {
b.mode = mode.c()
b.op = op.c()
b.set = true
b.gogc = true
return nil
}
//Get gets the BatchNormMode and BatchNormOps held in the descriptor
func (b *BatchNormDEx) Get() (mode BatchNormMode, op BatchNormOps, err error) {
if !b.set {
return BatchNormMode(b.mode), BatchNormOps(b.op), errors.New("BatchNormD not set")
}
return BatchNormMode(b.mode), BatchNormOps(b.op), nil
}
func (b *BatchNormDEx) String() string {
return fmt.Sprintf(
"BatchNormDEx Values\n"+
"-----------------\n"+
"BatchNormMode: %s\n"+
"BatchNormOps: %s\n", BatchNormMode(b.mode).String(), BatchNormOps(b.op).String())
}
//DeriveBNTensorDescriptor derives a tensor used for the batch norm operation
func (b *BatchNormDEx) DeriveBNTensorDescriptor(xDesc *TensorD) (bndesc *TensorD, err error) {
if !b.set {
return nil, errors.New("BatchNormD not set")
}
return cudnnDeriveBNTensorDescriptor(xDesc, BatchNormMode(b.mode), b.gogc)
}
//GetForwardTrainingWorkspaceSize gets the forward training ex workspacesize
func (b *BatchNormDEx) GetForwardTrainingWorkspaceSize(h *Handle,
mode BatchNormMode,
op BatchNormOps,
xD, zD, yD, bnScaleBiasMeanVarDesc *TensorD,
actD *ActivationD) (wspaceSIB uint, err error) {
if !b.set {
return 0, errors.New("BatchNormD not set")
}
var sib C.size_t
if h.w != nil {
err = h.w.Work(func() error {
return Status(C.cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize(
h.x,
mode.c(),
op.c(),
xD.descriptor,
zD.descriptor,
yD.descriptor,
bnScaleBiasMeanVarDesc.descriptor,
actD.descriptor,
&sib)).error("(b *BatchNormDEx) GetForwardTrainingWorkspaceSize")
})
} else {
err = Status(C.cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize(
h.x,
mode.c(),
op.c(),
xD.descriptor,
zD.descriptor,
yD.descriptor,
bnScaleBiasMeanVarDesc.descriptor,
actD.descriptor,
&sib)).error("(b *BatchNormDEx) GetForwardTrainingWorkspaceSize")
}
wspaceSIB = uint(sib)
return wspaceSIB, err
}
//GeBackwardWorkspaceSize gets the workspace size in bytes for the backward operation
func (b *BatchNormDEx) GeBackwardWorkspaceSize(
h *Handle,
xD, yD, dyD, dzD, dxD, dbnScaleBiasMeanVarDesc *TensorD,
actD *ActivationD,
) (wspaceSIB uint, err error) {
if !b.set {
return 0, errors.New("BatchNormD not set")
}
var sib C.size_t
if h.w != nil {
err = h.w.Work(func() error {
return Status(C.cudnnGetBatchNormalizationBackwardExWorkspaceSize(h.x, b.mode, b.op, xD.descriptor, yD.descriptor, dyD.descriptor, dzD.descriptor, dxD.descriptor, dbnScaleBiasMeanVarDesc.descriptor, actD.descriptor, &sib)).error("(b *BatchNormDEx) GeBackwardWorkspaceSize")
})
} else {
err = Status(C.cudnnGetBatchNormalizationBackwardExWorkspaceSize(h.x, b.mode, b.op, xD.descriptor, yD.descriptor, dyD.descriptor, dzD.descriptor, dxD.descriptor, dbnScaleBiasMeanVarDesc.descriptor, actD.descriptor, &sib)).error("(b *BatchNormDEx) GeBackwardWorkspaceSize")
}
wspaceSIB = uint(sib)
return wspaceSIB, err
}
//GetTrainingReserveSpaceSize gets the reserve space size for ex operation
func (b *BatchNormDEx) GetTrainingReserveSpaceSize(h *Handle,
actD *ActivationD,
xD *TensorD,
) (rspaceSIB uint, err error) {
if !b.set {
return 0, errors.New("BatchNormD not set")
}
var sib C.size_t
if h.w != nil {
err = h.w.Work(func() error {
return Status(C.cudnnGetBatchNormalizationTrainingExReserveSpaceSize(h.x, b.mode, b.op, actD.descriptor, xD.descriptor, &sib)).error("(b *BatchNormDEx) GetTrainingReserveSpaceSize")
})
} else {
err = Status(C.cudnnGetBatchNormalizationTrainingExReserveSpaceSize(h.x, b.mode, b.op, actD.descriptor, xD.descriptor, &sib)).error("(b *BatchNormDEx) GetTrainingReserveSpaceSize")
}
rspaceSIB = uint(sib)
return rspaceSIB, err
}
//ForwardTraining does the forward training ex algorithm.
func (b *BatchNormDEx) ForwardTraining(
h *Handle,
alpha, beta float64, //alpha -result blend factor, beta - dest blend factor
xD *TensorD,
x cutil.Mem,
zD *TensorD,
z cutil.Mem,
yD *TensorD,
y cutil.Mem, //output
bnScaleBiasMeanVarDesc *TensorD,
scale cutil.Mem,
bias cutil.Mem,
expoAverageFactor float64,
resultRunningMean cutil.Mem,
resultRunningVariance cutil.Mem,
epsilon float64,
resultSaveMean cutil.Mem, //optional can be null this and reslutSaveInVariance either both have to be null or not // output
reslutSaveInVariance cutil.Mem, //optional can be null this and resultSaveMean either both have to be null or not. //output
actD *ActivationD,
wspace cutil.Mem,
wspacesib uint,
rspace cutil.Mem,
rspacesib uint,
) error {
if !b.set {
return errors.New("BatchNormD not set")
}
a := cscalarbydatatype(yD.dtype, alpha)
be := cscalarbydatatype(yD.dtype, beta)
if h.w != nil {
return h.w.Work(func() error {
if resultSaveMean == nil || reslutSaveInVariance == nil {
return Status(C.cudnnBatchNormalizationForwardTrainingEx(
h.x,
b.mode,
b.op,
a.CPtr(), be.CPtr(),
xD.descriptor,
x.Ptr(),
zD.descriptor,
z.Ptr(),
yD.descriptor,
y.Ptr(),
bnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
C.double(expoAverageFactor),
resultRunningMean.Ptr(),
resultRunningVariance.Ptr(),
C.double(epsilon),
nil,
nil,
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) ForwardTraining")
}
return Status(C.cudnnBatchNormalizationForwardTrainingEx(
h.x,
b.mode,
b.op,
a.CPtr(), be.CPtr(),
xD.descriptor,
x.Ptr(),
zD.descriptor,
z.Ptr(),
yD.descriptor,
y.Ptr(),
bnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
C.double(expoAverageFactor),
resultRunningMean.Ptr(),
resultRunningVariance.Ptr(),
C.double(epsilon),
resultSaveMean.Ptr(),
reslutSaveInVariance.Ptr(),
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) ForwardTraining")
})
}
if resultSaveMean == nil || reslutSaveInVariance == nil {
return Status(C.cudnnBatchNormalizationForwardTrainingEx(
h.x,
b.mode,
b.op,
a.CPtr(), be.CPtr(),
xD.descriptor,
x.Ptr(),
zD.descriptor,
z.Ptr(),
yD.descriptor,
y.Ptr(),
bnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
C.double(expoAverageFactor),
resultRunningMean.Ptr(),
resultRunningVariance.Ptr(),
C.double(epsilon),
nil,
nil,
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) ForwardTraining")
}
return Status(C.cudnnBatchNormalizationForwardTrainingEx(
h.x,
b.mode,
b.op,
a.CPtr(), be.CPtr(),
xD.descriptor,
x.Ptr(),
zD.descriptor,
z.Ptr(),
yD.descriptor,
y.Ptr(),
bnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
C.double(expoAverageFactor),
resultRunningMean.Ptr(),
resultRunningVariance.Ptr(),
C.double(epsilon),
resultSaveMean.Ptr(),
reslutSaveInVariance.Ptr(),
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) ForwardTraining")
}
//ForwardTrainingUS is loke ForwardTraining but using unsafe.Pointers instead of cutil.Mems
func (b *BatchNormDEx) ForwardTrainingUS(
h *Handle,
alpha, beta float64, //alpha -result blend factor, beta - dest blend factor
xD *TensorD,
x unsafe.Pointer,
zD *TensorD,
z unsafe.Pointer,
yD *TensorD,
y unsafe.Pointer, //output
bnScaleBiasMeanVarDesc *TensorD,
scale unsafe.Pointer,
bias unsafe.Pointer,
expoAverageFactor float64,
resultRunningMean unsafe.Pointer,
resultRunningVariance unsafe.Pointer,
epsilon float64,
resultSaveMean unsafe.Pointer, //optional can be null this and reslutSaveInVariance either both have to be null or not // output
reslutSaveInVariance unsafe.Pointer, //optional can be null this and resultSaveMean either both have to be null or not. //output
actD *ActivationD,
wspace unsafe.Pointer,
wspacesib uint,
rspace unsafe.Pointer,
rspacesib uint,
) error {
if !b.set {
return errors.New("BatchNormD not set")
}
a := cscalarbydatatype(yD.dtype, alpha)
be := cscalarbydatatype(yD.dtype, beta)
if h.w != nil {
return h.w.Work(func() error {
return Status(C.cudnnBatchNormalizationForwardTrainingEx(
h.x,
b.mode,
b.op,
a.CPtr(), be.CPtr(),
xD.descriptor, x,
zD.descriptor, z,
yD.descriptor, y,
bnScaleBiasMeanVarDesc.descriptor, scale, bias,
C.double(expoAverageFactor),
resultRunningMean,
resultRunningVariance,
C.double(epsilon),
resultSaveMean,
reslutSaveInVariance,
actD.descriptor,
wspace,
C.size_t(wspacesib),
rspace,
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) ForwardTrainingUS")
})
}
return Status(C.cudnnBatchNormalizationForwardTrainingEx(
h.x,
b.mode,
b.op,
a.CPtr(), be.CPtr(),
xD.descriptor, x,
zD.descriptor, z,
yD.descriptor, y,
bnScaleBiasMeanVarDesc.descriptor, scale, bias,
C.double(expoAverageFactor),
resultRunningMean,
resultRunningVariance,
C.double(epsilon),
resultSaveMean,
reslutSaveInVariance,
actD.descriptor,
wspace,
C.size_t(wspacesib),
rspace,
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) ForwardTrainingUS")
}
//Backward does the backward ex algorithm.
func (b *BatchNormDEx) Backward(
h *Handle,
alphadata, betadata, alphaparam, betaparam float64, //alpha -result blend factor, beta - dest blend factor
xD *TensorD,
x cutil.Mem,
yD *TensorD,
y cutil.Mem,
dyD *TensorD,
dy cutil.Mem,
dzD *TensorD,
dz cutil.Mem,
dxD *TensorD,
dx cutil.Mem,
dbnScaleBiasMeanVarDesc *TensorD,
scale cutil.Mem,
bias cutil.Mem,
dscale cutil.Mem, //output - for training scale and bias
dbias cutil.Mem, //output - for training scale and bias
epsilon float64, //input - use the same as forward pass
fromresultSaveMean cutil.Mem, //optional can be null this and reslutSaveInVariance either both have to be null or not // input
fromreslutSaveInVariance cutil.Mem, //optional can be null this and resultSaveMean either both have to be null or not. //input
actD *ActivationD,
wspace cutil.Mem,
wspacesib uint,
rspace cutil.Mem,
rspacesib uint,
) error {
if !b.set {
return errors.New("BatchNormDEx not set")
}
ad := cscalarbydatatype(yD.dtype, alphadata)
bd := cscalarbydatatype(yD.dtype, betadata)
ap := cscalarbydatatype(yD.dtype, alphaparam)
bp := cscalarbydatatype(yD.dtype, betaparam)
if h.w != nil {
return h.w.Work(func() error {
if fromreslutSaveInVariance == nil || fromresultSaveMean == nil {
return Status(C.cudnnBatchNormalizationBackwardEx(
h.x,
b.mode,
b.op,
ad.CPtr(), bd.CPtr(), ap.CPtr(), bp.CPtr(),
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
dyD.descriptor,
dy.Ptr(),
dzD.descriptor,
dz.Ptr(),
dxD.descriptor,
dx.Ptr(),
dbnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
dscale.Ptr(),
dbias.Ptr(),
C.double(epsilon),
nil,
nil,
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) Backward")
}
return Status(C.cudnnBatchNormalizationBackwardEx(
h.x,
b.mode,
b.op,
ad.CPtr(), bd.CPtr(), ap.CPtr(), bp.CPtr(),
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
dyD.descriptor,
dy.Ptr(),
dzD.descriptor,
dz.Ptr(),
dxD.descriptor,
dx.Ptr(),
dbnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
dscale.Ptr(),
dbias.Ptr(),
C.double(epsilon),
fromresultSaveMean.Ptr(),
fromreslutSaveInVariance.Ptr(),
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) Backward")
})
}
if fromreslutSaveInVariance == nil || fromresultSaveMean == nil {
return Status(C.cudnnBatchNormalizationBackwardEx(
h.x,
b.mode,
b.op,
ad.CPtr(), bd.CPtr(), ap.CPtr(), bp.CPtr(),
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
dyD.descriptor,
dy.Ptr(),
dzD.descriptor,
dz.Ptr(),
dxD.descriptor,
dx.Ptr(),
dbnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
dscale.Ptr(),
dbias.Ptr(),
C.double(epsilon),
nil,
nil,
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) Backward")
}
return Status(C.cudnnBatchNormalizationBackwardEx(
h.x,
b.mode,
b.op,
ad.CPtr(), bd.CPtr(), ap.CPtr(), bp.CPtr(),
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
dyD.descriptor,
dy.Ptr(),
dzD.descriptor,
dz.Ptr(),
dxD.descriptor,
dx.Ptr(),
dbnScaleBiasMeanVarDesc.descriptor,
scale.Ptr(),
bias.Ptr(),
dscale.Ptr(),
dbias.Ptr(),
C.double(epsilon),
fromresultSaveMean.Ptr(),
fromreslutSaveInVariance.Ptr(),
actD.descriptor,
wspace.Ptr(),
C.size_t(wspacesib),
rspace.Ptr(),
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) Backward")
}
//BackwardUS is just like Backward but uses unsafe.Pointers instead of cutil.Mem.
func (b *BatchNormDEx) BackwardUS(
h *Handle,
alphadata, betadata, alphaparam, betaparam float64, //alpha -result blend factor, beta - dest blend factor
xD *TensorD,
x unsafe.Pointer,
yD *TensorD,
y unsafe.Pointer,
dyD *TensorD,
dy unsafe.Pointer,
dzD *TensorD,
dz unsafe.Pointer,
dxD *TensorD,
dx unsafe.Pointer,
dbnScaleBiasMeanVarDesc *TensorD,
scale unsafe.Pointer,
bias unsafe.Pointer,
dscale unsafe.Pointer, //output - for training scale and bias
dbias unsafe.Pointer, //output - for training scale and bias
epsilon float64, //input - use the same as forward pass
fromresultSaveMean unsafe.Pointer, //optional can be null this and reslutSaveInVariance either both have to be null or not // input
fromreslutSaveInVariance unsafe.Pointer, //optional can be null this and resultSaveMean either both have to be null or not. //input
actD *ActivationD,
wspace unsafe.Pointer,
wspacesib uint,
rspace unsafe.Pointer,
rspacesib uint,
) error {
if !b.set {
return errors.New("BatchNormDEx not set")
}
ad := cscalarbydatatype(yD.dtype, alphadata)
bd := cscalarbydatatype(yD.dtype, betadata)
ap := cscalarbydatatype(yD.dtype, alphaparam)
bp := cscalarbydatatype(yD.dtype, betaparam)
if h.w != nil {
return h.w.Work(func() error {
return Status(C.cudnnBatchNormalizationBackwardEx(
h.x,
b.mode,
b.op,
ad.CPtr(), bd.CPtr(), ap.CPtr(), bp.CPtr(),
xD.descriptor, x,
yD.descriptor, y,
dyD.descriptor, dy,
dzD.descriptor, dz,
dxD.descriptor, dx,
dbnScaleBiasMeanVarDesc.descriptor, scale, bias, dscale, dbias,
C.double(epsilon),
fromresultSaveMean,
fromreslutSaveInVariance,
actD.descriptor,
wspace,
C.size_t(wspacesib),
rspace,
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) BackwardUS")
})
}
return Status(C.cudnnBatchNormalizationBackwardEx(
h.x,
b.mode,
b.op,
ad.CPtr(), bd.CPtr(), ap.CPtr(), bp.CPtr(),
xD.descriptor, x,
yD.descriptor, y,
dyD.descriptor, dy,
dzD.descriptor, dz,
dxD.descriptor, dx,
dbnScaleBiasMeanVarDesc.descriptor, scale, bias, dscale, dbias,
C.double(epsilon),
fromresultSaveMean,
fromreslutSaveInVariance,
actD.descriptor,
wspace,
C.size_t(wspacesib),
rspace,
C.size_t(rspacesib),
)).error("(b *BatchNormDEx) BackwardUS")
}
/*ForwardInference info was pulled from cudnn documentation
This function performs the forward BatchNormalization layer computation for inference phase.
This layer is based on the paper "Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift", S. Ioffe, C. Szegedy, 2015.
Note: Only 4D and 5D tensors are supported.
Note: The input transformation performed by this function is defined as: y := alpha*y + beta *(bnScale * (x-estimatedMean)/sqrt(epsilon + estimatedVariance)+bnBias)
Note: The epsilon value has to be the same during training, backpropagation and inference.
Note: For training phase use cudnnBatchNormalizationForwardTraining.
Note: Much higher performance when HW-packed tensors are used for all of x, dy, dx.
Parameters::
handle(input): Handle to a previously created cuDNN library descriptor.
mode(input): Mode of operation (spatial or per-activation). BatchNormMode
alpha, beta (input): Pointers to scaling factors (in host memory) used to blend the layer output value with prior value in the destination tensor as follows:
dstValue = alpha[0]*resultValue + beta[0]*priorDstValue. Please refer to this section for additional details.
xDesc, yDesc, x, y: Tensor descriptors and pointers in device memory for the layer's x and y data.
bnScaleBiasMeanVarDesc, bnScaleData, bnBiasData(inputs): Tensor descriptor and pointers in device memory for the batch normalization scale and bias parameters
(in the original paper bias is referred to as beta and scale as gamma).
estimatedMean, estimatedVariance (inputs): Mean and variance tensors (these have the same descriptor as the bias and scale).
It is suggested that resultRunningMean, resultRunningVariance from the cudnnBatchNormalizationForwardTraining
call accumulated during the training phase are passed as inputs here.
epsilon(input): Epsilon value used in the batch normalization formula. Minimum allowed value is CUDNN_BN_MIN_EPSILON defined in cudnn.h.
Possible error values returned by this function and their meanings are listed below.
Returns
CUDNN_STATUS_SUCCESS
The computation was performed successfully.
CUDNN_STATUS_NOT_SUPPORTED
The function does not support the provided configuration.
CUDNN_STATUS_BAD_PARAM
At least one of the following conditions are met:
One of the pointers alpha, beta, x, y, bnScaleData, bnBiasData, estimatedMean, estimatedInvVariance is NULL.
Number of xDesc or yDesc tensor descriptor dimensions is not within the [4,5] range.
bnScaleBiasMeanVarDesc dimensions are not 1xC(x1)x1x1 for spatial or 1xC(xD)xHxW for per-activation mode (parenthesis for 5D).
epsilon value is less than CUDNN_BN_MIN_EPSILON
Dimensions or data types mismatch for xDesc, yDesc
*/
/*
* Performs Batch Normalization during Inference:
* y[i] = bnScale[k]*(x[i]-estimatedMean[k])/sqrt(epsilon+estimatedVariance[k]) + bnBias[k]
* with bnScale, bnBias, runningMean, runningInvVariance tensors indexed
* according to spatial or per-activation mode. Refer to cudnnBatchNormalizationForwardTraining
* above for notes on function arguments.
*/
func (b *BatchNormDEx) ForwardInference(
handle *Handle,
alpha, beta float64, /* alpha[0] = result blend factor, beta[0] = dest layer blend factor */
xD *TensorD,
x cutil.Mem, /* NxCxHxW */
yD *TensorD,
y cutil.Mem, /* NxCxHxW */
ScaleBiasMeanVarDesc *TensorD,
scale, bias, estimatedMean, estimatedVariance cutil.Mem, //all share the ScaleBiasMeanVarDesc descriptor
epsilon float64,
) error {
if !b.set {
return errors.New("BatchNormDEx not set")
}
a := cscalarbydatatype(yD.dtype, alpha)
be := cscalarbydatatype(yD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnBatchNormalizationForwardInference(
handle.x,
b.mode,
a.CPtr(),
be.CPtr(),
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
ScaleBiasMeanVarDesc.descriptor,
scale.Ptr(), bias.Ptr(), estimatedMean.Ptr(), estimatedVariance.Ptr(),
C.double(epsilon),
)).error("(b *BatchNormDEx) ForwardInference")
})
}
return Status(C.cudnnBatchNormalizationForwardInference(
handle.x,
b.mode,
a.CPtr(),
be.CPtr(),
xD.descriptor,
x.Ptr(),
yD.descriptor,
y.Ptr(),
ScaleBiasMeanVarDesc.descriptor,
scale.Ptr(), bias.Ptr(), estimatedMean.Ptr(), estimatedVariance.Ptr(),
C.double(epsilon),
)).error("(b *BatchNormDEx) ForwardInference")
}
//ForwardInferenceUS is just like ForwardInference but uses unsafe.Pointers instead of cutil.Mem
func (b *BatchNormDEx) ForwardInferenceUS(
handle *Handle,
alpha, beta float64, /* alpha[0] = result blend factor, beta[0] = dest layer blend factor */
xD *TensorD,
x unsafe.Pointer, /* NxCxHxW */
yD *TensorD,
y unsafe.Pointer, /* NxCxHxW */
ScaleBiasMeanVarDesc *TensorD,
scale, bias, estimatedMean, estimatedVariance unsafe.Pointer, //all share the ScaleBiasMeanVarDesc descriptor
epsilon float64,
) error {
if !b.set {
return errors.New("BatchNormDEx not set")
}
a := cscalarbydatatype(yD.dtype, alpha)
be := cscalarbydatatype(yD.dtype, beta)
if handle.w != nil {
return handle.w.Work(func() error {
return Status(C.cudnnBatchNormalizationForwardInference(
handle.x,
b.mode,
a.CPtr(),
be.CPtr(),
xD.descriptor, x,
yD.descriptor, y,
ScaleBiasMeanVarDesc.descriptor,
scale, bias, estimatedMean, estimatedVariance,
C.double(epsilon),
)).error("(b *BatchNormDEx) ForwardInferenceUS")
})
}
return Status(C.cudnnBatchNormalizationForwardInference(
handle.x,
b.mode,
a.CPtr(),
be.CPtr(),
xD.descriptor, x,
yD.descriptor, y,
ScaleBiasMeanVarDesc.descriptor,
scale, bias, estimatedMean, estimatedVariance,
C.double(epsilon),
)).error("(b *BatchNormDEx) ForwardInferenceUS")
}
//MinEpsilon is the Minimum Epsilon required. It is now zero, but it used to be 1e-5
func (b *BatchNormDEx) MinEpsilon() float64 {
return float64(C.CUDNN_BN_MIN_EPSILON)
}