DynTensor

Struct DynTensor 

Source
pub struct DynTensor<B: Backend> { /* private fields */ }
Expand description

A dynamic Tensor wrapper that can be sliced.

Implementations§

Source§

impl<B: Backend> DynTensor<B>

Source

pub fn new<const R: usize, K>(tensor: Tensor<B, R, K>) -> Self
where K: BasicOps<B> + 'static,

Create a new TensorStub from a tensor.

Source

pub fn rank(&self) -> usize

Get the tensor rank.

Source

pub fn shape(&self) -> Shape

Get the tensor shape.

Source

pub fn num_elements(&self) -> usize

Get the number of elements in the tensor.

Source

pub fn size_estimate(&self) -> usize

Returns the size estimate of the tensor in bytes.

This is self.dtype().size() * self.num_elements().

Source

pub fn dtype(&self) -> DType

Get the tensor data type.

Source

pub fn kind(&self) -> KindFlag

Get the tensor kind.

Source

pub fn device(&self) -> B::Device

Get the tensor device.

Source

pub fn downcast_clone<const R: usize, K>(&self) -> Option<Tensor<B, R, K>>
where K: 'static + BasicOps<B>,

Downcasts the tensor to a specific rank and kind.

§Result
  • Some(Tensor<B, R, K>): if the params are correct,
  • None: otherwise.
Source

pub fn unwrap_clone<const R: usize, K>(&self) -> Tensor<B, R, K>
where K: 'static + BasicOps<B>,

Downcasts to a static tensor.

§Result
  • the static tensor: if the params are correct,
§Panics

If the types are incorrect.

Source

pub fn slice<const R: usize, S>(self, slices: S) -> Result<Self, DynTensorError>
where S: SliceArg<R>,

Slice the stub tensor.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • slices: a SliceArg<R>.
§Result
  • Ok(DynTensor): the sliced tensor.
  • Err(DynTensorError): an error.
Source

pub fn slice_dyn(self, slices: &[Slice]) -> Result<Self, DynTensorError>

A dynamic version of DynTensor::slice.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • slices: a dynamic slice of Slice.
§Result
  • Ok(DynTensor): the sliced tensor.
  • Err(DynTensorError): an error.
Source

pub fn slice_assign<const R2: usize, S, V>( self, slices: S, values: V, ) -> Result<Self, DynTensorError>
where S: SliceArg<R2>, V: ValuesArg<B>,

Assign values to a slice.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • slices: a SlicesArg<R2>.
  • values: a coercible value; see ValuesArg.
§Result
  • Ok(DynTensor): a converted tensor.
  • Err(DynTensorError): an error.
Source

pub fn slice_assign_dyn<V>( self, slices: &[Slice], values: V, ) -> Result<Self, DynTensorError>
where V: ValuesArg<B>,

Dynamic slice rank version of DynTensor::slice_assign.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • slices: a dynamic slice of Slice.
  • values: a coercible value; see ValuesArg.
§Result
  • Ok(DynTensor): a converted tensor.
  • Err(DynTensorError): an error.
Source

pub fn flatten(self) -> Result<Self, DynTensorError>

Flatten the tensor.

Dispatches via rank_dispatch::dispatch_rank.

§Result
  • Ok(DynTensor): a flattened (rank=1) tensor.
  • Err(DynTensorError): an error.
Source

pub fn cast(self, dtype: DType) -> Result<Self, DynTensorError>

Cast the tensor.

Auto-converts kind if necessary.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • dtype: the target data type.
§Result
  • Ok(DynTensor): a converted tensor.
  • Err(DynTensorError): an error.
Source

pub fn to_device(self, device: &B::Device) -> Result<Self, DynTensorError>

Move the tensor to the given device.

Moving to the same device is an inexpensive no-op.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • device: the target device.
§Result
  • Ok(DynTensor<B>): the moved tensor.
  • Err(DynTensorError): an error.
Source

pub fn from_data( data: TensorData, device: &B::Device, ) -> Result<Self, DynTensorError>

Convert a TensorData to a DynTensor.

Dispatches via rank_dispatch::dispatch_rank.

§Arguments
  • data: source TensorData.
  • device: the target device.
§Result
  • Ok(DynTensor<B>): the converted tensor.
  • Err(DynTensorError): an error.
Source

pub fn into_data(self) -> Result<TensorData, DynTensorError>

Convert the tensor to a TensorData.

Dispatches via rank_dispatch::dispatch_rank.

§Result
  • Ok(TensorData): the converted data.
  • Err(DynTensorError): an error.
Source

pub fn to_data(self) -> Result<TensorData, DynTensorError>

Convert the tensor to a TensorData.

Dispatches via rank_dispatch::dispatch_rank.

§Result
  • Ok(TensorData): the converted data.
  • Err(DynTensorError): an error.

Trait Implementations§

Source§

impl<B: Clone + Backend> Clone for DynTensor<B>
where B::Device: Clone,

Source§

fn clone(&self) -> DynTensor<B>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<B: Debug + Backend> Debug for DynTensor<B>
where B::Device: Debug,

Source§

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

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

impl<B: Backend, const R: usize, K> From<Tensor<B, R, K>> for DynTensor<B>
where K: 'static + BasicOps<B>,

Source§

fn from(val: Tensor<B, R, K>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<B> Freeze for DynTensor<B>
where <B as Backend>::Device: Freeze,

§

impl<B> !RefUnwindSafe for DynTensor<B>

§

impl<B> Send for DynTensor<B>

§

impl<B> Sync for DynTensor<B>

§

impl<B> Unpin for DynTensor<B>
where <B as Backend>::Device: Unpin, B: Unpin,

§

impl<B> !UnwindSafe for DynTensor<B>

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> CloneBox for T
where T: 'static + Any + Debug + Clone + Send + Sync,

Source§

fn clone_box(&self) -> Box<dyn CloneBox>

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<B, T> ValuesArg<B> for T
where B: Backend, T: Into<DynTensor<B>>,

Source§

fn into_values( self, device: &<B as Backend>::Device, ) -> Result<DynTensor<B>, DynTensorError>

Convert to a DynTensor on a given device.