1use crate::{Error, Matrix, Result};
6
7use super::super::{Module, ModuleRegistry, matrix};
8
9pub struct AvgPool2d {
13 kernel_size: usize,
14 stride: usize,
15 padding: usize,
16 registry: ModuleRegistry,
17}
18
19impl AvgPool2d {
20 pub fn new(kernel_size: usize) -> Result<Self> {
26 Self::new_with_options(kernel_size, kernel_size, 0)
27 }
28
29 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 pub const fn kernel_size(&self) -> usize {
50 self.kernel_size
51 }
52
53 pub const fn stride(&self) -> usize {
55 self.stride
56 }
57
58 pub const fn padding(&self) -> usize {
60 self.padding
61 }
62
63 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
83pub struct MaxPool2d {
87 kernel_size: usize,
88 stride: usize,
89 padding: usize,
90 registry: ModuleRegistry,
91}
92
93impl MaxPool2d {
94 pub fn new(kernel_size: usize) -> Result<Self> {
100 Self::new_with_options(kernel_size, kernel_size, 0)
101 }
102
103 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 pub const fn kernel_size(&self) -> usize {
124 self.kernel_size
125 }
126
127 pub const fn stride(&self) -> usize {
129 self.stride
130 }
131
132 pub const fn padding(&self) -> usize {
134 self.padding
135 }
136
137 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
157pub struct MaxPool1d {
161 kernel_size: usize,
162 stride: usize,
163 padding: usize,
164 registry: ModuleRegistry,
165}
166
167impl MaxPool1d {
168 pub fn new(kernel_size: usize) -> Result<Self> {
174 Self::new_with_options(kernel_size, kernel_size, 0)
175 }
176
177 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 pub const fn kernel_size(&self) -> usize {
198 self.kernel_size
199 }
200
201 pub const fn stride(&self) -> usize {
203 self.stride
204 }
205
206 pub const fn padding(&self) -> usize {
208 self.padding
209 }
210
211 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
231pub struct AdaptiveMaxPool1d {
235 output_length: usize,
236 registry: ModuleRegistry,
237}
238
239impl AdaptiveMaxPool1d {
240 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 pub const fn output_length(&self) -> usize {
259 self.output_length
260 }
261
262 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
283pub struct AdaptiveAvgPool2d {
287 output_height: usize,
288 output_width: usize,
289 registry: ModuleRegistry,
290}
291
292impl AdaptiveAvgPool2d {
293 pub fn new(output_size: usize) -> Result<Self> {
299 Self::new_with_output_size(output_size, output_size)
300 }
301
302 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 pub const fn output_height(&self) -> usize {
322 self.output_height
323 }
324
325 pub const fn output_width(&self) -> usize {
327 self.output_width
328 }
329
330 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
350pub struct MaxPool3d {
354 kernel_size: usize,
355 stride: usize,
356 padding: usize,
357 registry: ModuleRegistry,
358}
359
360impl MaxPool3d {
361 pub fn new(kernel_size: usize) -> Result<Self> {
367 Self::new_with_options(kernel_size, kernel_size, 0)
368 }
369
370 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 pub const fn kernel_size(&self) -> usize {
391 self.kernel_size
392 }
393
394 pub const fn stride(&self) -> usize {
396 self.stride
397 }
398
399 pub const fn padding(&self) -> usize {
401 self.padding
402 }
403
404 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
424pub struct AvgPool3d {
428 kernel_size: usize,
429 stride: usize,
430 padding: usize,
431 registry: ModuleRegistry,
432}
433
434impl AvgPool3d {
435 pub fn new(kernel_size: usize) -> Result<Self> {
441 Self::new_with_options(kernel_size, kernel_size, 0)
442 }
443
444 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 pub const fn kernel_size(&self) -> usize {
465 self.kernel_size
466 }
467
468 pub const fn stride(&self) -> usize {
470 self.stride
471 }
472
473 pub const fn padding(&self) -> usize {
475 self.padding
476 }
477
478 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
498pub struct AdaptiveMaxPool2d {
502 output_height: usize,
503 output_width: usize,
504 registry: ModuleRegistry,
505}
506
507impl AdaptiveMaxPool2d {
508 pub fn new(output_size: usize) -> Result<Self> {
514 Self::new_with_output_size(output_size, output_size)
515 }
516
517 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 pub const fn output_height(&self) -> usize {
537 self.output_height
538 }
539
540 pub const fn output_width(&self) -> usize {
542 self.output_width
543 }
544
545 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
566pub struct AdaptiveMaxPool3d {
570 output_depth: usize,
571 output_height: usize,
572 output_width: usize,
573 registry: ModuleRegistry,
574}
575
576impl AdaptiveMaxPool3d {
577 pub fn new(output_size: usize) -> Result<Self> {
583 Self::new_with_output_size(output_size, output_size, output_size)
584 }
585
586 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 pub const fn output_depth(&self) -> usize {
611 self.output_depth
612 }
613
614 pub const fn output_height(&self) -> usize {
616 self.output_height
617 }
618
619 pub const fn output_width(&self) -> usize {
621 self.output_width
622 }
623
624 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
653pub struct AdaptiveAvgPool3d {
657 output_depth: usize,
658 output_height: usize,
659 output_width: usize,
660 registry: ModuleRegistry,
661}
662
663impl AdaptiveAvgPool3d {
664 pub fn new(output_size: usize) -> Result<Self> {
670 Self::new_with_output_size(output_size, output_size, output_size)
671 }
672
673 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 pub const fn output_depth(&self) -> usize {
698 self.output_depth
699 }
700
701 pub const fn output_height(&self) -> usize {
703 self.output_height
704 }
705
706 pub const fn output_width(&self) -> usize {
708 self.output_width
709 }
710
711 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
736pub struct MaxUnpool1d {
740 kernel_size: usize,
741 stride: usize,
742 padding: usize,
743 registry: ModuleRegistry,
744}
745
746impl MaxUnpool1d {
747 pub fn new(kernel_size: usize) -> Result<Self> {
753 Self::new_with_options(kernel_size, kernel_size, 0)
754 }
755
756 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 pub const fn kernel_size(&self) -> usize {
777 self.kernel_size
778 }
779
780 pub const fn stride(&self) -> usize {
782 self.stride
783 }
784
785 pub const fn padding(&self) -> usize {
787 self.padding
788 }
789
790 fn implied_output_length(&self, input_length: usize) -> usize {
792 (input_length - 1) * self.stride - 2 * self.padding + self.kernel_size
793 }
794
795 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 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
859pub struct MaxUnpool2d {
863 kernel_size: usize,
864 stride: usize,
865 padding: usize,
866 registry: ModuleRegistry,
867}
868
869impl MaxUnpool2d {
870 pub fn new(kernel_size: usize) -> Result<Self> {
876 Self::new_with_options(kernel_size, kernel_size, 0)
877 }
878
879 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 pub const fn kernel_size(&self) -> usize {
900 self.kernel_size
901 }
902
903 pub const fn stride(&self) -> usize {
905 self.stride
906 }
907
908 pub const fn padding(&self) -> usize {
910 self.padding
911 }
912
913 fn implied_output_extent(&self, input_extent: usize) -> usize {
915 (input_extent - 1) * self.stride - 2 * self.padding + self.kernel_size
916 }
917
918 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 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}