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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! The protocol that 1 recurrent cell follows
//!
//! A recurrent layer is 2 separable parts. The first part is the scaffolding. It reads the
//! feature count from the input shape and draws the gates. It projects the whole input in 1
//! matrix product, walks the time axis, and keeps what the backward pass needs. The scaffolding
//! also reduces the per-step gate gradients into 1 gradient per array. That part is the same
//! for every cell, and [`Rnn`](super::rnn::Rnn) holds the only copy of it.
//!
//! The second part is the arithmetic of 1 timestep. That part is what makes a SimpleRNN
//! different from an LSTM. [`RnnCell`] is the protocol for it.
//!
//! The protocol is internal to the crate and it is sealed. The 3 cells of the crate are the only
//! implementations, so the shape of the protocol can change with no effect on any public name.
use crateError;
use crate;
use crateFusedGates;
use ;
use Cow;
/// 1 group of gate blocks, and the array that their recurrent projection consumed
///
/// The recurrent kernel packs 1 column block per gate. The gradient of that kernel is the
/// product of whatever entered the recurrent projection against the pre-activation gradient of
/// the gate. Most cells project the previous hidden state into every gate, so 1 product covers
/// the whole kernel. A GRU projects the previous hidden state into its update and reset blocks,
/// and `r_t .* h_prev` into its candidate block. A GRU therefore needs 2 products.
///
/// The groups of a cell must cover every gate block exactly once. A gap leaves a column block of
/// the gradient at 0, which stops 1 gate from learning and reports no error at all.
pub
/// The arithmetic of 1 timestep of 1 recurrent layer
///
/// [`Rnn`](super::rnn::Rnn) holds a cell and drives it. The cell holds no weight and no state of
/// its own beyond its activation. The weights arrive as a [`FusedGates`] argument, and the
/// recurrent state arrives as a slice that the cell reads and then overwrites.
///
/// # Notes
///
/// Every array of the protocol has the shape `[batch, units]`, except the projected input, which
/// is `[batch, gate count * units]`. The cell reads the unit count from the gates with
/// [`FusedGates::units`], so no cell holds a second copy of that number.
pub
/// The result of 1 activation inside a cell step
///
/// `output` is the activated tensor that the step uses. [`Activated::into_record`] gives the
/// tensor that the backward pass of the activation reads.
pub
/// Applies the activation of a cell to a pre-activation
///
/// A training pass of an [`ActivationInput::PreActivation`] activation keeps `z`. Every other
/// pass drops `z`. No pass copies the output.
///
/// # Parameters
///
/// - `activation` - Activation of the cell
/// - `z` - The pre-activation
/// - `training` - True when the step parks a record
///
/// # Returns
///
/// - `Result<Activated, Error>` - The output, and the pre-activation when the step keeps it
///
/// # Errors
///
/// - [`Error::InvalidInput`] - If the activation refuses its input
pub
/// Builds the activation cache from 1 record
///
/// # Parameters
///
/// - `activation` - Activation of the cell
/// - `record` - The tensor that [`Activated::into_record`] gave
///
/// # Returns
///
/// - `ActivationCache` - A cache that holds a copy of `record`
pub
/// Gives the activated output that 1 record stands for
///
/// An [`ActivationInput::Output`] record is the output, and the function borrows it. An
/// [`ActivationInput::PreActivation`] record is `z`, and the function runs the forward pass
/// again.
///
/// # Parameters
///
/// - `activation` - Activation of the cell
/// - `cache` - The cache that [`record_cache`] built from `record`
/// - `record` - The tensor that [`Activated::into_record`] gave
///
/// # Returns
///
/// - `Result<Cow<'a, Array2<f32>>, Error>` - The activated output
///
/// # Errors
///
/// - [`Error::InvalidInput`] - If the activation refuses the cache
pub
/// Runs the backward pass of the activation of a cell
///
/// # Parameters
///
/// - `activation` - Activation of the cell
/// - `cache` - The cache that [`record_cache`] built
/// - `grad_output` - The gradient of the activated output
///
/// # Returns
///
/// - `Result<Array2<f32>, Error>` - The gradient of the pre-activation
///
/// # Errors
///
/// - [`Error::InvalidInput`] - If the activation refuses the cache
/// - [`Error::ShapeMismatch`] - If `grad_output` and the cache differ in shape
pub
/// Converts a tensor that an activation gave back to 2 dimensions
///
/// An activation keeps the shape of its input, and every cell gives it a 2D input
pub