code: def batch_dynamic_time_warping(distance, shapes=None):
      """full batched DTW without any constraints
  
      distance:  (batchsize, max_M, max_N) matrix
      shapes: (batchsize,) vector specifying (M, N) for each entry
      """
      # ptr: 0=left, 1=up-left, 2=up
      ptr2dij = {0: (0, -1), 1: (-1, -1), 2: (-1, 0)}
  
      bsz, m, n = distance.size()
      cumdist = torch.zeros_like(distance)
      backptr = torch.zeros_like(distance).type(torch.int32) - 1
  
      # initialize
      cumdist[:, 0, :] = distance[:, 0, :].cumsum(dim=-1)
      cumdist[:, :, 0] = distance[:, :, 0].cumsum(dim=-1)
      backptr[:, 0, :] = 0
      backptr[:, :, 0] = 2
  
      # DP with optimized anti-diagonal parallelization, O(M+N) steps
      for offset in range(2, m + n - 1):
          ind = antidiag_indices(offset, 1, m, 1, n)
          c = torch.stack(
              [
                  cumdist[:, ind[0], ind[1] - 1],
                  cumdist[:, ind[0] - 1, ind[1] - 1],
                  cumdist[:, ind[0] - 1, ind[1]],
              ],
              dim=2,
          )
          v, b = c.min(axis=-1)
          backptr[:, ind[0], ind[1]] = b.int()
          cumdist[:, ind[0], ind[1]] = v + distance[:, ind[0], ind[1]]
  
      # backtrace
      pathmap = torch.zeros_like(backptr)
      for b in range(bsz):
          i = m - 1 if shapes is None else (shapes[b][0] - 1).item()
          j = n - 1 if shapes is None else (shapes[b][1] - 1).item()
          dtwpath = [(i, j)]
          while (i != 0 or j != 0) and len(dtwpath) < 10000:
              assert i >= 0 and j >= 0
              di, dj = ptr2dij[backptr[b, i, j].item()]
              i, j = i + di, j + dj
              dtwpath.append((i, j))
          dtwpath = dtwpath[::-1]
          indices = torch.from_numpy(np.array(dtwpath))
          pathmap[b, indices[:, 0], indices[:, 1]] = 1
  
      return cumdist, backptr, pathmap
  
imports: import logging
  import os
  import os.path
  import torch
  import torch.nn.functional
  import numpy
  from fairseq.data.audio.text_to_speech_dataset import TextToSpeechDatasetCreator
  from fairseq.tasks import register_task
  from fairseq.tasks.speech_to_text import SpeechToTextTask
  from fairseq.speech_generator import AutoRegressiveSpeechGenerator, NonAutoregressiveSpeechGenerator, TeacherForcingAutoRegressiveSpeechGenerator
label_desc: Use image node_id 4 for calling a global function with example usage: batch_dynamic_time_warping(distance, shapes) and returns: cumdist,  backptr,  pathmap