-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathErrFctSoftmClassCrossEntNgram.cpp
290 lines (252 loc) · 8.6 KB
/
ErrFctSoftmClassCrossEntNgram.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
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
/*
* 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
*/
#include "ErrFctSoftmClassCrossEntNgram.h"
#ifdef BLAS_CUDA
#include "Gpu.cuh"
#endif
ErrFctSoftmClassCrossEntNgram::ErrFctSoftmClassCrossEntNgram(Mach &mach)
: ErrFct(mach), grad_class(NULL)
{
debug0("*** ErrFctSoftmClassCrossEntNgram() constructor\n");
#ifdef BLAS_CUDA
err = NULL;
host_err = NULL;
#endif
}
ErrFctSoftmClassCrossEntNgram::ErrFctSoftmClassCrossEntNgram(const ErrFctSoftmClassCrossEntNgram &efct)
: ErrFct(efct), grad_class(NULL)
{
debug0("*** ErrFctSoftmCrossEntNgram() copy constructor\n");
#ifdef BLAS_CUDA
err = NULL;
host_err = NULL;
#endif
}
ErrFctSoftmClassCrossEntNgram::~ErrFctSoftmClassCrossEntNgram()
{
#ifdef BLAS_CUDA
if (grad_class)
cudaFree(grad_class);
if (host_err) {
delete [] host_err;
host_err = NULL;
}
if (err) {
cudaFree(err);
err = NULL;
}
#else
if (grad_class)
delete [] grad_class;
#endif
}
void ErrFctSoftmClassCrossEntNgram::SetUp(MachSoftmaxClass* p_mach_class, WordList* p_wlist)
{
wlist = p_wlist;
mach_class = p_mach_class;
// Get some information from wlist
class_sizes = wlist->GetClassSizes();
n_classes = class_sizes.size();
#ifdef BLAS_CUDA
if (grad_class)
cudaFree(grad_class);
grad_class = Gpu::Alloc(n_classes*bsize, "class gradient in ErrFctSoftmClassCrossEntNgram");
// allocate GPU memory to store errors (class and conditional NLLs)
// and host memory to transfer it
if (err)
cudaFree(err);
err = Gpu::Alloc(2, "sum of class NLL, then sum of conditional NLL");
if (host_err)
delete [] host_err;
host_err = new REAL[2];
#else
if (grad_class)
delete [] grad_class;
grad_class = new REAL[n_classes*bsize];
#endif
// Build the output layer (softmax with classes)
mach_class->SetUp(wlist);
}
//************
// this->class_output contains the probability of each of the n_classes class,
// for all examples in the minibatch.
// this->output contains, for each example in the minibatch, a collection of
// conditional probability vectors, one for each class: for each word i
// in that class c, P(w=i|c, h), the conditional probability that the next
// word (w) is i, given the class c and given the context (h).
// In this function, only the conditional probabilities for words belonging to the
// class of the target word, c(t), are correct (other are garbage).
REAL ErrFctSoftmClassCrossEntNgram::CalcValue(int eff_bsize)
{
double err_value = 0.;
if (eff_bsize <= 0)
eff_bsize = bsize;
#ifdef BLAS_CUDA
Gpu::SetConfig(gpu_conf);
// The first component of the cost is the NLL of the correct class
err_value += Gpu::ErrFctSoftmCrossEntNgramCalcValue(eff_bsize, n_classes,
output_class, target_class);
// Second part is the conditional NLL of the correct word in the class.
err_value += Gpu::ErrFctSoftmCrossEntNgramCalcValue(eff_bsize, dim, output, target);
#else
REAL *tcptr=target_class;
REAL *ocptr=output_class;
REAL *tptr = target;
REAL *optr = output;
for (int b=0; b<eff_bsize; b++) {
// The first component of the cost is the NLL of the correct class
err_value += safelog(ocptr[(int) *tcptr++]);
// Second part is the conditional NLL of the correct word in the class.
err_value += safelog(optr[(int) *tptr]);
ocptr += n_classes;
optr += dim;
tptr++;
}
#endif
return (REAL) err_value;
}
#if 0 // not used any more use CalcValueBatch instead
REAL ErrFctSoftmClassCrossEntNgram::CalcValueNth(int idx)
{
#ifdef BLAS_CUDA
Gpu::SetConfig(gpu_conf);
Error("ErrFctSoftmClassCrossEntNgram::CalcValueNth not implemented yet on GPU");
return 0.;
#else
REAL *tcptr = target_class + idx;
REAL *ocptr = output_class + idx*n_classes;
REAL *tptr = target + idx;
REAL *optr = output + idx*dim;
// The first component of the cost is the NLL of the correct class
// Second part is the conditional NLL of the correct word in the class.
return safelog(ocptr[(int) *tcptr]) + safelog(optr[(int) *tptr]);
#endif
}
#endif
//*********************************************************************************r
// CalcValueBatch
void ErrFctSoftmClassCrossEntNgram::CalcValueBatch(int eff_bsize, REAL *res)
{
if (eff_bsize<=0) eff_bsize=bsize;
Error("ErrFctSoftmClassCrossEntNgram::CalcValueBatch() not yet implemented");
}
//*********************************************************************************r
// CalcMax
void ErrFctSoftmClassCrossEntNgram::CalcMax(int eff_bsize, REAL *res, int *pos)
{
if (eff_bsize<=0) eff_bsize=bsize;
Error("ErrFctSoftmClassCrossEntNgram::CalcMax() not yet implemented");
}
// Computes the derivative of the NLL on the softmax class output, and on the
// conditional softmax output corresponding to the target class.
// As in ErrFctSoftmCrossEntNgram::CalcGrad, we include the derivative through
// the softmax, to avoid wasteful computations.
REAL ErrFctSoftmClassCrossEntNgram::CalcGrad(int eff_bsize)
{
double err_value = 0.;
if (eff_bsize <= 0)
eff_bsize = bsize;
#ifdef BLAS_CUDA
// First part: gradient wrt the class output
Gpu::SetConfig(gpu_conf);
Gpu::ErrFctSoftmCrossEntNgramCalcGrad(eff_bsize, n_classes, output_class, grad_class,
target_class, err);
// Second part: gradient wrt the conditional NLL
// We use offset and class size stored in target_class_info
Gpu::ErrFctSoftmClassCrossEntNgramCalcGrad(eff_bsize, dim, output, grad, target,
target_class_info, err + 1);
Gpu::MemcpyAsync(host_err, err, 2*sizeof(REAL), cudaMemcpyDeviceToHost);
Gpu::StreamSynchronize();
Gpu::CheckError("ErrFctSoftmClassCrossEntNgram::CalcGrad");
err_value = host_err[0] + host_err[1];
#else
REAL *ocptr=output_class;
REAL *gcptr=grad_class;
REAL *tcptr=target_class;
REAL *optr=output;
REAL *gptr=grad;
REAL *tptr=target;
// initialize grad_class
int n=eff_bsize*n_classes;
REAL f1 = -1.0;
memcpy(grad_class, output_class, n*sizeof(REAL));
SCAL(&n, &f1, grad_class, &inc1);
for (int b=0; b<eff_bsize; b++) {
int offset = target_class_info[2*b];
int clsize = target_class_info[2*b+1];
if (*tptr<0.0) ErrorN("negative index %f at %d",*tptr,b);
int tgt_idx = (int) *tptr;
int tgt_class = (int) *tcptr;
// compute grad_class
gcptr[tgt_class] += 1.0;
err_value += safelog(ocptr[tgt_class]);
// update the part of grad that corresponds to the words in target class,
// which is a segment starting at offset after the beginning
// of the row, and of length clsize.
memcpy(gptr+offset, optr+offset, clsize*sizeof(REAL));
SCAL(&clsize, &f1, gptr+offset, &inc1);
gptr[tgt_idx] += 1.0;
err_value += safelog(optr[tgt_idx]);
// Increment pointers for next example
tcptr++;
gcptr += n_classes;
ocptr += n_classes;
tptr++;
gptr += dim;
optr += dim;
}
#endif
return (REAL) err_value;
}
REAL ErrFctSoftmClassCrossEntNgram::CalcGradNull(int eff_bsize)
{
Error("ErrFctSoftmClassCrossEntNgram::CalcGradNull not implemented yet");
return 0.;
}
REAL ErrFctSoftmClassCrossEntNgram::CalcWordClassError(int eff_bsize)
{
int err_value = 0;
if (eff_bsize <= 0)
eff_bsize = bsize;
#ifdef BLAS_CUDA
return Gpu::ErrFctSoftmClassError(eff_bsize, n_classes, output_class, target_class);
#else
REAL *tcptr=target_class;
REAL *ocptr=output_class;
for (int b=0; b<eff_bsize; b++) {
// Find the maximum predicted class probability
REAL max_oclass = ocptr[0];
int argmax = 0;
for (int i=1; i<n_classes; i++) {
if (ocptr[i] > max_oclass) {
argmax = i;
max_oclass = ocptr[i];
}
}
if ((int) *tcptr != argmax)
err_value++;
debug2("%d/%d, ", (int) *tcptr, argmax);
ocptr += n_classes;
tcptr++;
}
#endif
debug1("%d\n", err_value);
return (REAL) err_value;
}