Skip to main content

tenferro_tensor/
lib.rs

1//! Core tensor types, views, backend traits, and backend-independent contracts.
2//!
3//! # Owned Tensors And Views
4//!
5//! [`TypedTensor<T>`](TypedTensor) and the dtype-erased [`Tensor`] enum are
6//! owned tensor values. They are the right representation when a result is
7//! materialized as compact column-major storage.
8//!
9//! [`TypedTensorView`] is a borrowed typed view over an existing tensor buffer.
10//! It carries logical shape, arbitrary strides, and an offset, so metadata-only
11//! layout changes such as transposes, slices, and broadcasts can be represented
12//! without copying. Backend-aware code materializes and copies views through
13//! [`TensorViewCanonicalization`], preserving placement and backend execution
14//! policy.
15//!
16//! [`TensorRead`] is the dtype-erased borrowed input type used by eager kernels
17//! and backend dispatch. It can borrow either an owned [`Tensor`] or a
18//! [`TensorView`] with arbitrary strides. Prefer `TensorRead` for read-only
19//! operation inputs so callers are not forced to materialize layout-only views.
20//!
21//! [`TensorOwnedView`] and [`TensorValue`] are the owned lazy-value forms. Use
22//! them when an API must store a view result beyond the lifetime of a borrowed
23//! input, then expose a short-lived `TensorRead` at kernel-dispatch time.
24//!
25//! Use [`Tensor::as_slice`] or [`TypedTensorView::as_slice`] only when compact
26//! contiguous storage is part of the API contract. Use shape/stride-aware kernel
27//! paths or `TensorRead` otherwise.
28//!
29//! # Examples
30//!
31//! ```rust
32//! use tenferro_tensor::{Tensor, TypedTensor};
33//!
34//! let a = Tensor::F64(TypedTensor::from_vec_col_major(vec![2], vec![1.0, 2.0]).unwrap());
35//! assert_eq!(a.shape(), &[2]);
36//! ```
37
38/// Lightweight backend-independent host tensor data model.
39///
40/// Execution-capable tensors and backends in this crate remain separate from
41/// the host-only core model during the crate-boundary split.
42pub mod core {
43    pub use tenferro_tensor_core::{
44        col_major_strides, DType, DynRank, ErrorKind, HostTensor, HostTensorView, IntoShapeVec,
45        Rank, Result, ShapeMismatch, ShapeVec, SliceSpec, StrideVec, Tensor, TensorLayout,
46        TensorRank, TensorRef, TensorScalar, TensorView, ValidationError, ValidationKind,
47    };
48}
49
50pub use tenferro_tensor_core::{
51    ErrorKind, IntoShapeVec, ShapeMismatch, ShapeVec, SliceSpec, StrideVec, TensorRef,
52    ValidationError, ValidationKind,
53};
54
55pub mod backend;
56pub mod cache;
57pub mod capability;
58pub mod config;
59pub mod dispatch;
60pub mod error;
61pub mod types;
62pub mod validate;
63
64pub use backend::{
65    default_backend_session, BackendCachedDot, BackendRuntimeCache, BackendSession,
66    BackendSessionHost, ContractionScalar, DotGeneralAccumulation, ElementwiseReadOp,
67    SessionCachedDot, TensorAnalytic, TensorBackend, TensorBackendOps, TensorBuffer,
68    TensorDeviceTransfer, TensorDot, TensorElementwise, TensorFusion, TensorIndexing,
69    TensorReduction, TensorStructural, TensorViewCanonicalization,
70};
71pub use cache::{CacheStats, RuntimeCacheControl};
72pub use capability::{
73    capability_output_dtype, BackendId, CapabilityAxis, CapabilityQuery, OperationCapability,
74    SupportLevel, TensorBackendCapability,
75};
76pub use config::{
77    CompareDir, DotGeneralConfig, GatherConfig, PadConfig, ScatterConfig, SliceConfig,
78};
79pub use error::{BoxError, Error, Result};
80pub use types::{
81    col_major_strides, AllocationDomainId, AllocationId, BackendBuffer, Buffer, BufferHandle,
82    CpuDomainId, DType, DeviceId, DeviceKind, DynRank, GpuBackendKind, HostAccessError,
83    HostReadGuard, HostWriteGuard, MemoryKind, Placement, Rank, SharedTensorAllocationDomain,
84    StridedSliceSpec, Tensor, TensorBufferRef, TensorBufferRefMut, TensorLayout, TensorOwnedView,
85    TensorRank, TensorRead, TensorScalar, TensorValue, TensorView, TensorViewMut, TensorWrite,
86    TypedTensor, TypedTensorView, TypedTensorViewMut, TypedTensorViewMutPair, TypedTensorWrite,
87};
88
89pub(crate) fn core_dtype(dtype: DType) -> tenferro_tensor_core::DType {
90    match dtype {
91        DType::F32 => tenferro_tensor_core::DType::F32,
92        DType::F64 => tenferro_tensor_core::DType::F64,
93        DType::I32 => tenferro_tensor_core::DType::I32,
94        DType::I64 => tenferro_tensor_core::DType::I64,
95        DType::Bool => tenferro_tensor_core::DType::Bool,
96        DType::C32 => tenferro_tensor_core::DType::C32,
97        DType::C64 => tenferro_tensor_core::DType::C64,
98    }
99}
100
101#[cfg(test)]
102mod tests;