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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Neural network layers: the layer subsystem aggregator
//!
//! Declares every layer submodule and glob-re-exports the public layer types. It also defines
//! the infrastructure shared across the subsystem. The
//! [`ParamCounts`] report says how many parameter
//! elements a layer holds, split into trainable and non-trainable.
//!
//! 5 macros remove the boilerplate that a layer would otherwise repeat by hand.
//!
//! 2 macros give a layer its weight methods. `no_trainable_parameters_layer_functions` emits
//! the stubs of a parameter-free layer. `named_weight_layer_functions` builds the named array
//! list of a layer that holds arrays, from a compact `kind "name" => field` roster.
//! `optional_named_weight` is the helper that the second macro expands into, and it is not
//! meant to be called on its own.
//!
//! The other 2 macros cover the build-shape report of a layer that holds a `built: Option<Shape>`
//! field. `built_layer_shape_functions` emits `known_input_shapes`, `is_built`, and
//! `build_config` together. `build_config_function` emits `is_built` and `build_config` alone,
//! for a layer that writes its own `known_input_shapes`.
//!
//! The submodules fall into a few categories:
//!
//! - Core layers (re-exported):
//! - [`activation`]
//! - [`border`]
//! - [`convolution`]
//! - [`dense`]
//! - [`embedding`]
//! - [`flatten`]
//! - [`identity`]
//! - [`merge`]
//! - [`permute`]
//! - [`pooling`]
//! - [`recurrent`]
//! - [`regularization`]
//! - [`repeat_vector`]
//! - [`rescaling`]
//! - [`reshape`]
//! - [`reverse`]
//! - [`upsampling`]
//! - Shared (private) helpers: `conv_op_helpers` (the depthwise convolution kernel, shared by
//! the depthwise and separable convolution layers) and `shape_helpers` (pooling/convolution
//! output-shape calculators)
//! - Validation: `validation` (shared input/weight checks)
//! - Serialization: [`checkpoint`]
//! (the named on-disk format, and the load that applies it)
//!
//! [`ParamCounts`]: crate::neural_network::layers::ParamCounts
//! [`activation`]: crate::neural_network::layers::activation
//! [`border`]: crate::neural_network::layers::border
//! [`convolution`]: crate::neural_network::layers::convolution
//! [`dense`]: crate::neural_network::layers::dense
//! [`embedding`]: crate::neural_network::layers::embedding
//! [`flatten`]: crate::neural_network::layers::flatten
//! [`identity`]: crate::neural_network::layers::identity
//! [`merge`]: crate::neural_network::layers::merge
//! [`permute`]: crate::neural_network::layers::permute
//! [`pooling`]: crate::neural_network::layers::pooling
//! [`recurrent`]: crate::neural_network::layers::recurrent
//! [`regularization`]: crate::neural_network::layers::regularization
//! [`repeat_vector`]: crate::neural_network::layers::repeat_vector
//! [`rescaling`]: crate::neural_network::layers::rescaling
//! [`reshape`]: crate::neural_network::layers::reshape
//! [`reverse`]: crate::neural_network::layers::reverse
//! [`upsampling`]: crate::neural_network::layers::upsampling
//! [`checkpoint`]: crate::neural_network::layers::checkpoint
/// How many parameter elements a layer holds, split by whether training updates them
///
/// The 2 counts are independent, and a layer reports both. A Dense layer holds trainable
/// elements only. A pooling or activation layer holds none of either.
/// [`BatchNormalization`] holds both: `gamma` and `beta` are trainable, and the running mean
/// and the running variance are not. The running statistics move on every training forward
/// pass. No optimizer updates them, so they stay non-trainable
///
/// [`Sequential::summary`](crate::neural_network::sequential::Sequential::summary) adds the 2
/// columns over the model and prints the total of both
/// Activation layers for neural networks
/// Padding with 0 and cropping layers that resize the spatial axes at their ends
/// The named checkpoint format: what a saved model holds, and how a load applies it
/// The depthwise convolution kernel, shared by the depthwise and separable convolution layers
/// Convolutional layer for neural networks
/// Dense (Fully Connected) layer implementation for neural networks
/// A trainable lookup table that turns whole-number indices into dense vectors
/// A layer that flattens a 3D, 4D, or 5D tensor into a 2D tensor
/// A layer that passes its input through unchanged
/// Layers that take several inputs and reduce or join them into 1 output
/// A layer that reorders the axes after the batch axis
/// Pooling layer for neural networks
/// Recurrent layer for neural networks
/// Regularization layers for neural networks
/// A layer that repeats a feature vector into a sequence
/// A layer that applies a fixed affine map to every element
/// A layer that rewrites the axes after the batch axis into a target shape
/// A layer that reverses the order along 1 axis
/// Output-shape calculators for pooling and convolution layers
/// Upsampling layers that enlarge the spatial axes by a whole-number factor
/// Shared input/weight validation for the layer module
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// Generates the trait method stubs for layers without trainable parameters
///
/// Such layers rely on the default [`LayerBase::parameters_mut`] (an empty list, so the optimizer
/// skips them). This macro supplies the remaining required `param_count`, `weights`, and
/// `weights_mut` methods
///
/// It is path-exported via a `pub(in ...) use` re-export, so callers import it explicitly
/// rather than depending on textual macro ordering:
/// `use crate::neural_network::layers::no_trainable_parameters_layer_functions;`
///
/// The generated `param_count` returns `ParamCounts::none()`, and both weight methods return
/// the empty vector, so such a layer contributes no path to a checkpoint
///
/// The 3 methods cover the arrays of the layer itself. A layer that holds sublayers can use
/// this macro, because a model reaches the arrays of each sublayer through
/// [`LayerBase::sublayers`]
///
/// [`LayerBase::parameters_mut`]: crate::neural_network::traits::LayerBase::parameters_mut
/// [`LayerBase::sublayers`]: crate::neural_network::traits::LayerBase::sublayers
pub use no_trainable_parameters_layer_functions;
/// Generates the `weights` and `weights_mut` methods of a layer that holds arrays
///
/// 1 list serves both directions, so the name, the kind, and the order of an array cannot
/// drift between the read and write paths. The checkpoint format reads the name and the kind
/// from it, so this list gives the last part of every checkpoint path of the layer
///
/// The list holds the arrays of the layer itself. A layer that holds sublayers does not put
/// the arrays of a sublayer in this list. The model reaches them through
/// [`LayerBase::sublayers`]
///
/// Each entry reads `trainable "<name>" => <field>` or `non_trainable "<name>" => <field>`.
/// The name is the name of the array in the checkpoint, and the field is the field of the
/// layer struct that holds it. A field of a nested struct is written with dots, such as
/// `gates.kernel`. The 2 halves stay next to each other, so a renamed field breaks this list
/// instead of leaving a stale name behind. The dots of a field are Rust syntax alone. The name
/// must not hold a `.`, because a checkpoint path joins its parts with `.`
///
/// An entry that ends in `if <flag>` is optional. The flag is a `bool` field of the same
/// layer, and the array reaches the list only while the flag is true. `Dense` writes
/// `trainable "bias" => bias if use_bias`. A layer built with `use_bias` set to false exposes
/// the kernel alone, so its checkpoint holds 1 path. An optional entry moves no other entry,
/// because a checkpoint addresses an array by name and never by position
///
/// Give each array the same name that [`LayerBase::parameters_mut`] gives it. Both views must
/// refer to the same storage. An optimizer writes through the parameter view, and a checkpoint
/// reads through the weight view.
///
/// It is path-exported like `no_trainable_parameters_layer_functions`:
/// `use crate::neural_network::layers::named_weight_layer_functions;`
///
/// [`LayerBase::parameters_mut`]: crate::neural_network::traits::LayerBase::parameters_mut
/// [`LayerBase::sublayers`]: crate::neural_network::traits::LayerBase::sublayers
/// Reports weights mut.
///
/// # Returns
///
/// - `Vec<WeightMut<'_>>` - The named write views of the layer arrays.
};
}
pub use named_weight_layer_functions;
/// Generates the 2 build reports of a layer that holds a `built: Option<Shape>` field
///
/// [`LayerBase::known_input_shapes`] gives the shape the layer built for, and
/// [`LayerBase::build_config`] gives the same shape with the batch axis freed, which is what a
/// checkpoint records. A layer whose displayed input shape comes from somewhere else uses
/// `build_config_function` instead and writes its own `known_input_shapes`
///
/// It is path-exported like `no_trainable_parameters_layer_functions`:
/// `use crate::neural_network::layers::built_layer_shape_functions;`
///
/// [`LayerBase::known_input_shapes`]: crate::neural_network::traits::LayerBase::known_input_shapes
/// [`LayerBase::build_config`]: crate::neural_network::traits::LayerBase::build_config
pub use built_layer_shape_functions;
/// Generates [`LayerBase::build_config`] of a layer that holds a `built: Option<Shape>` field
///
/// The generated `is_built` reads the `built` field alone. A layer that holds sublayers must
/// report itself as built only when every sublayer is built, so it writes its own `is_built`
///
/// [`LayerBase::build_config`]: crate::neural_network::traits::LayerBase::build_config
pub use build_config_function;
/// Wraps 1 named array as the `Option` that `named_weight_layer_functions` collects
///
/// The rule with 1 argument takes an array that the layer always holds, and it is always
/// `Some`. The rule with 2 takes an optional array and the flag that decides whether the layer
/// holds it. Splitting the 2 rules keeps the always-present case free of any run-time test
pub use optional_named_weight;