use crate::{Error, Matrix, Result};
use super::super::{Module, ModuleRegistry, matrix};
pub struct Identity {
registry: ModuleRegistry,
}
impl Identity {
pub const fn new() -> Self {
Self {
registry: ModuleRegistry::new(),
}
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(input.clone())
}
}
impl Default for Identity {
fn default() -> Self {
Self::new()
}
}
impl Module for Identity {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
Identity::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct Flatten {
start_dim: isize,
end_dim: isize,
registry: ModuleRegistry,
}
impl Flatten {
pub const fn new(start_dim: isize, end_dim: isize) -> Self {
Self {
start_dim,
end_dim,
registry: ModuleRegistry::new(),
}
}
pub const fn start_dim(&self) -> isize {
self.start_dim
}
pub const fn end_dim(&self) -> isize {
self.end_dim
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let rank = input.shape().len();
if rank <= 1 || self.start_dim >= 0 && self.start_dim as usize >= rank {
return Ok(input.clone());
}
let start = resolve_dim(self.start_dim, rank, "start")?;
let end = resolve_dim(self.end_dim, rank, "end")?;
if start > end {
return Err(Error::invalid_argument(format!(
"Flatten start dimension {start} exceeds end dimension {end}"
)));
}
let flattened = input.shape()[start..=end]
.iter()
.try_fold(1_usize, |extent, dimension| {
extent
.checked_mul(*dimension)
.ok_or_else(|| Error::resource_exhausted("Flatten extent overflows usize"))
})?;
let mut shape = Vec::with_capacity(rank - (end - start));
shape.extend_from_slice(&input.shape()[..start]);
shape.push(flattened);
shape.extend_from_slice(&input.shape()[end + 1..]);
input.reshape(shape)
}
}
impl Default for Flatten {
fn default() -> Self {
Self::new(1, -1)
}
}
impl Module for Flatten {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
Flatten::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct Unfold {
kernel_size: usize,
stride: usize,
padding: usize,
dilation: usize,
registry: ModuleRegistry,
}
impl Unfold {
pub fn new(kernel_size: usize, stride: usize, padding: usize, dilation: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 || dilation == 0 {
return Err(crate::Error::invalid_argument(
"Unfold requires nonzero kernel size, stride, and dilation",
));
}
Ok(Self {
kernel_size,
stride,
padding,
dilation,
registry: ModuleRegistry::new(),
})
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::unfold(
input,
self.kernel_size,
self.stride,
self.padding,
self.dilation,
)
}
pub const fn kernel_size(&self) -> usize {
self.kernel_size
}
pub const fn stride(&self) -> usize {
self.stride
}
pub const fn padding(&self) -> usize {
self.padding
}
pub const fn dilation(&self) -> usize {
self.dilation
}
}
impl Module for Unfold {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
Unfold::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct Fold {
output_height: usize,
output_width: usize,
kernel_size: usize,
stride: usize,
padding: usize,
dilation: usize,
registry: ModuleRegistry,
}
impl Fold {
pub fn new(
output_height: usize,
output_width: usize,
kernel_size: usize,
stride: usize,
padding: usize,
dilation: usize,
) -> Result<Self> {
if output_height == 0 || output_width == 0 || kernel_size == 0 || stride == 0 || dilation == 0 {
return Err(crate::Error::invalid_argument(
"Fold requires nonzero output extent, kernel size, stride, and dilation",
));
}
Ok(Self {
output_height,
output_width,
kernel_size,
stride,
padding,
dilation,
registry: ModuleRegistry::new(),
})
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::fold(
input,
self.output_height,
self.output_width,
self.kernel_size,
self.stride,
self.padding,
self.dilation,
)
}
pub const fn output_height(&self) -> usize {
self.output_height
}
pub const fn output_width(&self) -> usize {
self.output_width
}
pub const fn kernel_size(&self) -> usize {
self.kernel_size
}
pub const fn stride(&self) -> usize {
self.stride
}
pub const fn padding(&self) -> usize {
self.padding
}
pub const fn dilation(&self) -> usize {
self.dilation
}
}
impl Module for Fold {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
Fold::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
fn resolve_dim(dimension: isize, rank: usize, label: &str) -> Result<usize> {
let rank =
isize::try_from(rank).map_err(|_| Error::resource_exhausted("Matrix rank exceeds isize"))?;
let resolved = if dimension < 0 {
rank
.checked_add(dimension)
.ok_or_else(|| Error::invalid_argument(format!("Flatten {label} dimension is invalid")))?
} else {
dimension
};
if !(0..rank).contains(&resolved) {
return Err(Error::invalid_argument(format!(
"Flatten {label} dimension {dimension} is outside rank {rank}"
)));
}
Ok(resolved as usize)
}