Skip to content

Handle an empty target in ctc_loss - #952

Open
KainanP wants to merge 1 commit into
FluxML:masterfrom
KainanP:ctc-empty-target
Open

KainanP wants to merge 1 commit into
FluxML:masterfrom
KainanP:ctc-empty-target

Conversation

@KainanP

@KainanP KainanP commented Dec 3, 2026

Copy link
Copy Markdown

Fixes #949.

ctc_loss(ŷ, Int[]) and its gradient threw BoundsError: attempt to access 1-element Vector{Int64} at index [2]. An empty target is a valid CTC label sequence: its only alignment is a blank at every time step, so the loss is -sum(logsoftmax(ŷ)[blank, :]).

Cause: with y = Int[], add_blanks returns just the blank, so α and β have a single row. Three places assumed at least three rows: α[2, 1] = ..., the loss logaddexp(α[end, T], α[end-1, T]) in ctc_alpha, and β[U′-1, T] = 0 in ∇ctc_loss.

Fix (src/ctc.jl): skip the second-row initialisations when U′ == 1, and take the loss as -α[1, T] in that case. The recursions already handle a single row. The gradient is then softmax(ŷ) minus one in the blank row of each column, as expected.

The CUDA ctc_alpha doesn't throw here, but it computes the loss as logsumexp(alphas[end-1:end]) with linear indexing. For a one-row matrix those are α[1, T-1] and α[1, T], so the loss would be wrong. It now reads alphas[max(1, U′-1):U′, T], which is the same pair as before for non-empty targets. I found this by reading the code. I have no GPU, so this part and its test were not run on CUDA. As a check, I ran the two CUDA kernels on the CPU with a single thread (stubbed threadIdx/sync_threads, in a local script that is not part of this PR). With the new line they reproduce the CPU loss and gradient for both empty and non-empty targets. With the old line the loss for an empty target is wrong.

Tests: in test/cpu/ctc.jl, the loss and Zygote gradient for an empty target are checked against -sum(logsoftmax(x)[end, :]) and softmax(x) .- [0, 0, 1] on the two hand-checked inputs already in the file and on a single time step. These checks use plain gradient, not test_gradients, so they add little compile time. test/gpu/cuda/ctc.jl compares the CUDA loss and gradient for an empty target with the CPU ones. On master, test/runtests.jl cpu/ctc gives 5 passed and 6 errored (the new checks, all with the BoundsError). With this PR it passes 11/11 (Julia 1.13.4, full test environment including Enzyme). A separate check of Enzyme.gradient through NNlib's ctc_loss rule with an empty target also matches.

This PR was written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

With `y = Int[]`, `add_blanks` returns the single blank, so the
alpha/beta matrices have one row. `ctc_alpha` still set `α[2, 1]` and
took the loss from `α[end, T]` and `α[end-1, T]`, and `∇ctc_loss` set
`β[U′-1, T]`, so both the loss and its gradient threw a BoundsError.

An empty target is aligned only by a blank at every step: the loss is
`-α[1, T]`, the sum of the blank's negative log-probabilities. Skip the
second row when there is none and take the loss from the single row.
The recursions need no change. The gradient is then `softmax(ŷ)` minus
one for the blank in each column.

The CUDA `ctc_alpha` read the last two entries of `alphas` with linear
indexing, which for a one-row matrix are `α[1, T-1]` and `α[1, T]`, so
it returned a wrong loss instead of throwing. Index the last column
explicitly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

ctc_loss throws a BoundsError for an empty target sequence

1 participant