Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #949.
ctc_loss(ŷ, Int[])and its gradient threwBoundsError: 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_blanksreturns just the blank, soαandβhave a single row. Three places assumed at least three rows:α[2, 1] = ..., the losslogaddexp(α[end, T], α[end-1, T])inctc_alpha, andβ[U′-1, T] = 0in∇ctc_loss.Fix (
src/ctc.jl): skip the second-row initialisations whenU′ == 1, and take the loss as-α[1, T]in that case. The recursions already handle a single row. The gradient is thensoftmax(ŷ)minus one in the blank row of each column, as expected.The CUDA
ctc_alphadoesn't throw here, but it computes the loss aslogsumexp(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 readsalphas[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 (stubbedthreadIdx/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, :])andsoftmax(x) .- [0, 0, 1]on the two hand-checked inputs already in the file and on a single time step. These checks use plaingradient, nottest_gradients, so they add little compile time.test/gpu/cuda/ctc.jlcompares the CUDA loss and gradient for an empty target with the CPU ones. On master,test/runtests.jl cpu/ctcgives 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 ofEnzyme.gradientthrough NNlib'sctc_lossrule with an empty target also matches.This PR was written with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code