Struct TensorData

Source
pub struct TensorData {
    pub bytes: Bytes,
    pub shape: Vec<usize>,
    pub dtype: DType,
}
Expand description

Data structure for tensors.

Fields§

§bytes: Bytes

The values of the tensor (as bytes).

§shape: Vec<usize>

The shape of the tensor.

§dtype: DType

The data type of the tensor.

Implementations§

Source§

impl TensorData

Source

pub fn new<E: Element, S: Into<Vec<usize>>>(value: Vec<E>, shape: S) -> Self

Creates a new tensor data structure.

Source

pub fn quantized<E: Element, S: Into<Vec<usize>>>( value: Vec<E>, shape: S, strategy: QuantizationStrategy, ) -> Self

Creates a new quantized tensor data structure.

Source

pub fn from_bytes<S: Into<Vec<usize>>>( bytes: Vec<u8>, shape: S, dtype: DType, ) -> Self

Creates a new tensor data structure from raw bytes.

Prefer TensorData::new or TensorData::quantized over this method unless you are certain that the bytes representation is valid.

Source

pub fn as_slice<E: Element>(&self) -> Result<&[E], DataError>

Returns the immutable slice view of the tensor data.

Source

pub fn as_mut_slice<E: Element>(&mut self) -> Result<&mut [E], DataError>

Returns the mutable slice view of the tensor data.

§Panics

If the target element type is different from the stored element type.

Source

pub fn to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>

Returns the tensor data as a vector of scalar values.

Source

pub fn into_vec<E: Element>(self) -> Result<Vec<E>, DataError>

Returns the tensor data as a vector of scalar values.

Source

pub fn iter<E: Element>(&self) -> Box<dyn Iterator<Item = E> + '_>

Returns an iterator over the values of the tensor data.

Source

pub fn num_elements(&self) -> usize

Returns the total number of elements of the tensor data.

Source

pub fn random<E: Element, R: RngCore, S: Into<Vec<usize>>>( shape: S, distribution: Distribution, rng: &mut R, ) -> Self

Populates the data with random values.

Source

pub fn zeros<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData

Populates the data with zeros.

Source

pub fn ones<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData

Populates the data with ones.

Source

pub fn full<E: Element, S: Into<Vec<usize>>>( shape: S, fill_value: E, ) -> TensorData

Populates the data with the given value

Source

pub fn convert<E: Element>(self) -> Self

Converts the data to a different element type.

Source

pub fn convert_dtype(self, dtype: DType) -> Self

Converts the data to a different element type.

Source

pub fn as_bytes(&self) -> &[u8]

Returns the data as a slice of bytes.

Source

pub fn into_bytes(self) -> Bytes

Returns the bytes representation of the data.

Source

pub fn with_quantization(self, quantization: QuantizationStrategy) -> Self

Applies the data quantization strategy.

§Panics

Panics if the data type is not supported for quantization.

Source

pub fn dequantize(self) -> Result<Self, DataError>

Dequantizes the data according to its quantization scheme.

Source

pub fn assert_eq(&self, other: &Self, strict: bool)

Asserts the data is equal to another data.

§Arguments
  • other - The other data.
  • strict - If true, the data types must the be same. Otherwise, the comparison is done in the current data type.
§Panics

Panics if the data is not equal.

Source

pub fn assert_approx_eq<F: Float + Element>( &self, other: &Self, tolerance: Tolerance<F>, )

Asserts the data is approximately equal to another data.

§Arguments
  • other - The other data.
  • tolerance - The tolerance of the comparison.
§Panics

Panics if the data is not approximately equal.

Source

pub fn assert_within_range<E: Element>(&self, range: Range<E>)

Asserts each value is within a given range.

§Arguments
  • range - The range.
§Panics

If any value is not within the half-open range bounded inclusively below and exclusively above (start..end).

Source

pub fn assert_within_range_inclusive<E: Element>( &self, range: RangeInclusive<E>, )

Asserts each value is within a given inclusive range.

§Arguments
  • range - The range.
§Panics

If any value is not within the half-open range bounded inclusively (start..=end).

Trait Implementations§

Source§

impl Clone for TensorData

Source§

fn clone(&self) -> TensorData

Returns a copy 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 Debug for TensorData

Source§

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

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

impl<'de> Deserialize<'de> for TensorData

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for TensorData

Source§

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

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

impl<E: Element> From<&[E]> for TensorData

Source§

fn from(elems: &[E]) -> Self

Converts to this type from the input type.
Source§

impl From<&[usize]> for TensorData

Source§

fn from(elems: &[usize]) -> Self

Converts to this type from the input type.
Source§

impl<Elem: Element, const A: usize, const B: usize, const C: usize, const D: usize, const E: usize> From<[[[[[Elem; E]; D]; C]; B]; A]> for TensorData

Source§

fn from(elems: [[[[[Elem; E]; D]; C]; B]; A]) -> Self

Converts to this type from the input type.
Source§

impl<E: Element, const A: usize, const B: usize, const C: usize, const D: usize> From<[[[[E; D]; C]; B]; A]> for TensorData

Source§

fn from(elems: [[[[E; D]; C]; B]; A]) -> Self

Converts to this type from the input type.
Source§

impl<E: Element, const A: usize, const B: usize, const C: usize> From<[[[E; C]; B]; A]> for TensorData

Source§

fn from(elems: [[[E; C]; B]; A]) -> Self

Converts to this type from the input type.
Source§

impl<E: Element, const A: usize, const B: usize> From<[[E; B]; A]> for TensorData

Source§

fn from(elems: [[E; B]; A]) -> Self

Converts to this type from the input type.
Source§

impl<E: Element, const A: usize> From<[E; A]> for TensorData

Source§

fn from(elems: [E; A]) -> Self

Converts to this type from the input type.
Source§

impl<const A: usize> From<[usize; A]> for TensorData

Source§

fn from(elems: [usize; A]) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for TensorData

Source§

fn eq(&self, other: &TensorData) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for TensorData

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for TensorData

Source§

impl StructuralPartialEq for TensorData

Auto Trait Implementations§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,