use crate::{Error, Matrix, Result};
use super::super::{Module, ModuleRegistry};
pub struct ConstantPad1d {
padding: [usize; 2],
value: f32,
registry: ModuleRegistry,
}
impl ConstantPad1d {
pub const fn new(padding: usize, value: f32) -> Self {
Self::new_with_padding([padding; 2], value)
}
pub const fn new_with_padding(padding: [usize; 2], value: f32) -> Self {
Self {
padding,
value,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 2] {
&self.padding
}
pub const fn value(&self) -> f32 {
self.value
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 1 {
return Err(Error::invalid_argument(
"ConstantPad1d requires an input of rank at least 1",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..1 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, self.value)
}
}
impl Module for ConstantPad1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ConstantPad1d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ConstantPad2d {
padding: [usize; 4],
value: f32,
registry: ModuleRegistry,
}
impl ConstantPad2d {
pub const fn new(padding: usize, value: f32) -> Self {
Self::new_with_padding([padding; 4], value)
}
pub const fn new_with_padding(padding: [usize; 4], value: f32) -> Self {
Self {
padding,
value,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 4] {
&self.padding
}
pub const fn value(&self) -> f32 {
self.value
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 2 {
return Err(Error::invalid_argument(
"ConstantPad2d requires an input of rank at least 2",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..2 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, self.value)
}
}
impl Module for ConstantPad2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ConstantPad2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ConstantPad3d {
padding: [usize; 6],
value: f32,
registry: ModuleRegistry,
}
impl ConstantPad3d {
pub const fn new(padding: usize, value: f32) -> Self {
Self::new_with_padding([padding; 6], value)
}
pub const fn new_with_padding(padding: [usize; 6], value: f32) -> Self {
Self {
padding,
value,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 6] {
&self.padding
}
pub const fn value(&self) -> f32 {
self.value
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 3 {
return Err(Error::invalid_argument(
"ConstantPad3d requires an input of rank at least 3",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..3 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, self.value)
}
}
impl Module for ConstantPad3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ConstantPad3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ReflectionPad1d {
padding: [usize; 2],
registry: ModuleRegistry,
}
impl ReflectionPad1d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 2])
}
pub const fn new_with_padding(padding: [usize; 2]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 2] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 1 {
return Err(Error::invalid_argument(
"ReflectionPad1d requires an input of rank at least 1",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..1 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Reflect, 0.0)
}
}
impl Default for ReflectionPad1d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ReflectionPad1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ReflectionPad1d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ReflectionPad2d {
padding: [usize; 4],
registry: ModuleRegistry,
}
impl ReflectionPad2d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 4])
}
pub const fn new_with_padding(padding: [usize; 4]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 4] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 2 {
return Err(Error::invalid_argument(
"ReflectionPad2d requires an input of rank at least 2",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..2 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Reflect, 0.0)
}
}
impl Default for ReflectionPad2d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ReflectionPad2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ReflectionPad2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ReflectionPad3d {
padding: [usize; 6],
registry: ModuleRegistry,
}
impl ReflectionPad3d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 6])
}
pub const fn new_with_padding(padding: [usize; 6]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 6] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 3 {
return Err(Error::invalid_argument(
"ReflectionPad3d requires an input of rank at least 3",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..3 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Reflect, 0.0)
}
}
impl Default for ReflectionPad3d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ReflectionPad3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ReflectionPad3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ReplicationPad1d {
padding: [usize; 2],
registry: ModuleRegistry,
}
impl ReplicationPad1d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 2])
}
pub const fn new_with_padding(padding: [usize; 2]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 2] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 1 {
return Err(Error::invalid_argument(
"ReplicationPad1d requires an input of rank at least 1",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..1 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Replicate, 0.0)
}
}
impl Default for ReplicationPad1d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ReplicationPad1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ReplicationPad1d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ReplicationPad2d {
padding: [usize; 4],
registry: ModuleRegistry,
}
impl ReplicationPad2d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 4])
}
pub const fn new_with_padding(padding: [usize; 4]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 4] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 2 {
return Err(Error::invalid_argument(
"ReplicationPad2d requires an input of rank at least 2",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..2 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Replicate, 0.0)
}
}
impl Default for ReplicationPad2d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ReplicationPad2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ReplicationPad2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ReplicationPad3d {
padding: [usize; 6],
registry: ModuleRegistry,
}
impl ReplicationPad3d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 6])
}
pub const fn new_with_padding(padding: [usize; 6]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 6] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 3 {
return Err(Error::invalid_argument(
"ReplicationPad3d requires an input of rank at least 3",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..3 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Replicate, 0.0)
}
}
impl Default for ReplicationPad3d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ReplicationPad3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ReplicationPad3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct CircularPad1d {
padding: [usize; 2],
registry: ModuleRegistry,
}
impl CircularPad1d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 2])
}
pub const fn new_with_padding(padding: [usize; 2]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 2] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 1 {
return Err(Error::invalid_argument(
"CircularPad1d requires an input of rank at least 1",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..1 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Circular, 0.0)
}
}
impl Default for CircularPad1d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for CircularPad1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
CircularPad1d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct CircularPad2d {
padding: [usize; 4],
registry: ModuleRegistry,
}
impl CircularPad2d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 4])
}
pub const fn new_with_padding(padding: [usize; 4]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 4] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 2 {
return Err(Error::invalid_argument(
"CircularPad2d requires an input of rank at least 2",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..2 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Circular, 0.0)
}
}
impl Default for CircularPad2d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for CircularPad2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
CircularPad2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct CircularPad3d {
padding: [usize; 6],
registry: ModuleRegistry,
}
impl CircularPad3d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 6])
}
pub const fn new_with_padding(padding: [usize; 6]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 6] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 3 {
return Err(Error::invalid_argument(
"CircularPad3d requires an input of rank at least 3",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..3 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Circular, 0.0)
}
}
impl Default for CircularPad3d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for CircularPad3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
CircularPad3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct ZeroPad2d {
padding: [usize; 4],
registry: ModuleRegistry,
}
impl ZeroPad2d {
pub const fn new(padding: usize) -> Self {
Self::new_with_padding([padding; 4])
}
pub const fn new_with_padding(padding: [usize; 4]) -> Self {
Self {
padding,
registry: ModuleRegistry::new(),
}
}
pub const fn padding(&self) -> &[usize; 4] {
&self.padding
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank < 2 {
return Err(Error::invalid_argument(
"ZeroPad2d requires an input of rank at least 2",
));
}
let mut pads = vec![0_usize; 2 * rank];
for index in 0..2 {
let axis = rank - 1 - index;
pads[2 * axis] = self.padding[2 * index];
pads[2 * axis + 1] = self.padding[2 * index + 1];
}
crate::matrix::pad(input, &pads, crate::matrix::PadMode::Constant, 0.0)
}
}
impl Default for ZeroPad2d {
fn default() -> Self {
Self::new(0)
}
}
impl Module for ZeroPad2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ZeroPad2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}