pub trait TensorBackendCapability {
// Required methods
fn backend_id(&self) -> BackendId;
fn capabilities(&self) -> &'static [OperationCapability];
// Provided methods
fn capability(&self, query: CapabilityQuery) -> Option<OperationCapability> { ... }
fn require_capability(
&self,
query: CapabilityQuery,
axis: CapabilityAxis,
) -> Result<OperationCapability> { ... }
}Expand description
Backend capability query surface.
§Examples
use tenferro_core_ops::PrimitiveOpKind;
use tenferro_tensor::{
BackendId, CapabilityQuery, DType, OperationCapability, SupportLevel,
TensorBackendCapability,
};
struct Backend;
const ENTRIES: &[OperationCapability] = &[OperationCapability {
backend: BackendId::Cpu,
op: PrimitiveOpKind::Add,
dtype: DType::F32,
output_dtype: DType::F32,
result: SupportLevel::Native,
read_inputs: SupportLevel::Native,
write_output: SupportLevel::Native,
strided_output: SupportLevel::Native,
accumulation: SupportLevel::Unsupported,
}];
impl TensorBackendCapability for Backend {
fn backend_id(&self) -> BackendId { BackendId::Cpu }
fn capabilities(&self) -> &'static [OperationCapability] { ENTRIES }
}
assert!(Backend
.capability(CapabilityQuery::new(PrimitiveOpKind::Add, DType::F32))
.is_some());Required Methods§
fn backend_id(&self) -> BackendId
fn capabilities(&self) -> &'static [OperationCapability]
Provided Methods§
Sourcefn capability(&self, query: CapabilityQuery) -> Option<OperationCapability>
fn capability(&self, query: CapabilityQuery) -> Option<OperationCapability>
Look up one operation/dtype capability for this backend.
§Examples
use tenferro_core_ops::PrimitiveOpKind;
use tenferro_tensor::{
BackendId, CapabilityQuery, DType, OperationCapability, SupportLevel,
TensorBackendCapability,
};
struct Backend;
const ENTRIES: &[OperationCapability] = &[OperationCapability {
backend: BackendId::Cpu,
op: PrimitiveOpKind::Mul,
dtype: DType::I64,
output_dtype: DType::I64,
result: SupportLevel::Native,
read_inputs: SupportLevel::Native,
write_output: SupportLevel::Unsupported,
strided_output: SupportLevel::Unsupported,
accumulation: SupportLevel::Unsupported,
}];
impl TensorBackendCapability for Backend {
fn backend_id(&self) -> BackendId { BackendId::Cpu }
fn capabilities(&self) -> &'static [OperationCapability] { ENTRIES }
}
let entry = Backend
.capability(CapabilityQuery::new(PrimitiveOpKind::Mul, DType::I64))
.unwrap();
assert_eq!(entry.result, SupportLevel::Native);Sourcefn require_capability(
&self,
query: CapabilityQuery,
axis: CapabilityAxis,
) -> Result<OperationCapability>
fn require_capability( &self, query: CapabilityQuery, axis: CapabilityAxis, ) -> Result<OperationCapability>
Require support for one operation/dtype/axis, returning a structured unsupported error otherwise.
§Examples
use tenferro_core_ops::PrimitiveOpKind;
use tenferro_tensor::{
BackendId, CapabilityAxis, CapabilityQuery, DType, Error, OperationCapability,
SupportLevel, TensorBackendCapability,
};
struct Backend;
const ENTRIES: &[OperationCapability] = &[OperationCapability {
backend: BackendId::Cuda,
op: PrimitiveOpKind::Neg,
dtype: DType::I32,
output_dtype: DType::I32,
result: SupportLevel::Unsupported,
read_inputs: SupportLevel::Unsupported,
write_output: SupportLevel::Unsupported,
strided_output: SupportLevel::Unsupported,
accumulation: SupportLevel::Unsupported,
}];
impl TensorBackendCapability for Backend {
fn backend_id(&self) -> BackendId { BackendId::Cuda }
fn capabilities(&self) -> &'static [OperationCapability] { ENTRIES }
}
let err = Backend
.require_capability(
CapabilityQuery::new(PrimitiveOpKind::Neg, DType::I32),
CapabilityAxis::OwnedResult,
)
.unwrap_err();
assert!(matches!(err, Error::UnsupportedDType { op: "neg", dtype: DType::I32, .. }));§Errors
Returns crate::Error::UnsupportedDType when the backend capability
table does not support the requested operation and dtype.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".