use crate::error::Result;
use crate::unified_math::{MathOp, TensorDescriptor};
use bytes::Bytes;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use super::types::{HardwareCapabilities, HardwareType};
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub trait ComputeExecutor: Send + Sync {
fn name(&self) -> &str;
fn hardware_type(&self) -> HardwareType;
fn capabilities(&self) -> &HardwareCapabilities;
fn can_execute(&self, op: &MathOp, inputs: &[TensorDescriptor]) -> bool;
fn score_operation(&self, op: &MathOp, inputs: &[TensorDescriptor]) -> f64;
fn execute(
&self,
op: &MathOp,
inputs: Vec<Arc<dyn TensorStorage>>,
) -> BoxFuture<'_, Result<Arc<dyn TensorStorage>>>;
fn allocate(
&self,
descriptor: TensorDescriptor,
) -> BoxFuture<'_, Result<Arc<dyn TensorStorage>>>;
fn transfer(
&self,
tensor: Arc<dyn TensorStorage>,
) -> BoxFuture<'_, Result<Arc<dyn TensorStorage>>>;
}
pub trait TensorStorage: Send + Sync {
fn descriptor(&self) -> &TensorDescriptor;
fn hardware_type(&self) -> HardwareType;
fn read_to_cpu(&self) -> BoxFuture<'_, Result<Bytes>>;
fn write_from_cpu(&mut self, data: &[u8]) -> BoxFuture<'_, Result<()>>;
fn write_from_cpu_bytes(&mut self, data: Bytes) -> BoxFuture<'_, Result<()>> {
let slice = data.as_ref().to_vec();
self.write_from_cpu(&slice)
}
fn is_cpu(&self) -> bool {
self.hardware_type() == HardwareType::CPU
}
fn is_gpu(&self) -> bool {
self.hardware_type() == HardwareType::GPU
}
fn is_tpu(&self) -> bool {
self.hardware_type() == HardwareType::TPU
}
fn as_wgpu_buffer(&self) -> Option<Arc<::wgpu::Buffer>> {
None
}
}