use crate::dist::placement::{Local, Placement};
use crate::err::BackendError;
use crate::exec::context::ExecutionContext;
use crate::exec::request::TensorHandle;
use crate::exec::{TensorMeta, Validated};
use crate::shapes::ShapeBuf;
use crate::tensor::device::{Device, DeviceId};
use crate::tensor::dtype::{DType, DTypeDescriptor};
pub trait StorageBackend<P: Placement = Local>: Sized {
const BACKEND_NAME: &'static str;
type Storage<K: DType>: Clone;
type Device: Device;
fn metadata<K: DType>(storage: &Self::Storage<K>) -> &TensorMeta;
fn shape<K: DType>(storage: &Self::Storage<K>) -> ShapeBuf {
Self::metadata(storage).shape().clone()
}
fn storage_dtype<K: DType>(storage: &Self::Storage<K>) -> Option<DTypeDescriptor> {
Some(Self::metadata(storage).dtype())
}
fn storage_device<K: DType>(storage: &Self::Storage<K>) -> Option<DeviceId> {
Some(Self::metadata(storage).device)
}
fn fresh_autograd_identity<K: DType>(storage: Self::Storage<K>) -> Self::Storage<K> {
storage
}
#[doc(hidden)]
fn execution_storage<K: DType>(
storage: &Self::Storage<K>,
) -> (&dyn core::any::Any, Option<usize>)
where
Self::Storage<K>: core::any::Any,
{
(storage, None)
}
}
pub struct ExecutionRequest<'a, O, B>
where
O: crate::exec::catalog::Operation,
B: StorageBackend,
{
pub operation: &'a Validated<crate::exec::catalog::Descriptor<O>>,
pub inputs: &'a [TensorHandle<'a>],
pub context: &'a ExecutionContext<B>,
pub payload: Option<crate::exec::request::ExecutionPayload<'a>>,
}
pub trait ExecuteOutput {}
pub trait StorageOutput {}
impl<T: StorageOutput> ExecuteOutput for T {}
impl ExecuteOutput for crate::shapes::ShapeBuf {}
impl ExecuteOutput for crate::exec::ProofLevel {}
impl ExecuteOutput for f64 {}
impl ExecuteOutput for i64 {}
impl ExecuteOutput for () {}
impl ExecuteOutput for f32 {}
impl ExecuteOutput for i32 {}
impl ExecuteOutput for alloc::vec::Vec<u8> {}
impl ExecuteOutput for alloc::vec::Vec<usize> {}
impl<T: ExecuteOutput> ExecuteOutput for alloc::vec::Vec<T> {}
impl<L: ExecuteOutput, R: ExecuteOutput> ExecuteOutput for (L, R) {}
pub trait Execute<O>: StorageBackend + Sized
where
O: crate::exec::catalog::Operation,
{
type Output: ExecuteOutput;
fn supports_custom(&self, _query: &crate::exec::CapabilityQuery) -> crate::exec::SupportLevel {
crate::exec::SupportLevel::Native
}
fn supports_custom_operation(
&self,
_operation: &crate::exec::OperationIdentity,
_training: bool,
_math_mode: crate::exec::MathMode,
) -> crate::exec::SupportLevel {
crate::exec::SupportLevel::Native
}
fn execute(
&self,
request: ExecutionRequest<'_, O, Self>,
) -> core::result::Result<Self::Output, BackendError>;
}