pub trait TensorStorage:
Send
+ Sync
+ Debug
+ Display
+ DynEq
+ Downcast {
// Required methods
fn byte_len(&self) -> usize;
fn is_empty(&self) -> bool;
fn deep_clone(&self) -> Box<dyn TensorStorage>;
fn as_plain_ram(&self) -> Option<&PlainStorage>;
fn as_plain_ram_mut(&mut self) -> Option<&mut PlainStorage>;
fn into_plain_ram(self: Box<Self>) -> Option<PlainStorage>;
fn dyn_hash(&self, state: &mut dyn Hasher);
fn exotic_fact(
&self,
shape: &[usize],
) -> TractResult<Option<Box<dyn ExoticFact>>>;
// Provided methods
fn is_exotic(&self) -> bool { ... }
fn in_ram(&self) -> bool { ... }
fn materialize_plain_ram(&self) -> TractResult<&PlainStorage> { ... }
fn slice(
&self,
_dt: DatumType,
_shape: &[usize],
_axis: usize,
_start: usize,
_end: usize,
) -> TractResult<Option<Tensor>> { ... }
}Expand description
Trait abstracting over tensor storage backends.
Two independent axes describe one: layout, is_exotic, and placement,
in_ram. PlainStorage is the primary implementation, plain and in ram by
construction; every other backend is held behind
StorageKind::Exotic(Box<dyn TensorStorage>), whichever pair of answers it
gives.
Required Methods§
fn byte_len(&self) -> usize
fn is_empty(&self) -> bool
fn deep_clone(&self) -> Box<dyn TensorStorage>
fn as_plain_ram(&self) -> Option<&PlainStorage>
fn as_plain_ram_mut(&mut self) -> Option<&mut PlainStorage>
fn into_plain_ram(self: Box<Self>) -> Option<PlainStorage>
fn dyn_hash(&self, state: &mut dyn Hasher)
Sourcefn exotic_fact(
&self,
shape: &[usize],
) -> TractResult<Option<Box<dyn ExoticFact>>>
fn exotic_fact( &self, shape: &[usize], ) -> TractResult<Option<Box<dyn ExoticFact>>>
Build the ExoticFact that describes this storage for use in TypedFact.
Plain storage returns None. Exotic storages should return the
appropriate fact so that From<Arc<Tensor>> for TypedFact preserves
exotic-ness.
Provided Methods§
Sourcefn is_exotic(&self) -> bool
fn is_exotic(&self) -> bool
True when the tensor’s datum type and shape do not describe it on their
own, so a fact over it carries an ExoticFact.
The layout axis, orthogonal to in_ram: all four combinations exist,
a dense tensor in device memory being plain and out of ram,
block-quant weights in device memory exotic and out of ram.
Defaults to true: a storage is exotic until it says otherwise, so one that forgets is refused where a fact is required rather than silently mistyped.
Sourcefn in_ram(&self) -> bool
fn in_ram(&self) -> bool
True when the bytes are in host memory, readable without a transfer.
The placement axis. Defaults to true: storage holds its own bytes unless
it says otherwise. Answers for the bytes in whatever layout the storage
keeps them – block-quant storage is in ram while packed – so it takes
both axes, as_plain_ram, for a plain read to be sure to work.
Sourcefn materialize_plain_ram(&self) -> TractResult<&PlainStorage>
fn materialize_plain_ram(&self) -> TractResult<&PlainStorage>
Plain storage for the tensor’s bytes, producing it if this storage can.
This is the accessor path: Tensor::as_bytes and friends go through it,
so a storage that holds its bytes somewhere else (on a device, say) gets
a chance to bring them back here, and to keep the result so the next
access is free. as_plain_ram stays the cheap accessor: it answers with
what is available right now and never produces anything.
Sourcefn slice(
&self,
_dt: DatumType,
_shape: &[usize],
_axis: usize,
_start: usize,
_end: usize,
) -> TractResult<Option<Tensor>>
fn slice( &self, _dt: DatumType, _shape: &[usize], _axis: usize, _start: usize, _end: usize, ) -> TractResult<Option<Tensor>>
Slice along axis, if this storage can do it without copying.
None means “not capable” and the caller falls back to a generic copy,
so an implementation is free to refuse any case it cannot serve. What it
must not do is return a tensor that is not a valid dense one: Some is a
claim that the result stands on its own everywhere a tensor is accepted.
Implementations§
Source§impl dyn TensorStorage
impl dyn TensorStorage
Sourcepub fn is<__T: TensorStorage>(&self) -> bool
pub fn is<__T: TensorStorage>(&self) -> bool
Returns true if the trait object wraps an object of type __T.
Sourcepub fn downcast<__T: TensorStorage>(
self: Box<Self>,
) -> Result<Box<__T>, Box<Self>>
pub fn downcast<__T: TensorStorage>( self: Box<Self>, ) -> Result<Box<__T>, Box<Self>>
Returns a boxed object from a boxed trait object if the underlying object is of type
__T. Returns the original boxed trait if it isn’t.
Sourcepub fn downcast_rc<__T: TensorStorage>(
self: Rc<Self>,
) -> Result<Rc<__T>, Rc<Self>>
pub fn downcast_rc<__T: TensorStorage>( self: Rc<Self>, ) -> Result<Rc<__T>, Rc<Self>>
Returns an Rc-ed object from an Rc-ed trait object if the underlying object is of
type __T. Returns the original Rc-ed trait if it isn’t.
Sourcepub fn downcast_ref<__T: TensorStorage>(&self) -> Option<&__T>
pub fn downcast_ref<__T: TensorStorage>(&self) -> Option<&__T>
Returns a reference to the object within the trait object if it is of type __T, or
None if it isn’t.
Sourcepub fn downcast_mut<__T: TensorStorage>(&mut self) -> Option<&mut __T>
pub fn downcast_mut<__T: TensorStorage>(&mut self) -> Option<&mut __T>
Returns a mutable reference to the object within the trait object if it is of type
__T, or None if it isn’t.
Trait Implementations§
impl<'eq> Eq for dyn TensorStorage + 'eq
impl<'eq> Eq for dyn TensorStorage + Send + 'eq
impl<'eq> Eq for dyn TensorStorage + Sync + 'eq
impl<'eq> Eq for dyn TensorStorage + Send + Sync + 'eq
Source§impl<'eq> PartialEq for dyn TensorStorage + 'eq
impl<'eq> PartialEq for dyn TensorStorage + 'eq
Source§impl<'eq> PartialEq for dyn TensorStorage + Send + 'eq
impl<'eq> PartialEq for dyn TensorStorage + Send + 'eq
Source§impl<'eq> PartialEq for dyn TensorStorage + Sync + 'eq
impl<'eq> PartialEq for dyn TensorStorage + Sync + 'eq
Source§impl<'eq> PartialEq<&Box<dyn TensorStorage + 'eq>> for Box<dyn TensorStorage + 'eq>
impl<'eq> PartialEq<&Box<dyn TensorStorage + 'eq>> for Box<dyn TensorStorage + 'eq>
Source§impl<'eq> PartialEq<&Box<dyn TensorStorage + Send + 'eq>> for Box<dyn TensorStorage + Send + 'eq>
impl<'eq> PartialEq<&Box<dyn TensorStorage + Send + 'eq>> for Box<dyn TensorStorage + Send + 'eq>
Source§impl<'eq> PartialEq<&Box<dyn TensorStorage + Sync + 'eq>> for Box<dyn TensorStorage + Sync + 'eq>
impl<'eq> PartialEq<&Box<dyn TensorStorage + Sync + 'eq>> for Box<dyn TensorStorage + Sync + 'eq>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".