coeus-ops 0.11.0

Numerical tensor operators for Coeus.
Documentation
//! Reduction CPU kernel delegations: reduce, argmax/argmin, topk, cumulative
//! sum/product scans.

use coeus_core::{BackendError, CpuAddressableStorage, CpuAddressableStorageMut, Layout, Scalar};

use super::error::map_leto_error;
use super::CpuBackend;
use crate::backend_ops::ReductionOp;

#[inline]
pub(super) fn reduce<T, B>(
    _backend: &B,
    op: ReductionOp,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::reduce_into(op, a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("reduction", error))
}

/// Mean along `axis`; needs a `FloatElement` for the reciprocal.
pub(super) fn reduce_mean<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar + coeus_core::FloatElement,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::reduce_mean_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("reduction", error))
}

/// Product along `axis`; needs a `FloatElement` for the multiplication.
pub(super) fn reduce_prod<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar + coeus_core::FloatElement,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::reduce_prod_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("reduction", error))
}

#[inline]
pub(super) fn argmax<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<i64>,
    c_layout: &Layout,
) where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
    B::DeviceBuffer<i64>: CpuAddressableStorageMut<i64>,
{
    coeus_leto::argmax_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .expect("coeus-leto argmax failed");
}

#[inline]
pub(super) fn argmin<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<i64>,
    c_layout: &Layout,
) where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
    B::DeviceBuffer<i64>: CpuAddressableStorageMut<i64>,
{
    coeus_leto::argmin_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .expect("coeus-leto argmin failed");
}

#[inline]
pub(super) fn topk<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    k: usize,
    axis: usize,
    largest: bool,
    values: &mut B::DeviceBuffer<T>,
    _values_layout: &Layout,
    indices: &mut B::DeviceBuffer<i64>,
    _indices_layout: &Layout,
) where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
    B::DeviceBuffer<i64>: CpuAddressableStorageMut<i64>,
{
    crate::reduction::topk::topk_impl(
        a.as_slice(),
        a_layout.shape(),
        k,
        axis,
        largest,
        values.as_mut_slice(),
        indices.as_mut_slice(),
    );
}

#[inline]
pub(super) fn cumsum<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::cumsum_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("cumsum", error))
}

#[inline]
pub(super) fn suffix_sum<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::suffix_sum_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("suffix_sum", error))
}

#[inline]
pub(super) fn cumprod<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::cumprod_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("cumprod", error))
}

#[inline]
pub(super) fn suffix_prod<T, B>(
    _backend: &B,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    axis: usize,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), BackendError>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::suffix_prod_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("suffix_prod", error))
}