Skip to main content

ExtensionExecutor

Struct ExtensionExecutor 

Source
pub struct ExtensionExecutor<B>
where B: TensorBackend + 'static,
{ /* private fields */ }
Expand description

Runtime owner for backend-specific extension dispatch and caches.

Implementations§

Source§

impl<B> ExtensionExecutor<B>
where B: TensorBackend + 'static,

Source

pub fn new() -> ExtensionExecutor<B>

Create an executor with an empty registry and default cache limits.

§Examples
use tenferro_runtime::ExtensionExecutor;
use tenferro_cpu::CpuBackend;

let executor = ExtensionExecutor::<CpuBackend>::new();
assert_eq!(executor.cache_stats().entries, 0);
Source

pub fn with_parts( registry: ExtensionRegistry<B>, caches: ExtensionCacheStore, ) -> ExtensionExecutor<B>

Create an executor from explicit registry and cache store.

Source

pub fn registry(&self) -> &ExtensionRegistry<B>

Borrow the runtime executor registry.

Source

pub fn registry_mut(&mut self) -> &mut ExtensionRegistry<B>

Borrow the runtime executor registry mutably.

Source

pub fn caches(&self) -> &ExtensionCacheStore

Borrow the extension cache store.

Source

pub fn caches_mut(&mut self) -> &mut ExtensionCacheStore

Borrow the extension cache store mutably.

Source

pub fn execute( &mut self, backend: &mut B, op: &(dyn ExtensionOp + 'static), inputs: &[&Tensor], ) -> Result<Vec<Tensor>, Error>

Execute an extension using a registered runtime executor.

Source

pub fn execute_reads( &mut self, backend: &mut B, op: &(dyn ExtensionOp + 'static), inputs: &[TensorRead<'_>], ) -> Result<Vec<Tensor>, Error>

Execute an extension using borrowed tensor reads.

§Examples
use std::any::Any;
use std::hash::Hasher;
use std::sync::Arc;

use tenferro_cpu::CpuBackend;
use tenferro_ops::{ext_op::ExtensionOp, SymDim};
use tenferro_runtime::{
    DType, ExtensionExecutionContext, ExtensionExecutor, ExtensionRuntime, Tensor,
};
use tenferro_tensor::TensorRead;

#[derive(Clone, Debug)]
struct IdentityOp;

impl ExtensionOp for IdentityOp {
    fn family_id(&self) -> &'static str {
        "example.identity.v1"
    }

    fn payload_hash(&self, _hasher: &mut dyn Hasher) {}

    fn payload_eq(&self, other: &dyn ExtensionOp) -> bool {
        other.as_any().is::<IdentityOp>()
    }

    fn clone_arc(&self) -> Arc<dyn ExtensionOp> {
        Arc::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn input_count(&self) -> usize {
        1
    }

    fn output_count(&self) -> usize {
        1
    }

    fn infer_output_meta(
        &self,
        input_dtypes: &[DType],
        input_shapes: &[&[SymDim]],
    ) -> Vec<(DType, Vec<SymDim>)> {
        vec![(input_dtypes[0], input_shapes[0].to_vec())]
    }

    fn eager_execute(&self, inputs: &[&Tensor]) -> tenferro_tensor::Result<Vec<Tensor>> {
        Ok(vec![inputs[0].clone()])
    }
}

#[derive(Debug)]
struct IdentityRuntime;

impl ExtensionRuntime<CpuBackend> for IdentityRuntime {
    fn family_id(&self) -> &'static str {
        "example.identity.v1"
    }

    fn execute(
        &self,
        op: &dyn ExtensionOp,
        inputs: &[&Tensor],
        _ctx: &mut ExtensionExecutionContext<'_, CpuBackend>,
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        op.eager_execute(inputs)
    }

    fn execute_reads(
        &self,
        op: &dyn ExtensionOp,
        inputs: &[TensorRead<'_>],
        ctx: &mut ExtensionExecutionContext<'_, CpuBackend>,
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        let materialized_inputs: Vec<Tensor> = inputs
            .iter()
            .map(TensorRead::to_tensor)
            .collect::<tenferro_tensor::Result<_>>()?;
        let input_refs: Vec<&Tensor> = materialized_inputs.iter().collect();
        self.execute(op, &input_refs, ctx)
    }
}

let mut executor = ExtensionExecutor::<CpuBackend>::new();
executor.registry_mut().register(Arc::new(IdentityRuntime))?;
let input = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
let read = TensorRead::from_tensor(&input);
let mut backend = CpuBackend::new();

let outputs = executor.execute_reads(&mut backend, &IdentityOp, &[read])?;

assert_eq!(outputs[0].as_slice::<f64>().unwrap(), &[1.0, 2.0]);
Source

pub fn clear_caches(&mut self)

Clear every runtime extension cache entry.

Source

pub fn cache_stats(&self) -> CacheStats

Return extension cache stats for all entries.

Source

pub fn cache_limits(&self) -> ExtensionCacheLimits

Return the extension cache retention limits.

Source

pub fn set_cache_limits(&mut self, limits: ExtensionCacheLimits)

Replace extension cache retention limits.

Trait Implementations§

Source§

impl<B> Debug for ExtensionExecutor<B>
where B: TensorBackend + 'static,

Source§

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

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

impl<B> Default for ExtensionExecutor<B>
where B: TensorBackend + 'static,

Source§

fn default() -> ExtensionExecutor<B>

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

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

§

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,