PyTorch adds CTC loss backward pass for MPS on Apple Silicon
Apple Silicon users can now train CTC-based speech models natively.
PyTorch has officially added the backward pass for `ctc_loss` on Apple's Metal Performance Shaders (MPS) backend in commit aab577b, closing issue #160828. The implementation breaks the gradient computation into two kernel calls: one calculates `log_beta` and another performs a logsumexp reduction of `log_beta + log_alpha` to write the input gradient. The reduction step is parallelized by creating one thread per `T * N`, where `log_probs.shape = (T, N, C)` — with `T` as input length and `N` as batch size. Each thread iterates over the target sequence, applying the formula `∇y = (y - exp(R + L - log y)) ∇L` to assign gradients.
This pull request, co-authored with Claude, avoids code duplication by refactoring the existing `log_alpha` code to support conditional calculation of `log_beta`. The move enables native training of CTC (Connectionist Temporal Classification) loss on Apple Silicon GPUs—critical for speech recognition, OCR, and sequence alignment tasks. Previously, users had to fall back to CPU or CUDA, limiting performance on Mac hardware. Developers can test the feature on the latest nightly builds of PyTorch.
- Backward pass uses two kernel calls: log_beta and logsumexp reduction
- Parallelized with one thread per T * N (input length × batch size)
- Co-authored with Claude; closes issue #160828 for MPS support
Why It Matters
Enables efficient CTC loss training on Apple Silicon, expanding PyTorch's MPS coverage for speech models.