1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Connectionist temporal classification over backend-owned log probabilities.
use ;
/// Concatenated target labels and valid sequence lengths for a CTC batch.
///
/// Each length slice has one entry per sample. Targets contain exactly the
/// sum of `target_lengths` labels; every label is in range and differs from
/// `blank`. Input lengths cannot exceed the log-probability frame extent.
/// Mean CTC loss and additive derivatives with respect to log probabilities.
///
/// Inputs have shape `[frames, batch, classes]`. Reduction is the batch mean
/// of each sample's negative log-likelihood divided by its target length,
/// with an empty target using divisor one. Backward differentiates independent
/// log probabilities; composition with log-softmax produces logit gradients.
///
/// CPU backends borrow storage for the Leto provider. This separate capability
/// adds no CTC requirement to backends that do not implement sequence alignment.
///
/// # Examples
///
/// One frame and an empty target admit only the blank alignment. The derivative
/// with respect to its log probability is minus one.
///
/// ```
/// use coeus_core::{CpuAddressableStorage, CpuStorage, Layout, SequentialBackend};
/// use coeus_ops::{CtcBatch, CtcOps};
///
/// let backend = SequentialBackend::new();
/// let layout = Layout::new([1, 1, 2].into());
/// let scalar = Layout::new([1].into());
/// let input = CpuStorage::from_slice(&[-std::f32::consts::LN_2; 2]);
/// let mut loss = CpuStorage::from_slice(&[0.0_f32]);
/// let state = backend.ctc_forward(&input, &layout,
/// CtcBatch { targets: &[], input_lengths: &[1], target_lengths: &[0], blank: 0 },
/// &mut loss, &scalar)?;
/// assert_eq!(loss.as_slice(), &[std::f32::consts::LN_2]);
/// let upstream = CpuStorage::from_slice(&[1.0_f32]);
/// let mut gradient = CpuStorage::from_slice(&[0.0_f32; 2]);
/// backend.ctc_backward_accumulate(&state, &upstream, &scalar, &mut gradient, &layout)?;
/// assert_eq!(gradient.as_slice(), &[-1.0, 0.0]);
/// # Ok::<(), coeus_core::BackendError>(())
/// ```