ORI-Muchim commited on
Commit
57b1314
1 Parent(s): 785c297

Upload 2 files

Browse files
monotonic_align/__init__.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy import zeros, int32, float32
2
+ from torch import from_numpy
3
+
4
+ from .core import maximum_path_jit
5
+
6
+
7
+ def maximum_path(neg_cent, mask):
8
+ """ numba optimized version.
9
+ neg_cent: [b, t_t, t_s]
10
+ mask: [b, t_t, t_s]
11
+ """
12
+ device = neg_cent.device
13
+ dtype = neg_cent.dtype
14
+ neg_cent = neg_cent.data.cpu().numpy().astype(float32)
15
+ path = zeros(neg_cent.shape, dtype=int32)
16
+
17
+ t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(int32)
18
+ t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(int32)
19
+ maximum_path_jit(path, neg_cent, t_t_max, t_s_max)
20
+ return from_numpy(path).to(device=device, dtype=dtype)
21
+
monotonic_align/core.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numba
2
+
3
+
4
+ @numba.jit(numba.void(numba.int32[:, :, ::1], numba.float32[:, :, ::1], numba.int32[::1], numba.int32[::1]),
5
+ nopython=True, nogil=True)
6
+ def maximum_path_jit(paths, values, t_ys, t_xs):
7
+ b = paths.shape[0]
8
+ max_neg_val = -1e9
9
+ for i in range(int(b)):
10
+ path = paths[i]
11
+ value = values[i]
12
+ t_y = t_ys[i]
13
+ t_x = t_xs[i]
14
+
15
+ v_prev = v_cur = 0.0
16
+ index = t_x - 1
17
+
18
+ for y in range(t_y):
19
+ for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
20
+ if x == y:
21
+ v_cur = max_neg_val
22
+ else:
23
+ v_cur = value[y - 1, x]
24
+ if x == 0:
25
+ if y == 0:
26
+ v_prev = 0.
27
+ else:
28
+ v_prev = max_neg_val
29
+ else:
30
+ v_prev = value[y - 1, x - 1]
31
+ value[y, x] += max(v_prev, v_cur)
32
+
33
+ for y in range(t_y - 1, -1, -1):
34
+ path[y, index] = 1
35
+ if index != 0 and (index == y or value[y - 1, index] < value[y - 1, index - 1]):
36
+ index = index - 1