Skip to main content

ExtensionExecutor

Struct ExtensionExecutor 

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

Runtime owner for backend-specific extension dispatch and caches.

Implementations§

Source§

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

Source

pub fn new() -> Self

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, ) -> Self

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, inputs: &[&Tensor], ) -> Result<Vec<Tensor>>

Execute an extension using a registered runtime executor.

Source

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

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: TensorBackend + 'static> Debug for ExtensionExecutor<B>

Source§

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

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

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

Source§

fn default() -> Self

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
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, 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.