Mahiruoshi commited on
Commit
0b47210
·
1 Parent(s): a6fc341

Upload 14 files

Browse files
monotonic_align/__init__.py CHANGED
@@ -1,19 +1,19 @@
1
- import numpy as np
2
- import torch
3
- from .monotonic_align.core import maximum_path_c
4
 
 
5
 
6
  def maximum_path(neg_cent, mask):
7
- """ Cython optimized version.
8
  neg_cent: [b, t_t, t_s]
9
  mask: [b, t_t, t_s]
10
  """
11
  device = neg_cent.device
12
  dtype = neg_cent.dtype
13
- neg_cent = neg_cent.data.cpu().numpy().astype(np.float32)
14
- path = np.zeros(neg_cent.shape, dtype=np.int32)
15
 
16
- t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(np.int32)
17
- t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(np.int32)
18
- maximum_path_c(path, neg_cent, t_t_max, t_s_max)
19
- return torch.from_numpy(path).to(device=device, dtype=dtype)
 
1
+ from numpy import zeros, int32, float32
2
+ from torch import from_numpy
 
3
 
4
+ from .core import maximum_path_jit
5
 
6
  def maximum_path(neg_cent, mask):
7
+ """ numba optimized version.
8
  neg_cent: [b, t_t, t_s]
9
  mask: [b, t_t, t_s]
10
  """
11
  device = neg_cent.device
12
  dtype = neg_cent.dtype
13
+ neg_cent = neg_cent.data.cpu().numpy().astype(float32)
14
+ path = zeros(neg_cent.shape, dtype=int32)
15
 
16
+ t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(int32)
17
+ t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(int32)
18
+ maximum_path_jit(path, neg_cent, t_t_max, t_s_max)
19
+ return from_numpy(path).to(device=device, dtype=dtype)
monotonic_align/__pycache__/__init__.cpython-37.pyc ADDED
Binary file (822 Bytes). View file
 
monotonic_align/__pycache__/__init__.cpython-38.pyc CHANGED
Binary files a/monotonic_align/__pycache__/__init__.cpython-38.pyc and b/monotonic_align/__pycache__/__init__.cpython-38.pyc differ
 
monotonic_align/__pycache__/core.cpython-37.pyc ADDED
Binary file (995 Bytes). View file
 
monotonic_align/__pycache__/core.cpython-38.pyc ADDED
Binary file (968 Bytes). View file
 
monotonic_align/build/lib.win-amd64-3.8/monotonic_align/core.cp38-win_amd64.pyd CHANGED
Binary files a/monotonic_align/build/lib.win-amd64-3.8/monotonic_align/core.cp38-win_amd64.pyd and b/monotonic_align/build/lib.win-amd64-3.8/monotonic_align/core.cp38-win_amd64.pyd differ
 
monotonic_align/build/temp.win-amd64-3.8/Release/core.cp38-win_amd64.exp CHANGED
Binary files a/monotonic_align/build/temp.win-amd64-3.8/Release/core.cp38-win_amd64.exp and b/monotonic_align/build/temp.win-amd64-3.8/Release/core.cp38-win_amd64.exp differ
 
monotonic_align/build/temp.win-amd64-3.8/Release/core.obj CHANGED
Binary files a/monotonic_align/build/temp.win-amd64-3.8/Release/core.obj and b/monotonic_align/build/temp.win-amd64-3.8/Release/core.obj differ
 
monotonic_align/core.c CHANGED
@@ -21,7 +21,7 @@ END: Cython Metadata */
21
  #else
22
  #define CYTHON_ABI "0_29_21"
23
  #define CYTHON_HEX_VERSION 0x001D15F0
24
- #define CYTHON_FUTURE_DIVISION 0
25
  #include <stddef.h>
26
  #ifndef offsetof
27
  #define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
@@ -949,7 +949,7 @@ struct __pyx_memoryview_obj;
949
  struct __pyx_memoryviewslice_obj;
950
  struct __pyx_opt_args_15monotonic_align_4core_maximum_path_each;
951
 
952
- /* "monotonic_align/core.pyx":7
953
  * @cython.boundscheck(False)
954
  * @cython.wraparound(False)
955
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
@@ -2031,7 +2031,7 @@ static PyObject *__pyx_tuple__25;
2031
  static PyObject *__pyx_codeobj__26;
2032
  /* Late includes */
2033
 
2034
- /* "monotonic_align/core.pyx":7
2035
  * @cython.boundscheck(False)
2036
  * @cython.wraparound(False)
2037
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
@@ -2068,7 +2068,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2068
  }
2069
  }
2070
 
2071
- /* "monotonic_align/core.pyx":13
2072
  * cdef float v_cur
2073
  * cdef float tmp
2074
  * cdef int index = t_x - 1 # <<<<<<<<<<<<<<
@@ -2077,7 +2077,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2077
  */
2078
  __pyx_v_index = (__pyx_v_t_x - 1);
2079
 
2080
- /* "monotonic_align/core.pyx":15
2081
  * cdef int index = t_x - 1
2082
  *
2083
  * for y in range(t_y): # <<<<<<<<<<<<<<
@@ -2089,7 +2089,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2089
  for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
2090
  __pyx_v_y = __pyx_t_3;
2091
 
2092
- /* "monotonic_align/core.pyx":16
2093
  *
2094
  * for y in range(t_y):
2095
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)): # <<<<<<<<<<<<<<
@@ -2115,7 +2115,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2115
  for (__pyx_t_5 = __pyx_t_7; __pyx_t_5 < __pyx_t_6; __pyx_t_5+=1) {
2116
  __pyx_v_x = __pyx_t_5;
2117
 
2118
- /* "monotonic_align/core.pyx":17
2119
  * for y in range(t_y):
2120
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
2121
  * if x == y: # <<<<<<<<<<<<<<
@@ -2125,7 +2125,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2125
  __pyx_t_8 = ((__pyx_v_x == __pyx_v_y) != 0);
2126
  if (__pyx_t_8) {
2127
 
2128
- /* "monotonic_align/core.pyx":18
2129
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
2130
  * if x == y:
2131
  * v_cur = max_neg_val # <<<<<<<<<<<<<<
@@ -2134,7 +2134,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2134
  */
2135
  __pyx_v_v_cur = __pyx_v_max_neg_val;
2136
 
2137
- /* "monotonic_align/core.pyx":17
2138
  * for y in range(t_y):
2139
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
2140
  * if x == y: # <<<<<<<<<<<<<<
@@ -2144,7 +2144,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2144
  goto __pyx_L7;
2145
  }
2146
 
2147
- /* "monotonic_align/core.pyx":20
2148
  * v_cur = max_neg_val
2149
  * else:
2150
  * v_cur = value[y-1, x] # <<<<<<<<<<<<<<
@@ -2158,7 +2158,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2158
  }
2159
  __pyx_L7:;
2160
 
2161
- /* "monotonic_align/core.pyx":21
2162
  * else:
2163
  * v_cur = value[y-1, x]
2164
  * if x == 0: # <<<<<<<<<<<<<<
@@ -2168,7 +2168,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2168
  __pyx_t_8 = ((__pyx_v_x == 0) != 0);
2169
  if (__pyx_t_8) {
2170
 
2171
- /* "monotonic_align/core.pyx":22
2172
  * v_cur = value[y-1, x]
2173
  * if x == 0:
2174
  * if y == 0: # <<<<<<<<<<<<<<
@@ -2178,7 +2178,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2178
  __pyx_t_8 = ((__pyx_v_y == 0) != 0);
2179
  if (__pyx_t_8) {
2180
 
2181
- /* "monotonic_align/core.pyx":23
2182
  * if x == 0:
2183
  * if y == 0:
2184
  * v_prev = 0. # <<<<<<<<<<<<<<
@@ -2187,7 +2187,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2187
  */
2188
  __pyx_v_v_prev = 0.;
2189
 
2190
- /* "monotonic_align/core.pyx":22
2191
  * v_cur = value[y-1, x]
2192
  * if x == 0:
2193
  * if y == 0: # <<<<<<<<<<<<<<
@@ -2197,7 +2197,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2197
  goto __pyx_L9;
2198
  }
2199
 
2200
- /* "monotonic_align/core.pyx":25
2201
  * v_prev = 0.
2202
  * else:
2203
  * v_prev = max_neg_val # <<<<<<<<<<<<<<
@@ -2209,7 +2209,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2209
  }
2210
  __pyx_L9:;
2211
 
2212
- /* "monotonic_align/core.pyx":21
2213
  * else:
2214
  * v_cur = value[y-1, x]
2215
  * if x == 0: # <<<<<<<<<<<<<<
@@ -2219,7 +2219,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2219
  goto __pyx_L8;
2220
  }
2221
 
2222
- /* "monotonic_align/core.pyx":27
2223
  * v_prev = max_neg_val
2224
  * else:
2225
  * v_prev = value[y-1, x-1] # <<<<<<<<<<<<<<
@@ -2233,7 +2233,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2233
  }
2234
  __pyx_L8:;
2235
 
2236
- /* "monotonic_align/core.pyx":28
2237
  * else:
2238
  * v_prev = value[y-1, x-1]
2239
  * value[y, x] += max(v_prev, v_cur) # <<<<<<<<<<<<<<
@@ -2253,7 +2253,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2253
  }
2254
  }
2255
 
2256
- /* "monotonic_align/core.pyx":30
2257
  * value[y, x] += max(v_prev, v_cur)
2258
  *
2259
  * for y in range(t_y - 1, -1, -1): # <<<<<<<<<<<<<<
@@ -2263,7 +2263,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2263
  for (__pyx_t_1 = (__pyx_v_t_y - 1); __pyx_t_1 > -1; __pyx_t_1-=1) {
2264
  __pyx_v_y = __pyx_t_1;
2265
 
2266
- /* "monotonic_align/core.pyx":31
2267
  *
2268
  * for y in range(t_y - 1, -1, -1):
2269
  * path[y, index] = 1 # <<<<<<<<<<<<<<
@@ -2274,7 +2274,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2274
  __pyx_t_9 = __pyx_v_index;
2275
  *((int *) ( /* dim=1 */ ((char *) (((int *) ( /* dim=0 */ (__pyx_v_path.data + __pyx_t_10 * __pyx_v_path.strides[0]) )) + __pyx_t_9)) )) = 1;
2276
 
2277
- /* "monotonic_align/core.pyx":32
2278
  * for y in range(t_y - 1, -1, -1):
2279
  * path[y, index] = 1
2280
  * if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]): # <<<<<<<<<<<<<<
@@ -2302,7 +2302,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2302
  __pyx_L13_bool_binop_done:;
2303
  if (__pyx_t_8) {
2304
 
2305
- /* "monotonic_align/core.pyx":33
2306
  * path[y, index] = 1
2307
  * if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]):
2308
  * index = index - 1 # <<<<<<<<<<<<<<
@@ -2311,7 +2311,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2311
  */
2312
  __pyx_v_index = (__pyx_v_index - 1);
2313
 
2314
- /* "monotonic_align/core.pyx":32
2315
  * for y in range(t_y - 1, -1, -1):
2316
  * path[y, index] = 1
2317
  * if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]): # <<<<<<<<<<<<<<
@@ -2321,7 +2321,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2321
  }
2322
  }
2323
 
2324
- /* "monotonic_align/core.pyx":7
2325
  * @cython.boundscheck(False)
2326
  * @cython.wraparound(False)
2327
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
@@ -2332,7 +2332,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_each(__Pyx_memviewslice
2332
  /* function exit code */
2333
  }
2334
 
2335
- /* "monotonic_align/core.pyx":38
2336
  * @cython.boundscheck(False)
2337
  * @cython.wraparound(False)
2338
  * cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil: # <<<<<<<<<<<<<<
@@ -2352,7 +2352,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_c(__Pyx_memviewslice __
2352
  Py_ssize_t __pyx_t_6;
2353
  Py_ssize_t __pyx_t_7;
2354
 
2355
- /* "monotonic_align/core.pyx":39
2356
  * @cython.wraparound(False)
2357
  * cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil:
2358
  * cdef int b = paths.shape[0] # <<<<<<<<<<<<<<
@@ -2361,7 +2361,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_c(__Pyx_memviewslice __
2361
  */
2362
  __pyx_v_b = (__pyx_v_paths.shape[0]);
2363
 
2364
- /* "monotonic_align/core.pyx":41
2365
  * cdef int b = paths.shape[0]
2366
  * cdef int i
2367
  * for i in prange(b, nogil=True): # <<<<<<<<<<<<<<
@@ -2397,7 +2397,7 @@ static void __pyx_f_15monotonic_align_4core_maximum_path_c(__Pyx_memviewslice __
2397
  {
2398
  __pyx_v_i = (int)(0 + 1 * __pyx_t_2);
2399
 
2400
- /* "monotonic_align/core.pyx":42
2401
  * cdef int i
2402
  * for i in prange(b, nogil=True):
2403
  * maximum_path_each(paths[i], values[i], t_ys[i], t_xs[i]) # <<<<<<<<<<<<<<
@@ -2458,7 +2458,7 @@ __pyx_t_6 = __pyx_v_i;
2458
  #endif
2459
  }
2460
 
2461
- /* "monotonic_align/core.pyx":41
2462
  * cdef int b = paths.shape[0]
2463
  * cdef int i
2464
  * for i in prange(b, nogil=True): # <<<<<<<<<<<<<<
@@ -2476,7 +2476,7 @@ __pyx_t_6 = __pyx_v_i;
2476
  }
2477
  }
2478
 
2479
- /* "monotonic_align/core.pyx":38
2480
  * @cython.boundscheck(False)
2481
  * @cython.wraparound(False)
2482
  * cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil: # <<<<<<<<<<<<<<
@@ -2527,23 +2527,23 @@ static PyObject *__pyx_pw_15monotonic_align_4core_1maximum_path_c(PyObject *__py
2527
  case 1:
2528
  if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_values)) != 0)) kw_args--;
2529
  else {
2530
- __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, 1); __PYX_ERR(0, 38, __pyx_L3_error)
2531
  }
2532
  CYTHON_FALLTHROUGH;
2533
  case 2:
2534
  if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_t_ys)) != 0)) kw_args--;
2535
  else {
2536
- __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, 2); __PYX_ERR(0, 38, __pyx_L3_error)
2537
  }
2538
  CYTHON_FALLTHROUGH;
2539
  case 3:
2540
  if (likely((values[3] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_t_xs)) != 0)) kw_args--;
2541
  else {
2542
- __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, 3); __PYX_ERR(0, 38, __pyx_L3_error)
2543
  }
2544
  }
2545
  if (unlikely(kw_args > 0)) {
2546
- if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "maximum_path_c") < 0)) __PYX_ERR(0, 38, __pyx_L3_error)
2547
  }
2548
  } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
2549
  goto __pyx_L5_argtuple_error;
@@ -2553,14 +2553,14 @@ static PyObject *__pyx_pw_15monotonic_align_4core_1maximum_path_c(PyObject *__py
2553
  values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
2554
  values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
2555
  }
2556
- __pyx_v_paths = __Pyx_PyObject_to_MemoryviewSlice_d_d_dc_int(values[0], PyBUF_WRITABLE); if (unlikely(!__pyx_v_paths.memview)) __PYX_ERR(0, 38, __pyx_L3_error)
2557
- __pyx_v_values = __Pyx_PyObject_to_MemoryviewSlice_d_d_dc_float(values[1], PyBUF_WRITABLE); if (unlikely(!__pyx_v_values.memview)) __PYX_ERR(0, 38, __pyx_L3_error)
2558
- __pyx_v_t_ys = __Pyx_PyObject_to_MemoryviewSlice_dc_int(values[2], PyBUF_WRITABLE); if (unlikely(!__pyx_v_t_ys.memview)) __PYX_ERR(0, 38, __pyx_L3_error)
2559
- __pyx_v_t_xs = __Pyx_PyObject_to_MemoryviewSlice_dc_int(values[3], PyBUF_WRITABLE); if (unlikely(!__pyx_v_t_xs.memview)) __PYX_ERR(0, 38, __pyx_L3_error)
2560
  }
2561
  goto __pyx_L4_argument_unpacking_done;
2562
  __pyx_L5_argtuple_error:;
2563
- __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 38, __pyx_L3_error)
2564
  __pyx_L3_error:;
2565
  __Pyx_AddTraceback("monotonic_align.core.maximum_path_c", __pyx_clineno, __pyx_lineno, __pyx_filename);
2566
  __Pyx_RefNannyFinishContext();
@@ -2582,11 +2582,11 @@ static PyObject *__pyx_pf_15monotonic_align_4core_maximum_path_c(CYTHON_UNUSED P
2582
  int __pyx_clineno = 0;
2583
  __Pyx_RefNannySetupContext("maximum_path_c", 0);
2584
  __Pyx_XDECREF(__pyx_r);
2585
- if (unlikely(!__pyx_v_paths.memview)) { __Pyx_RaiseUnboundLocalError("paths"); __PYX_ERR(0, 38, __pyx_L1_error) }
2586
- if (unlikely(!__pyx_v_values.memview)) { __Pyx_RaiseUnboundLocalError("values"); __PYX_ERR(0, 38, __pyx_L1_error) }
2587
- if (unlikely(!__pyx_v_t_ys.memview)) { __Pyx_RaiseUnboundLocalError("t_ys"); __PYX_ERR(0, 38, __pyx_L1_error) }
2588
- if (unlikely(!__pyx_v_t_xs.memview)) { __Pyx_RaiseUnboundLocalError("t_xs"); __PYX_ERR(0, 38, __pyx_L1_error) }
2589
- __pyx_t_1 = __Pyx_void_to_None(__pyx_f_15monotonic_align_4core_maximum_path_c(__pyx_v_paths, __pyx_v_values, __pyx_v_t_ys, __pyx_v_t_xs, 0)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 38, __pyx_L1_error)
2590
  __Pyx_GOTREF(__pyx_t_1);
2591
  __pyx_r = __pyx_t_1;
2592
  __pyx_t_1 = 0;
@@ -16480,7 +16480,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = {
16480
  {0, 0, 0, 0, 0, 0, 0}
16481
  };
16482
  static CYTHON_SMALL_CODE int __Pyx_InitCachedBuiltins(void) {
16483
- __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) __PYX_ERR(0, 15, __pyx_L1_error)
16484
  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) __PYX_ERR(1, 133, __pyx_L1_error)
16485
  __pyx_builtin_MemoryError = __Pyx_GetBuiltinName(__pyx_n_s_MemoryError); if (!__pyx_builtin_MemoryError) __PYX_ERR(1, 148, __pyx_L1_error)
16486
  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) __PYX_ERR(1, 151, __pyx_L1_error)
@@ -17107,7 +17107,7 @@ if (!__Pyx_RefNanny) {
17107
  if (__Pyx_patch_abc() < 0) __PYX_ERR(0, 1, __pyx_L1_error)
17108
  #endif
17109
 
17110
- /* "monotonic_align/core.pyx":7
17111
  * @cython.boundscheck(False)
17112
  * @cython.wraparound(False)
17113
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
@@ -17117,9 +17117,9 @@ if (!__Pyx_RefNanny) {
17117
  __pyx_k_ = (-1e9);
17118
 
17119
  /* "monotonic_align/core.pyx":1
17120
- * cimport cython # <<<<<<<<<<<<<<
17121
- * from cython.parallel import prange
17122
  *
 
17123
  */
17124
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
17125
  __Pyx_GOTREF(__pyx_t_1);
 
21
  #else
22
  #define CYTHON_ABI "0_29_21"
23
  #define CYTHON_HEX_VERSION 0x001D15F0
24
+ #define CYTHON_FUTURE_DIVISION 1
25
  #include <stddef.h>
26
  #ifndef offsetof
27
  #define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
 
949
  struct __pyx_memoryviewslice_obj;
950
  struct __pyx_opt_args_15monotonic_align_4core_maximum_path_each;
951
 
952
+ /* "monotonic_align/core.pyx":9
953
  * @cython.boundscheck(False)
954
  * @cython.wraparound(False)
955
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
 
2031
  static PyObject *__pyx_codeobj__26;
2032
  /* Late includes */
2033
 
2034
+ /* "monotonic_align/core.pyx":9
2035
  * @cython.boundscheck(False)
2036
  * @cython.wraparound(False)
2037
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
 
2068
  }
2069
  }
2070
 
2071
+ /* "monotonic_align/core.pyx":15
2072
  * cdef float v_cur
2073
  * cdef float tmp
2074
  * cdef int index = t_x - 1 # <<<<<<<<<<<<<<
 
2077
  */
2078
  __pyx_v_index = (__pyx_v_t_x - 1);
2079
 
2080
+ /* "monotonic_align/core.pyx":17
2081
  * cdef int index = t_x - 1
2082
  *
2083
  * for y in range(t_y): # <<<<<<<<<<<<<<
 
2089
  for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
2090
  __pyx_v_y = __pyx_t_3;
2091
 
2092
+ /* "monotonic_align/core.pyx":18
2093
  *
2094
  * for y in range(t_y):
2095
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)): # <<<<<<<<<<<<<<
 
2115
  for (__pyx_t_5 = __pyx_t_7; __pyx_t_5 < __pyx_t_6; __pyx_t_5+=1) {
2116
  __pyx_v_x = __pyx_t_5;
2117
 
2118
+ /* "monotonic_align/core.pyx":19
2119
  * for y in range(t_y):
2120
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
2121
  * if x == y: # <<<<<<<<<<<<<<
 
2125
  __pyx_t_8 = ((__pyx_v_x == __pyx_v_y) != 0);
2126
  if (__pyx_t_8) {
2127
 
2128
+ /* "monotonic_align/core.pyx":20
2129
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
2130
  * if x == y:
2131
  * v_cur = max_neg_val # <<<<<<<<<<<<<<
 
2134
  */
2135
  __pyx_v_v_cur = __pyx_v_max_neg_val;
2136
 
2137
+ /* "monotonic_align/core.pyx":19
2138
  * for y in range(t_y):
2139
  * for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
2140
  * if x == y: # <<<<<<<<<<<<<<
 
2144
  goto __pyx_L7;
2145
  }
2146
 
2147
+ /* "monotonic_align/core.pyx":22
2148
  * v_cur = max_neg_val
2149
  * else:
2150
  * v_cur = value[y-1, x] # <<<<<<<<<<<<<<
 
2158
  }
2159
  __pyx_L7:;
2160
 
2161
+ /* "monotonic_align/core.pyx":23
2162
  * else:
2163
  * v_cur = value[y-1, x]
2164
  * if x == 0: # <<<<<<<<<<<<<<
 
2168
  __pyx_t_8 = ((__pyx_v_x == 0) != 0);
2169
  if (__pyx_t_8) {
2170
 
2171
+ /* "monotonic_align/core.pyx":24
2172
  * v_cur = value[y-1, x]
2173
  * if x == 0:
2174
  * if y == 0: # <<<<<<<<<<<<<<
 
2178
  __pyx_t_8 = ((__pyx_v_y == 0) != 0);
2179
  if (__pyx_t_8) {
2180
 
2181
+ /* "monotonic_align/core.pyx":25
2182
  * if x == 0:
2183
  * if y == 0:
2184
  * v_prev = 0. # <<<<<<<<<<<<<<
 
2187
  */
2188
  __pyx_v_v_prev = 0.;
2189
 
2190
+ /* "monotonic_align/core.pyx":24
2191
  * v_cur = value[y-1, x]
2192
  * if x == 0:
2193
  * if y == 0: # <<<<<<<<<<<<<<
 
2197
  goto __pyx_L9;
2198
  }
2199
 
2200
+ /* "monotonic_align/core.pyx":27
2201
  * v_prev = 0.
2202
  * else:
2203
  * v_prev = max_neg_val # <<<<<<<<<<<<<<
 
2209
  }
2210
  __pyx_L9:;
2211
 
2212
+ /* "monotonic_align/core.pyx":23
2213
  * else:
2214
  * v_cur = value[y-1, x]
2215
  * if x == 0: # <<<<<<<<<<<<<<
 
2219
  goto __pyx_L8;
2220
  }
2221
 
2222
+ /* "monotonic_align/core.pyx":29
2223
  * v_prev = max_neg_val
2224
  * else:
2225
  * v_prev = value[y-1, x-1] # <<<<<<<<<<<<<<
 
2233
  }
2234
  __pyx_L8:;
2235
 
2236
+ /* "monotonic_align/core.pyx":30
2237
  * else:
2238
  * v_prev = value[y-1, x-1]
2239
  * value[y, x] += max(v_prev, v_cur) # <<<<<<<<<<<<<<
 
2253
  }
2254
  }
2255
 
2256
+ /* "monotonic_align/core.pyx":32
2257
  * value[y, x] += max(v_prev, v_cur)
2258
  *
2259
  * for y in range(t_y - 1, -1, -1): # <<<<<<<<<<<<<<
 
2263
  for (__pyx_t_1 = (__pyx_v_t_y - 1); __pyx_t_1 > -1; __pyx_t_1-=1) {
2264
  __pyx_v_y = __pyx_t_1;
2265
 
2266
+ /* "monotonic_align/core.pyx":33
2267
  *
2268
  * for y in range(t_y - 1, -1, -1):
2269
  * path[y, index] = 1 # <<<<<<<<<<<<<<
 
2274
  __pyx_t_9 = __pyx_v_index;
2275
  *((int *) ( /* dim=1 */ ((char *) (((int *) ( /* dim=0 */ (__pyx_v_path.data + __pyx_t_10 * __pyx_v_path.strides[0]) )) + __pyx_t_9)) )) = 1;
2276
 
2277
+ /* "monotonic_align/core.pyx":34
2278
  * for y in range(t_y - 1, -1, -1):
2279
  * path[y, index] = 1
2280
  * if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]): # <<<<<<<<<<<<<<
 
2302
  __pyx_L13_bool_binop_done:;
2303
  if (__pyx_t_8) {
2304
 
2305
+ /* "monotonic_align/core.pyx":35
2306
  * path[y, index] = 1
2307
  * if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]):
2308
  * index = index - 1 # <<<<<<<<<<<<<<
 
2311
  */
2312
  __pyx_v_index = (__pyx_v_index - 1);
2313
 
2314
+ /* "monotonic_align/core.pyx":34
2315
  * for y in range(t_y - 1, -1, -1):
2316
  * path[y, index] = 1
2317
  * if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]): # <<<<<<<<<<<<<<
 
2321
  }
2322
  }
2323
 
2324
+ /* "monotonic_align/core.pyx":9
2325
  * @cython.boundscheck(False)
2326
  * @cython.wraparound(False)
2327
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
 
2332
  /* function exit code */
2333
  }
2334
 
2335
+ /* "monotonic_align/core.pyx":40
2336
  * @cython.boundscheck(False)
2337
  * @cython.wraparound(False)
2338
  * cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil: # <<<<<<<<<<<<<<
 
2352
  Py_ssize_t __pyx_t_6;
2353
  Py_ssize_t __pyx_t_7;
2354
 
2355
+ /* "monotonic_align/core.pyx":41
2356
  * @cython.wraparound(False)
2357
  * cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil:
2358
  * cdef int b = paths.shape[0] # <<<<<<<<<<<<<<
 
2361
  */
2362
  __pyx_v_b = (__pyx_v_paths.shape[0]);
2363
 
2364
+ /* "monotonic_align/core.pyx":43
2365
  * cdef int b = paths.shape[0]
2366
  * cdef int i
2367
  * for i in prange(b, nogil=True): # <<<<<<<<<<<<<<
 
2397
  {
2398
  __pyx_v_i = (int)(0 + 1 * __pyx_t_2);
2399
 
2400
+ /* "monotonic_align/core.pyx":44
2401
  * cdef int i
2402
  * for i in prange(b, nogil=True):
2403
  * maximum_path_each(paths[i], values[i], t_ys[i], t_xs[i]) # <<<<<<<<<<<<<<
 
2458
  #endif
2459
  }
2460
 
2461
+ /* "monotonic_align/core.pyx":43
2462
  * cdef int b = paths.shape[0]
2463
  * cdef int i
2464
  * for i in prange(b, nogil=True): # <<<<<<<<<<<<<<
 
2476
  }
2477
  }
2478
 
2479
+ /* "monotonic_align/core.pyx":40
2480
  * @cython.boundscheck(False)
2481
  * @cython.wraparound(False)
2482
  * cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_ys, int[::1] t_xs) nogil: # <<<<<<<<<<<<<<
 
2527
  case 1:
2528
  if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_values)) != 0)) kw_args--;
2529
  else {
2530
+ __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, 1); __PYX_ERR(0, 40, __pyx_L3_error)
2531
  }
2532
  CYTHON_FALLTHROUGH;
2533
  case 2:
2534
  if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_t_ys)) != 0)) kw_args--;
2535
  else {
2536
+ __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, 2); __PYX_ERR(0, 40, __pyx_L3_error)
2537
  }
2538
  CYTHON_FALLTHROUGH;
2539
  case 3:
2540
  if (likely((values[3] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_t_xs)) != 0)) kw_args--;
2541
  else {
2542
+ __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, 3); __PYX_ERR(0, 40, __pyx_L3_error)
2543
  }
2544
  }
2545
  if (unlikely(kw_args > 0)) {
2546
+ if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "maximum_path_c") < 0)) __PYX_ERR(0, 40, __pyx_L3_error)
2547
  }
2548
  } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
2549
  goto __pyx_L5_argtuple_error;
 
2553
  values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
2554
  values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
2555
  }
2556
+ __pyx_v_paths = __Pyx_PyObject_to_MemoryviewSlice_d_d_dc_int(values[0], PyBUF_WRITABLE); if (unlikely(!__pyx_v_paths.memview)) __PYX_ERR(0, 40, __pyx_L3_error)
2557
+ __pyx_v_values = __Pyx_PyObject_to_MemoryviewSlice_d_d_dc_float(values[1], PyBUF_WRITABLE); if (unlikely(!__pyx_v_values.memview)) __PYX_ERR(0, 40, __pyx_L3_error)
2558
+ __pyx_v_t_ys = __Pyx_PyObject_to_MemoryviewSlice_dc_int(values[2], PyBUF_WRITABLE); if (unlikely(!__pyx_v_t_ys.memview)) __PYX_ERR(0, 40, __pyx_L3_error)
2559
+ __pyx_v_t_xs = __Pyx_PyObject_to_MemoryviewSlice_dc_int(values[3], PyBUF_WRITABLE); if (unlikely(!__pyx_v_t_xs.memview)) __PYX_ERR(0, 40, __pyx_L3_error)
2560
  }
2561
  goto __pyx_L4_argument_unpacking_done;
2562
  __pyx_L5_argtuple_error:;
2563
+ __Pyx_RaiseArgtupleInvalid("maximum_path_c", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 40, __pyx_L3_error)
2564
  __pyx_L3_error:;
2565
  __Pyx_AddTraceback("monotonic_align.core.maximum_path_c", __pyx_clineno, __pyx_lineno, __pyx_filename);
2566
  __Pyx_RefNannyFinishContext();
 
2582
  int __pyx_clineno = 0;
2583
  __Pyx_RefNannySetupContext("maximum_path_c", 0);
2584
  __Pyx_XDECREF(__pyx_r);
2585
+ if (unlikely(!__pyx_v_paths.memview)) { __Pyx_RaiseUnboundLocalError("paths"); __PYX_ERR(0, 40, __pyx_L1_error) }
2586
+ if (unlikely(!__pyx_v_values.memview)) { __Pyx_RaiseUnboundLocalError("values"); __PYX_ERR(0, 40, __pyx_L1_error) }
2587
+ if (unlikely(!__pyx_v_t_ys.memview)) { __Pyx_RaiseUnboundLocalError("t_ys"); __PYX_ERR(0, 40, __pyx_L1_error) }
2588
+ if (unlikely(!__pyx_v_t_xs.memview)) { __Pyx_RaiseUnboundLocalError("t_xs"); __PYX_ERR(0, 40, __pyx_L1_error) }
2589
+ __pyx_t_1 = __Pyx_void_to_None(__pyx_f_15monotonic_align_4core_maximum_path_c(__pyx_v_paths, __pyx_v_values, __pyx_v_t_ys, __pyx_v_t_xs, 0)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 40, __pyx_L1_error)
2590
  __Pyx_GOTREF(__pyx_t_1);
2591
  __pyx_r = __pyx_t_1;
2592
  __pyx_t_1 = 0;
 
16480
  {0, 0, 0, 0, 0, 0, 0}
16481
  };
16482
  static CYTHON_SMALL_CODE int __Pyx_InitCachedBuiltins(void) {
16483
+ __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) __PYX_ERR(0, 17, __pyx_L1_error)
16484
  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) __PYX_ERR(1, 133, __pyx_L1_error)
16485
  __pyx_builtin_MemoryError = __Pyx_GetBuiltinName(__pyx_n_s_MemoryError); if (!__pyx_builtin_MemoryError) __PYX_ERR(1, 148, __pyx_L1_error)
16486
  __pyx_builtin_enumerate = __Pyx_GetBuiltinName(__pyx_n_s_enumerate); if (!__pyx_builtin_enumerate) __PYX_ERR(1, 151, __pyx_L1_error)
 
17107
  if (__Pyx_patch_abc() < 0) __PYX_ERR(0, 1, __pyx_L1_error)
17108
  #endif
17109
 
17110
+ /* "monotonic_align/core.pyx":9
17111
  * @cython.boundscheck(False)
17112
  * @cython.wraparound(False)
17113
  * cdef void maximum_path_each(int[:,::1] path, float[:,::1] value, int t_y, int t_x, float max_neg_val=-1e9) nogil: # <<<<<<<<<<<<<<
 
17117
  __pyx_k_ = (-1e9);
17118
 
17119
  /* "monotonic_align/core.pyx":1
17120
+ * # cython:language_level=3 # <<<<<<<<<<<<<<
 
17121
  *
17122
+ * cimport cython
17123
  */
17124
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
17125
  __Pyx_GOTREF(__pyx_t_1);
monotonic_align/core.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numba
2
+
3
+
4
+ @numba.jit(numba.void(numba.int32[:,:,::1], numba.float32[:,:,::1], numba.int32[::1], numba.int32[::1]), nopython=True, nogil=True)
5
+ def maximum_path_jit(paths, values, t_ys, t_xs):
6
+ b = paths.shape[0]
7
+ max_neg_val=-1e9
8
+ for i in range(int(b)):
9
+ path = paths[i]
10
+ value = values[i]
11
+ t_y = t_ys[i]
12
+ t_x = t_xs[i]
13
+
14
+ v_prev = v_cur = 0.0
15
+ index = t_x - 1
16
+
17
+ for y in range(t_y):
18
+ for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
19
+ if x == y:
20
+ v_cur = max_neg_val
21
+ else:
22
+ v_cur = value[y-1, x]
23
+ if x == 0:
24
+ if y == 0:
25
+ v_prev = 0.
26
+ else:
27
+ v_prev = max_neg_val
28
+ else:
29
+ v_prev = value[y-1, x-1]
30
+ value[y, x] += max(v_prev, v_cur)
31
+
32
+ for y in range(t_y - 1, -1, -1):
33
+ path[y, index] = 1
34
+ if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]):
35
+ index = index - 1
monotonic_align/core.pyx CHANGED
@@ -1,3 +1,5 @@
 
 
1
  cimport cython
2
  from cython.parallel import prange
3
 
@@ -39,4 +41,4 @@ cpdef void maximum_path_c(int[:,:,::1] paths, float[:,:,::1] values, int[::1] t_
39
  cdef int b = paths.shape[0]
40
  cdef int i
41
  for i in prange(b, nogil=True):
42
- maximum_path_each(paths[i], values[i], t_ys[i], t_xs[i])
 
1
+ # cython:language_level=3
2
+
3
  cimport cython
4
  from cython.parallel import prange
5
 
 
41
  cdef int b = paths.shape[0]
42
  cdef int i
43
  for i in prange(b, nogil=True):
44
+ maximum_path_each(paths[i], values[i], t_ys[i], t_xs[i])
monotonic_align/setup.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from distutils.core import setup
2
  from Cython.Build import cythonize
3
  import numpy
@@ -6,4 +7,4 @@ setup(
6
  name = 'monotonic_align',
7
  ext_modules = cythonize("core.pyx"),
8
  include_dirs=[numpy.get_include()]
9
- )
 
1
+ # cython:language_level=3
2
  from distutils.core import setup
3
  from Cython.Build import cythonize
4
  import numpy
 
7
  name = 'monotonic_align',
8
  ext_modules = cythonize("core.pyx"),
9
  include_dirs=[numpy.get_include()]
10
+ )