use crate::backend_ops::BackendOps;
use coeus_core::{Backend, CpuAddressableStorage, CpuAddressableStorageMut, Float, FloatElement};
use coeus_tensor::Tensor;
#[inline]
fn region_start(i: usize, in_size: usize, out_size: usize) -> usize {
(i * in_size) / out_size
}
#[inline]
fn region_end(i: usize, in_size: usize, out_size: usize) -> usize {
((i + 1) * in_size).div_ceil(out_size)
}
pub fn adaptive_avg_pool1d<T: Float + FloatElement, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
output_size: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert_eq!(input.ndim(), 3, "adaptive_avg_pool1d expects [N, C, L]");
let n = input.shape()[0];
let c = input.shape()[1];
let l = input.shape()[2];
let mut out = unsafe { Tensor::alloc_on([n, c, output_size], backend) }?;
for ni in 0..n {
for ci in 0..c {
for oi in 0..output_size {
let start = region_start(oi, l, output_size);
let end = region_end(oi, l, output_size);
let count = T::from_count(end - start);
let mut acc = T::ZERO;
for li in start..end {
acc += input.get(&[ni, ci, li]);
}
out.set(&[ni, ci, oi], acc / count)?;
}
}
}
Ok(out)
}
pub fn adaptive_max_pool1d<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
output_size: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert_eq!(input.ndim(), 3, "adaptive_max_pool1d expects [N, C, L]");
let n = input.shape()[0];
let c = input.shape()[1];
let l = input.shape()[2];
let mut out = unsafe { Tensor::alloc_on([n, c, output_size], backend) }?;
for ni in 0..n {
for ci in 0..c {
for oi in 0..output_size {
let start = region_start(oi, l, output_size);
let end = region_end(oi, l, output_size);
let mut max_val: Option<T> = None;
for li in start..end {
let v = input.get(&[ni, ci, li]);
max_val = Some(max_val.map_or(v, |m| if v > m { v } else { m }));
}
out.set(&[ni, ci, oi], max_val.unwrap_or(T::ZERO))?;
}
}
}
Ok(out)
}
pub fn adaptive_avg_pool2d<T: Float + FloatElement, B: BackendOps<T> + Default + Backend>(
input: &Tensor<T, B>,
out_h: usize,
out_w: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert_eq!(input.ndim(), 4, "adaptive_avg_pool2d expects [N, C, H, W]");
let n = input.shape()[0];
let c = input.shape()[1];
let h = input.shape()[2];
let w = input.shape()[3];
let inp_cont;
let inp = if input.is_contiguous() && input.layout().offset() == 0 {
input
} else {
inp_cont = input.to_contiguous_on(backend)?;
&inp_cont
};
use crate::ptr::{MutPtr, Ptr};
let mut out = unsafe { Tensor::alloc_on([n, c, out_h, out_w], backend) }?;
let inp_ptr = Ptr(inp.storage().as_slice().as_ptr());
let out_ptr = MutPtr(out.storage_mut()?.as_mut_slice()?.as_mut_ptr());
let nc = n * c;
backend.parallel_for(0, nc, move |idx| {
let ni = idx / c;
let ci = idx % c;
let inp_nc = ni * c * h * w + ci * h * w;
let out_nc = ni * c * out_h * out_w + ci * out_h * out_w;
for oh in 0..out_h {
let hs = region_start(oh, h, out_h);
let he = region_end(oh, h, out_h);
for ow in 0..out_w {
let ws = region_start(ow, w, out_w);
let we = region_end(ow, w, out_w);
let count = T::from_count((he - hs) * (we - ws));
let mut acc = T::ZERO;
for hi in hs..he {
for wi in ws..we {
acc += unsafe { inp_ptr.read(inp_nc + hi * w + wi) };
}
}
unsafe { out_ptr.write(out_nc + oh * out_w + ow, acc / count) };
}
}
});
Ok(out)
}
pub fn adaptive_max_pool2d<T: Float, B: BackendOps<T> + Default + Backend>(
input: &Tensor<T, B>,
out_h: usize,
out_w: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert_eq!(input.ndim(), 4, "adaptive_max_pool2d expects [N, C, H, W]");
let n = input.shape()[0];
let c = input.shape()[1];
let h = input.shape()[2];
let w = input.shape()[3];
let inp_cont;
let inp = if input.is_contiguous() && input.layout().offset() == 0 {
input
} else {
inp_cont = input.to_contiguous_on(backend)?;
&inp_cont
};
use crate::ptr::{MutPtr, Ptr};
let mut out = unsafe { Tensor::alloc_on([n, c, out_h, out_w], backend) }?;
let inp_ptr = Ptr(inp.storage().as_slice().as_ptr());
let out_ptr = MutPtr(out.storage_mut()?.as_mut_slice()?.as_mut_ptr());
let nc = n * c;
backend.parallel_for(0, nc, move |idx| {
let ni = idx / c;
let ci = idx % c;
let inp_nc = ni * c * h * w + ci * h * w;
let out_nc = ni * c * out_h * out_w + ci * out_h * out_w;
for oh in 0..out_h {
let hs = region_start(oh, h, out_h);
let he = region_end(oh, h, out_h);
for ow in 0..out_w {
let ws = region_start(ow, w, out_w);
let we = region_end(ow, w, out_w);
let mut max_val: Option<T> = None;
for hi in hs..he {
for wi in ws..we {
let v = unsafe { inp_ptr.read(inp_nc + hi * w + wi) };
max_val = Some(max_val.map_or(v, |m| if v > m { v } else { m }));
}
}
unsafe { out_ptr.write(out_nc + oh * out_w + ow, max_val.unwrap_or(T::ZERO)) };
}
}
});
Ok(out)
}