Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

word-level timestamps in transcribe() #869

Merged
merged 26 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8f9357f
word-level timestamps in `transcribe()`
jongwook Jan 20, 2023
46ea501
moving to `timing.py`
jongwook Jan 21, 2023
cfd2b81
Merge branch 'main' into word-level-timestamps
jongwook Jan 21, 2023
742d2f4
numba implementation for dtw, replacing dtw-python
jongwook Jan 22, 2023
fb12414
Merge branch 'main' into word-level-timestamps
jongwook Jan 22, 2023
80331c0
triton implementation for dtw
jongwook Jan 23, 2023
1d2ed66
add test for dtw implementations
jongwook Jan 23, 2023
b61e8f4
triton implementation of median_filter
jongwook Jan 24, 2023
54f2901
a simple word-level timestamps test
jongwook Jan 24, 2023
8ce6277
add scipy as dev dependency
jongwook Jan 24, 2023
812f446
Merge branch 'main' into word-level-timestamps
jongwook Jan 24, 2023
cd5191f
installs an older version of Triton if CUDA < 11.4
jongwook Jan 24, 2023
f64d8bc
Merge branch 'main' into word-level-timestamps
jongwook Jan 24, 2023
89133bd
Merge branch 'main' into word-level-timestamps
jongwook Jan 24, 2023
d4f9399
fix broken merge
jongwook Jan 24, 2023
040aa04
Merge branch 'main' into word-level-timestamps
jongwook Jan 24, 2023
8e2756b
loosen nvcc version match regex
jongwook Jan 25, 2023
6c431c4
find_alignment() function
jongwook Jan 25, 2023
ff6cbfd
Merge branch 'main' into word-level-timestamps
jongwook Feb 2, 2023
5fa4356
miscellaneous improvements
jongwook Feb 2, 2023
48537aa
skip median filtering when the input is too small
jongwook Feb 2, 2023
8eb29c3
Expose punctuation options in cli and transcribe() (#973)
ryanheise Feb 16, 2023
6ed4c11
Merge branch 'main' into word-level-timestamps
jongwook Mar 6, 2023
31cd418
fix merge error
jongwook Mar 6, 2023
145f325
fix merge error 2
jongwook Mar 6, 2023
2b079c4
annotating that word_timestamps is experimental
jongwook Mar 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
triton implementation of median_filter
  • Loading branch information
jongwook committed Jan 24, 2023
commit b61e8f4fd1b912b8d13ec13800bbf80d73905894
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import random as rand

import numpy
import pytest


@pytest.fixture
def random():
rand.seed(42)
numpy.random.seed(42)
33 changes: 29 additions & 4 deletions tests/test_timing.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import pytest
import numpy as np
import scipy.ndimage
import torch

from whisper.timing import dtw_cpu, dtw_cuda
from whisper.timing import dtw_cpu, dtw_cuda, median_filter


sizes = [
(10, 20), (32, 16), (123, 1500), (234, 189)
(10, 20), (32, 16), (123, 1500), (234, 189),
]
shapes = [
(4, 5, 20, 345), (6, 12, 240, 512),
]


@pytest.mark.parametrize("N, M", sizes)
def test_dtw(N: int, M: int):
Copy link

@saunair saunair Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was your reason to not use the dtw library licensing concerns or just speedup?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtw-python is GPL, as mentioned here -
#869 (comment)

np.random.seed(42)
steps = np.concatenate([np.zeros(N - 1), np.ones(M - 1)])
np.random.shuffle(steps)
x = np.random.random((N, M)).astype(np.float32)
Expand Down Expand Up @@ -47,11 +50,33 @@ def test_dtw(N: int, M: int):
@pytest.mark.requires_cuda
@pytest.mark.parametrize("N, M", sizes)
def test_dtw_cuda_equivalence(N: int, M: int):
np.random.seed(42)
x_numpy = np.random.randn(N, M).astype(np.float32)
x_cuda = torch.from_numpy(x_numpy).cuda()

trace_cpu = dtw_cpu(x_numpy)
trace_cuda = dtw_cuda(x_cuda)

assert np.allclose(trace_cpu, trace_cuda)


@pytest.mark.parametrize("shape", shapes)
def test_median_filter(shape):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Is there a licensing issue using scipy.median_filter or is this cuda implementation just faster?

x = torch.randn(*shape)

for filter_width in [3, 5, 7, 13]:
filtered = median_filter(x, filter_width)
scipy_filtered = scipy.ndimage.median_filter(x, (1, 1, 1, filter_width), mode="nearest")

assert np.allclose(filtered, scipy_filtered)


@pytest.mark.requires_cuda
@pytest.mark.parametrize("shape", shapes)
def test_median_filter_equivalence(shape):
x = torch.randn(*shape)

for filter_width in [3, 5, 7, 13]:
filtered_cpu = median_filter(x, filter_width)
filtered_gpu = median_filter(x.cuda(), filter_width).cpu()

assert np.allclose(filtered_cpu, filtered_gpu)
10 changes: 7 additions & 3 deletions whisper/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ def median_filter(x: torch.Tensor, filter_width: int):
assert 3 <= x.ndim <= 4, "`median_filter()` is implemented for only 3D or 4D tensors"
assert filter_width > 0 and filter_width % 2 == 1, "`filter_width` should be an odd number"

padded = F.pad(x, (0, 0, filter_width // 2, filter_width // 2), mode='replicate')
slices = padded.unfold(-1, filter_width, 1)
return slices.median(dim=-1).values
x = F.pad(x, (filter_width // 2, filter_width // 2, 0, 0), mode='replicate')
if x.is_cuda:
from .triton_ops import median_filter_cuda
return median_filter_cuda(x, filter_width)

# sort() is faster than torch.median (https://github.com/pytorch/pytorch/issues/51450)
return x.unfold(-1, filter_width, 1).sort()[0][..., filter_width // 2]


@numba.jit
Expand Down
59 changes: 59 additions & 0 deletions whisper/triton_ops.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import math

import numpy as np
import torch
from functools import lru_cache

try:
import triton
import triton.language as tl
Expand Down Expand Up @@ -31,3 +37,56 @@ def dtw_kernel(cost, trace, x, x_stride, cost_stride, trace_stride, N, M, BLOCK_
tl.store(trace_ptr + offsets, 2, mask=mask & (c2 <= c0) & (c2 <= c1))
tl.store(trace_ptr + offsets, 1, mask=mask & (c1 <= c0) & (c1 <= c2))
tl.store(trace_ptr + offsets, 0, mask=mask & (c0 <= c1) & (c0 <= c2))


@lru_cache(maxsize=None)
def median_kernel(filter_width: int):
@triton.jit
def kernel(y, x, x_stride, y_stride, BLOCK_SIZE: tl.constexpr): # x.shape[-1] == filter_width
row_idx = tl.program_id(0)
offsets = tl.arange(0, BLOCK_SIZE)
mask = offsets < y_stride

x_ptr = x + row_idx * x_stride
y_ptr = y + row_idx * y_stride

LOAD_ALL_ROWS_HERE

BUBBLESORT_HERE

tl.store(y_ptr + offsets, MIDDLE_ROW_HERE, mask=mask)

kernel = triton.JITFunction(kernel.fn)
kernel.src = kernel.src.replace(" LOAD_ALL_ROWS_HERE", "\n".join([
f" row{i} = tl.load(x_ptr + offsets + {i}, mask=mask)"
for i in range(filter_width)
]))
kernel.src = kernel.src.replace(" BUBBLESORT_HERE", "\n\n".join([
"\n\n".join([
"\n".join([
f" smaller = tl.where(row{j} < row{j + 1}, row{j}, row{j + 1})",
f" larger = tl.where(row{j} > row{j + 1}, row{j}, row{j + 1})",
f" row{j} = smaller",
f" row{j + 1} = larger",
])
for j in range(filter_width - i - 1)
])
for i in range(filter_width // 2 + 1)
]))
kernel.src = kernel.src.replace("MIDDLE_ROW_HERE", f"row{filter_width // 2}")

return kernel


def median_filter_cuda(x: torch.Tensor, filter_width: int):
"""Apply a median filter of given width along the last dimension of x"""
slices = x.contiguous().unfold(-1, filter_width, 1)
grid = np.prod(slices.shape[:-2])

kernel = median_kernel(filter_width)
y = torch.empty_like(slices[..., 0])

BLOCK_SIZE = 1 << (y.stride(-2) - 1).bit_length()
kernel[(grid,)](y, x, x.stride(-2), y.stride(-2), BLOCK_SIZE=BLOCK_SIZE)

return y