SoftDTW (Ron Shapira Weber) — dtw_loss_functions documentation

# SoftDTW (Ron Shapira Weber)

class dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.SoftDTW(*, gamma: float = 1.0, bandwidth: float | None = None, normalize: bool = False, dist: str = 'sqeuclidean', fused: bool | None = None)

  Bases: `Module`

  User-facing module.

  - dist: currently supports “sqeuclidean”

  - normalize: SoftDTW(x,y) - 0.5*(SoftDTW(x,x)+SoftDTW(y,y))

  - fused:

      None -> auto (use fused only when possible) True -> require fused (error if not possible) False -> never fused (always materialize D and use D-based autograd)

  Methods

  `forward`(x, y) | Define the computation performed at every call.

  forward(x: Tensor, y: Tensor) → Tensor

    Define the computation performed at every call.

    Should be overridden by all subclasses.

    Note

    Although the recipe for forward pass needs to be defined within this function, one should call the `Module` instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.softdtw(x: Tensor, y: Tensor, *, gamma: float = 1.0, bandwidth: float | None = None, normalize: bool = False, dist: str = 'sqeuclidean', fused: bool | None = None) → Tensor

  Convenience functional API.

  x: (B,N,D) or (N,D) y: (B,M,D) or (M,D) fused: None (auto), True (require fused), False (never fused) returns: (B,)

dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.softdtw_barycenter(X: Tensor, *, gamma: float = 1.0, weights: Tensor | None = None, max_iter: int = 100, lr: float = 0.1, init: Tensor | None = None, device: str | device | None = None, verbose: bool = False, fused: bool | None = None, early_stopping: bool = True, patience: int = 10, tol: float = 1e-05) → Tensor

  Compute a SoftDTW barycenter (time series average) through optimization.

  This function finds the barycenter that minimizes the weighted sum of SoftDTW distances to all input time series using gradient-based optimization.

  Parameters:

    - X – Input time series of shape (B, N, D) where: - B: batch size (number of sequences) - N: sequence length - D: feature dimension

    - gamma – SoftDTW regularization parameter. Default: 1.0

    - weights – Optional weights for each sequence, shape (B,). Default: uniform

    - max_iter – Maximum optimization iterations. Default: 100

    - lr – Learning rate for optimization. Default: 0.1

    - init – Initial barycenter, shape (N, D). If None, uses weighted mean. Default: None

    - device – Device to compute on. If None, uses X’s device. Default: None

    - verbose – Print iteration progress and timing. Default: False

    - fused – Fused mode selection. Default: None (auto-select) - None: Auto-select (use fused if CUDA available) - True: Require fused mode (error if not available) - False: Never use fused mode (always use standard distance matrix)

    - early_stopping – Stop early if loss plateaus. Default: True

    - patience – Iterations without improvement before stopping. Default: 10

    - tol – Absolute improvement threshold for early stopping. Default: 1e-5 Note: Uses absolute improvement (best_loss - loss_val > tol), which handles negative SoftDTW values correctly

  Returns:

    Barycenter of shape (N, D)

dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.softdtw_barycenter_cpu(X: Tensor, *, gamma: float = 1.0, weights: Tensor | None = None, max_iter: int = 100, lr: float = 0.1, init: Tensor | None = None, verbose: bool = False, fused: bool | None = None, early_stopping: bool = True, patience: int = 10, tol: float = 1e-05) → Tensor

  Compute a SoftDTW barycenter on CPU (convenience wrapper).

  Parameters:

    - X – Input time series of shape (B, N, D)

    - gamma – SoftDTW regularization parameter. Default: 1.0

    - weights – Optional weights for each sequence. Default: uniform

    - max_iter – Maximum optimization iterations. Default: 100

    - lr – Learning rate for optimization. Default: 0.01

    - init – Initial barycenter. If None, uses weighted mean. Default: None

    - verbose – Print iteration progress and timing. Default: False

    - fused – Fused mode selection. Default: None (auto-select)

    - early_stopping – Stop early if loss plateaus. Default: True

    - patience – Iterations without improvement before stopping. Default: 10

    - tol – Improvement threshold for early stopping. Default: 1e-5

  Returns:

    Barycenter of shape (N, D)

## Subpackages

- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.cuda package
  - Submodules
    - dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.cuda.kernels module
      - `softdtw_backward_kernel_legacy()`
      - `softdtw_backward_log_cuda()`
      - `softdtw_backward_log_diag_cuda()`
      - `softdtw_backward_log_diag_sqeuclid_cuda()`
      - `softdtw_forward_diag_cuda()`
      - `softdtw_forward_diag_sqeuclid_cuda()`
      - `softdtw_forward_kernel()`
    - dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.cuda.launcher module
      - `softdtw_backward_cpu()`
      - `softdtw_backward_cuda_fused_sqeuclid()`
      - `softdtw_backward_cuda_log()`
      - `softdtw_forward_cpu()`
      - `softdtw_forward_cuda()`
      - `softdtw_forward_cuda_fused_sqeuclid()`
- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.utils package
  - Submodules
    - dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.utils.checks module
      - `check_D()`

## Submodules

- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.autograd module
  - `SoftDTWAutograd`
    - `SoftDTWAutograd.backward()`
    - `SoftDTWAutograd.forward()`
- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.autograd_xy module
  - `SoftDTWXYAutograd`
    - `SoftDTWXYAutograd.backward()`
    - `SoftDTWXYAutograd.forward()`
- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.barycenters module
  - `softdtw_barycenter()`
  - `softdtw_barycenter_cpu()`
- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.distances module
  - `pairwise_distance()`
  - `sqeuclidean()`
- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.functional module
  - `softdtw()`
- dtw_loss_functions.soft_dtw_implementations.soft_dtw_cuda_ron.module module
  - `SoftDTW`
    - `SoftDTW.forward()`