use crate::module::{Module, ModuleError};
use coeus_autograd::Var;
use coeus_core::{CpuAddressableStorage, CpuAddressableStorageMut, Float, MoiraiBackend, Scalar};
use coeus_tensor::Tensor;
use std::marker::PhantomData;
fn avg_pool_matrix_t<T: Float + coeus_core::FloatElement, B: coeus_ops::BackendOps<T> + Default>(
in_len: usize,
out_len: usize,
backend: &B,
) -> Result<Var<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
let mut pt = vec![T::ZERO; in_len * out_len];
for o in 0..out_len {
let start = o * in_len / out_len;
let end = ((o + 1) * in_len).div_ceil(out_len);
let inv = <T as coeus_core::FloatElement>::from_count_reciprocal(end - start);
for slot in pt
.iter_mut()
.skip(start * out_len + o)
.step_by(out_len)
.take(end - start)
{
*slot = inv;
}
}
Var::new(
Tensor::from_slice_on([in_len, out_len], &pt, backend)?,
false,
)
}
fn masked_adaptive_max<
T: Float + coeus_core::FloatElement + coeus_cpu::RealScalar,
B: coeus_ops::BackendOps<T> + Default,
>(
x2: &Var<T, B>,
rows: usize,
in_len: usize,
out_len: usize,
backend: &B,
) -> Result<Var<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
let mut outside = vec![T::ONE; out_len * in_len];
for o in 0..out_len {
let start = o * in_len / out_len;
let end = ((o + 1) * in_len).div_ceil(out_len);
outside[o * in_len + start..o * in_len + end].fill(T::ZERO);
}
let outside_var = Var::new(
Tensor::from_slice_on([1, out_len, in_len], &outside, backend)?,
false,
)?;
let xb = coeus_autograd::broadcast_to(
&coeus_autograd::reshape(x2, [rows, 1, in_len])?,
vec![rows, out_len, in_len],
)?;
let ob = coeus_autograd::broadcast_to(&outside_var, vec![rows, out_len, in_len])?;
let masked = coeus_autograd::masked_fill(
&xb,
&ob,
<T as coeus_core::FloatElement>::from_f64(f64::NEG_INFINITY),
)?;
coeus_autograd::max_axis(&masked, 2)
}
#[derive(Clone, Debug)]
pub struct AdaptiveAvgPool1d<T: Scalar, B: coeus_ops::BackendOps<T> + Default = MoiraiBackend> {
output_size: usize,
_marker: PhantomData<(T, B)>,
}
impl<T: coeus_core::FloatElement + Scalar, B: coeus_ops::BackendOps<T> + Default>
AdaptiveAvgPool1d<T, B>
{
pub const fn new(output_size: usize) -> Self {
Self {
output_size,
_marker: PhantomData,
}
}
}
impl<
T: coeus_core::FloatElement + Float + coeus_cpu::RealScalar,
B: coeus_ops::BackendOps<T> + Default,
> Module<T, B> for AdaptiveAvgPool1d<T, B>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
fn parameters(&self) -> Vec<Var<T, B>> {
vec![]
}
fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
let shape = input.tensor.shape_cloned();
if shape.len() != 3 {
return Err(ModuleError::InvalidRank {
module: "AdaptiveAvgPool1d",
expected: "3",
actual: shape.len(),
});
}
let (n, c, l) = (shape[0], shape[1], shape[2]);
let o = self.output_size;
if l == 0 || o == 0 {
return Err(ModuleError::ShapeMismatch {
module: "AdaptiveAvgPool1d",
parameter: "spatial input and output dimensions",
expected: vec![1, 1],
actual: vec![l, o],
});
}
let backend = B::default();
let p_t = avg_pool_matrix_t::<T, B>(l, o, &backend)?;
let x2 = coeus_autograd::reshape(input, [n * c, l])?;
let out2 = coeus_autograd::matmul(&x2, &p_t)?;
Ok(coeus_autograd::reshape(&out2, [n, c, o])?)
}
}
#[derive(Clone, Debug)]
pub struct AdaptiveAvgPool2d<T: Scalar, B: coeus_ops::BackendOps<T> + Default = MoiraiBackend> {
out_h: usize,
out_w: usize,
_marker: PhantomData<(T, B)>,
}
impl<T: coeus_core::FloatElement + Scalar, B: coeus_ops::BackendOps<T> + Default>
AdaptiveAvgPool2d<T, B>
{
pub const fn new(out_h: usize, out_w: usize) -> Self {
Self {
out_h,
out_w,
_marker: PhantomData,
}
}
pub const fn square(size: usize) -> Self {
Self::new(size, size)
}
}
impl<
T: coeus_core::FloatElement + Float + coeus_cpu::RealScalar,
B: coeus_ops::BackendOps<T> + Default,
> Module<T, B> for AdaptiveAvgPool2d<T, B>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
fn parameters(&self) -> Vec<Var<T, B>> {
vec![]
}
fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
let shape = input.tensor.shape_cloned();
if shape.len() != 4 {
return Err(ModuleError::InvalidRank {
module: "AdaptiveAvgPool2d",
expected: "4",
actual: shape.len(),
});
}
let (n, c, h, w) = (shape[0], shape[1], shape[2], shape[3]);
let (oh, ow) = (self.out_h, self.out_w);
if h == 0 || w == 0 || oh == 0 || ow == 0 {
return Err(ModuleError::ShapeMismatch {
module: "AdaptiveAvgPool2d",
parameter: "spatial input and output dimensions",
expected: vec![1; 4],
actual: vec![h, w, oh, ow],
});
}
let backend = B::default();
if oh == 1 && ow == 1 {
let after_h = coeus_autograd::mean_axis(input, 2)?; return Ok(coeus_autograd::mean_axis(&after_h, 3)?); }
let pw_t = avg_pool_matrix_t::<T, B>(w, ow, &backend)?;
let yw = coeus_autograd::matmul(&coeus_autograd::reshape(input, [n * c * h, w])?, &pw_t)?;
let yw = coeus_autograd::reshape(&yw, [n, c, h, ow])?;
let ph_t = avg_pool_matrix_t::<T, B>(h, oh, &backend)?;
let yw_p = coeus_autograd::permute(&yw, &[0, 1, 3, 2])?;
let yh = coeus_autograd::matmul(&coeus_autograd::reshape(&yw_p, [n * c * ow, h])?, &ph_t)?;
let yh = coeus_autograd::reshape(&yh, [n, c, ow, oh])?;
let out = coeus_autograd::permute(&yh, &[0, 1, 3, 2])?;
Ok(coeus_autograd::reshape(&out, [n, c, oh, ow])?)
}
}
#[derive(Clone, Debug)]
pub struct AdaptiveMaxPool1d<T: Scalar, B: coeus_ops::BackendOps<T> + Default = MoiraiBackend> {
output_size: usize,
_marker: PhantomData<(T, B)>,
}
impl<T: coeus_core::FloatElement + Scalar, B: coeus_ops::BackendOps<T> + Default>
AdaptiveMaxPool1d<T, B>
{
pub const fn new(output_size: usize) -> Self {
Self {
output_size,
_marker: PhantomData,
}
}
}
impl<
T: coeus_core::FloatElement + Float + coeus_cpu::RealScalar,
B: coeus_ops::BackendOps<T> + Default,
> Module<T, B> for AdaptiveMaxPool1d<T, B>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
fn parameters(&self) -> Vec<Var<T, B>> {
vec![]
}
fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
let shape = input.tensor.shape_cloned();
if shape.len() != 3 {
return Err(ModuleError::InvalidRank {
module: "AdaptiveMaxPool1d",
expected: "3",
actual: shape.len(),
});
}
let (n, c, l) = (shape[0], shape[1], shape[2]);
let o = self.output_size;
if l == 0 || o == 0 {
return Err(ModuleError::ShapeMismatch {
module: "AdaptiveMaxPool1d",
parameter: "spatial input and output dimensions",
expected: vec![1, 1],
actual: vec![l, o],
});
}
let backend = B::default();
let mut out_tensor = unsafe { Tensor::alloc_on([n, c, o], &backend) }?;
let (out_storage, out_layout) = out_tensor.storage_mut_and_layout()?;
let out_buf = out_storage.as_mut_slice()?;
let (out_off, out_st) = (out_layout.offset(), out_layout.strides());
let in_buf = input.tensor.storage().as_slice();
let in_layout = input.tensor.layout();
let (in_off, in_st) = (in_layout.offset(), in_layout.strides());
for n_i in 0..n {
for c_i in 0..c {
let in_base = in_off + n_i * in_st[0] + c_i * in_st[1];
let out_base = out_off + n_i * out_st[0] + c_i * out_st[1];
for o_i in 0..o {
let (start, end) = coeus_autograd::ops::nn::pool::adaptive_region(o_i, l, o);
let mut best = in_buf[in_base + start * in_st[2]];
for i in (start + 1)..end {
let v = in_buf[in_base + i * in_st[2]];
if v > best {
best = v;
}
}
out_buf[out_base + o_i * out_st[2]] = best;
}
}
}
Ok(coeus_autograd::adaptive_max_pool1d(
input, out_tensor, l, o,
)?)
}
}
#[derive(Clone, Debug)]
pub struct AdaptiveMaxPool2d<T: Scalar, B: coeus_ops::BackendOps<T> + Default = MoiraiBackend> {
out_h: usize,
out_w: usize,
_marker: PhantomData<(T, B)>,
}
impl<T: coeus_core::FloatElement + Scalar, B: coeus_ops::BackendOps<T> + Default>
AdaptiveMaxPool2d<T, B>
{
pub const fn new(out_h: usize, out_w: usize) -> Self {
Self {
out_h,
out_w,
_marker: PhantomData,
}
}
pub const fn square(size: usize) -> Self {
Self::new(size, size)
}
}
impl<
T: coeus_core::FloatElement + Float + coeus_cpu::RealScalar,
B: coeus_ops::BackendOps<T> + Default,
> Module<T, B> for AdaptiveMaxPool2d<T, B>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
fn parameters(&self) -> Vec<Var<T, B>> {
vec![]
}
fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
let shape = input.tensor.shape_cloned();
if shape.len() != 4 {
return Err(ModuleError::InvalidRank {
module: "AdaptiveMaxPool2d",
expected: "4",
actual: shape.len(),
});
}
let (n, c, h, w) = (shape[0], shape[1], shape[2], shape[3]);
let (oh, ow) = (self.out_h, self.out_w);
if h == 0 || w == 0 || oh == 0 || ow == 0 {
return Err(ModuleError::ShapeMismatch {
module: "AdaptiveMaxPool2d",
parameter: "spatial input and output dimensions",
expected: vec![1; 4],
actual: vec![h, w, oh, ow],
});
}
let backend = B::default();
if oh == 1 && ow == 1 {
let after_h = coeus_autograd::max_axis(input, 2)?; return Ok(coeus_autograd::max_axis(&after_h, 3)?); }
let xw = coeus_autograd::reshape(input, [n * c * h, w])?;
let pw = masked_adaptive_max::<T, B>(&xw, n * c * h, w, ow, &backend)?;
let yw = coeus_autograd::reshape(&pw, [n, c, h, ow])?;
let yw_p = coeus_autograd::permute(&yw, &[0, 1, 3, 2])?;
let ph = masked_adaptive_max::<T, B>(
&coeus_autograd::reshape(&yw_p, [n * c * ow, h])?,
n * c * ow,
h,
oh,
&backend,
)?;
let yh = coeus_autograd::reshape(&ph, [n, c, ow, oh])?;
let out = coeus_autograd::permute(&yh, &[0, 1, 3, 2])?;
Ok(coeus_autograd::reshape(&out, [n, c, oh, ow])?)
}
}