1use crate::{Error, Matrix, Result};
6
7use super::super::{Module, ModuleRegistry, matrix};
8
9pub struct Identity {
13 registry: ModuleRegistry,
14}
15
16impl Identity {
17 pub const fn new() -> Self {
19 Self {
20 registry: ModuleRegistry::new(),
21 }
22 }
23
24 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
26 Ok(input.clone())
27 }
28}
29
30impl Default for Identity {
31 fn default() -> Self {
32 Self::new()
33 }
34}
35
36impl Module for Identity {
37 fn forward(&self, input: &Matrix) -> Result<Matrix> {
38 Identity::forward(self, input)
39 }
40
41 fn registry(&self) -> &ModuleRegistry {
42 &self.registry
43 }
44}
45
46pub struct Flatten {
53 start_dim: isize,
54 end_dim: isize,
55 registry: ModuleRegistry,
56}
57
58impl Flatten {
59 pub const fn new(start_dim: isize, end_dim: isize) -> Self {
61 Self {
62 start_dim,
63 end_dim,
64 registry: ModuleRegistry::new(),
65 }
66 }
67
68 pub const fn start_dim(&self) -> isize {
70 self.start_dim
71 }
72
73 pub const fn end_dim(&self) -> isize {
75 self.end_dim
76 }
77
78 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
88 let rank = input.shape().len();
89 if rank <= 1 || self.start_dim >= 0 && self.start_dim as usize >= rank {
90 return Ok(input.clone());
91 }
92 let start = resolve_dim(self.start_dim, rank, "start")?;
93 let end = resolve_dim(self.end_dim, rank, "end")?;
94 if start > end {
95 return Err(Error::invalid_argument(format!(
96 "Flatten start dimension {start} exceeds end dimension {end}"
97 )));
98 }
99 let flattened = input.shape()[start..=end]
100 .iter()
101 .try_fold(1_usize, |extent, dimension| {
102 extent
103 .checked_mul(*dimension)
104 .ok_or_else(|| Error::resource_exhausted("Flatten extent overflows usize"))
105 })?;
106 let mut shape = Vec::with_capacity(rank - (end - start));
107 shape.extend_from_slice(&input.shape()[..start]);
108 shape.push(flattened);
109 shape.extend_from_slice(&input.shape()[end + 1..]);
110 input.reshape(shape)
111 }
112}
113
114impl Default for Flatten {
115 fn default() -> Self {
116 Self::new(1, -1)
117 }
118}
119
120impl Module for Flatten {
121 fn forward(&self, input: &Matrix) -> Result<Matrix> {
122 Flatten::forward(self, input)
123 }
124
125 fn registry(&self) -> &ModuleRegistry {
126 &self.registry
127 }
128}
129
130pub struct Unfold {
134 kernel_size: usize,
135 stride: usize,
136 padding: usize,
137 dilation: usize,
138 registry: ModuleRegistry,
139}
140
141impl Unfold {
142 pub fn new(kernel_size: usize, stride: usize, padding: usize, dilation: usize) -> Result<Self> {
148 if kernel_size == 0 || stride == 0 || dilation == 0 {
149 return Err(crate::Error::invalid_argument(
150 "Unfold requires nonzero kernel size, stride, and dilation",
151 ));
152 }
153 Ok(Self {
154 kernel_size,
155 stride,
156 padding,
157 dilation,
158 registry: ModuleRegistry::new(),
159 })
160 }
161
162 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
168 matrix::unfold(
169 input,
170 self.kernel_size,
171 self.stride,
172 self.padding,
173 self.dilation,
174 )
175 }
176
177 pub const fn kernel_size(&self) -> usize {
179 self.kernel_size
180 }
181
182 pub const fn stride(&self) -> usize {
184 self.stride
185 }
186
187 pub const fn padding(&self) -> usize {
189 self.padding
190 }
191
192 pub const fn dilation(&self) -> usize {
194 self.dilation
195 }
196}
197
198impl Module for Unfold {
199 fn forward(&self, input: &Matrix) -> Result<Matrix> {
200 Unfold::forward(self, input)
201 }
202
203 fn registry(&self) -> &ModuleRegistry {
204 &self.registry
205 }
206}
207
208pub struct Fold {
212 output_height: usize,
213 output_width: usize,
214 kernel_size: usize,
215 stride: usize,
216 padding: usize,
217 dilation: usize,
218 registry: ModuleRegistry,
219}
220
221impl Fold {
222 pub fn new(
229 output_height: usize,
230 output_width: usize,
231 kernel_size: usize,
232 stride: usize,
233 padding: usize,
234 dilation: usize,
235 ) -> Result<Self> {
236 if output_height == 0 || output_width == 0 || kernel_size == 0 || stride == 0 || dilation == 0 {
237 return Err(crate::Error::invalid_argument(
238 "Fold requires nonzero output extent, kernel size, stride, and dilation",
239 ));
240 }
241 Ok(Self {
242 output_height,
243 output_width,
244 kernel_size,
245 stride,
246 padding,
247 dilation,
248 registry: ModuleRegistry::new(),
249 })
250 }
251
252 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
259 matrix::fold(
260 input,
261 self.output_height,
262 self.output_width,
263 self.kernel_size,
264 self.stride,
265 self.padding,
266 self.dilation,
267 )
268 }
269
270 pub const fn output_height(&self) -> usize {
272 self.output_height
273 }
274
275 pub const fn output_width(&self) -> usize {
277 self.output_width
278 }
279
280 pub const fn kernel_size(&self) -> usize {
282 self.kernel_size
283 }
284
285 pub const fn stride(&self) -> usize {
287 self.stride
288 }
289
290 pub const fn padding(&self) -> usize {
292 self.padding
293 }
294
295 pub const fn dilation(&self) -> usize {
297 self.dilation
298 }
299}
300
301impl Module for Fold {
302 fn forward(&self, input: &Matrix) -> Result<Matrix> {
303 Fold::forward(self, input)
304 }
305
306 fn registry(&self) -> &ModuleRegistry {
307 &self.registry
308 }
309}
310
311fn resolve_dim(dimension: isize, rank: usize, label: &str) -> Result<usize> {
312 let rank =
313 isize::try_from(rank).map_err(|_| Error::resource_exhausted("Matrix rank exceeds isize"))?;
314 let resolved = if dimension < 0 {
315 rank
316 .checked_add(dimension)
317 .ok_or_else(|| Error::invalid_argument(format!("Flatten {label} dimension is invalid")))?
318 } else {
319 dimension
320 };
321 if !(0..rank).contains(&resolved) {
322 return Err(Error::invalid_argument(format!(
323 "Flatten {label} dimension {dimension} is outside rank {rank}"
324 )));
325 }
326 Ok(resolved as usize)
327}