Skip to main content

CpuBackend

Struct CpuBackend 

Source
pub struct CpuBackend { /* private fields */ }
Expand description

CPU execution backend.

§Examples

use tenferro_cpu::CpuBackend;

let backend = CpuBackend::new();

Implementations§

Source§

impl CpuBackend

Source

pub fn new() -> Self

Create a CPU backend using the environment-driven CPU context.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::new();
Source

pub fn with_kind(kind: CpuBackendKind) -> Result<Self>

Create a CPU backend using the selected compiled provider.

§Examples
use tenferro_cpu::{CpuBackend, CpuBackendKind};

let backend = CpuBackend::with_kind(CpuBackendKind::default_compiled()).unwrap();
assert_eq!(backend.kind(), CpuBackendKind::default_compiled());
Source

pub fn try_new() -> Result<Self>

Try to create a CPU backend using RAYON_NUM_THREADS.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::try_new()
    .unwrap_or_else(|_| CpuBackend::with_threads(1).unwrap());
let _ = backend.num_threads();
Source

pub fn from_context(ctx: Arc<CpuContext>) -> Self

Create a CPU backend from an existing context.

§Examples
use std::sync::Arc;
use tenferro_cpu::{CpuBackend, CpuContext};

let ctx = Arc::new(CpuContext::with_threads(2).unwrap());
let backend = CpuBackend::from_context(ctx);
assert_eq!(backend.num_threads(), 2);
Source

pub fn from_context_with_buffer_pool_limit( ctx: Arc<CpuContext>, max_retained_capacity_bytes: usize, ) -> Self

Create a CPU backend from an existing context and buffer-pool retention cap.

The cap is measured in retained vector capacity bytes. A cap of zero disables buffer retention.

§Examples
use std::sync::Arc;
use tenferro_cpu::{CpuBackend, CpuContext};

let ctx = Arc::new(CpuContext::with_threads(1).unwrap());
let backend = CpuBackend::from_context_with_buffer_pool_limit(ctx, 0);
assert_eq!(backend.buffer_pool_limit_bytes(), 0);
Source

pub fn with_threads(num_threads: usize) -> Result<Self>

Create a CPU backend with a custom thread count.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::with_threads(2).unwrap();
assert_eq!(backend.num_threads(), 2);
§Errors

Returns an error when num_threads is zero or Rayon rejects the pool.

Source

pub fn with_threads_and_kind( num_threads: usize, kind: CpuBackendKind, ) -> Result<Self>

Create a CPU backend with a custom thread count and provider.

§Examples
use tenferro_cpu::{CpuBackend, CpuBackendKind};

let backend = CpuBackend::with_threads_and_kind(
    1,
    CpuBackendKind::default_compiled(),
)?;
assert_eq!(backend.num_threads(), 1);
§Errors

Returns an error when num_threads is zero, Rayon rejects the pool, or the selected provider is unavailable.

Source

pub fn kind(&self) -> CpuBackendKind

Return the runtime CPU provider selected by this backend.

§Examples
use tenferro_cpu::{CpuBackend, CpuBackendKind};

let backend = CpuBackend::new();
assert_eq!(backend.kind(), CpuBackendKind::default_compiled());
Source

pub fn num_threads(&self) -> usize

Return the number of threads in this backend’s CPU context.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::with_threads(2).unwrap();
assert_eq!(backend.num_threads(), 2);
Source

pub fn buffer_pool_len(&self) -> usize

Number of retained typed host buffers currently held by this backend.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::new();
assert_eq!(backend.buffer_pool_len(), 0);
Source

pub fn buffer_pool_stats(&self) -> BufferPoolStats

Snapshot reusable typed host buffers currently retained by this backend.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::new();
let stats = backend.buffer_pool_stats();
assert_eq!(stats.buffers, 0);
assert_eq!(stats.capacity_bytes, 0);
Source

pub fn buffer_pool_cache_stats(&self) -> CacheStats

Return cache-style stats for the CPU buffer pool.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::new();
let stats = backend.buffer_pool_cache_stats();
assert_eq!(stats.entries, 0);
assert_eq!(stats.retained_bytes, 0);
Source

pub fn buffer_pool_limit_bytes(&self) -> usize

Current CPU buffer-pool retention limit in bytes.

§Examples
use std::sync::Arc;
use tenferro_cpu::{CpuBackend, CpuContext};

let backend = CpuBackend::from_context_with_buffer_pool_limit(
    Arc::new(CpuContext::with_threads(1).unwrap()),
    4096,
);
assert_eq!(backend.buffer_pool_limit_bytes(), 4096);
Source

pub fn set_buffer_pool_limit_bytes( &mut self, max_retained_capacity_bytes: usize, )

Update the CPU buffer-pool retention limit in bytes.

Shrinking the limit evicts retained buffers immediately. A limit of zero disables buffer retention.

§Examples
use tenferro_cpu::CpuBackend;

let mut backend = CpuBackend::new();
backend.set_buffer_pool_limit_bytes(0);
assert_eq!(backend.buffer_pool_limit_bytes(), 0);
assert_eq!(backend.buffer_pool_len(), 0);
Source

pub fn reset_buffer_pool(&mut self)

Reset reusable typed host buffers currently retained by this backend.

This releases pool-owned vectors to the process allocator. Operating system RSS may not fall immediately because allocators can retain freed pages for future allocations.

§Examples
use tenferro_cpu::CpuBackend;

let mut backend = CpuBackend::new();
backend.reset_buffer_pool();
assert_eq!(backend.buffer_pool_len(), 0);
Source

pub fn install<R: Send>(&self, op: impl FnOnce() -> R + Send) -> R

Run a closure in this backend’s CPU execution scope.

§Examples
use tenferro_cpu::CpuBackend;

let backend = CpuBackend::with_threads(1).unwrap();
let value = backend.install(|| 1 + 1);
assert_eq!(value, 2);

Trait Implementations§

Source§

impl BackendSessionHost for CpuBackend

Source§

fn with_backend_session<R: Send>( &mut self, f: impl FnOnce(&mut dyn BackendSession) -> R + Send, ) -> R

Source§

impl Debug for CpuBackend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CpuBackend

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl TensorAnalytic for CpuBackend

Source§

fn exp(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn exp_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn log(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn log_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn sin(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn sin_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn cos(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn cos_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn tanh(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn tanh_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn sqrt(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn sqrt_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn rsqrt(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn rsqrt_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn pow(&mut self, lhs: &Tensor, rhs: &Tensor) -> Result<Tensor>

Source§

fn pow_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, ) -> Result<Tensor>

Source§

fn expm1(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn expm1_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn log1p(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn log1p_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

impl TensorBuffer for CpuBackend

Source§

fn reclaim_buffer(&mut self, tensor: Tensor)

Source§

impl TensorDeviceTransfer for CpuBackend

Source§

fn download_to_host(&mut self, tensor: &Tensor) -> Result<Tensor>

Source§

fn upload_host_tensor(&mut self, tensor: &Tensor) -> Result<Tensor>

Source§

impl TensorDot for CpuBackend

Source§

fn dot_general( &mut self, lhs: &Tensor, rhs: &Tensor, config: &DotGeneralConfig, ) -> Result<Tensor>

Source§

impl TensorElementwise for CpuBackend

Source§

fn add(&mut self, lhs: &Tensor, rhs: &Tensor) -> Result<Tensor>

Source§

fn add_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, ) -> Result<Tensor>

Elementwise addition accepting either owned tensors or borrowed views. Read more
Source§

fn mul(&mut self, lhs: &Tensor, rhs: &Tensor) -> Result<Tensor>

Source§

fn mul_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, ) -> Result<Tensor>

Source§

fn neg(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn neg_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn conj(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn conj_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn div(&mut self, lhs: &Tensor, rhs: &Tensor) -> Result<Tensor>

Source§

fn div_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, ) -> Result<Tensor>

Source§

fn abs(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn abs_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn sign(&mut self, input: &Tensor) -> Result<Tensor>

Source§

fn sign_read(&mut self, input: TensorRead<'_>) -> Result<Tensor>

Source§

fn maximum(&mut self, lhs: &Tensor, rhs: &Tensor) -> Result<Tensor>

Source§

fn maximum_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, ) -> Result<Tensor>

Source§

fn minimum(&mut self, lhs: &Tensor, rhs: &Tensor) -> Result<Tensor>

Source§

fn minimum_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, ) -> Result<Tensor>

Source§

fn compare( &mut self, lhs: &Tensor, rhs: &Tensor, dir: &CompareDir, ) -> Result<Tensor>

Source§

fn compare_read( &mut self, lhs: TensorRead<'_>, rhs: TensorRead<'_>, dir: &CompareDir, ) -> Result<Tensor>

Source§

fn select( &mut self, pred: &Tensor, on_true: &Tensor, on_false: &Tensor, ) -> Result<Tensor>

Source§

fn select_read( &mut self, pred: TensorRead<'_>, on_true: TensorRead<'_>, on_false: TensorRead<'_>, ) -> Result<Tensor>

Source§

fn clamp( &mut self, input: &Tensor, lower: &Tensor, upper: &Tensor, ) -> Result<Tensor>

Source§

fn clamp_read( &mut self, input: TensorRead<'_>, lower: TensorRead<'_>, upper: TensorRead<'_>, ) -> Result<Tensor>

Source§

impl TensorIndexing for CpuBackend

Source§

fn gather( &mut self, operand: &Tensor, start_indices: &Tensor, config: &GatherConfig, ) -> Result<Tensor>

Source§

fn scatter( &mut self, operand: &Tensor, scatter_indices: &Tensor, updates: &Tensor, config: &ScatterConfig, ) -> Result<Tensor>

Source§

fn slice(&mut self, input: &Tensor, config: &SliceConfig) -> Result<Tensor>

Source§

fn dynamic_slice( &mut self, input: &Tensor, starts: &Tensor, slice_sizes: &[usize], ) -> Result<Tensor>

Source§

fn dynamic_update_slice( &mut self, operand: &Tensor, update: &Tensor, starts: &Tensor, ) -> Result<Tensor>

Source§

fn pad(&mut self, input: &Tensor, config: &PadConfig) -> Result<Tensor>

Source§

fn concatenate(&mut self, inputs: &[&Tensor], axis: usize) -> Result<Tensor>

Source§

fn reverse(&mut self, input: &Tensor, axes: &[usize]) -> Result<Tensor>

Source§

impl TensorReduction for CpuBackend

Source§

fn reduce_sum(&mut self, input: &Tensor, axes: &[usize]) -> Result<Tensor>

Source§

fn reduce_sum_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>

Sum elements across axes from an owned tensor or borrowed view. Read more
Source§

fn reduce_prod(&mut self, input: &Tensor, axes: &[usize]) -> Result<Tensor>

Source§

fn reduce_prod_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>

Multiply elements across axes from an owned tensor or borrowed view. Read more
Source§

fn reduce_max(&mut self, input: &Tensor, axes: &[usize]) -> Result<Tensor>

Source§

fn reduce_max_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>

Take maximum values across axes from an owned tensor or borrowed view. Read more
Source§

fn reduce_min(&mut self, input: &Tensor, axes: &[usize]) -> Result<Tensor>

Source§

fn reduce_min_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>

Take minimum values across axes from an owned tensor or borrowed view. Read more
Source§

impl TensorStructural for CpuBackend

Source§

fn transpose(&mut self, input: &Tensor, perm: &[usize]) -> Result<Tensor>

Source§

fn transpose_read( &mut self, input: TensorRead<'_>, perm: &[usize], ) -> Result<Tensor>

Source§

fn reshape(&mut self, input: &Tensor, shape: &[usize]) -> Result<Tensor>

Source§

fn reshape_read( &mut self, input: TensorRead<'_>, shape: &[usize], ) -> Result<Tensor>

Source§

fn broadcast_in_dim( &mut self, input: &Tensor, shape: &[usize], dims: &[usize], ) -> Result<Tensor>

Source§

fn broadcast_in_dim_read( &mut self, input: TensorRead<'_>, shape: &[usize], dims: &[usize], ) -> Result<Tensor>

Source§

fn cast(&mut self, input: &Tensor, to: DType) -> Result<Tensor>

Cast a tensor to another dtype using explicit dtype projection. Read more
Source§

fn extract_diagonal( &mut self, input: &Tensor, axis_a: usize, axis_b: usize, ) -> Result<Tensor>

Source§

fn embed_diagonal( &mut self, input: &Tensor, axis_a: usize, axis_b: usize, ) -> Result<Tensor>

Source§

fn tril(&mut self, input: &Tensor, k: i64) -> Result<Tensor>

Source§

fn triu(&mut self, input: &Tensor, k: i64) -> Result<Tensor>

Source§

fn convert(&mut self, input: &Tensor, to: DType) -> Result<Tensor, Error>

Convert a tensor to another dtype using checked dtype conversion. Read more
Source§

impl<T, R> TensorViewCanonicalization<T, R> for CpuBackend
where T: Clone + 'static, R: TensorRank,

Source§

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

Source§

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

Source§

impl BackendCachedDot for CpuBackend

Source§

impl BackendRuntimeCache for CpuBackend

Source§

impl TensorBackend for CpuBackend

Source§

impl TensorFusion for CpuBackend

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> BackendSession for T
where T: TensorBackendOps + SessionCachedDot + ?Sized,

§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> SessionCachedDot for T
where T: TensorBackend + ?Sized,