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.3.0.tar.gz (44.5 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.3.0-cp312-abi3-win_amd64.whl (76.0 kB view details)

Uploaded CPython 3.12+Windows x86-64

torchdtw-0.3.0-cp312-abi3-manylinux_2_34_x86_64.whl (1.7 MB view details)

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

torchdtw-0.3.0-cp312-abi3-macosx_14_0_arm64.whl (82.7 kB view details)

Uploaded CPython 3.12+macOS 14.0+ ARM64

File details

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

File metadata

  • Download URL: torchdtw-0.3.0.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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.3.0.tar.gz
Algorithm Hash digest
SHA256 fe7d9080e71aabbe6b027a9254df4e32fb4892e8d18e7b2335a50f0b66babcc1
MD5 9f7e334dd6347dfd1c614da54ae702ad
BLAKE2b-256 6970d1b297609f364d8bd48b5ee635d35981816255e7c77f579b2922d2404f34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.3.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 76.0 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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.3.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 29c58089f169a5edf4faeaf70ffd4b1c3b07687fc470e317e6b979a78f3315b7
MD5 ea22649589e1bcb5273fd23331e7fe05
BLAKE2b-256 decd38182a935ea286ccf29a7684da3b75e98839559a8d1faa8c9d9bbf21f19e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.3.0-cp312-abi3-manylinux_2_34_x86_64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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.3.0-cp312-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 896e0badf689558ac306e2a32047ba99b752978716bf54388261bf778e97ed02
MD5 938c57b9a9dc1da4db595694b148a33e
BLAKE2b-256 ef9963addfd5879c73658bd3fc517b77ec8ba4481950ce7262a6167ac907580b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.3.0-cp312-abi3-macosx_14_0_arm64.whl
  • Upload date:
  • Size: 82.7 kB
  • Tags: CPython 3.12+, macOS 14.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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.3.0-cp312-abi3-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 fbd5245290e4b6bf1c480edcdae4a5c11441502a63e61194dab16110c7a2a54d
MD5 ac8830b2cbb6311487c8c18bd9799440
BLAKE2b-256 e8115ac1762e4927482cc16e0f704704f57dd44c7257b64a68f847d8d1314b22

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