tenferro-cpu 0.12.0

CPU backend, kernels, provider selection, and CPU resource pools for tenferro.
Documentation
//! Borrowed operand layout shared by the CPU tensor kernels.
//!
//! A kernel reads an operand as `(data, shape, strides, offset)` rather than as
//! a compact owned tensor, so an owned tensor and a borrowed view reach the same
//! kernel without a staging copy (#2214). Owned tensors are compact, so their
//! descriptor is their buffer with column-major strides and offset zero.

use tenferro_tensor::TypedTensorView;

/// Borrowed operand: element buffer plus the layout its strides describe.
pub(crate) struct Operand<'a, T> {
    /// Backing storage; `offset` and `strides` index into it.
    pub(crate) data: &'a [T],
    /// Logical dimensions.
    pub(crate) shape: &'a [usize],
    /// Element strides, in `data` element units.
    pub(crate) strides: &'a [isize],
    /// Element offset of the first logical element.
    pub(crate) offset: isize,
}

/// An empty descriptor, so short operand lists can live in a `tinyvec::TinyVec`
/// (whose inline array requires `Default`); it is never read as an operand.
impl<T> Default for Operand<'_, T> {
    fn default() -> Self {
        Self {
            data: &[],
            shape: &[],
            strides: &[],
            offset: 0,
        }
    }
}

impl<'a, T: tenferro_tensor::TensorScalar + Copy + 'static> Operand<'a, T> {
    /// Descriptor of a compact owned tensor, borrowing its cached column-major strides.
    pub(crate) fn from_typed(
        op: &'static str,
        tensor: &'a tenferro_tensor::TypedTensor<T>,
    ) -> crate::Result<Self> {
        Ok(Self {
            data: crate::typed_host_data(op, tensor)?,
            shape: tensor.shape(),
            strides: tensor.strides(),
            offset: 0,
        })
    }
}

/// Descriptor of a borrowed view, keeping its strides and offset.
///
/// A view whose root is a shared allocation is admitted through the provider's
/// shared host borrow, the same rule `typed_view_from_view` applies; every other
/// backend view keeps the historical "download to host" refusal.
pub(crate) fn operand_from_view<'a, T, R>(
    view: &'a TypedTensorView<'a, T, R>,
) -> crate::Result<Operand<'a, T>>
where
    T: Copy + 'static,
    R: tenferro_tensor::TensorRank,
{
    let data = match view.shared_backend_storage() {
        Some(buffer) => crate::shared_view_read_slice("operand", buffer)?,
        None => view.host_storage()?,
    };
    Ok(Operand {
        data,
        shape: view.shape(),
        strides: view.strides(),
        offset: view.offset(),
    })
}