Skip to main content

oa/ml/nn/
pool.gen.rs

1// @generated by tool/gen/nn/generate.py; DO NOT EDIT.
2// Source: tool/gen/nn/schema/ml/ml_nn_pool.json
3// generator_version=8; schema_sha256=97508fece201e71e1cd128ff6775efb9ed9dbdb6cbc2d47f709d6bbbc456746b
4
5use crate::{Error, Matrix, Result};
6
7use super::super::{Module, ModuleRegistry, matrix};
8
9// OA donor: oacpp/source/cpp/lib/oa/ml/nn/avgpool2d/avgPool2d.cpp
10// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry; Matrix provider owns dispatch and adjoints
11/// Parameterless two-dimensional average-pooling module for NCHW matrices.
12pub struct AvgPool2d {
13	kernel_size: usize,
14	stride: usize,
15	padding: usize,
16	registry: ModuleRegistry,
17}
18
19impl AvgPool2d {
20	/// Construct AvgPool2d with `stride == kernel_size` and no padding.
21	///
22	/// # Errors
23	///
24	/// Returns an error when `kernel_size` is zero.
25	pub fn new(kernel_size: usize) -> Result<Self> {
26		Self::new_with_options(kernel_size, kernel_size, 0)
27	}
28
29	/// Construct AvgPool2d with explicit square-kernel geometry.
30	///
31	/// # Errors
32	///
33	/// Returns an error when `kernel_size` or `stride` is zero.
34	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
35		if kernel_size == 0 || stride == 0 {
36			return Err(Error::invalid_argument(
37				"AvgPool2d kernel size and stride must be nonzero",
38			));
39		}
40		Ok(Self {
41			kernel_size,
42			stride,
43			padding,
44			registry: ModuleRegistry::new(),
45		})
46	}
47
48	/// Return the square kernel extent.
49	pub const fn kernel_size(&self) -> usize {
50		self.kernel_size
51	}
52
53	/// Return the spatial stride.
54	pub const fn stride(&self) -> usize {
55		self.stride
56	}
57
58	/// Return the symmetric zero-padding extent.
59	pub const fn padding(&self) -> usize {
60		self.padding
61	}
62
63	/// Pool one NCHW Matrix without submitting or waiting.
64	///
65	/// # Errors
66	///
67	/// Returns an error from the underlying Matrix operation.
68	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
69		matrix::avg_pool_2d(input, self.kernel_size, self.stride, self.padding)
70	}
71}
72
73impl Module for AvgPool2d {
74	fn forward(&self, input: &Matrix) -> Result<Matrix> {
75		AvgPool2d::forward(self, input)
76	}
77
78	fn registry(&self) -> &ModuleRegistry {
79		&self.registry
80	}
81}
82
83// OA donor: oacpp/source/cpp/lib/oa/ml/nn/maxpool2d/maxPool2d.cpp
84// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry; Matrix provider owns dispatch and adjoints
85/// Parameterless two-dimensional max-pooling module for NCHW matrices.
86pub struct MaxPool2d {
87	kernel_size: usize,
88	stride: usize,
89	padding: usize,
90	registry: ModuleRegistry,
91}
92
93impl MaxPool2d {
94	/// Construct MaxPool2d with `stride == kernel_size` and no padding.
95	///
96	/// # Errors
97	///
98	/// Returns an error when `kernel_size` is zero.
99	pub fn new(kernel_size: usize) -> Result<Self> {
100		Self::new_with_options(kernel_size, kernel_size, 0)
101	}
102
103	/// Construct MaxPool2d with explicit square-kernel geometry.
104	///
105	/// # Errors
106	///
107	/// Returns an error when `kernel_size` or `stride` is zero.
108	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
109		if kernel_size == 0 || stride == 0 {
110			return Err(Error::invalid_argument(
111				"MaxPool2d kernel size and stride must be nonzero",
112			));
113		}
114		Ok(Self {
115			kernel_size,
116			stride,
117			padding,
118			registry: ModuleRegistry::new(),
119		})
120	}
121
122	/// Return the square kernel extent.
123	pub const fn kernel_size(&self) -> usize {
124		self.kernel_size
125	}
126
127	/// Return the spatial stride.
128	pub const fn stride(&self) -> usize {
129		self.stride
130	}
131
132	/// Return the symmetric padding extent.
133	pub const fn padding(&self) -> usize {
134		self.padding
135	}
136
137	/// Pool one NCHW Matrix and retain argmax state for reverse mode.
138	///
139	/// # Errors
140	///
141	/// Returns an error from the underlying Matrix operation.
142	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
143		Ok(matrix::max_pool_2d(input, self.kernel_size, self.stride, self.padding)?.output)
144	}
145}
146
147impl Module for MaxPool2d {
148	fn forward(&self, input: &Matrix) -> Result<Matrix> {
149		MaxPool2d::forward(self, input)
150	}
151
152	fn registry(&self) -> &ModuleRegistry {
153		&self.registry
154	}
155}
156
157// OA donor: oacpp/source/cpp/lib/oa/ml/nn/maxpool2d/maxPool2d.cpp
158// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the NCL kernel; Matrix provider owns dispatch and adjoints
159/// Parameterless one-dimensional max-pooling module for NCL matrices.
160pub struct MaxPool1d {
161	kernel_size: usize,
162	stride: usize,
163	padding: usize,
164	registry: ModuleRegistry,
165}
166
167impl MaxPool1d {
168	/// Construct MaxPool1d with `stride == kernel_size` and no padding.
169	///
170	/// # Errors
171	///
172	/// Returns an error when `kernel_size` is zero.
173	pub fn new(kernel_size: usize) -> Result<Self> {
174		Self::new_with_options(kernel_size, kernel_size, 0)
175	}
176
177	/// Construct MaxPool1d with explicit kernel geometry.
178	///
179	/// # Errors
180	///
181	/// Returns an error when `kernel_size` or `stride` is zero.
182	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
183		if kernel_size == 0 || stride == 0 {
184			return Err(Error::invalid_argument(
185				"MaxPool1d kernel size and stride must be nonzero",
186			));
187		}
188		Ok(Self {
189			kernel_size,
190			stride,
191			padding,
192			registry: ModuleRegistry::new(),
193		})
194	}
195
196	/// Return the kernel extent.
197	pub const fn kernel_size(&self) -> usize {
198		self.kernel_size
199	}
200
201	/// Return the spatial stride.
202	pub const fn stride(&self) -> usize {
203		self.stride
204	}
205
206	/// Return the symmetric padding extent.
207	pub const fn padding(&self) -> usize {
208		self.padding
209	}
210
211	/// Pool one NCL Matrix and retain argmax state for reverse mode.
212	///
213	/// # Errors
214	///
215	/// Returns an error from the underlying Matrix operation.
216	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
217		Ok(matrix::max_pool_1d(input, self.kernel_size, self.stride, self.padding)?.output)
218	}
219}
220
221impl Module for MaxPool1d {
222	fn forward(&self, input: &Matrix) -> Result<Matrix> {
223		MaxPool1d::forward(self, input)
224	}
225
226	fn registry(&self) -> &ModuleRegistry {
227		&self.registry
228	}
229}
230
231// OA donor: oacpp/source/cpp/lib/oa/ml/nn/adaptiveavgpool2d/adaptiveAvgPool2d.cpp
232// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the adaptive-bin argmax kernel; Matrix provider owns dispatch and adjoints
233/// Parameterless adaptive max-pooling module for NCL matrices.
234pub struct AdaptiveMaxPool1d {
235	output_length: usize,
236	registry: ModuleRegistry,
237}
238
239impl AdaptiveMaxPool1d {
240	/// Construct AdaptiveMaxPool1d with the requested output extent.
241	///
242	/// # Errors
243	///
244	/// Returns an error when `output_length` is zero.
245	pub fn new(output_length: usize) -> Result<Self> {
246		if output_length == 0 {
247			return Err(Error::invalid_argument(
248				"AdaptiveMaxPool1d output extent must be nonzero",
249			));
250		}
251		Ok(Self {
252			output_length,
253			registry: ModuleRegistry::new(),
254		})
255	}
256
257	/// Return the requested output extent.
258	pub const fn output_length(&self) -> usize {
259		self.output_length
260	}
261
262	/// Adaptively pool one NCL Matrix and retain argmax state for reverse
263	/// mode.
264	///
265	/// # Errors
266	///
267	/// Returns an error from the underlying Matrix operation.
268	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
269		Ok(matrix::adaptive_max_pool_1d(input, self.output_length)?.output)
270	}
271}
272
273impl Module for AdaptiveMaxPool1d {
274	fn forward(&self, input: &Matrix) -> Result<Matrix> {
275		AdaptiveMaxPool1d::forward(self, input)
276	}
277
278	fn registry(&self) -> &ModuleRegistry {
279		&self.registry
280	}
281}
282
283// OA donor: oacpp/source/cpp/lib/oa/ml/nn/adaptiveavgpool2d/adaptiveAvgPool2d.cpp
284// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry; Matrix provider owns dispatch and adjoints; preserves documented true rectangular adaptive-bin correction to donor square-window approximation
285/// Parameterless adaptive average-pooling module for NCHW matrices.
286pub struct AdaptiveAvgPool2d {
287	output_height: usize,
288	output_width: usize,
289	registry: ModuleRegistry,
290}
291
292impl AdaptiveAvgPool2d {
293	/// Construct AdaptiveAvgPool2d with a square output extent.
294	///
295	/// # Errors
296	///
297	/// Returns an error when `output_size` is zero.
298	pub fn new(output_size: usize) -> Result<Self> {
299		Self::new_with_output_size(output_size, output_size)
300	}
301
302	/// Construct AdaptiveAvgPool2d with independent output height and width.
303	///
304	/// # Errors
305	///
306	/// Returns an error when either output extent is zero.
307	pub fn new_with_output_size(output_height: usize, output_width: usize) -> Result<Self> {
308		if output_height == 0 || output_width == 0 {
309			return Err(Error::invalid_argument(
310				"AdaptiveAvgPool2d output extents must be nonzero",
311			));
312		}
313		Ok(Self {
314			output_height,
315			output_width,
316			registry: ModuleRegistry::new(),
317		})
318	}
319
320	/// Return the requested output height.
321	pub const fn output_height(&self) -> usize {
322		self.output_height
323	}
324
325	/// Return the requested output width.
326	pub const fn output_width(&self) -> usize {
327		self.output_width
328	}
329
330	/// Adaptively pool one NCHW Matrix without submitting or waiting.
331	///
332	/// # Errors
333	///
334	/// Returns an error from the underlying Matrix operation.
335	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
336		matrix::adaptive_avg_pool_2d(input, self.output_height, self.output_width)
337	}
338}
339
340impl Module for AdaptiveAvgPool2d {
341	fn forward(&self, input: &Matrix) -> Result<Matrix> {
342		AdaptiveAvgPool2d::forward(self, input)
343	}
344
345	fn registry(&self) -> &ModuleRegistry {
346		&self.registry
347	}
348}
349
350// OA donor: oacpp/source/cpp/lib/oa/ml/nn/maxpool2d/maxPool2d.cpp
351// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the NCDHW argmax kernel; Matrix provider owns dispatch and adjoints
352/// Parameterless three-dimensional max-pooling module for NCDHW matrices.
353pub struct MaxPool3d {
354	kernel_size: usize,
355	stride: usize,
356	padding: usize,
357	registry: ModuleRegistry,
358}
359
360impl MaxPool3d {
361	/// Construct MaxPool3d with `stride == kernel_size` and no padding.
362	///
363	/// # Errors
364	///
365	/// Returns an error when `kernel_size` is zero.
366	pub fn new(kernel_size: usize) -> Result<Self> {
367		Self::new_with_options(kernel_size, kernel_size, 0)
368	}
369
370	/// Construct MaxPool3d with explicit cubic-kernel geometry.
371	///
372	/// # Errors
373	///
374	/// Returns an error when `kernel_size` or `stride` is zero.
375	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
376		if kernel_size == 0 || stride == 0 {
377			return Err(Error::invalid_argument(
378				"MaxPool3d kernel size and stride must be nonzero",
379			));
380		}
381		Ok(Self {
382			kernel_size,
383			stride,
384			padding,
385			registry: ModuleRegistry::new(),
386		})
387	}
388
389	/// Return the cubic kernel extent.
390	pub const fn kernel_size(&self) -> usize {
391		self.kernel_size
392	}
393
394	/// Return the spatial stride.
395	pub const fn stride(&self) -> usize {
396		self.stride
397	}
398
399	/// Return the symmetric padding extent.
400	pub const fn padding(&self) -> usize {
401		self.padding
402	}
403
404	/// Pool one NCDHW Matrix and retain argmax state for reverse mode.
405	///
406	/// # Errors
407	///
408	/// Returns an error from the underlying Matrix operation.
409	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
410		Ok(matrix::max_pool_3d(input, self.kernel_size, self.stride, self.padding)?.output)
411	}
412}
413
414impl Module for MaxPool3d {
415	fn forward(&self, input: &Matrix) -> Result<Matrix> {
416		MaxPool3d::forward(self, input)
417	}
418
419	fn registry(&self) -> &ModuleRegistry {
420		&self.registry
421	}
422}
423
424// OA donor: oacpp/source/cpp/lib/oa/ml/nn/avgpool2d/avgPool2d.cpp
425// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the NCDHW kernel; Matrix provider owns dispatch and adjoints
426/// Parameterless three-dimensional average-pooling module for NCDHW matrices.
427pub struct AvgPool3d {
428	kernel_size: usize,
429	stride: usize,
430	padding: usize,
431	registry: ModuleRegistry,
432}
433
434impl AvgPool3d {
435	/// Construct AvgPool3d with `stride == kernel_size` and no padding.
436	///
437	/// # Errors
438	///
439	/// Returns an error when `kernel_size` is zero.
440	pub fn new(kernel_size: usize) -> Result<Self> {
441		Self::new_with_options(kernel_size, kernel_size, 0)
442	}
443
444	/// Construct AvgPool3d with explicit cubic-kernel geometry.
445	///
446	/// # Errors
447	///
448	/// Returns an error when `kernel_size` or `stride` is zero.
449	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
450		if kernel_size == 0 || stride == 0 {
451			return Err(Error::invalid_argument(
452				"AvgPool3d kernel size and stride must be nonzero",
453			));
454		}
455		Ok(Self {
456			kernel_size,
457			stride,
458			padding,
459			registry: ModuleRegistry::new(),
460		})
461	}
462
463	/// Return the cubic kernel extent.
464	pub const fn kernel_size(&self) -> usize {
465		self.kernel_size
466	}
467
468	/// Return the spatial stride.
469	pub const fn stride(&self) -> usize {
470		self.stride
471	}
472
473	/// Return the symmetric zero-padding extent.
474	pub const fn padding(&self) -> usize {
475		self.padding
476	}
477
478	/// Pool one NCDHW Matrix without submitting or waiting.
479	///
480	/// # Errors
481	///
482	/// Returns an error from the underlying Matrix operation.
483	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
484		matrix::avg_pool_3d(input, self.kernel_size, self.stride, self.padding)
485	}
486}
487
488impl Module for AvgPool3d {
489	fn forward(&self, input: &Matrix) -> Result<Matrix> {
490		AvgPool3d::forward(self, input)
491	}
492
493	fn registry(&self) -> &ModuleRegistry {
494		&self.registry
495	}
496}
497
498// OA donor: oacpp/source/cpp/lib/oa/ml/nn/adaptiveavgpool2d/adaptiveAvgPool2d.cpp
499// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the adaptive-bin argmax kernel; Matrix provider owns dispatch and adjoints
500/// Parameterless adaptive max-pooling module for NCHW matrices.
501pub struct AdaptiveMaxPool2d {
502	output_height: usize,
503	output_width: usize,
504	registry: ModuleRegistry,
505}
506
507impl AdaptiveMaxPool2d {
508	/// Construct AdaptiveMaxPool2d with a square output extent.
509	///
510	/// # Errors
511	///
512	/// Returns an error when `output_size` is zero.
513	pub fn new(output_size: usize) -> Result<Self> {
514		Self::new_with_output_size(output_size, output_size)
515	}
516
517	/// Construct AdaptiveMaxPool2d with independent output height and width.
518	///
519	/// # Errors
520	///
521	/// Returns an error when either output extent is zero.
522	pub fn new_with_output_size(output_height: usize, output_width: usize) -> Result<Self> {
523		if output_height == 0 || output_width == 0 {
524			return Err(Error::invalid_argument(
525				"AdaptiveMaxPool2d output extents must be nonzero",
526			));
527		}
528		Ok(Self {
529			output_height,
530			output_width,
531			registry: ModuleRegistry::new(),
532		})
533	}
534
535	/// Return the requested output height.
536	pub const fn output_height(&self) -> usize {
537		self.output_height
538	}
539
540	/// Return the requested output width.
541	pub const fn output_width(&self) -> usize {
542		self.output_width
543	}
544
545	/// Adaptively pool one NCHW Matrix and retain argmax state for reverse
546	/// mode.
547	///
548	/// # Errors
549	///
550	/// Returns an error from the underlying Matrix operation.
551	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
552		Ok(matrix::adaptive_max_pool_2d(input, self.output_height, self.output_width)?.output)
553	}
554}
555
556impl Module for AdaptiveMaxPool2d {
557	fn forward(&self, input: &Matrix) -> Result<Matrix> {
558		AdaptiveMaxPool2d::forward(self, input)
559	}
560
561	fn registry(&self) -> &ModuleRegistry {
562		&self.registry
563	}
564}
565
566// OA donor: oacpp/source/cpp/lib/oa/ml/nn/adaptiveavgpool2d/adaptiveAvgPool2d.cpp
567// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the adaptive-bin argmax kernel; Matrix provider owns dispatch and adjoints
568/// Parameterless adaptive max-pooling module for NCDHW matrices.
569pub struct AdaptiveMaxPool3d {
570	output_depth: usize,
571	output_height: usize,
572	output_width: usize,
573	registry: ModuleRegistry,
574}
575
576impl AdaptiveMaxPool3d {
577	/// Construct AdaptiveMaxPool3d with a cubic output extent.
578	///
579	/// # Errors
580	///
581	/// Returns an error when `output_size` is zero.
582	pub fn new(output_size: usize) -> Result<Self> {
583		Self::new_with_output_size(output_size, output_size, output_size)
584	}
585
586	/// Construct AdaptiveMaxPool3d with independent output depth, height, and width.
587	///
588	/// # Errors
589	///
590	/// Returns an error when any output extent is zero.
591	pub fn new_with_output_size(
592		output_depth: usize,
593		output_height: usize,
594		output_width: usize,
595	) -> Result<Self> {
596		if output_depth == 0 || output_height == 0 || output_width == 0 {
597			return Err(Error::invalid_argument(
598				"AdaptiveMaxPool3d output extents must be nonzero",
599			));
600		}
601		Ok(Self {
602			output_depth,
603			output_height,
604			output_width,
605			registry: ModuleRegistry::new(),
606		})
607	}
608
609	/// Return the requested output depth.
610	pub const fn output_depth(&self) -> usize {
611		self.output_depth
612	}
613
614	/// Return the requested output height.
615	pub const fn output_height(&self) -> usize {
616		self.output_height
617	}
618
619	/// Return the requested output width.
620	pub const fn output_width(&self) -> usize {
621		self.output_width
622	}
623
624	/// Adaptively pool one NCDHW Matrix and retain argmax state for reverse
625	/// mode.
626	///
627	/// # Errors
628	///
629	/// Returns an error from the underlying Matrix operation.
630	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
631		Ok(
632			matrix::adaptive_max_pool_3d(
633				input,
634				self.output_depth,
635				self.output_height,
636				self.output_width,
637			)?
638			.output,
639		)
640	}
641}
642
643impl Module for AdaptiveMaxPool3d {
644	fn forward(&self, input: &Matrix) -> Result<Matrix> {
645		AdaptiveMaxPool3d::forward(self, input)
646	}
647
648	fn registry(&self) -> &ModuleRegistry {
649		&self.registry
650	}
651}
652
653// OA donor: oacpp/source/cpp/lib/oa/ml/nn/adaptiveavgpool2d/adaptiveAvgPool2d.cpp
654// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the NCDHW adaptive-bin kernel; Matrix provider owns dispatch and adjoints
655/// Parameterless adaptive average-pooling module for NCDHW matrices.
656pub struct AdaptiveAvgPool3d {
657	output_depth: usize,
658	output_height: usize,
659	output_width: usize,
660	registry: ModuleRegistry,
661}
662
663impl AdaptiveAvgPool3d {
664	/// Construct AdaptiveAvgPool3d with a cubic output extent.
665	///
666	/// # Errors
667	///
668	/// Returns an error when `output_size` is zero.
669	pub fn new(output_size: usize) -> Result<Self> {
670		Self::new_with_output_size(output_size, output_size, output_size)
671	}
672
673	/// Construct AdaptiveAvgPool3d with independent output depth, height, and width.
674	///
675	/// # Errors
676	///
677	/// Returns an error when any output extent is zero.
678	pub fn new_with_output_size(
679		output_depth: usize,
680		output_height: usize,
681		output_width: usize,
682	) -> Result<Self> {
683		if output_depth == 0 || output_height == 0 || output_width == 0 {
684			return Err(Error::invalid_argument(
685				"AdaptiveAvgPool3d output extents must be nonzero",
686			));
687		}
688		Ok(Self {
689			output_depth,
690			output_height,
691			output_width,
692			registry: ModuleRegistry::new(),
693		})
694	}
695
696	/// Return the requested output depth.
697	pub const fn output_depth(&self) -> usize {
698		self.output_depth
699	}
700
701	/// Return the requested output height.
702	pub const fn output_height(&self) -> usize {
703		self.output_height
704	}
705
706	/// Return the requested output width.
707	pub const fn output_width(&self) -> usize {
708		self.output_width
709	}
710
711	/// Adaptively pool one NCDHW Matrix without submitting or waiting.
712	///
713	/// # Errors
714	///
715	/// Returns an error from the underlying Matrix operation.
716	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
717		matrix::adaptive_avg_pool_3d(
718			input,
719			self.output_depth,
720			self.output_height,
721			self.output_width,
722		)
723	}
724}
725
726impl Module for AdaptiveAvgPool3d {
727	fn forward(&self, input: &Matrix) -> Result<Matrix> {
728		AdaptiveAvgPool3d::forward(self, input)
729	}
730
731	fn registry(&self) -> &ModuleRegistry {
732		&self.registry
733	}
734}
735
736// OA donor: oacpp/source/cpp/lib/oa/ml/nn/maxpool2d/maxPool2d.cpp
737// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the recorded-argmax unpool kernel; Module::forward rejects calls without indices; Matrix provider owns dispatch and adjoints
738/// Parameterless one-dimensional max-unpooling module consuming recorded argmax indices over NCL matrices.
739pub struct MaxUnpool1d {
740	kernel_size: usize,
741	stride: usize,
742	padding: usize,
743	registry: ModuleRegistry,
744}
745
746impl MaxUnpool1d {
747	/// Construct MaxUnpool1d with `stride == kernel_size` and no padding.
748	///
749	/// # Errors
750	///
751	/// Returns an error when `kernel_size` is zero.
752	pub fn new(kernel_size: usize) -> Result<Self> {
753		Self::new_with_options(kernel_size, kernel_size, 0)
754	}
755
756	/// Construct MaxUnpool1d with explicit geometry.
757	///
758	/// # Errors
759	///
760	/// Returns an error when `kernel_size` or `stride` is zero.
761	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
762		if kernel_size == 0 || stride == 0 {
763			return Err(Error::invalid_argument(
764				"MaxUnpool1d kernel size and stride must be nonzero",
765			));
766		}
767		Ok(Self {
768			kernel_size,
769			stride,
770			padding,
771			registry: ModuleRegistry::new(),
772		})
773	}
774
775	/// Return the kernel extent.
776	pub const fn kernel_size(&self) -> usize {
777		self.kernel_size
778	}
779
780	/// Return the spatial stride.
781	pub const fn stride(&self) -> usize {
782		self.stride
783	}
784
785	/// Return the symmetric padding extent.
786	pub const fn padding(&self) -> usize {
787		self.padding
788	}
789
790	/// Compute the implied output length for a pooled input length.
791	fn implied_output_length(&self, input_length: usize) -> usize {
792		(input_length - 1) * self.stride - 2 * self.padding + self.kernel_size
793	}
794
795	/// Unpool one NCL Matrix against recorded argmax indices using the
796	/// geometry-implied output length.
797	///
798	/// # Errors
799	///
800	/// Returns an error from the underlying Matrix operation or when `input`
801	/// is empty.
802	pub fn forward(&self, input: &Matrix, indices: &Matrix) -> Result<Matrix> {
803		let [_, _, input_length] = input.shape() else {
804			return Err(Error::invalid_argument(
805				"MaxUnpool1d input must be a rank-three NCL matrix",
806			));
807		};
808		if *input_length == 0 {
809			return Err(Error::invalid_argument(
810				"MaxUnpool1d input length must be nonzero",
811			));
812		}
813		matrix::max_unpool_1d(
814			input,
815			indices,
816			self.kernel_size,
817			self.stride,
818			self.padding,
819			self.implied_output_length(*input_length),
820		)
821	}
822
823	/// Unpool one NCL Matrix against recorded argmax indices into an explicit
824	/// output length.
825	///
826	/// # Errors
827	///
828	/// Returns an error from the underlying Matrix operation.
829	pub fn forward_with_output(
830		&self,
831		input: &Matrix,
832		indices: &Matrix,
833		output_length: usize,
834	) -> Result<Matrix> {
835		matrix::max_unpool_1d(
836			input,
837			indices,
838			self.kernel_size,
839			self.stride,
840			self.padding,
841			output_length,
842		)
843	}
844}
845
846impl Module for MaxUnpool1d {
847	fn forward(&self, input: &Matrix) -> Result<Matrix> {
848		let _ = input;
849		Err(Error::invalid_argument(
850			"MaxUnpool1d requires pooled input plus recorded argmax indices; call MaxUnpool1d::forward(input, indices)",
851		))
852	}
853
854	fn registry(&self) -> &ModuleRegistry {
855		&self.registry
856	}
857}
858
859// OA donor: oacpp/source/cpp/lib/oa/ml/nn/maxpool2d/maxPool2d.cpp
860// Adaptation: Mechanical generation of established Rust validation and ModuleRegistry over the recorded-argmax unpool kernel; Module::forward rejects calls without indices; Matrix provider owns dispatch and adjoints
861/// Parameterless two-dimensional max-unpooling module consuming recorded argmax indices over NCHW matrices.
862pub struct MaxUnpool2d {
863	kernel_size: usize,
864	stride: usize,
865	padding: usize,
866	registry: ModuleRegistry,
867}
868
869impl MaxUnpool2d {
870	/// Construct MaxUnpool2d with `stride == kernel_size` and no padding.
871	///
872	/// # Errors
873	///
874	/// Returns an error when `kernel_size` is zero.
875	pub fn new(kernel_size: usize) -> Result<Self> {
876		Self::new_with_options(kernel_size, kernel_size, 0)
877	}
878
879	/// Construct MaxUnpool2d with explicit square-kernel geometry.
880	///
881	/// # Errors
882	///
883	/// Returns an error when `kernel_size` or `stride` is zero.
884	pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
885		if kernel_size == 0 || stride == 0 {
886			return Err(Error::invalid_argument(
887				"MaxUnpool2d kernel size and stride must be nonzero",
888			));
889		}
890		Ok(Self {
891			kernel_size,
892			stride,
893			padding,
894			registry: ModuleRegistry::new(),
895		})
896	}
897
898	/// Return the square kernel extent.
899	pub const fn kernel_size(&self) -> usize {
900		self.kernel_size
901	}
902
903	/// Return the spatial stride.
904	pub const fn stride(&self) -> usize {
905		self.stride
906	}
907
908	/// Return the symmetric padding extent.
909	pub const fn padding(&self) -> usize {
910		self.padding
911	}
912
913	/// Compute the implied output extent for one pooled spatial extent.
914	fn implied_output_extent(&self, input_extent: usize) -> usize {
915		(input_extent - 1) * self.stride - 2 * self.padding + self.kernel_size
916	}
917
918	/// Unpool one NCHW Matrix against recorded argmax indices using the
919	/// geometry-implied output extent.
920	///
921	/// # Errors
922	///
923	/// Returns an error from the underlying Matrix operation or when `input`
924	/// is not a rank-four NCHW matrix with nonzero spatial extents.
925	pub fn forward(&self, input: &Matrix, indices: &Matrix) -> Result<Matrix> {
926		let [_, _, input_height, input_width] = input.shape() else {
927			return Err(Error::invalid_argument(
928				"MaxUnpool2d input must be a rank-four NCHW matrix",
929			));
930		};
931		if *input_height == 0 || *input_width == 0 {
932			return Err(Error::invalid_argument(
933				"MaxUnpool2d input spatial extents must be nonzero",
934			));
935		}
936		matrix::max_unpool_2d(
937			input,
938			indices,
939			self.kernel_size,
940			self.stride,
941			self.padding,
942			self.implied_output_extent(*input_height),
943			self.implied_output_extent(*input_width),
944		)
945	}
946
947	/// Unpool one NCHW Matrix against recorded argmax indices into an explicit
948	/// output extent.
949	///
950	/// # Errors
951	///
952	/// Returns an error from the underlying Matrix operation.
953	pub fn forward_with_output(
954		&self,
955		input: &Matrix,
956		indices: &Matrix,
957		output_height: usize,
958		output_width: usize,
959	) -> Result<Matrix> {
960		matrix::max_unpool_2d(
961			input,
962			indices,
963			self.kernel_size,
964			self.stride,
965			self.padding,
966			output_height,
967			output_width,
968		)
969	}
970}
971
972impl Module for MaxUnpool2d {
973	fn forward(&self, input: &Matrix) -> Result<Matrix> {
974		let _ = input;
975		Err(Error::invalid_argument(
976			"MaxUnpool2d requires pooled input plus recorded argmax indices; call MaxUnpool2d::forward(input, indices)",
977		))
978	}
979
980	fn registry(&self) -> &ModuleRegistry {
981		&self.registry
982	}
983}