concision_core/models/traits/
format.rs

1/*
2    Appellation: format <module>
3    Created At: 2026.01.06:14:38:32
4    Contrib: @FL03
5*/
6/// The [`RawModelLayout`] trait defines a minimal interface for objects capable of representing
7/// the _layout_; i.e. the number of input, hidden, and output features of a neural network
8/// model containing some number of hidden layers.
9///
10/// **Note**: This trait is implemented for the 3- and 4-tuple consiting of usize elements as
11/// well as for the `[usize; 3]` and `[usize; 4]` array types. In both these instances, the
12/// elements are ordered as (input, hidden, output) for the 3-element versions, and
13/// (input, hidden, output, layers) for the 4-element versions.
14pub trait RawModelLayout {
15    /// returns the total number of input features defined for the model
16    fn input(&self) -> usize;
17    /// returns the number of hidden features for the model
18    fn hidden(&self) -> usize;
19    /// the number of output features for the model
20    fn output(&self) -> usize;
21    /// returns the number of hidden layers within the network
22    fn depth(&self) -> usize;
23
24    /// the dimension of the input layer; (input, hidden)
25    fn dim_input(&self) -> (usize, usize) {
26        (self.input(), self.hidden())
27    }
28    /// the dimension of the hidden layers; (hidden, hidden)
29    fn dim_hidden(&self) -> (usize, usize) {
30        (self.hidden(), self.hidden())
31    }
32    /// the dimension of the output layer; (hidden, output)
33    fn dim_output(&self) -> (usize, usize) {
34        (self.hidden(), self.output())
35    }
36    /// the total number of parameters in the model
37    fn size(&self) -> usize {
38        self.size_input() + self.size_hidden() + self.size_output()
39    }
40    /// the total number of input parameters in the model
41    fn size_input(&self) -> usize {
42        self.input() * self.hidden()
43    }
44    /// the total number of hidden parameters in the model
45    fn size_hidden(&self) -> usize {
46        self.hidden() * self.hidden() * self.depth()
47    }
48    /// the total number of output parameters in the model
49    fn size_output(&self) -> usize {
50        self.hidden() * self.output()
51    }
52}
53/// The [`RawModelLayoutMut`] trait defines a mutable interface for objects capable of representing
54/// the _layout_; i.e. the number of input, hidden, and output features of
55pub trait RawModelLayoutMut: RawModelLayout {
56    /// returns a mutable reference to number of the input features for the model
57    fn input_mut(&mut self) -> &mut usize;
58    /// returns a mutable reference to the number of hidden features for the model
59    fn hidden_mut(&mut self) -> &mut usize;
60    /// returns a mutable reference to the number of hidden layers for the model
61    fn layers_mut(&mut self) -> &mut usize;
62    /// returns a mutable reference to the output features for the model
63    fn output_mut(&mut self) -> &mut usize;
64
65    #[inline]
66    /// update the number of input features for the model and return a mutable reference to the
67    /// current layout.
68    fn set_input(&mut self, input: usize) -> &mut Self {
69        *self.input_mut() = input;
70        self
71    }
72    #[inline]
73    /// update the number of hidden features for the model and return a mutable reference to
74    /// the current layout.
75    fn set_hidden(&mut self, hidden: usize) -> &mut Self {
76        *self.hidden_mut() = hidden;
77        self
78    }
79    #[inline]
80    /// update the number of hidden layers for the model and return a mutable reference to
81    /// the current layout.
82    fn set_layers(&mut self, layers: usize) -> &mut Self {
83        *self.layers_mut() = layers;
84        self
85    }
86    #[inline]
87    /// update the number of output features for the model and return a mutable reference to
88    /// the current layout.
89    fn set_output(&mut self, output: usize) -> &mut Self {
90        *self.output_mut() = output;
91        self
92    }
93}
94
95/// The [`LayoutExt`] trait defines an interface for object capable of representing the
96/// _layout_; i.e. the number of input, hidden, and output features of a neural network model
97/// containing some number of hidden layers.
98pub trait LayoutExt: RawModelLayout + RawModelLayoutMut + Clone + core::fmt::Debug {}
99
100/// The [`NetworkDepth`] trait is used to define the depth/kind of a neural network model.
101pub trait NetworkDepth {
102    private!();
103
104    fn is_deep(&self) -> bool {
105        false
106    }
107}
108
109macro_rules! impl_network_depth {
110    ( #[$tgt:ident] $vis:vis $s:ident {$($name:ident $({$($rest:tt)*})?),* $(,)?}) => {
111        $(
112            impl_network_depth!(@impl #[$tgt] $vis $s $name $({$($rest)*})?);
113        )*
114    };
115    (@impl #[$tgt:ident] $vis:vis enum $name:ident $({$($rest:tt)*})?) => {
116        #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
117        #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
118        $vis enum $name {}
119
120        impl $tgt for $name {
121            seal!();
122
123            $($($rest)*)?
124        }
125    };
126}
127
128impl_network_depth! {
129    #[NetworkDepth]
130    pub enum {
131        Deep {
132            fn is_deep(&self) -> bool {
133                true
134            }
135        },
136        Shallow,
137    }
138}
139
140/*
141 ************* Implementations *************
142*/
143
144impl<T> RawModelLayout for &T
145where
146    T: RawModelLayout,
147{
148    fn input(&self) -> usize {
149        <T as RawModelLayout>::input(self)
150    }
151    fn hidden(&self) -> usize {
152        <T as RawModelLayout>::hidden(self)
153    }
154    fn depth(&self) -> usize {
155        <T as RawModelLayout>::depth(self)
156    }
157    fn output(&self) -> usize {
158        <T as RawModelLayout>::output(self)
159    }
160}
161
162impl<T> RawModelLayout for &mut T
163where
164    T: RawModelLayout,
165{
166    fn input(&self) -> usize {
167        <T as RawModelLayout>::input(self)
168    }
169    fn hidden(&self) -> usize {
170        <T as RawModelLayout>::hidden(self)
171    }
172    fn depth(&self) -> usize {
173        <T as RawModelLayout>::depth(self)
174    }
175    fn output(&self) -> usize {
176        <T as RawModelLayout>::output(self)
177    }
178}
179
180impl<T> LayoutExt for T where T: RawModelLayoutMut + Copy + core::fmt::Debug {}
181
182impl RawModelLayout for (usize, usize, usize) {
183    fn input(&self) -> usize {
184        self.0
185    }
186    fn hidden(&self) -> usize {
187        self.1
188    }
189    fn depth(&self) -> usize {
190        1
191    }
192    fn output(&self) -> usize {
193        self.2
194    }
195}
196
197impl RawModelLayout for (usize, usize, usize, usize) {
198    fn input(&self) -> usize {
199        self.0
200    }
201    fn hidden(&self) -> usize {
202        self.1
203    }
204    fn output(&self) -> usize {
205        self.2
206    }
207
208    fn depth(&self) -> usize {
209        self.3
210    }
211}
212
213impl RawModelLayoutMut for (usize, usize, usize, usize) {
214    fn input_mut(&mut self) -> &mut usize {
215        &mut self.0
216    }
217
218    fn hidden_mut(&mut self) -> &mut usize {
219        &mut self.1
220    }
221
222    fn layers_mut(&mut self) -> &mut usize {
223        &mut self.2
224    }
225
226    fn output_mut(&mut self) -> &mut usize {
227        &mut self.3
228    }
229}
230
231impl RawModelLayout for [usize; 3] {
232    fn input(&self) -> usize {
233        self[0]
234    }
235
236    fn hidden(&self) -> usize {
237        self[1]
238    }
239
240    fn output(&self) -> usize {
241        self[2]
242    }
243
244    fn depth(&self) -> usize {
245        1
246    }
247}
248
249impl RawModelLayout for [usize; 4] {
250    fn input(&self) -> usize {
251        self[0]
252    }
253
254    fn hidden(&self) -> usize {
255        self[1]
256    }
257
258    fn output(&self) -> usize {
259        self[2]
260    }
261
262    fn depth(&self) -> usize {
263        self[3]
264    }
265}
266
267impl RawModelLayoutMut for [usize; 4] {
268    fn input_mut(&mut self) -> &mut usize {
269        &mut self[0]
270    }
271    fn hidden_mut(&mut self) -> &mut usize {
272        &mut self[1]
273    }
274    fn layers_mut(&mut self) -> &mut usize {
275        &mut self[2]
276    }
277    fn output_mut(&mut self) -> &mut usize {
278        &mut self[3]
279    }
280}