Skip to main content

TensorViewCanonicalization

Trait TensorViewCanonicalization 

Source
pub trait TensorViewCanonicalization<T: TensorScalar, R: TensorRank> {
    // Required methods
    fn to_contiguous(
        &mut self,
        view: &TypedTensorView<'_, T, R>,
    ) -> Result<TypedTensor<T, R>>;
    fn copy_into(
        &mut self,
        src: &TypedTensorView<'_, T, R>,
        dst: &mut TypedTensorViewMut<'_, T, R>,
    ) -> Result<()>;
}
Expand description

Backend-owned canonicalization for typed tensor views.

Implementations must preserve the input placement family. CPU backends canonicalize host views through explicit host copies and reject backend buffers with a diagnostic that asks the caller to download first. GPU backends canonicalize GPU-resident views on the same device and reject host buffers with an upload hint.

TensorViewCanonicalization::copy_into requires source and destination shapes, scalar dtypes, and placement families to match. The destination view must be internally non-overlapping, and source and destination backing allocations must not alias unless an implementation explicitly documents and supports that case. Implementations may reject layouts their native kernels cannot consume.

CUDA currently accepts only a compact column-major source view with offset zero that covers its full allocation; arbitrary-stride destinations remain supported. Canonicalization and copying are same-placement operations: they must not perform hidden host/device transfers or silently materialize an unsupported source layout.

This trait is intentionally separate from BackendSession so generic typed methods do not change the object-safety contract of dyn BackendSession.

§Examples

use tenferro_tensor::{DynRank, TensorViewCanonicalization, TypedTensor};

fn compact_i32<B: TensorViewCanonicalization<i32, DynRank>>(
    backend: &mut B,
    tensor: &TypedTensor<i32>,
) -> tenferro_tensor::Result<TypedTensor<i32>> {
    backend.to_contiguous(&tensor.as_view())
}

fn copy_i32<B: TensorViewCanonicalization<i32, DynRank>>(
    backend: &mut B,
    src: &TypedTensor<i32>,
    dst: &mut TypedTensor<i32>,
) -> tenferro_tensor::Result<()> {
    backend.copy_into(&src.as_view(), &mut dst.as_view_mut())
}

Required Methods§

Source

fn to_contiguous( &mut self, view: &TypedTensorView<'_, T, R>, ) -> Result<TypedTensor<T, R>>

§Errors

Returns crate::Error::Validation with a typed ValidationError source for invalid shapes, ranks, axes, dtypes, or output metadata. It returns crate::Error::BackendFailure or crate::Error::BackendSource when backend execution or storage access cannot provide the requested result.

Source

fn copy_into( &mut self, src: &TypedTensorView<'_, T, R>, dst: &mut TypedTensorViewMut<'_, T, R>, ) -> Result<()>

§Errors

Returns crate::Error::Validation with a typed ValidationError source for invalid shapes, ranks, axes, dtypes, or output metadata. It returns crate::Error::BackendFailure or crate::Error::BackendSource when backend execution or storage access cannot provide the requested result.

Implementors§