ddtw 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ddtw-0.1.0/LICENSE +21 -0
- ddtw-0.1.0/PKG-INFO +290 -0
- ddtw-0.1.0/README.md +230 -0
- ddtw-0.1.0/ddtw/__init__.py +57 -0
- ddtw-0.1.0/ddtw/backend/__init__.py +37 -0
- ddtw-0.1.0/ddtw/backend/backend_cpu_numba.py +447 -0
- ddtw-0.1.0/ddtw/backend/backend_cuda_cpp.py +215 -0
- ddtw-0.1.0/ddtw/backend/backend_torch.py +378 -0
- ddtw-0.1.0/ddtw/backend/cpp_extension.py +67 -0
- ddtw-0.1.0/ddtw/backend/csrc/ddtw_cuda.cu +1105 -0
- ddtw-0.1.0/ddtw/backend/csrc/ddtw_extension.cpp +64 -0
- ddtw-0.1.0/ddtw/cost_function.py +122 -0
- ddtw-0.1.0/ddtw/ddtw.py +436 -0
- ddtw-0.1.0/ddtw/ddtw_variants.py +906 -0
- ddtw-0.1.0/ddtw.egg-info/PKG-INFO +290 -0
- ddtw-0.1.0/ddtw.egg-info/SOURCES.txt +24 -0
- ddtw-0.1.0/ddtw.egg-info/dependency_links.txt +1 -0
- ddtw-0.1.0/ddtw.egg-info/requires.txt +40 -0
- ddtw-0.1.0/ddtw.egg-info/top_level.txt +1 -0
- ddtw-0.1.0/pyproject.toml +98 -0
- ddtw-0.1.0/setup.cfg +4 -0
- ddtw-0.1.0/test/test_CTC.py +145 -0
- ddtw-0.1.0/test/test_DTW.py +115 -0
- ddtw-0.1.0/test/test_PM.py +120 -0
- ddtw-0.1.0/test/test_SDTW.py +107 -0
- ddtw-0.1.0/test/test_subDTW.py +108 -0
ddtw-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Johannes Zeitler and Meinard Müller
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ddtw-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ddtw
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A unified and efficient PyTorch toolbox for differentiable sequence alignment.
|
|
5
|
+
Author: Johannes Zeitler, Meinard Mueller
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/groupmm/dDTW
|
|
8
|
+
Project-URL: Documentation, https://groupmm.github.io/dDTW
|
|
9
|
+
Project-URL: Source, https://github.com/groupmm/dDTW
|
|
10
|
+
Project-URL: Issues, https://github.com/groupmm/dDTW/issues
|
|
11
|
+
Keywords: ddtw,dynamic-time-warping,differentiable-dtw,soft-dtw,sequence-alignment,ctc,partial-matching,subsequence-DTW
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: torch>=2.0
|
|
27
|
+
Provides-Extra: cuda
|
|
28
|
+
Requires-Dist: ninja; extra == "cuda"
|
|
29
|
+
Provides-Extra: numba
|
|
30
|
+
Requires-Dist: numba; extra == "numba"
|
|
31
|
+
Provides-Extra: test
|
|
32
|
+
Requires-Dist: pytest; extra == "test"
|
|
33
|
+
Requires-Dist: numpy; extra == "test"
|
|
34
|
+
Requires-Dist: numba; extra == "test"
|
|
35
|
+
Requires-Dist: librosa; extra == "test"
|
|
36
|
+
Provides-Extra: benchmark
|
|
37
|
+
Requires-Dist: numpy; extra == "benchmark"
|
|
38
|
+
Requires-Dist: numba<0.66,>=0.65; extra == "benchmark"
|
|
39
|
+
Requires-Dist: ninja; extra == "benchmark"
|
|
40
|
+
Provides-Extra: notebook
|
|
41
|
+
Requires-Dist: jupyter; extra == "notebook"
|
|
42
|
+
Requires-Dist: matplotlib; extra == "notebook"
|
|
43
|
+
Requires-Dist: numpy; extra == "notebook"
|
|
44
|
+
Provides-Extra: docs
|
|
45
|
+
Requires-Dist: sphinx>=7; extra == "docs"
|
|
46
|
+
Requires-Dist: sphinx-rtd-theme>=2; extra == "docs"
|
|
47
|
+
Provides-Extra: dev
|
|
48
|
+
Requires-Dist: build; extra == "dev"
|
|
49
|
+
Requires-Dist: twine; extra == "dev"
|
|
50
|
+
Requires-Dist: ninja; extra == "dev"
|
|
51
|
+
Requires-Dist: pytest; extra == "dev"
|
|
52
|
+
Requires-Dist: numpy; extra == "dev"
|
|
53
|
+
Requires-Dist: numba<0.66,>=0.65; extra == "dev"
|
|
54
|
+
Requires-Dist: librosa; extra == "dev"
|
|
55
|
+
Requires-Dist: jupyter; extra == "dev"
|
|
56
|
+
Requires-Dist: matplotlib; extra == "dev"
|
|
57
|
+
Requires-Dist: sphinx>=7; extra == "dev"
|
|
58
|
+
Requires-Dist: sphinx-rtd-theme>=2; extra == "dev"
|
|
59
|
+
Dynamic: license-file
|
|
60
|
+
|
|
61
|
+
<p align="right">
|
|
62
|
+
<img src="https://raw.githubusercontent.com/groupmm/dDTW/master/docs/_static/figures/logo_ddtw.png" alt="dDTW logo" width="300">
|
|
63
|
+
</p>
|
|
64
|
+
|
|
65
|
+
# *d*DTW: A Unified and Efficient Toolbox for Differentiable Sequence Alignment
|
|
66
|
+
|
|
67
|
+
© [Johannes Zeitler](https://audiolabs-erlangen.de/fau/assistant/zeitler) and [Meinard Müller](https://www.audiolabs-erlangen.de/fau/professor/mueller), 2026
|
|
68
|
+
|
|
69
|
+
This repository contains the *d*DTW toolbox, a modular PyTorch toolbox with efficient CPU and GPU implementations that unifies DTW, soft/smooth/sparse DTW, subsequence DTW, partial matching, and CTC in a common framework.
|
|
70
|
+
|
|
71
|
+
If you use the *d*DTW toolbox, please cite the corresponding paper:
|
|
72
|
+
```
|
|
73
|
+
@article{ZeitlerM26_dDTW_toolbox,
|
|
74
|
+
author = {Johannes Zeitler and Meinard M{\"u}ller},
|
|
75
|
+
title = {{dDTW}: A Unified and Efficient Toolbox for Differentiable Sequence Alignment},
|
|
76
|
+
year = {2026},
|
|
77
|
+
note = {Under Review}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Installation and Usage
|
|
82
|
+
|
|
83
|
+
To install the *d*DTW toolbox locally, you can clone this repository or use pip:
|
|
84
|
+
``` bash
|
|
85
|
+
pip install ddtw
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
To use a loss function from the *d*DTW toolbox, such as SDTW, simply import the module and use it like a normal PyTorch loss:
|
|
89
|
+
```python
|
|
90
|
+
from ddtw import SDTW
|
|
91
|
+
|
|
92
|
+
loss_fn = SDTW()
|
|
93
|
+
loss = loss_fn(X,Y) # assuming X and Y are, e.g., predictions and targets
|
|
94
|
+
loss.backward()
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## System Requirements
|
|
98
|
+
|
|
99
|
+
The toolbox is implemented in Python and PyTorch. A basic CPU setup requires:
|
|
100
|
+
|
|
101
|
+
- Python with a recent PyTorch installation
|
|
102
|
+
- NumPy
|
|
103
|
+
- Numba when using the `cpu_numba` backend
|
|
104
|
+
|
|
105
|
+
The `torch` backend runs without a custom compiler. The optional but recommended `cuda_cpp` backend
|
|
106
|
+
requires a CUDA-capable PyTorch installation, an NVIDIA GPU, the CUDA toolkit
|
|
107
|
+
including `nvcc`, and a working C++ compiler toolchain because the extension is
|
|
108
|
+
compiled locally through `torch.utils.cpp_extension`.
|
|
109
|
+
|
|
110
|
+
### CUDA, PyTorch, and `nvcc` Version Matching
|
|
111
|
+
|
|
112
|
+
The `cuda_cpp` backend is compiled lazily the first time it is used. This means
|
|
113
|
+
`pip install ddtw` installs the Python package and CUDA/C++ source files,
|
|
114
|
+
but the native extension is built later by PyTorch's extension loader. For this
|
|
115
|
+
build to work, three CUDA-related components must be compatible:
|
|
116
|
+
|
|
117
|
+
- The NVIDIA driver must support the CUDA runtime used by PyTorch.
|
|
118
|
+
- The installed PyTorch wheel must be built for the intended CUDA version, shown
|
|
119
|
+
by `torch.version.cuda`.
|
|
120
|
+
- The active CUDA compiler `nvcc` must be from a matching CUDA toolkit and must
|
|
121
|
+
support the GPU architecture being compiled, for example Blackwell GPUs require
|
|
122
|
+
a recent CUDA toolkit.
|
|
123
|
+
|
|
124
|
+
You can check the active environment with:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
which nvcc
|
|
128
|
+
nvcc --version
|
|
129
|
+
|
|
130
|
+
python - <<'PY'
|
|
131
|
+
import torch
|
|
132
|
+
print("torch", torch.__version__)
|
|
133
|
+
print("torch CUDA", torch.version.cuda)
|
|
134
|
+
print("CUDA available", torch.cuda.is_available())
|
|
135
|
+
print("GPU", torch.cuda.get_device_name(0) if torch.cuda.is_available() else None)
|
|
136
|
+
print("capability", torch.cuda.get_device_capability(0) if torch.cuda.is_available() else None)
|
|
137
|
+
PY
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The examples below use conda for the CUDA compiler/toolkit and pip for PyTorch.
|
|
141
|
+
They install the full CUDA toolkit in the environment so `nvcc` and development
|
|
142
|
+
headers such as `cusparse.h` are available. For up-to-date PyTorch wheel
|
|
143
|
+
commands, also check the official PyTorch install selector at
|
|
144
|
+
https://pytorch.org/get-started/locally/.
|
|
145
|
+
|
|
146
|
+
Ready-made environment files are available in `environments/` for CUDA 11.8,
|
|
147
|
+
12.8, and 13.2. Create one from the repository root and install the activation
|
|
148
|
+
hooks once:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
conda env create -f environments/ddtw_cu128.yml
|
|
152
|
+
conda activate ddtw_cu128
|
|
153
|
+
bash environments/install_activation_hooks.sh
|
|
154
|
+
conda deactivate
|
|
155
|
+
conda activate ddtw_cu128
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The hooks clear inherited compiler and CUDA flags that can otherwise make
|
|
159
|
+
PyTorch's JIT extension builder pick the wrong host compiler or CUDA toolkit.
|
|
160
|
+
|
|
161
|
+
#### Example: CUDA 11.8
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
conda create -n ddtw_cu118 python=3.11
|
|
165
|
+
conda activate ddtw_cu118
|
|
166
|
+
|
|
167
|
+
conda install -c conda-forge gcc_linux-64=11 gxx_linux-64=11
|
|
168
|
+
conda install -c nvidia/label/cuda-11.8.0 cuda
|
|
169
|
+
|
|
170
|
+
pip install torch --index-url https://download.pytorch.org/whl/cu118
|
|
171
|
+
pip install -e ".[test,benchmark]"
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
#### Example: CUDA 12.8
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
conda create -n ddtw_cu128 python=3.12
|
|
178
|
+
conda activate ddtw_cu128
|
|
179
|
+
|
|
180
|
+
conda install -c conda-forge gcc_linux-64=14 gxx_linux-64=14
|
|
181
|
+
conda install -c nvidia/label/cuda-12.8.0 cuda
|
|
182
|
+
|
|
183
|
+
pip install torch --index-url https://download.pytorch.org/whl/cu128
|
|
184
|
+
pip install -e ".[test,benchmark]"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Example: CUDA 13.2
|
|
188
|
+
```bash
|
|
189
|
+
conda create -n ddtw_cu132 python=3.12
|
|
190
|
+
conda activate ddtw_cu132
|
|
191
|
+
|
|
192
|
+
conda install -c conda-forge gcc_linux-64 gxx_linux-64
|
|
193
|
+
conda install -c nvidia/label/cuda-13.2 cuda
|
|
194
|
+
|
|
195
|
+
pip install torch --index-url https://download.pytorch.org/whl/cu132
|
|
196
|
+
pip install -e ".[test,benchmark]"
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
After creating any CUDA environment, rebuild the extension from a clean state:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
rm -rf ddtw/backend/_cpp_build
|
|
203
|
+
python -m pytest test
|
|
204
|
+
python test/benchmark_CTC.py
|
|
205
|
+
python test/benchmark_SDTW.py #--implementation ddtw
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
If the CUDA binaries don't compile, make sure the activation hooks are installed.
|
|
209
|
+
They remove confusing linker/search flags while keeping the conda compiler
|
|
210
|
+
wrappers selected:
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
bash environments/install_activation_hooks.sh
|
|
214
|
+
conda deactivate
|
|
215
|
+
conda activate ddtw_cu128
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
The `benchmark_SDTW.py` baseline by Maghoumi uses an older Numba CUDA
|
|
219
|
+
implementation, which we tested for CUDA 11.8 and 12.8. In newer CUDA environments, it may fail with Numba PTX or CUDA context errors.
|
|
220
|
+
Use `--implementation ddtw` to benchmark only the toolbox CUDA backend.
|
|
221
|
+
|
|
222
|
+
Additional development and example dependencies are only needed for specific tasks:
|
|
223
|
+
|
|
224
|
+
- `pytest` for the test suite
|
|
225
|
+
- `librosa` for the DTW and subsequence DTW reference tests
|
|
226
|
+
- Jupyter and Matplotlib for the demo notebook
|
|
227
|
+
- Sphinx and the packages in `docs/requirements.txt` for documentation builds
|
|
228
|
+
|
|
229
|
+
## Demo Notebook
|
|
230
|
+
|
|
231
|
+
The notebook `demo_SDTW.ipynb` provides a compact walkthrough of the `SDTW` loss.
|
|
232
|
+
It constructs example input sequences, runs forward and backward passes, and shows
|
|
233
|
+
how to inspect the intermediate tensors stored on `loss_fn.core`, including the
|
|
234
|
+
pairwise cost matrix `C_matrix`, accumulated cost matrix `D_matrix`, soft alignment
|
|
235
|
+
matrix `E_matrix`, and cost-gradient matrix `H_matrix`.
|
|
236
|
+
|
|
237
|
+
The final part of the notebook visualizes how different softmin temperatures
|
|
238
|
+
`gamma` change the SDTW alignment. The notebook automatically selects CUDA when
|
|
239
|
+
available and otherwise runs on CPU.
|
|
240
|
+
|
|
241
|
+
## Tests
|
|
242
|
+
|
|
243
|
+
The tests are located in `test/` and can be run from the repository root with:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
python -m pytest test
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The tests compare the toolbox implementations against reference implementations
|
|
250
|
+
for DTW, SDTW, subsequence DTW, partial matching, and CTC. They test the
|
|
251
|
+
`torch`, `cpu_numba`, and `cuda_cpp` backends where available. CUDA/Numba tests are
|
|
252
|
+
skipped automatically when no CUDA device is available or if the corresponding optional dependency or extension cannot be loaded.
|
|
253
|
+
|
|
254
|
+
## Benchmarks
|
|
255
|
+
|
|
256
|
+
Two CUDA benchmark scripts are provided under `test/`:
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
python test/benchmark_SDTW.py
|
|
260
|
+
python test/benchmark_CTC.py
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
`benchmark_SDTW.py` compares `ddtw.SDTW` with [Maghoumi's Soft-DTW](https://github.com/Maghoumi/pytorch-softdtw-cuda)
|
|
264
|
+
reference implementation. `benchmark_CTC.py` compares `ddtw.CTC` with PyTorch's
|
|
265
|
+
`torch.nn.functional.ctc_loss`. Both scripts run forward and backward passes,
|
|
266
|
+
report median timing and peak CUDA memory allocation.
|
|
267
|
+
|
|
268
|
+
## Documentation
|
|
269
|
+
|
|
270
|
+
HTML documentation is maintained with Sphinx under `docs/`.
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
python -m pip install -r docs/requirements.txt
|
|
274
|
+
sphinx-build -M html docs docs/_build
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Open `docs/_build/html/index.html` after the build finishes.
|
|
278
|
+
|
|
279
|
+
## License
|
|
280
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
281
|
+
|
|
282
|
+
## Authors
|
|
283
|
+
[Johannes Zeitler](https://audiolabs-erlangen.de/fau/assistant/zeitler)
|
|
284
|
+
|
|
285
|
+
[Meinard Müller](https://www.audiolabs-erlangen.de/fau/professor/mueller)
|
|
286
|
+
|
|
287
|
+
## Acknowledgements
|
|
288
|
+
This work was funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) under Grant No. 500643750 (MU 2686/15-1) and Grant No. 521420645 (MU 2686/17-1). The authors are with the International Audio Laboratories Erlangen, a joint institution of the Friedrich-Alexander-Universität Erlangen-Nürnberg (FAU) and Fraunhofer Institute for Integrated Circuits IIS.
|
|
289
|
+
|
|
290
|
+
The software architecture of the *d*DTW toolbox is inspired by Mehran Maghoumi's [Soft DTW for PyTorch in CUDA](https://github.com/Maghoumi/pytorch-softdtw-cuda).
|
ddtw-0.1.0/README.md
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
<p align="right">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/groupmm/dDTW/master/docs/_static/figures/logo_ddtw.png" alt="dDTW logo" width="300">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# *d*DTW: A Unified and Efficient Toolbox for Differentiable Sequence Alignment
|
|
6
|
+
|
|
7
|
+
© [Johannes Zeitler](https://audiolabs-erlangen.de/fau/assistant/zeitler) and [Meinard Müller](https://www.audiolabs-erlangen.de/fau/professor/mueller), 2026
|
|
8
|
+
|
|
9
|
+
This repository contains the *d*DTW toolbox, a modular PyTorch toolbox with efficient CPU and GPU implementations that unifies DTW, soft/smooth/sparse DTW, subsequence DTW, partial matching, and CTC in a common framework.
|
|
10
|
+
|
|
11
|
+
If you use the *d*DTW toolbox, please cite the corresponding paper:
|
|
12
|
+
```
|
|
13
|
+
@article{ZeitlerM26_dDTW_toolbox,
|
|
14
|
+
author = {Johannes Zeitler and Meinard M{\"u}ller},
|
|
15
|
+
title = {{dDTW}: A Unified and Efficient Toolbox for Differentiable Sequence Alignment},
|
|
16
|
+
year = {2026},
|
|
17
|
+
note = {Under Review}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Installation and Usage
|
|
22
|
+
|
|
23
|
+
To install the *d*DTW toolbox locally, you can clone this repository or use pip:
|
|
24
|
+
``` bash
|
|
25
|
+
pip install ddtw
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
To use a loss function from the *d*DTW toolbox, such as SDTW, simply import the module and use it like a normal PyTorch loss:
|
|
29
|
+
```python
|
|
30
|
+
from ddtw import SDTW
|
|
31
|
+
|
|
32
|
+
loss_fn = SDTW()
|
|
33
|
+
loss = loss_fn(X,Y) # assuming X and Y are, e.g., predictions and targets
|
|
34
|
+
loss.backward()
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## System Requirements
|
|
38
|
+
|
|
39
|
+
The toolbox is implemented in Python and PyTorch. A basic CPU setup requires:
|
|
40
|
+
|
|
41
|
+
- Python with a recent PyTorch installation
|
|
42
|
+
- NumPy
|
|
43
|
+
- Numba when using the `cpu_numba` backend
|
|
44
|
+
|
|
45
|
+
The `torch` backend runs without a custom compiler. The optional but recommended `cuda_cpp` backend
|
|
46
|
+
requires a CUDA-capable PyTorch installation, an NVIDIA GPU, the CUDA toolkit
|
|
47
|
+
including `nvcc`, and a working C++ compiler toolchain because the extension is
|
|
48
|
+
compiled locally through `torch.utils.cpp_extension`.
|
|
49
|
+
|
|
50
|
+
### CUDA, PyTorch, and `nvcc` Version Matching
|
|
51
|
+
|
|
52
|
+
The `cuda_cpp` backend is compiled lazily the first time it is used. This means
|
|
53
|
+
`pip install ddtw` installs the Python package and CUDA/C++ source files,
|
|
54
|
+
but the native extension is built later by PyTorch's extension loader. For this
|
|
55
|
+
build to work, three CUDA-related components must be compatible:
|
|
56
|
+
|
|
57
|
+
- The NVIDIA driver must support the CUDA runtime used by PyTorch.
|
|
58
|
+
- The installed PyTorch wheel must be built for the intended CUDA version, shown
|
|
59
|
+
by `torch.version.cuda`.
|
|
60
|
+
- The active CUDA compiler `nvcc` must be from a matching CUDA toolkit and must
|
|
61
|
+
support the GPU architecture being compiled, for example Blackwell GPUs require
|
|
62
|
+
a recent CUDA toolkit.
|
|
63
|
+
|
|
64
|
+
You can check the active environment with:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
which nvcc
|
|
68
|
+
nvcc --version
|
|
69
|
+
|
|
70
|
+
python - <<'PY'
|
|
71
|
+
import torch
|
|
72
|
+
print("torch", torch.__version__)
|
|
73
|
+
print("torch CUDA", torch.version.cuda)
|
|
74
|
+
print("CUDA available", torch.cuda.is_available())
|
|
75
|
+
print("GPU", torch.cuda.get_device_name(0) if torch.cuda.is_available() else None)
|
|
76
|
+
print("capability", torch.cuda.get_device_capability(0) if torch.cuda.is_available() else None)
|
|
77
|
+
PY
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The examples below use conda for the CUDA compiler/toolkit and pip for PyTorch.
|
|
81
|
+
They install the full CUDA toolkit in the environment so `nvcc` and development
|
|
82
|
+
headers such as `cusparse.h` are available. For up-to-date PyTorch wheel
|
|
83
|
+
commands, also check the official PyTorch install selector at
|
|
84
|
+
https://pytorch.org/get-started/locally/.
|
|
85
|
+
|
|
86
|
+
Ready-made environment files are available in `environments/` for CUDA 11.8,
|
|
87
|
+
12.8, and 13.2. Create one from the repository root and install the activation
|
|
88
|
+
hooks once:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
conda env create -f environments/ddtw_cu128.yml
|
|
92
|
+
conda activate ddtw_cu128
|
|
93
|
+
bash environments/install_activation_hooks.sh
|
|
94
|
+
conda deactivate
|
|
95
|
+
conda activate ddtw_cu128
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The hooks clear inherited compiler and CUDA flags that can otherwise make
|
|
99
|
+
PyTorch's JIT extension builder pick the wrong host compiler or CUDA toolkit.
|
|
100
|
+
|
|
101
|
+
#### Example: CUDA 11.8
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
conda create -n ddtw_cu118 python=3.11
|
|
105
|
+
conda activate ddtw_cu118
|
|
106
|
+
|
|
107
|
+
conda install -c conda-forge gcc_linux-64=11 gxx_linux-64=11
|
|
108
|
+
conda install -c nvidia/label/cuda-11.8.0 cuda
|
|
109
|
+
|
|
110
|
+
pip install torch --index-url https://download.pytorch.org/whl/cu118
|
|
111
|
+
pip install -e ".[test,benchmark]"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Example: CUDA 12.8
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
conda create -n ddtw_cu128 python=3.12
|
|
118
|
+
conda activate ddtw_cu128
|
|
119
|
+
|
|
120
|
+
conda install -c conda-forge gcc_linux-64=14 gxx_linux-64=14
|
|
121
|
+
conda install -c nvidia/label/cuda-12.8.0 cuda
|
|
122
|
+
|
|
123
|
+
pip install torch --index-url https://download.pytorch.org/whl/cu128
|
|
124
|
+
pip install -e ".[test,benchmark]"
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### Example: CUDA 13.2
|
|
128
|
+
```bash
|
|
129
|
+
conda create -n ddtw_cu132 python=3.12
|
|
130
|
+
conda activate ddtw_cu132
|
|
131
|
+
|
|
132
|
+
conda install -c conda-forge gcc_linux-64 gxx_linux-64
|
|
133
|
+
conda install -c nvidia/label/cuda-13.2 cuda
|
|
134
|
+
|
|
135
|
+
pip install torch --index-url https://download.pytorch.org/whl/cu132
|
|
136
|
+
pip install -e ".[test,benchmark]"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
After creating any CUDA environment, rebuild the extension from a clean state:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
rm -rf ddtw/backend/_cpp_build
|
|
143
|
+
python -m pytest test
|
|
144
|
+
python test/benchmark_CTC.py
|
|
145
|
+
python test/benchmark_SDTW.py #--implementation ddtw
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
If the CUDA binaries don't compile, make sure the activation hooks are installed.
|
|
149
|
+
They remove confusing linker/search flags while keeping the conda compiler
|
|
150
|
+
wrappers selected:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
bash environments/install_activation_hooks.sh
|
|
154
|
+
conda deactivate
|
|
155
|
+
conda activate ddtw_cu128
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The `benchmark_SDTW.py` baseline by Maghoumi uses an older Numba CUDA
|
|
159
|
+
implementation, which we tested for CUDA 11.8 and 12.8. In newer CUDA environments, it may fail with Numba PTX or CUDA context errors.
|
|
160
|
+
Use `--implementation ddtw` to benchmark only the toolbox CUDA backend.
|
|
161
|
+
|
|
162
|
+
Additional development and example dependencies are only needed for specific tasks:
|
|
163
|
+
|
|
164
|
+
- `pytest` for the test suite
|
|
165
|
+
- `librosa` for the DTW and subsequence DTW reference tests
|
|
166
|
+
- Jupyter and Matplotlib for the demo notebook
|
|
167
|
+
- Sphinx and the packages in `docs/requirements.txt` for documentation builds
|
|
168
|
+
|
|
169
|
+
## Demo Notebook
|
|
170
|
+
|
|
171
|
+
The notebook `demo_SDTW.ipynb` provides a compact walkthrough of the `SDTW` loss.
|
|
172
|
+
It constructs example input sequences, runs forward and backward passes, and shows
|
|
173
|
+
how to inspect the intermediate tensors stored on `loss_fn.core`, including the
|
|
174
|
+
pairwise cost matrix `C_matrix`, accumulated cost matrix `D_matrix`, soft alignment
|
|
175
|
+
matrix `E_matrix`, and cost-gradient matrix `H_matrix`.
|
|
176
|
+
|
|
177
|
+
The final part of the notebook visualizes how different softmin temperatures
|
|
178
|
+
`gamma` change the SDTW alignment. The notebook automatically selects CUDA when
|
|
179
|
+
available and otherwise runs on CPU.
|
|
180
|
+
|
|
181
|
+
## Tests
|
|
182
|
+
|
|
183
|
+
The tests are located in `test/` and can be run from the repository root with:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
python -m pytest test
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
The tests compare the toolbox implementations against reference implementations
|
|
190
|
+
for DTW, SDTW, subsequence DTW, partial matching, and CTC. They test the
|
|
191
|
+
`torch`, `cpu_numba`, and `cuda_cpp` backends where available. CUDA/Numba tests are
|
|
192
|
+
skipped automatically when no CUDA device is available or if the corresponding optional dependency or extension cannot be loaded.
|
|
193
|
+
|
|
194
|
+
## Benchmarks
|
|
195
|
+
|
|
196
|
+
Two CUDA benchmark scripts are provided under `test/`:
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
python test/benchmark_SDTW.py
|
|
200
|
+
python test/benchmark_CTC.py
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`benchmark_SDTW.py` compares `ddtw.SDTW` with [Maghoumi's Soft-DTW](https://github.com/Maghoumi/pytorch-softdtw-cuda)
|
|
204
|
+
reference implementation. `benchmark_CTC.py` compares `ddtw.CTC` with PyTorch's
|
|
205
|
+
`torch.nn.functional.ctc_loss`. Both scripts run forward and backward passes,
|
|
206
|
+
report median timing and peak CUDA memory allocation.
|
|
207
|
+
|
|
208
|
+
## Documentation
|
|
209
|
+
|
|
210
|
+
HTML documentation is maintained with Sphinx under `docs/`.
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
python -m pip install -r docs/requirements.txt
|
|
214
|
+
sphinx-build -M html docs docs/_build
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Open `docs/_build/html/index.html` after the build finishes.
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
221
|
+
|
|
222
|
+
## Authors
|
|
223
|
+
[Johannes Zeitler](https://audiolabs-erlangen.de/fau/assistant/zeitler)
|
|
224
|
+
|
|
225
|
+
[Meinard Müller](https://www.audiolabs-erlangen.de/fau/professor/mueller)
|
|
226
|
+
|
|
227
|
+
## Acknowledgements
|
|
228
|
+
This work was funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) under Grant No. 500643750 (MU 2686/15-1) and Grant No. 521420645 (MU 2686/17-1). The authors are with the International Audio Laboratories Erlangen, a joint institution of the Friedrich-Alexander-Universität Erlangen-Nürnberg (FAU) and Fraunhofer Institute for Integrated Circuits IIS.
|
|
229
|
+
|
|
230
|
+
The software architecture of the *d*DTW toolbox is inspired by Mehran Maghoumi's [Soft DTW for PyTorch in CUDA](https://github.com/Maghoumi/pytorch-softdtw-cuda).
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
##################################################################################
|
|
2
|
+
# dDTW Toolbox #
|
|
3
|
+
##################################################################################
|
|
4
|
+
# #
|
|
5
|
+
# Authors: Johannes Zeitler and Meinard Müller, 2026 #
|
|
6
|
+
# #
|
|
7
|
+
# If you use this toolbox, please cite the accompanying paper: #
|
|
8
|
+
# Johannes Zeitler and Meinard Müller. dDTW: A Unified and Efficient Toolbox for #
|
|
9
|
+
# Differentiable Sequence Alignment. Submitted 2026. #
|
|
10
|
+
##################################################################################
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
##################################################################################
|
|
14
|
+
# MIT License #
|
|
15
|
+
# #
|
|
16
|
+
# Copyright 2026 Johannes Zeitler and Meinard Müller #
|
|
17
|
+
# #
|
|
18
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy #
|
|
19
|
+
# of this software and associated documentation files (the "Software"), to deal #
|
|
20
|
+
# in the Software without restriction, including without limitation the rights #
|
|
21
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
|
|
22
|
+
# copies of the Software, and to permit persons to whom the Software is #
|
|
23
|
+
# furnished to do so, subject to the following conditions: #
|
|
24
|
+
# #
|
|
25
|
+
# The above copyright notice and this permission notice shall be included in all #
|
|
26
|
+
# copies or substantial portions of the Software. #
|
|
27
|
+
# #
|
|
28
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
|
|
29
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
|
|
30
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
|
|
31
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
|
|
32
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
|
|
33
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
|
|
34
|
+
# SOFTWARE. #
|
|
35
|
+
##################################################################################
|
|
36
|
+
|
|
37
|
+
"""Public package interface for the dDTW toolbox."""
|
|
38
|
+
|
|
39
|
+
from .ddtw import dDTW
|
|
40
|
+
from .ddtw_variants import CTC
|
|
41
|
+
from .ddtw_variants import DTW
|
|
42
|
+
from .ddtw_variants import SDTW
|
|
43
|
+
from .ddtw_variants import partial_matching
|
|
44
|
+
from .ddtw_variants import smoothDTW
|
|
45
|
+
from .ddtw_variants import sparseDTW
|
|
46
|
+
from .ddtw_variants import subSDTW
|
|
47
|
+
|
|
48
|
+
__all__ = [
|
|
49
|
+
"dDTW",
|
|
50
|
+
"SDTW",
|
|
51
|
+
"DTW",
|
|
52
|
+
"smoothDTW",
|
|
53
|
+
"sparseDTW",
|
|
54
|
+
"subSDTW",
|
|
55
|
+
"CTC",
|
|
56
|
+
"partial_matching",
|
|
57
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
##################################################################################
|
|
2
|
+
# dDTW Toolbox #
|
|
3
|
+
##################################################################################
|
|
4
|
+
# #
|
|
5
|
+
# Authors: Johannes Zeitler and Meinard Müller, 2026 #
|
|
6
|
+
# #
|
|
7
|
+
# If you use this toolbox, please cite the accompanying paper: #
|
|
8
|
+
# Johannes Zeitler and Meinard Müller. dDTW: A Unified and Efficient Toolbox for #
|
|
9
|
+
# Differentiable Sequence Alignment. Submitted 2026. #
|
|
10
|
+
##################################################################################
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
##################################################################################
|
|
14
|
+
# MIT License #
|
|
15
|
+
# #
|
|
16
|
+
# Copyright 2026 Johannes Zeitler and Meinard Müller #
|
|
17
|
+
# #
|
|
18
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy #
|
|
19
|
+
# of this software and associated documentation files (the "Software"), to deal #
|
|
20
|
+
# in the Software without restriction, including without limitation the rights #
|
|
21
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
|
|
22
|
+
# copies of the Software, and to permit persons to whom the Software is #
|
|
23
|
+
# furnished to do so, subject to the following conditions: #
|
|
24
|
+
# #
|
|
25
|
+
# The above copyright notice and this permission notice shall be included in all #
|
|
26
|
+
# copies or substantial portions of the Software. #
|
|
27
|
+
# #
|
|
28
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
|
|
29
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
|
|
30
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
|
|
31
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
|
|
32
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
|
|
33
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
|
|
34
|
+
# SOFTWARE. #
|
|
35
|
+
##################################################################################
|
|
36
|
+
|
|
37
|
+
"""Backend implementations for dDTW."""
|