forked from hschwenk/cslm-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainerNgram.cpp
300 lines (258 loc) · 9.15 KB
/
TrainerNgram.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
291
292
293
294
295
296
297
298
299
/*
* 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 <unistd.h>
#include <time.h>
#include <algorithm>
#include "Mach.h"
#include "TrainerNgram.h"
TrainerNgram::TrainerNgram (Mach *pmach, Lrate *lrate, ErrFct *perrfct,
const char *train_fname, const char *dev_fname,
REAL p_wd, int p_maxep, int p_ep, bool p_wclass)
: Trainer(pmach,lrate,perrfct,NULL,NULL,p_wd,p_maxep,p_ep,0),
order(0)
{
debug0("*** Constructor TrainerNgram for training ***\n");
char msg[1024];
idim=mach->GetIdim(); odim=mach->GetOdim(); bsize=mach->GetBsize();
iaux = idim;
if (odim < 16) {
sprintf(msg,"TrainerNgram: output dimension of the machine is suspiciously small (%d)\n", odim);
//Error(msg);
printf(msg); // TODO:
}
// Allocate target memory to store targets as indices, not 1-hot vectors
if (buf_target)
Error("buf_target should not have been allocated");
#ifdef BLAS_CUDA
if (gpu_target)
Error("gpu_target should not have been allocated");
gpu_target = Gpu::Alloc(1*bsize, "targets (as idx) in TrainerNgram");
cudaError_t err = cudaMallocHost(&buf_target, 1*bsize*sizeof(REAL));
if(err != cudaSuccess){
Error("Not able to allocate pinned host memory");
}
#else
buf_target = new REAL[1*bsize];
#endif
if (train_fname) {
data_train = new Data(train_fname, NULL, p_wclass);
if (idim != data_train->GetIdim()) {
sprintf(msg,"TrainerNgram: input dimension of the training data (%d) does not match the one of the machine (%d)\n", data_train->GetIdim(), idim);
Error(msg);
}
if (data_train->GetOdim() != data_train->GetNbFactors()) {
sprintf(msg,"TrainerNgram: output dimension of the training data should be %d, found %d\n", data_train->GetNbFactors(), data_train->GetOdim());
Error(msg);
}
auxdim = data_train->GetAuxdim();
}
else
data_train=NULL;
if (dev_fname) {
data_dev = new Data(dev_fname, data_train, p_wclass);
data_dev_alloc=true;
if (idim != data_dev->GetIdim()) {
sprintf(msg,"TrainerNgram: input dimension of the validation data (%d) does not match the one of the machine (%d)\n", data_dev->GetIdim(), idim);
Error(msg);
}
if (data_dev->GetOdim() != data_dev->GetNbFactors()) {
sprintf(msg,"TrainerNgram: output dimension of the validation data should be %d, found %d\n", data_dev->GetNbFactors(), data_dev->GetOdim());
Error(msg);
}
int auxdim_dev = data_dev->GetAuxdim();
if (0 >= auxdim)
auxdim = auxdim_dev;
else if (auxdim != auxdim_dev)
ErrorN("TrainerNgram: auxiliary data dimension of the validation data should be %d, found %d", auxdim, auxdim_dev);
}
else {
data_dev=NULL;
data_dev_alloc=false;
}
iaux = (idim - auxdim);
buf_target_wid = new WordID[odim*bsize];
}
TrainerNgram::TrainerNgram (Mach *pmach, ErrFct *perrfct, Data *data, int aux_dim)
: Trainer(pmach,NULL,perrfct,NULL,NULL,0,0,0),
order(0)
{
debug0("*** Constructor TrainerNgram for testing ***\n");
char msg[1024];
idim=mach->GetIdim(); odim=mach->GetOdim(); bsize=mach->GetBsize();
if (odim < 16) {
sprintf(msg,"TrainerNgram: output dimension of the machine is suspiciously small (%d)\n", odim);
Error(msg);
}
data_train=NULL;
data_dev=data;
data_dev_alloc=false; // do not free it by this class !
if (data_dev) {
if (idim != data_dev->GetIdim()) {
sprintf(msg,"TrainerNgram: input dimension of the validation data (%d) does not match the one of the machine (%d)\n", data_dev->GetIdim(), idim);
Error(msg);
}
if (data_dev->GetOdim() != 1) {
sprintf(msg,"TrainerNgram: output dimension of the validation data should be 1, found %d\n", data_dev->GetOdim());
Error(msg);
}
auxdim = data_dev->GetAuxdim();
}
else
auxdim = aux_dim;
iaux = (idim - auxdim);
buf_target_wid = new WordID[odim*bsize];
}
TrainerNgram::~TrainerNgram()
{
delete [] buf_target_wid;
}
//**************************************************************************************
REAL TrainerNgram::Train()
{
if (!data_train) return -1;
#ifdef DEBUG
printf("*****************\n");
printf("TrainerNgram::Train():\n");
printf(" - data_in: %p \n", (void*) buf_input);
printf(" - target: %p \n", (void*) buf_target);
printf(" - grad_out: %p \n", (void*) errfct->GetGrad());
#endif
data_train->Rewind();
Timer ttrain; // total training time
ttrain.start();
REAL log_sum=0;
nb_ex=0;
#ifdef BLAS_CUDA
Gpu::SetConfig(mach->GetGpuConfig());
mach->SetDataIn(gpu_input); // we copy from buf_input to gpu_input
errfct->SetTarget(gpu_target); // we copy from buf_target to gpu_target
#else
mach->SetDataIn(buf_input);
errfct->SetTarget(buf_target);
#endif
errfct->SetOutput(mach->GetDataOut());
mach->SetGradOut(errfct->GetGrad());
bool data_available;
do {
// get a bunch of data
// TODO: exclude out of slist
int n=0;
data_available = true;
while (n < mach->GetBsize() && data_available) {
data_available = data_train->Next();
if (!data_available) break;
memcpy(buf_input + n*idim, data_train->input, idim*sizeof(REAL));
memcpy(buf_target + n*1, data_train->target, 1*sizeof(REAL));
n++;
}
if (n>0) {
#ifdef BLAS_CUDA
Gpu::MemcpyAsync(gpu_input, buf_input , n*idim*sizeof(REAL), cudaMemcpyHostToDevice);
Gpu::MemcpyAsync(gpu_target, buf_target , n*1*sizeof(REAL), cudaMemcpyHostToDevice);
#endif
mach->Forw(n,true);
log_sum += errfct->CalcGrad(n);
lrate->UpdateLrateOnForw(mach->GetNbForw());
mach->Backw(lrate->GetLrate(), wdecay, n);
#ifdef BLAS_CUDA
Gpu::StreamSynchronize();
#endif
}
nb_ex += n;
} while (data_available);
ttrain.stop();
ttrain.disp(" - training time: ");
printf("\n");
if (nb_ex>0) return exp(-log_sum / (REAL) nb_ex); // return perplexity
return -1;
}
//**************************************************************************************
// This should be overriden to do a task-specific validation
REAL TrainerNgram::TestDev(char *fname)
{
if (!data_dev) return -1;
ofstream fs;
REAL *log_probas=NULL;
if (fname) {
cout << " - dumping lln probability stream to file '" << fname << "'" << endl;
fs.open(fname,ios::out);
CHECK_FILE(fs,fname);
fs.precision(8);
fs << std::scientific;
log_probas = new REAL[bsize];
}
int nb_ex_dev=0;
REAL log_sum=0;
data_dev->Rewind();
#ifdef BLAS_CUDA
Gpu::SetConfig(mach->GetGpuConfig());
mach->SetDataIn(gpu_input); // we copy from buf_input to gpu_input
errfct->SetTarget(gpu_target); // we copy from buf_target to gpu_target
#else
mach->SetDataIn(buf_input);
errfct->SetTarget(buf_target);
#endif
errfct->SetOutput(mach->GetDataOut());
// TODO: we could copy all the examples on the GPU and then split into bunches locally
bool data_available;
do {
// get a bunch of data
// TODO: exlude out of slist
int n=0;
data_available = true;
while (n < mach->GetBsize() && data_available) {
data_available = data_dev->Next();
if (!data_available) break;
memcpy(buf_input + n*idim, data_dev->input, idim*sizeof(REAL));
memcpy(buf_target + n*1, data_dev->target, 1*sizeof(REAL));
n++;
}
// process the bunch
if (n>0) {
#ifdef BLAS_CUDA
Gpu::MemcpyAsync(gpu_input, buf_input , n*idim*sizeof(REAL), cudaMemcpyHostToDevice);
Gpu::MemcpyAsync(gpu_target, buf_target , n*1*sizeof(REAL), cudaMemcpyHostToDevice);
#endif
mach->Forw(n,false);
log_sum += errfct->CalcValue(n);
if (fname) {
// dump the log probas for all words in the current minibatch
errfct->CalcValueBatch(n, log_probas);
for (int ni=0; ni<n; ni++) fs << log_probas[ni] << endl;
}
}
nb_ex_dev += n;
debug2("%d: %f\n",nb_ex_dev,exp(-log_sum/nb_ex_dev));
} while (data_available);
if (fname) fs.close();
if (log_probas) delete [] log_probas;
REAL px = (nb_ex_dev>0) ? exp(-log_sum / (REAL) nb_ex_dev) : -1;
printf(" - %d %d-gram requests, ln_sum=%.2f, overall px=%.2f\n", nb_ex_dev, iaux+1, log_sum, px);
return px;
}
//**************************************************************************************
// information after finishing an epoch
void TrainerNgram::InfoPost ()
{
cout << " - epoch finished, " << nb_ex << " examples seen, average perplexity: " << err_train << endl;
}