forked from rwth-i6/returnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inv.py
418 lines (359 loc) · 14.1 KB
/
Inv.py
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
import theano
import theano.tensor as T
import os
Tfloat = theano.config.floatX # @UndefinedVariable
class InvOp(theano.Op):
__props__ = ('min_skip', 'max_skip', 'nstates', 'focus', 'nil', 'coverage', 'mode')
def __eq__(self, other):
return type(self) == type(other)
def __hash__(self):
return hash(type(self))
def __str__(self):
return self.__class__.__name__
def __init__(self, min_skip, max_skip, nstates, focus='last', nil=-1, coverage=0, mode='viterbi'):
self.min_skip = min_skip
self.max_skip = max_skip
self.coverage = coverage
self.nil = nil
self.focus = ['last','max'].index(focus)
self.nstates = nstates
self.mode = mode
def make_node(self, x, y, len_x, len_y):
x = theano.tensor.as_tensor_variable(x)
assert x.ndim == 3 # tensor: nframes x nseqs x dim
y = theano.tensor.as_tensor_variable(y)
assert y.ndim == 2 # matrix: nseqs x max_labelling_length
len_x = theano.tensor.as_tensor_variable(len_x)
len_y = theano.tensor.as_tensor_variable(len_y)
assert len_x.ndim == 1 # vector of seqs lengths
assert len_x.dtype == "int32"
assert len_y.ndim == 1 # vector of seqs lengths
assert len_y.dtype == "int32"
return theano.Apply(self, [x, y, len_x, len_y], [T.ftensor3()])
def c_support_code(self):
src = ""
path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/C_Support_Code.cpp', 'r') as f:
src += f.read()
with open(path + '/Inv.cpp', 'r') as f:
src += f.read()
return src
def c_compile_args(self):
return ['-fopenmp']
def c_code(self, node, name, inp, out, sub):
x, y, len_x, len_y = inp
attention = out[0]
nstates = self.nstates
min_skip = self.min_skip
max_skip = self.max_skip
nil = self.nil
viterbi = int(self.mode == 'viterbi')
focus = self.focus
coverage = self.coverage
fail = sub['fail']
return """
Py_XDECREF(%(attention)s);
int T = PyArray_DIM(%(x)s,0);
int B = PyArray_DIM(%(x)s,1);
int N = PyArray_DIM(%(y)s,0) * %(nstates)s;
npy_intp dims[] = {N, B, T};
%(attention)s = (PyArrayObject*) PyArray_Zeros(3, dims, PyArray_DescrFromType(NPY_FLOAT32), 0);
if (!%(attention)s)
%(fail)s;
{
ArrayF attentionWr(%(attention)s);
ArrayF xWr(%(x)s);
ArrayI yWr(%(y)s);
CArrayI len_xWr(%(len_x)s);
CArrayI len_yWr(%(len_y)s);
#pragma omp parallel for
for(int i = 0; i < B; ++i)
{
Inv cls;
SArrayF attentionSWr(attentionWr, 1, i);
if(%(viterbi)s)
{
cls.viterbi(CSArrayF(xWr, 1, i), CSArrayI(yWr, 1, i), len_xWr(i), len_yWr(i), %(nstates)s,
%(min_skip)s, %(max_skip)s, %(focus)s, %(nil)s, %(coverage)s, attentionSWr);
}
}
}
""" % locals()
# IMPORTANT: change this, if you change the c-code
#def c_code_cache_version(self):
# return (1.01,)
class InvOpBackTrace(theano.Op):
__props__ = ('min_skip', 'max_skip', 'nstates', 'focus', 'mode')
def __eq__(self, other):
return type(self) == type(other)
def __hash__(self):
return hash(type(self))
def __str__(self):
return self.__class__.__name__
def __init__(self, min_skip, max_skip, nstates, focus='last', nil=-1, coverage=0, mode='viterbi'):
self.min_skip = min_skip
self.max_skip = max_skip
self.focus = ['last', 'max'].index(focus)
self.nstates = nstates
self.mode = ['viterbi', 'full'].index(mode)
self.nil = nil
self.coverage = coverage
def make_node(self, x, y, len_x, len_y):
x = theano.tensor.as_tensor_variable(x)
assert x.ndim == 3 # tensor: nframes x nseqs x dim
y = theano.tensor.as_tensor_variable(y)
assert y.ndim == 2 # matrix: nseqs x max_labelling_length
len_x = theano.tensor.as_tensor_variable(len_x)
len_y = theano.tensor.as_tensor_variable(len_y)
assert len_x.ndim == 1 # vector of seqs lengths
assert len_x.dtype == "int32"
assert len_y.ndim == 1 # vector of seqs lengths
assert len_y.dtype == "int32"
return theano.Apply(self, [x, y, len_x, len_y], [T.ftensor3(),T.itensor3()])
def c_support_code(self):
src = ""
path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/C_Support_Code.cpp', 'r') as f:
src += f.read()
with open(path + '/Inv.cpp', 'r') as f:
src += f.read()
return src
def c_compile_args(self):
return ['-fopenmp']
def c_code(self, node, name, inp, out, sub):
x, y, len_x, len_y = inp
attention = out[0] # (N*S,B,T)
backtrace = out[1] # (N*S,B,T)
nstates = self.nstates
min_skip = self.min_skip
max_skip = self.max_skip
mode = self.mode
focus = self.focus
nil=self.nil
coverage=self.coverage
fail = sub['fail']
return """
Py_XDECREF(%(attention)s);
Py_XDECREF(%(backtrace)s);
npy_intp ydims[] = {PyArray_DIM(%(y)s,0) * %(nstates)s, PyArray_DIM(%(y)s,1), PyArray_DIM(%(x)s,0)};
//npy_intp ydims[] = {PyArray_DIM(%(x)s,2), PyArray_DIM(%(y)s,1), PyArray_DIM(%(x)s,0)};
%(attention)s = (PyArrayObject*) PyArray_Zeros(PyArray_NDIM(%(x)s), ydims, PyArray_DescrFromType(NPY_FLOAT32), 0);
%(backtrace)s = (PyArrayObject*) PyArray_Zeros(PyArray_NDIM(%(x)s), ydims, PyArray_DescrFromType(NPY_INT32), 0);
if (!%(attention)s)
%(fail)s;
{
ArrayF attentionWr(%(attention)s);
ArrayI backtraceWr(%(backtrace)s);
ArrayF xWr(%(x)s);
ArrayI yWr(%(y)s);
CArrayI len_xWr(%(len_x)s);
CArrayI len_yWr(%(len_y)s);
int numSeqs = len_xWr.dim(0);
#pragma omp parallel for
for(int i = 0; i < numSeqs; ++i)
{
Inv cls;
SArrayF attentionSWr(attentionWr, 1, i);
SArrayI backtraceSWr(backtraceWr, 1, i);
cls.viterbi_backtrace(CSArrayF(xWr, 1, i), CSArrayI(yWr, 1, i), len_xWr(i), len_yWr(i), %(nstates)s,
%(min_skip)s, %(max_skip)s, %(focus)s, %(nil)s, %(coverage)s, attentionSWr, backtraceSWr);
}
}
""" % locals()
class InvOpFull(theano.Op):
__props__ = ('min_skip', 'max_skip', 'nstates', 'focus', 'mode')
def __eq__(self, other):
return type(self) == type(other)
def __hash__(self):
return hash(type(self))
def __str__(self):
return self.__class__.__name__
def __init__(self, min_skip, max_skip, nstates, focus='last', mode='viterbi'):
self.min_skip = min_skip
self.max_skip = max_skip
self.focus = ['last', 'max'].index(focus)
self.nstates = nstates
self.mode = ['viterbi', 'full'].index(mode)
def make_node(self, x, y, len_x, len_y):
x = theano.tensor.as_tensor_variable(x)
assert x.ndim == 3 # tensor: nframes x nseqs x dim
y = theano.tensor.as_tensor_variable(y)
assert y.ndim == 2 # matrix: nseqs x max_labelling_length
len_x = theano.tensor.as_tensor_variable(len_x)
len_y = theano.tensor.as_tensor_variable(len_y)
assert len_x.ndim == 1 # vector of seqs lengths
assert len_x.dtype == "int32"
assert len_y.ndim == 1 # vector of seqs lengths
assert len_y.dtype == "int32"
return theano.Apply(self, [x, y, len_x, len_y], [T.ftensor3()])
def c_support_code(self):
src = ""
path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/C_Support_Code.cpp', 'r') as f:
src += f.read()
with open(path + '/Inv.cpp', 'r') as f:
src += f.read()
return src
def c_compile_args(self):
return ['-fopenmp']
def c_code(self, node, name, inp, out, sub):
x, y, len_x, len_y = inp
attention = out[0] # (N*S,B,T)
nstates = self.nstates
min_skip = self.min_skip
max_skip = self.max_skip
mode = self.mode
focus = self.focus
fail = sub['fail']
return """
Py_XDECREF(%(attention)s);
npy_intp ydims[] = {PyArray_DIM(%(y)s,0) * %(nstates)s, PyArray_DIM(%(y)s,1), PyArray_DIM(%(x)s,0)};
//npy_intp ydims[] = {PyArray_DIM(%(x)s,2), PyArray_DIM(%(y)s,1), PyArray_DIM(%(x)s,0)};
%(attention)s = (PyArrayObject*) PyArray_Zeros(PyArray_NDIM(%(x)s), ydims, PyArray_DescrFromType(NPY_FLOAT32), 0);
if (!%(attention)s)
%(fail)s;
{
ArrayF attentionWr(%(attention)s);
ArrayF xWr(%(x)s);
ArrayI yWr(%(y)s);
CArrayI len_xWr(%(len_x)s);
CArrayI len_yWr(%(len_y)s);
int numSeqs = len_xWr.dim(0);
#pragma omp parallel for
for(int i = 0; i < numSeqs; ++i)
{
Inv cls;
SArrayF attentionSWr(attentionWr, 1, i);
cls.full(CSArrayF(xWr, 1, i), CSArrayI(yWr, 1, i), len_xWr(i), len_yWr(i), %(nstates)s, %(min_skip)s, %(max_skip)s, %(focus)s, attentionSWr);
}
}
""" % locals()
class AlignOp(theano.Op):
def __eq__(self, other):
return type(self) == type(other)
def __hash__(self):
return hash(type(self))
def __str__(self):
return self.__class__.__name__
def make_node(self, x, y, len_x, len_y):
x = theano.tensor.as_tensor_variable(x)
assert x.ndim == 3 # tensor: nframes x nseqs x dim
y = theano.tensor.as_tensor_variable(y)
assert y.ndim == 2 # matrix: nseqs x max_labelling_length
len_x = theano.tensor.as_tensor_variable(len_x)
len_y = theano.tensor.as_tensor_variable(len_y)
assert len_x.ndim == 1 # vector of seqs lengths
assert len_x.dtype == "int32"
assert len_y.ndim == 1 # vector of seqs lengths
assert len_y.dtype == "int32"
return theano.Apply(self, [x, y, len_x, len_y], [T.ftensor3()])
def c_support_code(self):
src = ""
path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/C_Support_Code.cpp', 'r') as f:
src += f.read()
with open(path + '/Inv.cpp', 'r') as f:
src += f.read()
return src
def c_compile_args(self):
return ['-fopenmp']
class InvAlignOp(AlignOp):
__props__ = ('min_skip', 'max_skip', 'nstates', 'focus', 'nil', 'mode')
def c_code(self, node, name, inp, out, sub):
x, y, len_x, len_y = inp
attention = out[0] # (N*S,B,T)
nstates = self.nstates
min_skip = self.min_skip
max_skip = self.max_skip
mode = self.mode
focus = self.focus
fail = sub['fail']
return """
Py_XDECREF(%(attention)s);
int T = 1;
if(%(mode)s == 1)
T = PyArray_DIM(%(x)s, 0);
npy_intp ydims[] = {PyArray_DIM(%(y)s,0) * %(nstates)s, PyArray_DIM(%(y)s,1), T};
%(attention)s = (PyArrayObject*) PyArray_Zeros(PyArray_NDIM(%(x)s), ydims, PyArray_DescrFromType(NPY_FLOAT32), 0);
if (!%(attention)s)
%(fail)s;
{
ArrayF attentionWr(%(attention)s);
ArrayF xWr(%(x)s);
ArrayI yWr(%(y)s);
CArrayI len_xWr(%(len_x)s);
CArrayI len_yWr(%(len_y)s);
int numSeqs = len_xWr.dim(0);
#pragma omp parallel for
for(int i = 0; i < numSeqs; ++i)
{
InvAlign cls;
SArrayF attentionSWr(attentionWr, 1, i);
if(%(mode)s == 0)
cls.viterbi(CSArrayF(xWr, 1, i), CSArrayI(yWr, 1, i), len_xWr(i), len_yWr(i), %(nstates)s, %(min_skip)s, %(max_skip)s, %(focus)s, attentionSWr);
else
cls.full(CSArrayF(xWr, 1, i), CSArrayI(yWr, 1, i), len_xWr(i), len_yWr(i), %(nstates)s, %(min_skip)s, %(max_skip)s, %(focus)s, attentionSWr);
}
}
""" % locals()
class StdOpFull(theano.Op):
__props__ = ('skip_tdp', 'nstates')
def __eq__(self, other):
return type(self) == type(other)
def __hash__(self):
return hash(type(self))
def __str__(self):
return self.__class__.__name__
def __init__(self, skip_tdp, nstates):
self.nstates = nstates
self.skip_tdp = skip_tdp
def make_node(self, x, y, len_x, len_y):
x = theano.tensor.as_tensor_variable(x)
assert x.ndim == 3 # tensor: nframes x nseqs x dim
y = theano.tensor.as_tensor_variable(y)
assert y.ndim == 2 # matrix: nseqs x max_labelling_length
len_x = theano.tensor.as_tensor_variable(len_x)
len_y = theano.tensor.as_tensor_variable(len_y)
assert len_x.ndim == 1 # vector of seqs lengths
assert len_x.dtype == "int32"
assert len_y.ndim == 1 # vector of seqs lengths
assert len_y.dtype == "int32"
return theano.Apply(self, [x, y, len_x, len_y], [T.ftensor3()])
def c_support_code(self):
src = ""
path = os.path.dirname(os.path.abspath(__file__))
with open(path + '/C_Support_Code.cpp', 'r') as f:
src += f.read()
with open(path + '/Inv.cpp', 'r') as f:
src += f.read()
return src
def c_compile_args(self):
return ['-fopenmp']
def c_code(self, node, name, inp, out, sub):
x, y, len_x, len_y = inp
alignment = out[0]
nstates = self.nstates
skip_tdp = self.skip_tdp
fail = sub['fail']
return """
Py_XDECREF(%(attention)s);
npy_intp xdims[] = {PyArray_DIM(%(x)s,0), PyArray_DIM(%(x)s,1), PyArray_DIM(%(y)s,0) * %(nstates)s};
%(alignment)s = (PyArrayObject*) PyArray_Zeros(PyArray_NDIM(%(x)s), ydims, PyArray_DescrFromType(NPY_FLOAT32), 0);
if (!%(alignment)s)
%(fail)s;
{
ArrayF alignmentWr(%(alignment)s);
ArrayF xWr(%(x)s);
ArrayI yWr(%(y)s);
CArrayI len_xWr(%(len_x)s);
CArrayI len_yWr(%(len_y)s);
int numSeqs = len_xWr.dim(0);
#pragma omp parallel for
for(int i = 0; i < numSeqs; ++i)
{
Std cls;
SArrayF alignmentSWr(alignmentWr, 1, i);
cls.full(CSArrayF(xWr, 1, i), CSArrayI(yWr, 1, i), len_xWr(i), len_yWr(i), %(nstates)s, %(skip_tdp)s, alignmentSWr);
}
}
""" % locals()