Skip to main content

TensorData

Struct TensorData 

Source
pub struct TensorData {
    pub bytes: Bytes,
    pub shape: Shape,
    pub dtype: DType,
}
Expand description

Data structure for tensors.

Fields§

§bytes: Bytes

The values of the tensor (as bytes).

§shape: Shape

The shape of the tensor.

§dtype: DType

The data type of the tensor.

Implementations§

Source§

impl TensorData

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: ElementOrdered>(&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: ElementOrdered>( &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).

Source§

impl TensorData

Source

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

Creates a new tensor data structure.

Source

pub fn quantized<E: Element, S: Into<Shape>>( value: Vec<E>, shape: S, scheme: QuantScheme, qparams: &[f32], global: Option<f32>, ) -> Self

Creates a new quantized tensor data structure.

Source

pub fn from_bytes<S: Into<Shape>>(bytes: Bytes, shape: S, dtype: DType) -> Self

Creates a new tensor data structure from raw bytes.

Source

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

Creates a new tensor data structure from raw bytes stored in a vector.

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

Source

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

Returns the immutable slice view of the tensor data.

This materializes lazy storage into host-accessible memory when necessary.

§Errors

Returns an error if host access fails, the target element type doesn’t match the stored type, or the stored byte representation is invalid for E.

Source

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

Returns the mutable slice view of the tensor data.

This materializes lazy storage and performs copy-on-write when necessary.

§Errors

Returns an error if host access fails, the target element type doesn’t match the stored type, or the stored byte representation is invalid for E.

Source

pub fn rank(&self) -> usize

Returns the rank (the number of dimensions).

Source

pub fn num_elements(&self) -> usize

Returns the total number of elements of the tensor data.

Source

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

Populates the data with random values.

Source

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

Populates the data with zeros.

Source

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

Populates the data with ones.

Source

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

Populates the data with the given value

Source

pub fn full_dtype<E: Into<Scalar>, S: Into<Shape>>( shape: S, fill_value: E, dtype: DType, ) -> TensorData

Populates the data with the given value

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§

impl TensorData

Source

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

Copies and converts the data to a Vec<E>.

By contract, this is equivalent to: data.clone().try_into_vec_as::<E>()

Particular conversions may provide more efficient implementations.

§Errors

Returns an error if storage access fails, the conversion isn’t supported, or the stored representation or element count is invalid.

Source

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

Converts the data to a Vec<E>.

By contract, this is equivalent to: data.try_cast_as::<E>()?.try_into_vec::<E>()

Particular conversions may provide more efficient implementations.

§Errors

Returns an error if storage access fails, the conversion isn’t supported, or the stored representation or element count is invalid.

Source

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

👎Deprecated since 0.22.0:

use try_to_vec::<E>()

Copies the stored values to a vector without dtype conversion.

Source

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

👎Deprecated since 0.22.0:

use try_into_vec::<E>()

Converts the stored values into a vector without dtype conversion.

Source

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

Copies the stored values to a vector without dtype conversion.

§Errors

Returns an error if storage access fails, the stored dtype doesn’t match E, or the byte representation is invalid for E.

Source

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

Converts the stored values into a vector without dtype conversion.

This may reuse the underlying allocation when its layout and ownership permit it.

§Errors

Returns an error if storage access fails, the stored dtype doesn’t match E, or the byte representation is invalid for E.

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 convert<E: Element>(self) -> Self

Converts the data to the dtype represented by E.

§Panics

Panics if storage access fails, the conversion isn’t supported, or the stored representation or element count is invalid.

Source

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

Converts the data to dtype.

§Panics

Panics if storage access fails, the conversion isn’t supported, or the stored representation or element count is invalid.

Source

pub fn try_cast_as<E: Element>(self) -> Result<TensorData, DataError>

Converts the data to the dtype represented by E.

By contract, this is equivalent to: data.try_cast(E::dtype())

§Errors

Returns an error if storage access fails, the conversion isn’t supported, or the stored representation or element count is invalid.

Source

pub fn try_cast(self, dtype: DType) -> Result<TensorData, DataError>

Converts the data to dtype.

§Errors

Returns an error if storage access fails, the conversion isn’t supported, or the stored representation or element count is invalid.

Source§

impl TensorData

Source

pub fn try_view<E: Element>(&self) -> Result<TensorDataView<'_, E>, DataError>

Returns an Index view wrapper of the TensorData.

§Example
use burn_std::*;

let data = TensorData::from([[1.0, 2.0], [3.0, 4.0]]);
let shape = data.shape.clone();
let view: TensorDataView<f64> = data.try_view().unwrap();

assert_eq!(view[&[0, 0]], 1.0);
assert_eq!(view[&[0, 1]], 2.0);
assert_eq!(view[&[1, 0]], 3.0);
assert_eq!(view[&[1, 1]], 4.0);
§Errors

Returns an error if storage access fails or the dtype, byte representation, or element count is incompatible with the requested view.

Source

pub fn view<E: Element>(&self) -> TensorDataView<'_, E>

Returns a TensorDataView<E> of the TensorData.

§Example
use burn_std::*;

let data = TensorData::from([[1.0, 2.0], [3.0, 4.0]]);
let shape = data.shape.clone();
let view: TensorDataView<f64> = data.view();

assert_eq!(view[&[0, 0]], 1.0);
assert_eq!(view[&[0, 1]], 2.0);
assert_eq!(view[&[1, 0]], 3.0);
assert_eq!(view[&[1, 1]], 4.0);
§Returns

The view.

§Panics

Panics if the view can’t be created because storage access fails or the dtype, byte representation, or element count is incompatible with E.

Source

pub fn try_mut_view<E: Element>( &mut self, ) -> Result<TensorDataViewMut<'_, E>, DataError>

Returns a TensorDataViewMut<E> of the TensorData.

§Example
use burn_std::*;

let mut data = TensorData::from([[1.0, 2.0], [3.0, 4.0]]);
let shape = data.shape.clone();
let mut view: TensorDataViewMut<f64> = data.try_mut_view().unwrap();

assert_eq!(view[&[0, 0]], 1.0);
assert_eq!(view[&[0, 1]], 2.0);
assert_eq!(view[&[1, 0]], 3.0);
assert_eq!(view[&[1, 1]], 4.0);

view[&[0, 0]] = 10.0;
assert_eq!(view[&[0, 0]], 10.0);
§Errors

Returns an error if storage access fails or the dtype, byte representation, or element count is incompatible with the requested view.

Source

pub fn mut_view<E: Element>(&mut self) -> TensorDataViewMut<'_, E>

Returns a TensorDataViewMut<E> of the TensorData.

§Example
use burn_std::*;

let mut data = TensorData::from([[1.0, 2.0], [3.0, 4.0]]);
let shape = data.shape.clone();
let mut view: TensorDataViewMut<f64> = data.mut_view();

assert_eq!(view[&[0, 0]], 1.0);
assert_eq!(view[&[0, 1]], 2.0);
assert_eq!(view[&[1, 0]], 3.0);
assert_eq!(view[&[1, 1]], 4.0);

view[&[0, 0]] = 10.0;
assert_eq!(view[&[0, 0]], 10.0);
§Returns

The mut view.

§Panics

Panics if the view can’t be created because storage access fails or the dtype, byte representation, or element count is incompatible with E.

Trait Implementations§

Source§

impl Clone for TensorData

Source§

fn clone(&self) -> TensorData

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Eq for TensorData

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<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<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, 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, 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<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<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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> StoreValue for T

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 = !

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.