forked from hschwenk/cslm-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachSoftmaxStable.cpp
163 lines (145 loc) · 5.3 KB
/
MachSoftmaxStable.cpp
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
/*
* This file is part of the continuous space language and translation model toolkit
* for statistical machine translation and large vocabulary speech recognition.
*
* Copyright 2015, Holger Schwenk, LIUM, University of Le Mans, France
*
* The CSLM toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*
*/
using namespace std;
#include <iostream>
#include <math.h>
#include "Tools.h"
#include "MachSoftmaxStable.h"
#include "Blas.h"
#ifdef BLAS_CUDA
# include "Gpu.cuh"
#endif
MachSoftmaxStable::MachSoftmaxStable(const int p_idim, const int p_odim, const int p_bsize, const ulong p_nbfw, const ulong p_nbbw, const int shareid, const bool xdata)
: MachLin(p_idim, p_odim, p_bsize, p_nbfw, p_nbbw, shareid, xdata)
{
debug0("** constructor MachSoftmaxStable\n");
#if defined(BLAS_CUDA) && defined(BLAS_CUDA_NPPS_SUM)
int nbytes=0;
Gpu::SetConfig(gpu_conf);
nppsSumGetBufferSize_32f(odim, &nbytes);
debug2(" - CUDA MachSoftmaxStable: allocating %d bytes for fast sum of %d-dimensional output layer\n",nbytes,odim);
gpu_sum_buf = nppsMalloc_8u(nbytes);
#endif
#ifdef BLAS_CUDA
if(Gpu::GetDeviceProp(gpu_conf).warpSize != 32){
Error("KernelSoftmax used by MachSoftmaxStable suppose a wrapSize of 32. The code will return wrong result if run!");
}
#endif
}
MachSoftmaxStable::MachSoftmaxStable(const MachSoftmaxStable &m)
: MachLin(m)
{
debug0("** copy constructor MachSoftmaxStable\n");
#if defined(BLAS_CUDA) && defined(BLAS_CUDA_NPPS_SUM)
int nbytes=0;
nppsSumGetBufferSize_32f(odim, &nbytes);
debug2(" - CUDA MachSoftmaxStable: allocating %d bytes for fast sum of %d-dimensional output layer\n",nbytes,odim);
gpu_sum_buf = nppsMalloc_8u(nbytes);
#endif
#ifdef BLAS_CUDA
if(Gpu::GetDeviceProp(gpu_conf).warpSize != 32){
Error("KernelSoftmax used by MachSoftmaxStable suppose a wrapSize of 32. The code will return wrong result if run!");
}
#endif
}
MachSoftmaxStable::~MachSoftmaxStable()
{
debug0("** destructor MachSoftmaxStable\n");
#if defined(BLAS_CUDA) && defined(BLAS_CUDA_NPPS_SUM)
Gpu::SetConfig(gpu_conf);
if (gpu_sum_buf) nppsFree(gpu_sum_buf);
#endif
}
//-----------------------------------------------
// Tools
//-----------------------------------------------
void MachSoftmaxStable::Info(bool detailed, char *txt)
{
if (detailed) {
cout << "Information on softmax stable machine" << endl;
MachLin::Info(detailed);
}
else {
printf("%sMachSoftmaxStable %d-%d, bs=%d, passes=%lu/%lu", txt,idim, odim, bsize, nb_forw, nb_backw);
if (lr_coeff != 1.0) printf(", lrate-coeff=%.2f", lr_coeff);
#ifdef BLAS_CUDA
printf(", on GPU %d", Gpu::GetCudaDevice(Gpu::GetDevice(gpu_conf)));
#endif
tm.disp(", ");
tmn.disp(" + norm: ");
printf("\n");
debug5("%s data: %p -> %p, grad %p <- %p\n", txt, (void*)data_in, (void*)data_out, (void*)grad_in, (void*)grad_out);
}
}
//-----------------------------------------------
// Training
//-----------------------------------------------
void MachSoftmaxStable::Forw(int eff_bsize, bool in_train)
{
debug2("*** MachSoftmaxStable::Forw: %p -> %p\n",(void*)data_in,(void*)data_out);
if (eff_bsize<=0) eff_bsize=bsize;
MachLin::Forw(eff_bsize,in_train);
tmn.start();
// softmax stable normalization
#ifdef BLAS_CUDA
// device already set by MachLin::Forw()
Gpu::MachSoftmaxStableForw(eff_bsize,odim,data_out);
#else
for (int b=0; b<eff_bsize; b++) {
// Get the maximum value of data_out on row b
REAL max = data_out[b*odim];
for (int i=1; i<odim; i++) {
REAL x = data_out[b*odim + i];
if (x > max)
max = x;
}
// Compute exp(x - max) inplace for each x in row b of data_out, and their sum
REAL sum_exp = 0.;
for (int i=0; i<odim; i++) {
REAL exp_x = exp(data_out[b*odim + i] - max);
sum_exp += exp_x;
data_out[b*odim + i] = exp_x;
}
// Normalize the row, dividing all values by sum_exp
for (int i=0; i<odim; i++) {
data_out[b*odim + i] /= sum_exp;
}
}
#endif
// perform drop-out
MachLin::ForwDropout(eff_bsize,in_train);
tmn.stop();
}
void MachSoftmaxStable::Backw(const float lrate, const float wdecay, int eff_bsize)
{
debug2("*** MachSoftmaxStable Backw %p <- %p\n",(void*)grad_in,(void*)grad_out);
// derivate softmax activation function
// do_i / da_k = o_i (kronecker_ik - o_k)
// we suppose that do_i/da_k vanishes in the error function !!
// = o_i (1 - o_i)
// this can't be done here since the result depends
// on the error function (we must derivate each output w/r
// to ALL other outputs. This can't be stored in one vector)
// dE/da_i = sum_k dE/do_k do_k/da_i
// On the other hand, many terms vanish with usual error functions
MachLin::Backw(lrate, wdecay, eff_bsize);
}