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
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},
}
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchdtw-0.4.0.tar.gz.
File metadata
- Download URL: torchdtw-0.4.0.tar.gz
- Upload date:
- Size: 66.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67dafc7e8da2a917b438202069984cde93f4132758b3cb6f140ec03ae83db75d
|
|
| MD5 |
abbd08b6b23ff5169dccfef52de08a6e
|
|
| BLAKE2b-256 |
b1adebca616844f09cf92c38c83d872e9077269ee1760313f2e44750af3e70e8
|
File details
Details for the file torchdtw-0.4.0-cp312-abi3-win_amd64.whl.
File metadata
- Download URL: torchdtw-0.4.0-cp312-abi3-win_amd64.whl
- Upload date:
- Size: 72.4 kB
- Tags: CPython 3.12+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f05a8fc949b753e94b73005e08cdca3698311209b623ec9bfb95134d39386f5
|
|
| MD5 |
b937fca8125c20e54ff19bdf2236af03
|
|
| BLAKE2b-256 |
d4db2c27301b9cfd0fd5dacc5c9e0ed18e78e19de4da43d4c8a95b1553575590
|
File details
Details for the file torchdtw-0.4.0-cp312-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: torchdtw-0.4.0-cp312-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efa4b90c574e7f2ec9058a8a8912e0b04775a66f505e17c3a0afb893c50456c8
|
|
| MD5 |
10625742f0fb87275960eeb80f8c0128
|
|
| BLAKE2b-256 |
1a0abebc9924b19c8f3ddce2fa4c0f3f3f6f8028953d77640e485c3765508da2
|
File details
Details for the file torchdtw-0.4.0-cp312-abi3-macosx_14_0_arm64.whl.
File metadata
- Download URL: torchdtw-0.4.0-cp312-abi3-macosx_14_0_arm64.whl
- Upload date:
- Size: 99.2 kB
- Tags: CPython 3.12+, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
384b57342ca74fc948b0ca077802551eeba3e86f65f5262e1edfeadcfd3e1b9b
|
|
| MD5 |
90f5d4b3ef0381afcf7b572808d544de
|
|
| BLAKE2b-256 |
eda9806e67aa963ccfa39117a47acaccf458fd14086615892ab4d01de441cae8
|