Skip to main content

oa/ml/nn/
padding.gen.rs

1// @generated by tool/gen/nn/generate.py; DO NOT EDIT.
2// Source: tool/gen/nn/schema/ml/ml_nn_padding.json
3// generator_version=8; schema_sha256=4d36f55af95f0784d8712c8bffdebd4f1f5ca5eda98d525525c5019247f11e04
4
5use crate::{Error, Matrix, Result};
6
7use super::super::{Module, ModuleRegistry};
8
9// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/constant_pad_1d.cpp
10// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
11/// Pad the last dimension of a Matrix with a constant value.
12///
13/// Padding uses PyTorch ordering on the trailing 1 axis or axes:
14/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
15/// and `[left, right, top, bottom, front, back]` for three.
16pub struct ConstantPad1d {
17	padding: [usize; 2],
18	value: f32,
19	registry: ModuleRegistry,
20}
21
22impl ConstantPad1d {
23	/// Construct ConstantPad1d with equal padding on both sides of each axis.
24	pub const fn new(padding: usize, value: f32) -> Self {
25		Self::new_with_padding([padding; 2], value)
26	}
27
28	/// Construct ConstantPad1d with explicit per-side padding in PyTorch order.
29	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	/// Return the configured per-side padding in PyTorch order.
38	pub const fn padding(&self) -> &[usize; 2] {
39		&self.padding
40	}
41
42	/// Return the constant fill value.
43	pub const fn value(&self) -> f32 {
44		self.value
45	}
46
47	/// Pad the trailing 1 axis or axes of one Matrix.
48	///
49	/// # Errors
50	///
51	/// Returns an error when the input rank is below 1 or the underlying
52	/// pad contract fails.
53	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
80// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/constant_pad_2d.cpp
81// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
82/// Pad the trailing two dimensions of a Matrix with a constant value.
83///
84/// Padding uses PyTorch ordering on the trailing 2 axis or axes:
85/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
86/// and `[left, right, top, bottom, front, back]` for three.
87pub struct ConstantPad2d {
88	padding: [usize; 4],
89	value: f32,
90	registry: ModuleRegistry,
91}
92
93impl ConstantPad2d {
94	/// Construct ConstantPad2d with equal padding on both sides of each axis.
95	pub const fn new(padding: usize, value: f32) -> Self {
96		Self::new_with_padding([padding; 4], value)
97	}
98
99	/// Construct ConstantPad2d with explicit per-side padding in PyTorch order.
100	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	/// Return the configured per-side padding in PyTorch order.
109	pub const fn padding(&self) -> &[usize; 4] {
110		&self.padding
111	}
112
113	/// Return the constant fill value.
114	pub const fn value(&self) -> f32 {
115		self.value
116	}
117
118	/// Pad the trailing 2 axis or axes of one Matrix.
119	///
120	/// # Errors
121	///
122	/// Returns an error when the input rank is below 2 or the underlying
123	/// pad contract fails.
124	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
151// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/constant_pad_3d.cpp
152// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
153/// Pad the trailing three dimensions of a Matrix with a constant value.
154///
155/// Padding uses PyTorch ordering on the trailing 3 axis or axes:
156/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
157/// and `[left, right, top, bottom, front, back]` for three.
158pub struct ConstantPad3d {
159	padding: [usize; 6],
160	value: f32,
161	registry: ModuleRegistry,
162}
163
164impl ConstantPad3d {
165	/// Construct ConstantPad3d with equal padding on both sides of each axis.
166	pub const fn new(padding: usize, value: f32) -> Self {
167		Self::new_with_padding([padding; 6], value)
168	}
169
170	/// Construct ConstantPad3d with explicit per-side padding in PyTorch order.
171	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	/// Return the configured per-side padding in PyTorch order.
180	pub const fn padding(&self) -> &[usize; 6] {
181		&self.padding
182	}
183
184	/// Return the constant fill value.
185	pub const fn value(&self) -> f32 {
186		self.value
187	}
188
189	/// Pad the trailing 3 axis or axes of one Matrix.
190	///
191	/// # Errors
192	///
193	/// Returns an error when the input rank is below 3 or the underlying
194	/// pad contract fails.
195	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
222// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/reflection_pad_1d.cpp
223// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
224/// Pad the last dimension by mirroring the border without repeating the edge element.
225///
226/// Padding uses PyTorch ordering on the trailing 1 axis or axes:
227/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
228/// and `[left, right, top, bottom, front, back]` for three.
229pub struct ReflectionPad1d {
230	padding: [usize; 2],
231	registry: ModuleRegistry,
232}
233
234impl ReflectionPad1d {
235	/// Construct ReflectionPad1d with equal padding on both sides of each axis.
236	pub const fn new(padding: usize) -> Self {
237		Self::new_with_padding([padding; 2])
238	}
239
240	/// Construct ReflectionPad1d with explicit per-side padding in PyTorch order.
241	pub const fn new_with_padding(padding: [usize; 2]) -> Self {
242		Self {
243			padding,
244			registry: ModuleRegistry::new(),
245		}
246	}
247
248	/// Return the configured per-side padding in PyTorch order.
249	pub const fn padding(&self) -> &[usize; 2] {
250		&self.padding
251	}
252
253	/// Pad the trailing 1 axis or axes of one Matrix.
254	///
255	/// # Errors
256	///
257	/// Returns an error when the input rank is below 1 or the underlying
258	/// pad contract fails.
259	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
292// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/reflection_pad_2d.cpp
293// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
294/// Pad the trailing two dimensions by mirroring the border without repeating edge elements.
295///
296/// Padding uses PyTorch ordering on the trailing 2 axis or axes:
297/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
298/// and `[left, right, top, bottom, front, back]` for three.
299pub struct ReflectionPad2d {
300	padding: [usize; 4],
301	registry: ModuleRegistry,
302}
303
304impl ReflectionPad2d {
305	/// Construct ReflectionPad2d with equal padding on both sides of each axis.
306	pub const fn new(padding: usize) -> Self {
307		Self::new_with_padding([padding; 4])
308	}
309
310	/// Construct ReflectionPad2d with explicit per-side padding in PyTorch order.
311	pub const fn new_with_padding(padding: [usize; 4]) -> Self {
312		Self {
313			padding,
314			registry: ModuleRegistry::new(),
315		}
316	}
317
318	/// Return the configured per-side padding in PyTorch order.
319	pub const fn padding(&self) -> &[usize; 4] {
320		&self.padding
321	}
322
323	/// Pad the trailing 2 axis or axes of one Matrix.
324	///
325	/// # Errors
326	///
327	/// Returns an error when the input rank is below 2 or the underlying
328	/// pad contract fails.
329	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
362// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/reflection_pad_3d.cpp
363// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
364/// Pad the trailing three dimensions by mirroring the border without repeating edge elements.
365///
366/// Padding uses PyTorch ordering on the trailing 3 axis or axes:
367/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
368/// and `[left, right, top, bottom, front, back]` for three.
369pub struct ReflectionPad3d {
370	padding: [usize; 6],
371	registry: ModuleRegistry,
372}
373
374impl ReflectionPad3d {
375	/// Construct ReflectionPad3d with equal padding on both sides of each axis.
376	pub const fn new(padding: usize) -> Self {
377		Self::new_with_padding([padding; 6])
378	}
379
380	/// Construct ReflectionPad3d with explicit per-side padding in PyTorch order.
381	pub const fn new_with_padding(padding: [usize; 6]) -> Self {
382		Self {
383			padding,
384			registry: ModuleRegistry::new(),
385		}
386	}
387
388	/// Return the configured per-side padding in PyTorch order.
389	pub const fn padding(&self) -> &[usize; 6] {
390		&self.padding
391	}
392
393	/// Pad the trailing 3 axis or axes of one Matrix.
394	///
395	/// # Errors
396	///
397	/// Returns an error when the input rank is below 3 or the underlying
398	/// pad contract fails.
399	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
432// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/replication_pad_1d.cpp
433// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
434/// Pad the last dimension by clamping coordinates to the edge element.
435///
436/// Padding uses PyTorch ordering on the trailing 1 axis or axes:
437/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
438/// and `[left, right, top, bottom, front, back]` for three.
439pub struct ReplicationPad1d {
440	padding: [usize; 2],
441	registry: ModuleRegistry,
442}
443
444impl ReplicationPad1d {
445	/// Construct ReplicationPad1d with equal padding on both sides of each axis.
446	pub const fn new(padding: usize) -> Self {
447		Self::new_with_padding([padding; 2])
448	}
449
450	/// Construct ReplicationPad1d with explicit per-side padding in PyTorch order.
451	pub const fn new_with_padding(padding: [usize; 2]) -> Self {
452		Self {
453			padding,
454			registry: ModuleRegistry::new(),
455		}
456	}
457
458	/// Return the configured per-side padding in PyTorch order.
459	pub const fn padding(&self) -> &[usize; 2] {
460		&self.padding
461	}
462
463	/// Pad the trailing 1 axis or axes of one Matrix.
464	///
465	/// # Errors
466	///
467	/// Returns an error when the input rank is below 1 or the underlying
468	/// pad contract fails.
469	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
502// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/replication_pad_2d.cpp
503// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
504/// Pad the trailing two dimensions by clamping coordinates to edge elements.
505///
506/// Padding uses PyTorch ordering on the trailing 2 axis or axes:
507/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
508/// and `[left, right, top, bottom, front, back]` for three.
509pub struct ReplicationPad2d {
510	padding: [usize; 4],
511	registry: ModuleRegistry,
512}
513
514impl ReplicationPad2d {
515	/// Construct ReplicationPad2d with equal padding on both sides of each axis.
516	pub const fn new(padding: usize) -> Self {
517		Self::new_with_padding([padding; 4])
518	}
519
520	/// Construct ReplicationPad2d with explicit per-side padding in PyTorch order.
521	pub const fn new_with_padding(padding: [usize; 4]) -> Self {
522		Self {
523			padding,
524			registry: ModuleRegistry::new(),
525		}
526	}
527
528	/// Return the configured per-side padding in PyTorch order.
529	pub const fn padding(&self) -> &[usize; 4] {
530		&self.padding
531	}
532
533	/// Pad the trailing 2 axis or axes of one Matrix.
534	///
535	/// # Errors
536	///
537	/// Returns an error when the input rank is below 2 or the underlying
538	/// pad contract fails.
539	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
572// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/replication_pad_3d.cpp
573// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
574/// Pad the trailing three dimensions by clamping coordinates to edge elements.
575///
576/// Padding uses PyTorch ordering on the trailing 3 axis or axes:
577/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
578/// and `[left, right, top, bottom, front, back]` for three.
579pub struct ReplicationPad3d {
580	padding: [usize; 6],
581	registry: ModuleRegistry,
582}
583
584impl ReplicationPad3d {
585	/// Construct ReplicationPad3d with equal padding on both sides of each axis.
586	pub const fn new(padding: usize) -> Self {
587		Self::new_with_padding([padding; 6])
588	}
589
590	/// Construct ReplicationPad3d with explicit per-side padding in PyTorch order.
591	pub const fn new_with_padding(padding: [usize; 6]) -> Self {
592		Self {
593			padding,
594			registry: ModuleRegistry::new(),
595		}
596	}
597
598	/// Return the configured per-side padding in PyTorch order.
599	pub const fn padding(&self) -> &[usize; 6] {
600		&self.padding
601	}
602
603	/// Pad the trailing 3 axis or axes of one Matrix.
604	///
605	/// # Errors
606	///
607	/// Returns an error when the input rank is below 3 or the underlying
608	/// pad contract fails.
609	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
642// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/circular_pad_1d.cpp
643// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
644/// Pad the last dimension by wrapping coordinates around the axis extent.
645///
646/// Padding uses PyTorch ordering on the trailing 1 axis or axes:
647/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
648/// and `[left, right, top, bottom, front, back]` for three.
649pub struct CircularPad1d {
650	padding: [usize; 2],
651	registry: ModuleRegistry,
652}
653
654impl CircularPad1d {
655	/// Construct CircularPad1d with equal padding on both sides of each axis.
656	pub const fn new(padding: usize) -> Self {
657		Self::new_with_padding([padding; 2])
658	}
659
660	/// Construct CircularPad1d with explicit per-side padding in PyTorch order.
661	pub const fn new_with_padding(padding: [usize; 2]) -> Self {
662		Self {
663			padding,
664			registry: ModuleRegistry::new(),
665		}
666	}
667
668	/// Return the configured per-side padding in PyTorch order.
669	pub const fn padding(&self) -> &[usize; 2] {
670		&self.padding
671	}
672
673	/// Pad the trailing 1 axis or axes of one Matrix.
674	///
675	/// # Errors
676	///
677	/// Returns an error when the input rank is below 1 or the underlying
678	/// pad contract fails.
679	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
712// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/circular_pad_2d.cpp
713// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
714/// Pad the trailing two dimensions by wrapping coordinates around each axis extent.
715///
716/// Padding uses PyTorch ordering on the trailing 2 axis or axes:
717/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
718/// and `[left, right, top, bottom, front, back]` for three.
719pub struct CircularPad2d {
720	padding: [usize; 4],
721	registry: ModuleRegistry,
722}
723
724impl CircularPad2d {
725	/// Construct CircularPad2d with equal padding on both sides of each axis.
726	pub const fn new(padding: usize) -> Self {
727		Self::new_with_padding([padding; 4])
728	}
729
730	/// Construct CircularPad2d with explicit per-side padding in PyTorch order.
731	pub const fn new_with_padding(padding: [usize; 4]) -> Self {
732		Self {
733			padding,
734			registry: ModuleRegistry::new(),
735		}
736	}
737
738	/// Return the configured per-side padding in PyTorch order.
739	pub const fn padding(&self) -> &[usize; 4] {
740		&self.padding
741	}
742
743	/// Pad the trailing 2 axis or axes of one Matrix.
744	///
745	/// # Errors
746	///
747	/// Returns an error when the input rank is below 2 or the underlying
748	/// pad contract fails.
749	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
782// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/circular_pad_3d.cpp
783// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
784/// Pad the trailing three dimensions by wrapping coordinates around each axis extent.
785///
786/// Padding uses PyTorch ordering on the trailing 3 axis or axes:
787/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
788/// and `[left, right, top, bottom, front, back]` for three.
789pub struct CircularPad3d {
790	padding: [usize; 6],
791	registry: ModuleRegistry,
792}
793
794impl CircularPad3d {
795	/// Construct CircularPad3d with equal padding on both sides of each axis.
796	pub const fn new(padding: usize) -> Self {
797		Self::new_with_padding([padding; 6])
798	}
799
800	/// Construct CircularPad3d with explicit per-side padding in PyTorch order.
801	pub const fn new_with_padding(padding: [usize; 6]) -> Self {
802		Self {
803			padding,
804			registry: ModuleRegistry::new(),
805		}
806	}
807
808	/// Return the configured per-side padding in PyTorch order.
809	pub const fn padding(&self) -> &[usize; 6] {
810		&self.padding
811	}
812
813	/// Pad the trailing 3 axis or axes of one Matrix.
814	///
815	/// # Errors
816	///
817	/// Returns an error when the input rank is below 3 or the underlying
818	/// pad contract fails.
819	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
852// OA donor: oacpp/source/cpp/lib/oa/ml/nn/pad/zero_pad_2d.cpp
853// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
854/// Pad the trailing two dimensions of a Matrix with zeros.
855///
856/// Padding uses PyTorch ordering on the trailing 2 axis or axes:
857/// `[left, right]` for one dimension, `[left, right, top, bottom]` for two,
858/// and `[left, right, top, bottom, front, back]` for three.
859pub struct ZeroPad2d {
860	padding: [usize; 4],
861	registry: ModuleRegistry,
862}
863
864impl ZeroPad2d {
865	/// Construct ZeroPad2d with equal padding on both sides of each axis.
866	pub const fn new(padding: usize) -> Self {
867		Self::new_with_padding([padding; 4])
868	}
869
870	/// Construct ZeroPad2d with explicit per-side padding in PyTorch order.
871	pub const fn new_with_padding(padding: [usize; 4]) -> Self {
872		Self {
873			padding,
874			registry: ModuleRegistry::new(),
875		}
876	}
877
878	/// Return the configured per-side padding in PyTorch order.
879	pub const fn padding(&self) -> &[usize; 4] {
880		&self.padding
881	}
882
883	/// Pad the trailing 2 axis or axes of one Matrix.
884	///
885	/// # Errors
886	///
887	/// Returns an error when the input rank is below 2 or the underlying
888	/// pad contract fails.
889	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}