Skip to content

Fix IndexError in alignment.py when token id == vocab size (clamp upper bound)#1215

Open
uduntuntu wants to merge 3 commits intom-bain:mainfrom
uduntuntu:patch-1
Open

Fix IndexError in alignment.py when token id == vocab size (clamp upper bound)#1215
uduntuntu wants to merge 3 commits intom-bain:mainfrom
uduntuntu:patch-1

Conversation

@uduntuntu
Copy link
Copy Markdown

Reproduction on Finnish with alignment showed IndexError: index X is out of bounds for dimension 0 with size V.

Cause: only lower bound was clamped (min=0), so token==V indexed out of range.

Fix: clamp tokens to [0, V-1] and assert valid blank_id/non-empty emissions.

Included minimal repro script and observed OK results after patch.

# Run (in virtualenv):  python test_alignment_out_of_bounds.py
import torch
from whisperx import alignment as A

def run_case(V, tokens, blank_id=0, label=""):
    fe = torch.randn(V, dtype=torch.float32)
    t = torch.tensor(tokens, dtype=torch.long)
    try:
        out = A.get_wildcard_emission(fe, t, blank_id)
        ok = bool(torch.isfinite(out).all())
        print(f"[OK ] {label:40s} -> out.shape={tuple(out.shape)} finite={ok}")
    except Exception as e:
        print(f"[ERR] {label:40s} -> {type(e).__name__}: {e}")

print("Testing whisperx.alignment.get_wildcard_emission")
print("Function:", A.get_wildcard_emission.__code__.co_filename)

# OFF-BY-ONE repro: token == vocab size (V)
run_case(34, [34], 0, "off-by-one (V=34, token=V)")

# Mixed case: wildcard + valid + token==V
run_case(34, [-1, 0, 5, 34], 0, "wildcard + in-range + token=V")

# Edge case: empty emissions (should assert / error, but not IndexError from indexing)
run_case(0, [0], 0, "empty emissions (V=0)")

bjollans and others added 3 commits June 7, 2025 12:21
…below.

# Run:  python test_alignment_out_of_bounds.py
import torch
from whisperx import alignment as A

def run_case(V, tokens, blank_id=0, label=""):
    fe = torch.randn(V, dtype=torch.float32)
    t = torch.tensor(tokens, dtype=torch.long)
    try:
        out = A.get_wildcard_emission(fe, t, blank_id)
        ok = bool(torch.isfinite(out).all())
        print(f"[OK ] {label:36s} -> out.shape={tuple(out.shape)} finite={ok}")
    except Exception as e:
        print(f"[ERR] {label:36s} -> {type(e).__name__}: {e}")

print("Testing whisperx.alignment.get_wildcard_emission")
print("Function:", A.get_wildcard_emission.__code__.co_filename)

# OFF-BY-ONE repro: token equals vocab size (used to crash)
run_case(34, [34], 0, "off-by-one (V=34, token=V)")

# Mixed case: wildcard + valid + token==V
run_case(34, [-1, 0, 5, 34], 0, "wildcard + in-range + token=V")

# Edge case: empty emissions -> assert (not IndexError)
run_case(0, [0], 0, "empty emissions (V=0)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants