From 26a82bda255bc8bc5d7b1230fa3bdc0321607d71 Mon Sep 17 00:00:00 2001 From: Antoine Loriette Date: Fri, 1 Jul 2022 10:29:11 +0200 Subject: [PATCH 1/3] Sakoe-Chiba band for cpu --- soft_dtw_cuda.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/soft_dtw_cuda.py b/soft_dtw_cuda.py index 2ba494e..1fc9fd6 100644 --- a/soft_dtw_cuda.py +++ b/soft_dtw_cuda.py @@ -193,7 +193,10 @@ def compute_softdtw(D, gamma, bandwidth): for i in range(1, N + 1): # Check the pruning condition - if 0 < bandwidth < np.abs(i - j): + i_sc, j_sc = i, j + if M > N: i_sc = i * M / N + if M < N: j_sc = j * N / M + if 0 < bandwidth < np.abs(i_sc - j_sc): continue r0 = -R[b, i - 1, j - 1] / gamma @@ -226,7 +229,10 @@ def compute_softdtw_backward(D_, R, gamma, bandwidth): R[k, i, j] = -np.inf # Check the pruning condition - if 0 < bandwidth < np.abs(i - j): + i_sc, j_sc = i, j + if M > N: i_sc = i * M / N + if M < N: j_sc = j * N / M + if 0 < bandwidth < np.abs(i_sc - j_sc): continue a0 = (R[k, i + 1, j] - R[k, i, j] - D[k, i + 1, j]) / gamma From e9c26544d939e1bce7fbf18c0b4a88c63c428ef9 Mon Sep 17 00:00:00 2001 From: Antoine Loriette Date: Fri, 1 Jul 2022 10:36:45 +0200 Subject: [PATCH 2/3] Sakoe-Chiba band for gpu --- soft_dtw_cuda.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/soft_dtw_cuda.py b/soft_dtw_cuda.py index 1fc9fd6..9a7eb0b 100644 --- a/soft_dtw_cuda.py +++ b/soft_dtw_cuda.py @@ -61,8 +61,12 @@ def compute_softdtw_cuda(D, gamma, bandwidth, max_i, max_j, n_passes, R): # Only compute if element[i, j] is on the current anti-diagonal, and also is within bounds if I + J == p and (I < max_i and J < max_j): + # Don't compute if outside bandwidth - if not (abs(i - j) > bandwidth > 0): + i_sc, j_sc = i, j + if M > N: i_sc = i * M / N + if M < N: j_sc = j * N / M + if not (abs(i_sc - j_sc) > bandwidth > 0): r0 = -R[b, i - 1, j - 1] * inv_gamma r1 = -R[b, i - 1, j] * inv_gamma r2 = -R[b, i, j - 1] * inv_gamma @@ -101,7 +105,10 @@ def compute_softdtw_backward_cuda(D, R, inv_gamma, bandwidth, max_i, max_j, n_pa R[k, i, j] = -math.inf # Don't compute if outside bandwidth - if not (abs(i - j) > bandwidth > 0): + i_sc, j_sc = i, j + if M > N: i_sc = i * M / N + if M < N: j_sc = j * N / M + if not (abs(i_sc - j_sc) > bandwidth > 0): a = math.exp((R[k, i + 1, j] - R[k, i, j] - D[k, i + 1, j]) * inv_gamma) b = math.exp((R[k, i, j + 1] - R[k, i, j] - D[k, i, j + 1]) * inv_gamma) c = math.exp((R[k, i + 1, j + 1] - R[k, i, j] - D[k, i + 1, j + 1]) * inv_gamma) From c2d186894d088e403499b2982e5e8d438da411d9 Mon Sep 17 00:00:00 2001 From: Antoine Loriette Date: Fri, 1 Jul 2022 10:46:28 +0200 Subject: [PATCH 3/3] fix gpu indexing --- soft_dtw_cuda.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/soft_dtw_cuda.py b/soft_dtw_cuda.py index 9a7eb0b..b48ee7a 100644 --- a/soft_dtw_cuda.py +++ b/soft_dtw_cuda.py @@ -64,8 +64,8 @@ def compute_softdtw_cuda(D, gamma, bandwidth, max_i, max_j, n_passes, R): # Don't compute if outside bandwidth i_sc, j_sc = i, j - if M > N: i_sc = i * M / N - if M < N: j_sc = j * N / M + if max_j > max_i: i_sc = i * max_j / max_i + if max_j < max_i: j_sc = j * max_i / max_j if not (abs(i_sc - j_sc) > bandwidth > 0): r0 = -R[b, i - 1, j - 1] * inv_gamma r1 = -R[b, i - 1, j] * inv_gamma @@ -106,8 +106,8 @@ def compute_softdtw_backward_cuda(D, R, inv_gamma, bandwidth, max_i, max_j, n_pa # Don't compute if outside bandwidth i_sc, j_sc = i, j - if M > N: i_sc = i * M / N - if M < N: j_sc = j * N / M + if max_j > max_i: i_sc = i * max_j / max_i + if max_j < max_i: j_sc = j * max_i / max_j if not (abs(i_sc - j_sc) > bandwidth > 0): a = math.exp((R[k, i + 1, j] - R[k, i, j] - D[k, i + 1, j]) * inv_gamma) b = math.exp((R[k, i, j + 1] - R[k, i, j] - D[k, i, j + 1]) * inv_gamma)