use crate::module::{Module, ModuleError};
use crate::pool::checked_out_dim;
use coeus_autograd::Var;
use coeus_core::{Float, MoiraiBackend, Scalar};
use coeus_tensor::Tensor;
use std::marker::PhantomData;
#[derive(Clone)]
pub struct MaxPool1d<T: Scalar, B: coeus_ops::BackendOps<T> + Default = MoiraiBackend> {
kernel_size: usize,
stride: usize,
padding: usize,
dilation: usize,
_marker: PhantomData<(T, B)>,
}
impl<T: Scalar, B: coeus_ops::BackendOps<T> + Default> MaxPool1d<T, B> {
pub fn new(kernel_size: usize) -> Self {
Self::with_params(kernel_size, kernel_size, 0, 1)
}
pub fn with_params(kernel_size: usize, stride: usize, padding: usize, dilation: usize) -> Self {
Self {
kernel_size,
stride,
padding,
dilation,
_marker: PhantomData,
}
}
}
impl<T: Float, B: coeus_ops::BackendOps<T> + Default> Module<T, B> for MaxPool1d<T, B> {
fn parameters(&self) -> Vec<Var<T, B>> {
vec![]
}
fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
let backend = B::default();
let shape = input.tensor.shape();
if shape.len() != 3 {
return Err(ModuleError::InvalidRank {
module: "MaxPool1d",
expected: "3",
actual: shape.len(),
});
}
let [n, c, l] = [shape[0], shape[1], shape[2]];
let l_out = checked_out_dim(
"MaxPool1d",
l,
self.kernel_size,
self.padding,
self.stride,
self.dilation,
)?;
let mut out_tensor = unsafe { Tensor::alloc_on([n, c, l_out], &backend) }?;
let (out_storage, out_layout) = out_tensor.storage_mut_and_layout()?;
backend
.max_pool1d(
input.tensor.storage(),
input.tensor.layout(),
self.kernel_size,
self.stride,
self.padding,
self.dilation,
out_storage,
out_layout,
)
.map_err(|source| ModuleError::Backend {
module: "MaxPool1d",
source,
})?;
Ok(coeus_autograd::max_pool1d(
input,
out_tensor,
self.kernel_size,
self.stride,
self.padding,
self.dilation,
)?)
}
}
#[derive(Clone)]
pub struct AvgPool1d<T: Scalar, B: coeus_ops::BackendOps<T> + Default = MoiraiBackend> {
kernel_size: usize,
stride: usize,
padding: usize,
dilation: usize,
_marker: PhantomData<(T, B)>,
}
impl<T: Scalar, B: coeus_ops::BackendOps<T> + Default> AvgPool1d<T, B> {
pub fn new(kernel_size: usize) -> Self {
Self::with_params(kernel_size, kernel_size, 0, 1)
}
pub fn with_params(kernel_size: usize, stride: usize, padding: usize, dilation: usize) -> Self {
Self {
kernel_size,
stride,
padding,
dilation,
_marker: PhantomData,
}
}
}
impl<T: Float, B: coeus_ops::BackendOps<T> + Default> Module<T, B> for AvgPool1d<T, B> {
fn parameters(&self) -> Vec<Var<T, B>> {
vec![]
}
fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
let backend = B::default();
let shape = input.tensor.shape();
if shape.len() != 3 {
return Err(ModuleError::InvalidRank {
module: "AvgPool1d",
expected: "3",
actual: shape.len(),
});
}
let [n, c, l] = [shape[0], shape[1], shape[2]];
let l_out = checked_out_dim(
"AvgPool1d",
l,
self.kernel_size,
self.padding,
self.stride,
self.dilation,
)?;
let mut out_tensor = unsafe { Tensor::alloc_on([n, c, l_out], &backend) }?;
let (out_storage, out_layout) = out_tensor.storage_mut_and_layout()?;
backend
.avg_pool1d(
input.tensor.storage(),
input.tensor.layout(),
self.kernel_size,
self.stride,
self.padding,
self.dilation,
out_storage,
out_layout,
)
.map_err(|source| ModuleError::Backend {
module: "AvgPool1d",
source,
})?;
Ok(coeus_autograd::avg_pool1d(
input,
out_tensor,
self.kernel_size,
self.stride,
self.padding,
self.dilation,
)?)
}
}