Skip to main content

PyTorch DTW C++ extension

Project description

PyTorch DTW C++ extension

Dynamic time warping in native PyTorch, with CPU and CUDA backends.

pip install torchdtw

This package requires PyTorch 2.10 or later. It is developed using the PyTorch 2.10 Stable ABI, and compiled with instructions for CUDA cards from Volta to Blackwell. It is available on Linux (with CUDA support), macOS, and Windows (without CUDA). This was originally made for fastabx, but it can be used in other projects. Only the exact DTW is implemented, there is no plan to add variants.

Usage

This package provides three functions:

DTW

def dtw(distances: torch.Tensor) -> torch.Tensor

Compute the DTW cost of the given distances 2D tensor.

Arguments:

  • distances: A 2D tensor of shape (n, m) representing the pairwise distances between two sequences.

Returns:

A scalar tensor with the cost.

DTW path

def dtw_path(distances: torch.Tensor) -> torch.Tensor

Compute the DTW path of the given distances 2D tensor.

No CUDA variant or batched implementation are provided for now.

Arguments:

  • distances: A 2D tensor of shape (n, m) representing the pairwise distances between two sequences.

Returns:

A 2D tensor of shape (*, 2) with the path indices.

Batched DTW

def dtw_batch(distances: torch.Tensor, sx: torch.Tensor, sy: torch.Tensor, *,
              symmetric: bool) -> torch.Tensor

Compute the batched DTW cost on the distances 4D tensor.

Arguments:

  • distances: A 4D tensor of shape (n1, n2, s1, s2) representing the pairwise distances between two batches of sequences.
  • sx: A 1D tensor of shape (n1,) representing the lengths of the sequences in the first batch.
  • sy: A 1D tensor of shape (n2,) representing the lengths of the sequences in the second batch.
  • symmetric: Whether or not the DTW is symmetric (i.e., the two batches are the same).

Returns:

A 2D tensor of shape (n1, n2) with the costs.

Benchmark

Check this folder for comparisons against reference implementations.

Citation

Please cite the fastabx paper if you use this package in your work:

@misc{fastabx,
  title={fastabx: A library for efficient computation of ABX discriminability},
  author={Maxime Poli and Emmanuel Chemla and Emmanuel Dupoux},
  year={2025},
  eprint={2505.02692},
  archivePrefix={arXiv},
  primaryClass={cs.CL},
  url={https://arxiv.org/abs/2505.02692},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchdtw-0.2.0.tar.gz (40.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

torchdtw-0.2.0-cp312-abi3-win_amd64.whl (31.1 kB view details)

Uploaded CPython 3.12+Windows x86-64

torchdtw-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl (284.4 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.34+ x86-64

torchdtw-0.2.0-cp312-abi3-macosx_14_0_arm64.whl (26.0 kB view details)

Uploaded CPython 3.12+macOS 14.0+ ARM64

File details

Details for the file torchdtw-0.2.0.tar.gz.

File metadata

  • Download URL: torchdtw-0.2.0.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for torchdtw-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1b5e71c6183a6412ec817df0b06a7ef9321a4fe0495d85ced12a3f6f8564d7a8
MD5 d579f2b96614fda8ed0f037a7aef71c0
BLAKE2b-256 b7213708af69487e1b405f3a8d0f7df1c70e3a62707380d8ccbd79b7b59e99a0

See more details on using hashes here.

File details

Details for the file torchdtw-0.2.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: torchdtw-0.2.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for torchdtw-0.2.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2e0edc02104ed23d6c123d2608932b722340611c2ba12420c86d34db6436d3cc
MD5 66497f81fe4fa7d68a42badb29bf95b5
BLAKE2b-256 183a7e49b216f330b3a18863020270fbcdcebc7693a7d6b6f2b904320e66057d

See more details on using hashes here.

File details

Details for the file torchdtw-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl.

File metadata

  • Download URL: torchdtw-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl
  • Upload date:
  • Size: 284.4 kB
  • Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for torchdtw-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 8bccb5f0a93c5f9c4beea7312d973d30b1b3148ec0506c337cb0bc1e17b58fb7
MD5 175ee07a34b271d6a507400e54c0145c
BLAKE2b-256 65f13a270594d71e6085abbf10ae65d9c82d0bd85bfd3674dc7a93092a36da1c

See more details on using hashes here.

File details

Details for the file torchdtw-0.2.0-cp312-abi3-macosx_14_0_arm64.whl.

File metadata

  • Download URL: torchdtw-0.2.0-cp312-abi3-macosx_14_0_arm64.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: CPython 3.12+, macOS 14.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for torchdtw-0.2.0-cp312-abi3-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 eff8c56f4017d926e621f859dbc64d01e96240342a34479fc7f3c9b6babde7e5
MD5 e33996af1924db265f4d6a4062e79097
BLAKE2b-256 57aadc3e84c7cb144856e66e46f467057c36c1170af7deb074ffdf5c8867f703

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page