forked from hschwenk/cslm-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrFctSoftmCrossEntNgramMulti.cpp
192 lines (166 loc) · 5.76 KB
/
ErrFctSoftmCrossEntNgramMulti.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
/*
* 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 "Tools.h"
#include "ErrFctSoftmCrossEntNgramMulti.h"
#include "Blas.h"
#ifdef BLAS_CUDA
#include "Gpu.cuh"
#endif
ErrFctSoftmCrossEntNgramMulti::ErrFctSoftmCrossEntNgramMulti(Mach &mach, int n)
: ErrFctSoftmCrossEntNgram(mach), nb(n) // this allocates memory before we change the variable "dim"
{
if (mach.GetOdim()%nb != 0)
Error("ErrFctSoftmCrossEntNgramMulti: output layer size is not an integer multiple");
dim = mach.GetOdim() / nb;
debug2("ErrFctSoftmCrossEntNgramMulti: %d n-grams of size %d\n", nb, dim);
}
//**********************************************************************************
// E = log(sum_i d_i ln o_i)
// = ln o_t where t is the target index
// output: dimension voc_size
// target: dimension 1 with values [0,voc_size[
// We also take the log since this can't be done later if bsize>1
REAL ErrFctSoftmCrossEntNgramMulti::CalcValue(int eff_bsize)
{
if (eff_bsize<=0) eff_bsize=bsize;
#ifdef BLAS_CUDA
Gpu::SetConfig(gpu_conf);
Error("TODO");
return 0;
//return Gpu::ErrFctSoftmCrossEntNgramMultiCalcValue(eff_bsize, dim, nb, output, target);
#else
REAL *tptr=target;
REAL *optr=output;
double err=0.0;
for (int b=0; b<eff_bsize; b++) {
for (int n=0; n<nb; n++) {
int tidx=(int) *tptr++;
if (tidx==NULL_WORD) {
debug3("b=%d, n=%d, tidx=NULL, out=%f\n", b, n, optr[tidx]);
}
else {
debug4("b=%d, n=%d, tidx=%d, out=%f\n", b, n, tidx, optr[tidx]);
err += log(optr[tidx]);
}
optr += dim;
}
debug1("err=%f\n",err);
}
return (REAL) err;
#endif
}
//*********************************************************************************r
// CalcValuBatch
void ErrFctSoftmCrossEntNgramMulti::CalcValueBatch(int eff_bsize, REAL *res)
{
if (eff_bsize<=0) eff_bsize=bsize;
#ifdef BLAS_CUDA
Gpu::SetConfig(gpu_conf);
Error("TODO: ErrFctSoftmCrossEntNgramMulti::CalcValueBatch() for GPU");
//Gpu::ErrFctSoftmCrossEntNgramMultiCalcValueBatch(eff_bsize, dim, output, target, res);
#else
REAL *tptr=target;
REAL *optr=output;
for (int b=0; b<eff_bsize; b++) {
for (int n=0; n<nb; n++) {
int tidx=(int) *tptr++;
*res++ = (tidx==NULL_WORD) ? LOG_PROBA_NONE : safelog(optr[tidx]);
optr += dim;
}
}
#endif
}
//**********************************************************************************
// E = log(sum_i d_i ln o_i)
// = ln o_t where t is the target index
// output: dimension voc_size
// target: dimension 1 with values [0,voc_size[
// We also take the log since this can't be done later if bsize>1
#if 0 // not used any more use CalcValueBatch instead
REAL ErrFctSoftmCrossEntNgramMulti::CalcValueNth(int idx)
{
#ifdef BLAS_CUDA
Gpu::SetConfig(gpu_conf);
Error("CUDA: ErrFctSoftmCrossEntNgramMulti::CalcValueNth() not implemented");
return 0.0;
#else
Error("ErrFctSoftmCrossEntNgramMulti::CalcValueNth() not yet implemented");
REAL *optr=output + idx*nb*dim;
REAL *tptr=target + idx*nb;
double err=0.0;
for (int d=0; d<dim; d++)
err += log(optr[(int) *tptr++]);
optr += dim;
return log(optr[(int) *tptr]);
#endif
}
#endif
// We include here the derivation of the softmax outputs since we have
// dE/da_k = sum_i dE/do_i do_i/da_k
// Due to the sum, dE/do_i and do_i/da_k can't be calculated separately
// dE/do_i = d_i/o_i
// do_i/da_k = o_i (kronecker_ik - o_k)
// -> dE/da_k = sum_i d_i/o_i * o_i (kronecker_ik - o_k)
// = sum_i d_i (kronecker_ik - o_k)
// = (kronecker_tk - o_k) since d_i=0 for i!=t
REAL ErrFctSoftmCrossEntNgramMulti::CalcGrad(int eff_bsize)
{
if (eff_bsize<=0) eff_bsize=bsize;
debug3("ErrFctSoftmCrossEntNgramMulti::CalcGrad(): %d layers of dim %d, bsize=%d\n", nb, dim, eff_bsize);
#ifdef BLAS_CUDA
Gpu::SetConfig(gpu_conf);
REAL err = Gpu::ErrFctSoftmCrossEntNgramMultiCalcGrad(eff_bsize, dim, nb, output, grad, target);
debug1("ErrFctSoftmCrossEntNgramMulti::CalcGrad err=%f CUDA\n", err);
return err;
#else
REAL *optr=output;
REAL *gptr=grad;
REAL *tptr=target;
int tidx;
REAL err=0.0;
int n=eff_bsize*nb*dim;
REAL f1=-1.0;
memcpy(grad,output,n*sizeof(REAL));
SCAL(&n,&f1,grad,&inc1); // TODO: can be speed-up since many phrase are incomplete
for (int b=0; b<eff_bsize; b++) {
for (n=0; n<nb; n++) {
tidx=(int) *tptr++;
if (tidx==NULL_WORD) {
debug4("grad ngram-multi: b=%d, n=%d, tidx=NULL, out=%f -> err=%e\n", b, n, optr[tidx], err);
memset(gptr, 0, dim*sizeof(REAL));
}
else {
gptr[tidx] += 1.0;
err += log(optr[tidx]);
debug6("grad ngram-multi: b=%d, n=%d, tidx=%u, out=%f -> err=%e, grad@target=%e\n", b, n, tidx, optr[tidx], err, gptr[tidx]);
}
gptr+=dim; optr+=dim;
}
}
debug1("ErrFctSoftmCrossEntNgramMulti::CalcGrad err=%f\n", err);
return err;
#endif
}