coeus-ops 0.14.0

Numerical tensor operators for Coeus.
Documentation
//! CPU realizations of the backend kernel traits.
#![expect(
    clippy::too_many_arguments,
    reason = "CPU kernels mirror the per-axis geometry of the trait contracts they implement"
)]

use coeus_core::{Backend, BackendError};

mod convolution;
mod cross;
mod elementwise;
pub(crate) mod error;
mod finite_difference;
mod impls;
mod matmul;
mod pool;
mod random_init;
mod reduction;
mod rotate_half;
mod unfold_fold;

/// CPU execution backend marker trait.
///
/// Implemented by [`MoiraiBackend`] and [`SequentialBackend`] from `coeus_core`.
/// The trait is sealed: external crates cannot add new implementations.
///
/// [`MoiraiBackend`]: coeus_core::MoiraiBackend
/// [`SequentialBackend`]: coeus_core::SequentialBackend
///
/// # Examples
///
/// ```
/// use coeus_ops::CpuBackend;
/// use coeus_core::SequentialBackend;
///
/// fn accept_cpu<B: CpuBackend>(_: &B) {}
///
/// let backend = SequentialBackend::new();
/// accept_cpu(&backend);
/// ```
///
/// The parent [`ComputeBackend`](coeus_core::ComputeBackend) trait is sealed,
/// so this trait remains restricted to first-party backends without a second
/// private sealing layer. CPU-addressable emulation backends can therefore use
/// the same canonical operation implementations instead of cloning them.
pub trait CpuBackend: Backend<Error = BackendError> {
    /// Borrow the backend's index buffer (an `i64` word buffer) as a mutable
    /// slice.
    ///
    /// Indices are the one element type whose width Coeus fixes across
    /// backends, so the CPU realization exposes them through a single named
    /// borrow rather than a per-width family of accessors.
    fn as_mut_index_buffer<'a>(
        &self,
        buf: &'a mut Self::DeviceBuffer<i64>,
    ) -> Result<&'a mut [i64], BackendError>;
}

impl CpuBackend for coeus_core::SequentialBackend {
    #[inline]
    fn as_mut_index_buffer<'a>(
        &self,
        buf: &'a mut Self::DeviceBuffer<i64>,
    ) -> Result<&'a mut [i64], BackendError> {
        use coeus_core::CpuAddressableStorageMut;
        buf.as_mut_slice()
    }
}

impl CpuBackend for coeus_core::MoiraiBackend {
    #[inline]
    fn as_mut_index_buffer<'a>(
        &self,
        buf: &'a mut Self::DeviceBuffer<i64>,
    ) -> Result<&'a mut [i64], BackendError> {
        use coeus_core::CpuAddressableStorageMut;
        buf.as_mut_slice()
    }
}