use crate::{Error, Matrix, Result};
use super::super::{Module, ModuleRegistry, matrix};
pub struct AvgPool2d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl AvgPool2d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"AvgPool2d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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 fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::avg_pool_2d(input, self.kernel_size, self.stride, self.padding)
}
}
impl Module for AvgPool2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AvgPool2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct MaxPool2d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl MaxPool2d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"MaxPool2d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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 fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(matrix::max_pool_2d(input, self.kernel_size, self.stride, self.padding)?.output)
}
}
impl Module for MaxPool2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
MaxPool2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct MaxPool1d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl MaxPool1d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"MaxPool1d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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 fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(matrix::max_pool_1d(input, self.kernel_size, self.stride, self.padding)?.output)
}
}
impl Module for MaxPool1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
MaxPool1d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct AdaptiveMaxPool1d {
output_length: usize,
registry: ModuleRegistry,
}
impl AdaptiveMaxPool1d {
pub fn new(output_length: usize) -> Result<Self> {
if output_length == 0 {
return Err(Error::invalid_argument(
"AdaptiveMaxPool1d output extent must be nonzero",
));
}
Ok(Self {
output_length,
registry: ModuleRegistry::new(),
})
}
pub const fn output_length(&self) -> usize {
self.output_length
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(matrix::adaptive_max_pool_1d(input, self.output_length)?.output)
}
}
impl Module for AdaptiveMaxPool1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AdaptiveMaxPool1d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct AdaptiveAvgPool2d {
output_height: usize,
output_width: usize,
registry: ModuleRegistry,
}
impl AdaptiveAvgPool2d {
pub fn new(output_size: usize) -> Result<Self> {
Self::new_with_output_size(output_size, output_size)
}
pub fn new_with_output_size(output_height: usize, output_width: usize) -> Result<Self> {
if output_height == 0 || output_width == 0 {
return Err(Error::invalid_argument(
"AdaptiveAvgPool2d output extents must be nonzero",
));
}
Ok(Self {
output_height,
output_width,
registry: ModuleRegistry::new(),
})
}
pub const fn output_height(&self) -> usize {
self.output_height
}
pub const fn output_width(&self) -> usize {
self.output_width
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::adaptive_avg_pool_2d(input, self.output_height, self.output_width)
}
}
impl Module for AdaptiveAvgPool2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AdaptiveAvgPool2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct MaxPool3d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl MaxPool3d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"MaxPool3d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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 fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(matrix::max_pool_3d(input, self.kernel_size, self.stride, self.padding)?.output)
}
}
impl Module for MaxPool3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
MaxPool3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct AvgPool3d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl AvgPool3d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"AvgPool3d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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 fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::avg_pool_3d(input, self.kernel_size, self.stride, self.padding)
}
}
impl Module for AvgPool3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AvgPool3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct AdaptiveMaxPool2d {
output_height: usize,
output_width: usize,
registry: ModuleRegistry,
}
impl AdaptiveMaxPool2d {
pub fn new(output_size: usize) -> Result<Self> {
Self::new_with_output_size(output_size, output_size)
}
pub fn new_with_output_size(output_height: usize, output_width: usize) -> Result<Self> {
if output_height == 0 || output_width == 0 {
return Err(Error::invalid_argument(
"AdaptiveMaxPool2d output extents must be nonzero",
));
}
Ok(Self {
output_height,
output_width,
registry: ModuleRegistry::new(),
})
}
pub const fn output_height(&self) -> usize {
self.output_height
}
pub const fn output_width(&self) -> usize {
self.output_width
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(matrix::adaptive_max_pool_2d(input, self.output_height, self.output_width)?.output)
}
}
impl Module for AdaptiveMaxPool2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AdaptiveMaxPool2d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct AdaptiveMaxPool3d {
output_depth: usize,
output_height: usize,
output_width: usize,
registry: ModuleRegistry,
}
impl AdaptiveMaxPool3d {
pub fn new(output_size: usize) -> Result<Self> {
Self::new_with_output_size(output_size, output_size, output_size)
}
pub fn new_with_output_size(
output_depth: usize,
output_height: usize,
output_width: usize,
) -> Result<Self> {
if output_depth == 0 || output_height == 0 || output_width == 0 {
return Err(Error::invalid_argument(
"AdaptiveMaxPool3d output extents must be nonzero",
));
}
Ok(Self {
output_depth,
output_height,
output_width,
registry: ModuleRegistry::new(),
})
}
pub const fn output_depth(&self) -> usize {
self.output_depth
}
pub const fn output_height(&self) -> usize {
self.output_height
}
pub const fn output_width(&self) -> usize {
self.output_width
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
Ok(
matrix::adaptive_max_pool_3d(
input,
self.output_depth,
self.output_height,
self.output_width,
)?
.output,
)
}
}
impl Module for AdaptiveMaxPool3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AdaptiveMaxPool3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct AdaptiveAvgPool3d {
output_depth: usize,
output_height: usize,
output_width: usize,
registry: ModuleRegistry,
}
impl AdaptiveAvgPool3d {
pub fn new(output_size: usize) -> Result<Self> {
Self::new_with_output_size(output_size, output_size, output_size)
}
pub fn new_with_output_size(
output_depth: usize,
output_height: usize,
output_width: usize,
) -> Result<Self> {
if output_depth == 0 || output_height == 0 || output_width == 0 {
return Err(Error::invalid_argument(
"AdaptiveAvgPool3d output extents must be nonzero",
));
}
Ok(Self {
output_depth,
output_height,
output_width,
registry: ModuleRegistry::new(),
})
}
pub const fn output_depth(&self) -> usize {
self.output_depth
}
pub const fn output_height(&self) -> usize {
self.output_height
}
pub const fn output_width(&self) -> usize {
self.output_width
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::adaptive_avg_pool_3d(
input,
self.output_depth,
self.output_height,
self.output_width,
)
}
}
impl Module for AdaptiveAvgPool3d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AdaptiveAvgPool3d::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct MaxUnpool1d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl MaxUnpool1d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"MaxUnpool1d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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
}
fn implied_output_length(&self, input_length: usize) -> usize {
(input_length - 1) * self.stride - 2 * self.padding + self.kernel_size
}
pub fn forward(&self, input: &Matrix, indices: &Matrix) -> Result<Matrix> {
let [_, _, input_length] = input.shape() else {
return Err(Error::invalid_argument(
"MaxUnpool1d input must be a rank-three NCL matrix",
));
};
if *input_length == 0 {
return Err(Error::invalid_argument(
"MaxUnpool1d input length must be nonzero",
));
}
matrix::max_unpool_1d(
input,
indices,
self.kernel_size,
self.stride,
self.padding,
self.implied_output_length(*input_length),
)
}
pub fn forward_with_output(
&self,
input: &Matrix,
indices: &Matrix,
output_length: usize,
) -> Result<Matrix> {
matrix::max_unpool_1d(
input,
indices,
self.kernel_size,
self.stride,
self.padding,
output_length,
)
}
}
impl Module for MaxUnpool1d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
let _ = input;
Err(Error::invalid_argument(
"MaxUnpool1d requires pooled input plus recorded argmax indices; call MaxUnpool1d::forward(input, indices)",
))
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct MaxUnpool2d {
kernel_size: usize,
stride: usize,
padding: usize,
registry: ModuleRegistry,
}
impl MaxUnpool2d {
pub fn new(kernel_size: usize) -> Result<Self> {
Self::new_with_options(kernel_size, kernel_size, 0)
}
pub fn new_with_options(kernel_size: usize, stride: usize, padding: usize) -> Result<Self> {
if kernel_size == 0 || stride == 0 {
return Err(Error::invalid_argument(
"MaxUnpool2d kernel size and stride must be nonzero",
));
}
Ok(Self {
kernel_size,
stride,
padding,
registry: ModuleRegistry::new(),
})
}
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
}
fn implied_output_extent(&self, input_extent: usize) -> usize {
(input_extent - 1) * self.stride - 2 * self.padding + self.kernel_size
}
pub fn forward(&self, input: &Matrix, indices: &Matrix) -> Result<Matrix> {
let [_, _, input_height, input_width] = input.shape() else {
return Err(Error::invalid_argument(
"MaxUnpool2d input must be a rank-four NCHW matrix",
));
};
if *input_height == 0 || *input_width == 0 {
return Err(Error::invalid_argument(
"MaxUnpool2d input spatial extents must be nonzero",
));
}
matrix::max_unpool_2d(
input,
indices,
self.kernel_size,
self.stride,
self.padding,
self.implied_output_extent(*input_height),
self.implied_output_extent(*input_width),
)
}
pub fn forward_with_output(
&self,
input: &Matrix,
indices: &Matrix,
output_height: usize,
output_width: usize,
) -> Result<Matrix> {
matrix::max_unpool_2d(
input,
indices,
self.kernel_size,
self.stride,
self.padding,
output_height,
output_width,
)
}
}
impl Module for MaxUnpool2d {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
let _ = input;
Err(Error::invalid_argument(
"MaxUnpool2d requires pooled input plus recorded argmax indices; call MaxUnpool2d::forward(input, indices)",
))
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}