1use crate::{Error, Matrix, Result};
6
7use super::super::{Module, ModuleRegistry};
8
9pub struct ConstantPad1d {
17 padding: [usize; 2],
18 value: f32,
19 registry: ModuleRegistry,
20}
21
22impl ConstantPad1d {
23 pub const fn new(padding: usize, value: f32) -> Self {
25 Self::new_with_padding([padding; 2], value)
26 }
27
28 pub const fn new_with_padding(padding: [usize; 2], value: f32) -> Self {
30 Self {
31 padding,
32 value,
33 registry: ModuleRegistry::new(),
34 }
35 }
36
37 pub const fn padding(&self) -> &[usize; 2] {
39 &self.padding
40 }
41
42 pub const fn value(&self) -> f32 {
44 self.value
45 }
46
47 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
54 let rank = input.shape().len();
55 if rank < 1 {
56 return Err(Error::invalid_argument(
57 "ConstantPad1d requires an input of rank at least 1",
58 ));
59 }
60 let mut pads = vec![0_usize; 2 * rank];
61 for index in 0..1 {
62 let axis = rank - 1 - index;
63 pads[2 * axis] = self.padding[2 * index];
64 pads[2 * axis + 1] = self.padding[2 * index + 1];
65 }
66 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, self.value)
67 }
68}
69
70impl Module for ConstantPad1d {
71 fn forward(&self, input: &Matrix) -> Result<Matrix> {
72 ConstantPad1d::forward(self, input)
73 }
74
75 fn registry(&self) -> &ModuleRegistry {
76 &self.registry
77 }
78}
79
80pub struct ConstantPad2d {
88 padding: [usize; 4],
89 value: f32,
90 registry: ModuleRegistry,
91}
92
93impl ConstantPad2d {
94 pub const fn new(padding: usize, value: f32) -> Self {
96 Self::new_with_padding([padding; 4], value)
97 }
98
99 pub const fn new_with_padding(padding: [usize; 4], value: f32) -> Self {
101 Self {
102 padding,
103 value,
104 registry: ModuleRegistry::new(),
105 }
106 }
107
108 pub const fn padding(&self) -> &[usize; 4] {
110 &self.padding
111 }
112
113 pub const fn value(&self) -> f32 {
115 self.value
116 }
117
118 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
125 let rank = input.shape().len();
126 if rank < 2 {
127 return Err(Error::invalid_argument(
128 "ConstantPad2d requires an input of rank at least 2",
129 ));
130 }
131 let mut pads = vec![0_usize; 2 * rank];
132 for index in 0..2 {
133 let axis = rank - 1 - index;
134 pads[2 * axis] = self.padding[2 * index];
135 pads[2 * axis + 1] = self.padding[2 * index + 1];
136 }
137 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, self.value)
138 }
139}
140
141impl Module for ConstantPad2d {
142 fn forward(&self, input: &Matrix) -> Result<Matrix> {
143 ConstantPad2d::forward(self, input)
144 }
145
146 fn registry(&self) -> &ModuleRegistry {
147 &self.registry
148 }
149}
150
151pub struct ConstantPad3d {
159 padding: [usize; 6],
160 value: f32,
161 registry: ModuleRegistry,
162}
163
164impl ConstantPad3d {
165 pub const fn new(padding: usize, value: f32) -> Self {
167 Self::new_with_padding([padding; 6], value)
168 }
169
170 pub const fn new_with_padding(padding: [usize; 6], value: f32) -> Self {
172 Self {
173 padding,
174 value,
175 registry: ModuleRegistry::new(),
176 }
177 }
178
179 pub const fn padding(&self) -> &[usize; 6] {
181 &self.padding
182 }
183
184 pub const fn value(&self) -> f32 {
186 self.value
187 }
188
189 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
196 let rank = input.shape().len();
197 if rank < 3 {
198 return Err(Error::invalid_argument(
199 "ConstantPad3d requires an input of rank at least 3",
200 ));
201 }
202 let mut pads = vec![0_usize; 2 * rank];
203 for index in 0..3 {
204 let axis = rank - 1 - index;
205 pads[2 * axis] = self.padding[2 * index];
206 pads[2 * axis + 1] = self.padding[2 * index + 1];
207 }
208 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, self.value)
209 }
210}
211
212impl Module for ConstantPad3d {
213 fn forward(&self, input: &Matrix) -> Result<Matrix> {
214 ConstantPad3d::forward(self, input)
215 }
216
217 fn registry(&self) -> &ModuleRegistry {
218 &self.registry
219 }
220}
221
222pub struct ReflectionPad1d {
230 padding: [usize; 2],
231 registry: ModuleRegistry,
232}
233
234impl ReflectionPad1d {
235 pub const fn new(padding: usize) -> Self {
237 Self::new_with_padding([padding; 2])
238 }
239
240 pub const fn new_with_padding(padding: [usize; 2]) -> Self {
242 Self {
243 padding,
244 registry: ModuleRegistry::new(),
245 }
246 }
247
248 pub const fn padding(&self) -> &[usize; 2] {
250 &self.padding
251 }
252
253 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
260 let rank = input.shape().len();
261 if rank < 1 {
262 return Err(Error::invalid_argument(
263 "ReflectionPad1d requires an input of rank at least 1",
264 ));
265 }
266 let mut pads = vec![0_usize; 2 * rank];
267 for index in 0..1 {
268 let axis = rank - 1 - index;
269 pads[2 * axis] = self.padding[2 * index];
270 pads[2 * axis + 1] = self.padding[2 * index + 1];
271 }
272 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Reflect, 0.0)
273 }
274}
275
276impl Default for ReflectionPad1d {
277 fn default() -> Self {
278 Self::new(0)
279 }
280}
281
282impl Module for ReflectionPad1d {
283 fn forward(&self, input: &Matrix) -> Result<Matrix> {
284 ReflectionPad1d::forward(self, input)
285 }
286
287 fn registry(&self) -> &ModuleRegistry {
288 &self.registry
289 }
290}
291
292pub struct ReflectionPad2d {
300 padding: [usize; 4],
301 registry: ModuleRegistry,
302}
303
304impl ReflectionPad2d {
305 pub const fn new(padding: usize) -> Self {
307 Self::new_with_padding([padding; 4])
308 }
309
310 pub const fn new_with_padding(padding: [usize; 4]) -> Self {
312 Self {
313 padding,
314 registry: ModuleRegistry::new(),
315 }
316 }
317
318 pub const fn padding(&self) -> &[usize; 4] {
320 &self.padding
321 }
322
323 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
330 let rank = input.shape().len();
331 if rank < 2 {
332 return Err(Error::invalid_argument(
333 "ReflectionPad2d requires an input of rank at least 2",
334 ));
335 }
336 let mut pads = vec![0_usize; 2 * rank];
337 for index in 0..2 {
338 let axis = rank - 1 - index;
339 pads[2 * axis] = self.padding[2 * index];
340 pads[2 * axis + 1] = self.padding[2 * index + 1];
341 }
342 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Reflect, 0.0)
343 }
344}
345
346impl Default for ReflectionPad2d {
347 fn default() -> Self {
348 Self::new(0)
349 }
350}
351
352impl Module for ReflectionPad2d {
353 fn forward(&self, input: &Matrix) -> Result<Matrix> {
354 ReflectionPad2d::forward(self, input)
355 }
356
357 fn registry(&self) -> &ModuleRegistry {
358 &self.registry
359 }
360}
361
362pub struct ReflectionPad3d {
370 padding: [usize; 6],
371 registry: ModuleRegistry,
372}
373
374impl ReflectionPad3d {
375 pub const fn new(padding: usize) -> Self {
377 Self::new_with_padding([padding; 6])
378 }
379
380 pub const fn new_with_padding(padding: [usize; 6]) -> Self {
382 Self {
383 padding,
384 registry: ModuleRegistry::new(),
385 }
386 }
387
388 pub const fn padding(&self) -> &[usize; 6] {
390 &self.padding
391 }
392
393 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
400 let rank = input.shape().len();
401 if rank < 3 {
402 return Err(Error::invalid_argument(
403 "ReflectionPad3d requires an input of rank at least 3",
404 ));
405 }
406 let mut pads = vec![0_usize; 2 * rank];
407 for index in 0..3 {
408 let axis = rank - 1 - index;
409 pads[2 * axis] = self.padding[2 * index];
410 pads[2 * axis + 1] = self.padding[2 * index + 1];
411 }
412 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Reflect, 0.0)
413 }
414}
415
416impl Default for ReflectionPad3d {
417 fn default() -> Self {
418 Self::new(0)
419 }
420}
421
422impl Module for ReflectionPad3d {
423 fn forward(&self, input: &Matrix) -> Result<Matrix> {
424 ReflectionPad3d::forward(self, input)
425 }
426
427 fn registry(&self) -> &ModuleRegistry {
428 &self.registry
429 }
430}
431
432pub struct ReplicationPad1d {
440 padding: [usize; 2],
441 registry: ModuleRegistry,
442}
443
444impl ReplicationPad1d {
445 pub const fn new(padding: usize) -> Self {
447 Self::new_with_padding([padding; 2])
448 }
449
450 pub const fn new_with_padding(padding: [usize; 2]) -> Self {
452 Self {
453 padding,
454 registry: ModuleRegistry::new(),
455 }
456 }
457
458 pub const fn padding(&self) -> &[usize; 2] {
460 &self.padding
461 }
462
463 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
470 let rank = input.shape().len();
471 if rank < 1 {
472 return Err(Error::invalid_argument(
473 "ReplicationPad1d requires an input of rank at least 1",
474 ));
475 }
476 let mut pads = vec![0_usize; 2 * rank];
477 for index in 0..1 {
478 let axis = rank - 1 - index;
479 pads[2 * axis] = self.padding[2 * index];
480 pads[2 * axis + 1] = self.padding[2 * index + 1];
481 }
482 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Replicate, 0.0)
483 }
484}
485
486impl Default for ReplicationPad1d {
487 fn default() -> Self {
488 Self::new(0)
489 }
490}
491
492impl Module for ReplicationPad1d {
493 fn forward(&self, input: &Matrix) -> Result<Matrix> {
494 ReplicationPad1d::forward(self, input)
495 }
496
497 fn registry(&self) -> &ModuleRegistry {
498 &self.registry
499 }
500}
501
502pub struct ReplicationPad2d {
510 padding: [usize; 4],
511 registry: ModuleRegistry,
512}
513
514impl ReplicationPad2d {
515 pub const fn new(padding: usize) -> Self {
517 Self::new_with_padding([padding; 4])
518 }
519
520 pub const fn new_with_padding(padding: [usize; 4]) -> Self {
522 Self {
523 padding,
524 registry: ModuleRegistry::new(),
525 }
526 }
527
528 pub const fn padding(&self) -> &[usize; 4] {
530 &self.padding
531 }
532
533 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
540 let rank = input.shape().len();
541 if rank < 2 {
542 return Err(Error::invalid_argument(
543 "ReplicationPad2d requires an input of rank at least 2",
544 ));
545 }
546 let mut pads = vec![0_usize; 2 * rank];
547 for index in 0..2 {
548 let axis = rank - 1 - index;
549 pads[2 * axis] = self.padding[2 * index];
550 pads[2 * axis + 1] = self.padding[2 * index + 1];
551 }
552 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Replicate, 0.0)
553 }
554}
555
556impl Default for ReplicationPad2d {
557 fn default() -> Self {
558 Self::new(0)
559 }
560}
561
562impl Module for ReplicationPad2d {
563 fn forward(&self, input: &Matrix) -> Result<Matrix> {
564 ReplicationPad2d::forward(self, input)
565 }
566
567 fn registry(&self) -> &ModuleRegistry {
568 &self.registry
569 }
570}
571
572pub struct ReplicationPad3d {
580 padding: [usize; 6],
581 registry: ModuleRegistry,
582}
583
584impl ReplicationPad3d {
585 pub const fn new(padding: usize) -> Self {
587 Self::new_with_padding([padding; 6])
588 }
589
590 pub const fn new_with_padding(padding: [usize; 6]) -> Self {
592 Self {
593 padding,
594 registry: ModuleRegistry::new(),
595 }
596 }
597
598 pub const fn padding(&self) -> &[usize; 6] {
600 &self.padding
601 }
602
603 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
610 let rank = input.shape().len();
611 if rank < 3 {
612 return Err(Error::invalid_argument(
613 "ReplicationPad3d requires an input of rank at least 3",
614 ));
615 }
616 let mut pads = vec![0_usize; 2 * rank];
617 for index in 0..3 {
618 let axis = rank - 1 - index;
619 pads[2 * axis] = self.padding[2 * index];
620 pads[2 * axis + 1] = self.padding[2 * index + 1];
621 }
622 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Replicate, 0.0)
623 }
624}
625
626impl Default for ReplicationPad3d {
627 fn default() -> Self {
628 Self::new(0)
629 }
630}
631
632impl Module for ReplicationPad3d {
633 fn forward(&self, input: &Matrix) -> Result<Matrix> {
634 ReplicationPad3d::forward(self, input)
635 }
636
637 fn registry(&self) -> &ModuleRegistry {
638 &self.registry
639 }
640}
641
642pub struct CircularPad1d {
650 padding: [usize; 2],
651 registry: ModuleRegistry,
652}
653
654impl CircularPad1d {
655 pub const fn new(padding: usize) -> Self {
657 Self::new_with_padding([padding; 2])
658 }
659
660 pub const fn new_with_padding(padding: [usize; 2]) -> Self {
662 Self {
663 padding,
664 registry: ModuleRegistry::new(),
665 }
666 }
667
668 pub const fn padding(&self) -> &[usize; 2] {
670 &self.padding
671 }
672
673 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
680 let rank = input.shape().len();
681 if rank < 1 {
682 return Err(Error::invalid_argument(
683 "CircularPad1d requires an input of rank at least 1",
684 ));
685 }
686 let mut pads = vec![0_usize; 2 * rank];
687 for index in 0..1 {
688 let axis = rank - 1 - index;
689 pads[2 * axis] = self.padding[2 * index];
690 pads[2 * axis + 1] = self.padding[2 * index + 1];
691 }
692 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Circular, 0.0)
693 }
694}
695
696impl Default for CircularPad1d {
697 fn default() -> Self {
698 Self::new(0)
699 }
700}
701
702impl Module for CircularPad1d {
703 fn forward(&self, input: &Matrix) -> Result<Matrix> {
704 CircularPad1d::forward(self, input)
705 }
706
707 fn registry(&self) -> &ModuleRegistry {
708 &self.registry
709 }
710}
711
712pub struct CircularPad2d {
720 padding: [usize; 4],
721 registry: ModuleRegistry,
722}
723
724impl CircularPad2d {
725 pub const fn new(padding: usize) -> Self {
727 Self::new_with_padding([padding; 4])
728 }
729
730 pub const fn new_with_padding(padding: [usize; 4]) -> Self {
732 Self {
733 padding,
734 registry: ModuleRegistry::new(),
735 }
736 }
737
738 pub const fn padding(&self) -> &[usize; 4] {
740 &self.padding
741 }
742
743 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
750 let rank = input.shape().len();
751 if rank < 2 {
752 return Err(Error::invalid_argument(
753 "CircularPad2d requires an input of rank at least 2",
754 ));
755 }
756 let mut pads = vec![0_usize; 2 * rank];
757 for index in 0..2 {
758 let axis = rank - 1 - index;
759 pads[2 * axis] = self.padding[2 * index];
760 pads[2 * axis + 1] = self.padding[2 * index + 1];
761 }
762 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Circular, 0.0)
763 }
764}
765
766impl Default for CircularPad2d {
767 fn default() -> Self {
768 Self::new(0)
769 }
770}
771
772impl Module for CircularPad2d {
773 fn forward(&self, input: &Matrix) -> Result<Matrix> {
774 CircularPad2d::forward(self, input)
775 }
776
777 fn registry(&self) -> &ModuleRegistry {
778 &self.registry
779 }
780}
781
782pub struct CircularPad3d {
790 padding: [usize; 6],
791 registry: ModuleRegistry,
792}
793
794impl CircularPad3d {
795 pub const fn new(padding: usize) -> Self {
797 Self::new_with_padding([padding; 6])
798 }
799
800 pub const fn new_with_padding(padding: [usize; 6]) -> Self {
802 Self {
803 padding,
804 registry: ModuleRegistry::new(),
805 }
806 }
807
808 pub const fn padding(&self) -> &[usize; 6] {
810 &self.padding
811 }
812
813 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
820 let rank = input.shape().len();
821 if rank < 3 {
822 return Err(Error::invalid_argument(
823 "CircularPad3d requires an input of rank at least 3",
824 ));
825 }
826 let mut pads = vec![0_usize; 2 * rank];
827 for index in 0..3 {
828 let axis = rank - 1 - index;
829 pads[2 * axis] = self.padding[2 * index];
830 pads[2 * axis + 1] = self.padding[2 * index + 1];
831 }
832 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Circular, 0.0)
833 }
834}
835
836impl Default for CircularPad3d {
837 fn default() -> Self {
838 Self::new(0)
839 }
840}
841
842impl Module for CircularPad3d {
843 fn forward(&self, input: &Matrix) -> Result<Matrix> {
844 CircularPad3d::forward(self, input)
845 }
846
847 fn registry(&self) -> &ModuleRegistry {
848 &self.registry
849 }
850}
851
852pub struct ZeroPad2d {
860 padding: [usize; 4],
861 registry: ModuleRegistry,
862}
863
864impl ZeroPad2d {
865 pub const fn new(padding: usize) -> Self {
867 Self::new_with_padding([padding; 4])
868 }
869
870 pub const fn new_with_padding(padding: [usize; 4]) -> Self {
872 Self {
873 padding,
874 registry: ModuleRegistry::new(),
875 }
876 }
877
878 pub const fn padding(&self) -> &[usize; 4] {
880 &self.padding
881 }
882
883 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
890 let rank = input.shape().len();
891 if rank < 2 {
892 return Err(Error::invalid_argument(
893 "ZeroPad2d requires an input of rank at least 2",
894 ));
895 }
896 let mut pads = vec![0_usize; 2 * rank];
897 for index in 0..2 {
898 let axis = rank - 1 - index;
899 pads[2 * axis] = self.padding[2 * index];
900 pads[2 * axis + 1] = self.padding[2 * index + 1];
901 }
902 crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, 0.0)
903 }
904}
905
906impl Default for ZeroPad2d {
907 fn default() -> Self {
908 Self::new(0)
909 }
910}
911
912impl Module for ZeroPad2d {
913 fn forward(&self, input: &Matrix) -> Result<Matrix> {
914 ZeroPad2d::forward(self, input)
915 }
916
917 fn registry(&self) -> &ModuleRegistry {
918 &self.registry
919 }
920}