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

Metadata

Release files for torchdtw 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchdtw 0.4.2
File Size Uploaded
torchdtw-0.4.2.tar.gz 67.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for torchdtw 0.4.2
File Interpreter ABI Platform
torchdtw-0.4.2-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details
torchdtw-0.4.2-cp312-abi3-manylinux_2_34_x86_64.whl CPython 3.12 abi3 Linux glibc 2.34+ x86-64 Details
torchdtw-0.4.2-cp312-abi3-macosx_14_0_arm64.whl CPython 3.12 abi3 macOS 14.0+ ARM64 Details

Total release size: 2.2 MB

Release files / torchdtw-0.4.2.tar.gz

Download URL torchdtw-0.4.2.tar.gz
Size 67.6 kB
Tags Source
SHA-256 checksum
How to use checksums
62ceaf0e54e46b6250a6f507dcf266597d9a48a336caea0494e2f411614d82d9
BLAKE2b-256 checksum
How to use checksums
1838c443c7856f972372f6b76020bfefe01f4abdcab9e91afd4e9649e7e6ef6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}

Release files / torchdtw-0.4.2-cp312-abi3-win_amd64.whl

Download URL torchdtw-0.4.2-cp312-abi3-win_amd64.whl
Size 75.0 kB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
eaa58b50d907289fda94c3c1de5609269b89cc8f1927c063489e2d907cc7a948
BLAKE2b-256 checksum
How to use checksums
9bc4cb272c9b14eb69d559cd73b6f813e3a55db46a04099bc4594930bf5331c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}

Release files / torchdtw-0.4.2-cp312-abi3-manylinux_2_34_x86_64.whl

Download URL torchdtw-0.4.2-cp312-abi3-manylinux_2_34_x86_64.whl
Size 2.0 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
b03b8ca61a3ef64647c29bee61364f9dd399c85c16baed9624f2e02da57d0f1e
BLAKE2b-256 checksum
How to use checksums
51af90ccffa38ce679db5a40608a8df70b5792c7b8a330b9eb4c12ba08601c5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}

Release files / torchdtw-0.4.2-cp312-abi3-macosx_14_0_arm64.whl

Download URL torchdtw-0.4.2-cp312-abi3-macosx_14_0_arm64.whl
Size 102.1 kB
Tags CPython 3.12 abi3 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
653cc7b3812c62c3f23ff2f59b26c9c4e1952fd3d60fac36a9be66408b1680de
BLAKE2b-256 checksum
How to use checksums
135964249d5c8beb2520417637fb53fa602af592be230799dcb24400c6806b04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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}

Release history Release notifications | RSS feed

This release

0.4.2 This release

4 release files

0.4.1

4 release files

0.4.0

4 release files

0.3.0

4 release files

0.2.0

4 release files

0.1.1

4 release files

0.1.0

4 release files

0.0.2

4 release files

0.0.1

4 release 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