Skip to main content

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

dtw(distances)

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

Use +inf to mask forbidden alignments. NaN distances are unsupported: the result is unspecified and may differ between the CPU and CUDA backends. Integer distances accumulate the cost in their own dtype and may overflow on long sequences; use a wide enough integer dtype or a floating dtype.

Parameters:

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

Returns:

  • Tensor – A scalar tensor with the cost.

dtw_batch

dtw_batch(distances, sx, sy, *, symmetric)

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

Only the (sx[i], sy[j]) sub-block of each pair is read, so padding beyond the sequence lengths is ignored. Every sx[i] must be <= s1 and every sy[j] <= s2: the CPU backend validates this, but the CUDA backend assumes it and reads out of bounds if violated. Use +inf to mask forbidden alignments. NaN distances are unsupported: the result is unspecified and may differ between the CPU and CUDA backends. Integer distances accumulate the cost in their own dtype and may overflow on long sequences; use a wide enough integer dtype or a floating dtype.

Parameters:

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

Returns:

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

dtw_path

dtw_path(distances)

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

No CUDA variant or batched implementation are provided for now. Use +inf to mask forbidden alignments. NaN distances are unsupported and give an unspecified path.

Parameters:

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

Returns:

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

Performance

For many DTWs on short sequences, prefer dtw_batch over a Python loop of dtw calls. A single dtw_batch launches one CUDA kernel (one block per pair) or one parallel CPU loop, amortizing dispatch, allocation, and launch overhead across the whole batch.

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},
}

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.4.1.tar.gz (67.1 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.4.1-cp312-abi3-win_amd64.whl (74.0 kB view details)

Uploaded CPython 3.12+Windows x86-64

torchdtw-0.4.1-cp312-abi3-manylinux_2_34_x86_64.whl (2.0 MB view details)

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

torchdtw-0.4.1-cp312-abi3-macosx_14_0_arm64.whl (102.4 kB view details)

Uploaded CPython 3.12+macOS 14.0+ ARM64

File details

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

File metadata

  • Download URL: torchdtw-0.4.1.tar.gz
  • Upload date:
  • Size: 67.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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.4.1.tar.gz
Algorithm Hash digest
SHA256 2076735b942a673ef28819fc0eb5914c793fb864f6938c214cd036b92c7d32a7
MD5 25a8f4661329abd6e7379168012439bd
BLAKE2b-256 db4a71b632e518cf87a15c3274277ae4be2466dacc1548ea5997d929ec964950

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.4.1-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 74.0 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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.4.1-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b23cb1922d714829e23aad3afdd332d9c249764369440784008ae1eed980fc17
MD5 e00299ba6a4d09a29167e600f0fec204
BLAKE2b-256 f2970f460e0a29a0298079fb8da0a73172cece93252f60e03e8ad005c456945d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.4.1-cp312-abi3-manylinux_2_34_x86_64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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.4.1-cp312-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 aa23d170054594ecf078dc43f67f323bc5d2526e2c70854c3612381950f373ff
MD5 e832f1fb0f8c1f9f0a680c412188c129
BLAKE2b-256 bd6ebc4fdb18912338d65790409fb5e5ac3a52560ab9cf2c96f59454a589ac96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.4.1-cp312-abi3-macosx_14_0_arm64.whl
  • Upload date:
  • Size: 102.4 kB
  • Tags: CPython 3.12+, macOS 14.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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.4.1-cp312-abi3-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 6509b0bbfe4f385f5570023645e802c1ceb973a6eda2a3d057cf598f42c5ea09
MD5 bd896b59ebd2744a576c96234920c23c
BLAKE2b-256 0b451fa81bdaed597dc32b394ab5c37bdda65be6ff285750f98aa14e86708c66

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.1 This release

4 files

0.4.0

4 files

0.3.0

4 files

0.2.0

4 files

0.1.1

4 files

0.1.0

4 files

0.0.2

4 files

0.0.1

4 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page