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. 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.0.2.tar.gz (57.6 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.0.2-cp312-abi3-win_amd64.whl (30.8 kB view details)

Uploaded CPython 3.12+Windows x86-64

torchdtw-0.0.2-cp312-abi3-manylinux_2_34_x86_64.whl (252.6 kB view details)

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

torchdtw-0.0.2-cp312-abi3-macosx_14_0_arm64.whl (25.6 kB view details)

Uploaded CPython 3.12+macOS 14.0+ ARM64

File details

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

File metadata

  • Download URL: torchdtw-0.0.2.tar.gz
  • Upload date:
  • Size: 57.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","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.0.2.tar.gz
Algorithm Hash digest
SHA256 23c776b03360b0b9be7782b03b9184ab1869ea9561d03593e13c88078852e57c
MD5 a632de5b7a3f34e8a8aea7daa32ed2b3
BLAKE2b-256 644d9e43879b78259e52df32733aaede2937c9cac7e33862e50e0d67e30fa90f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.0.2-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","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.0.2-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3a937c43a0e743fe093b7b3580bc1b0b60d61fb23796570117f3d8a7afddd09c
MD5 75a131b8d6c6c606b3b3391e95d68113
BLAKE2b-256 204a6a08751a110c2f7b598e92a48f95f589dde32612494acc9b7a24e80683ce

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.0.2-cp312-abi3-manylinux_2_34_x86_64.whl
  • Upload date:
  • Size: 252.6 kB
  • Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","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.0.2-cp312-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f81bbd5fe5e1c1938ecff840c1a67a761d3dfaeba4b5d5ff6f5d81ce5cb36cdb
MD5 da38a491c788aa861c87f10d25973713
BLAKE2b-256 118910d666e3b23a3b62c503acd0d84e4614a9623aaa6cbe7298915d7016a2e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchdtw-0.0.2-cp312-abi3-macosx_14_0_arm64.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: CPython 3.12+, macOS 14.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","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.0.2-cp312-abi3-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 31a1029efaeafc4dd3f8014dca72f5c5d48b6df5099082d627b5fc07c2721618
MD5 c032234a30344ea069957b189ec68bbf
BLAKE2b-256 a5aa2f50ec5ac907ad622970bbec46e2f4c5a6d14a393a6e0502648e11c785ff

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