pub struct TensorData {
pub bytes: Bytes,
pub shape: Shape,
pub dtype: DType,
}Expand description
Data structure for tensors.
Fields§
§bytes: BytesThe values of the tensor (as bytes).
shape: ShapeThe shape of the tensor.
dtype: DTypeThe data type of the tensor.
Implementations§
Source§impl TensorData
impl TensorData
Sourcepub fn assert_approx_eq<F: Float + Element>(
&self,
other: &Self,
tolerance: Tolerance<F>,
)
pub fn assert_approx_eq<F: Float + Element>( &self, other: &Self, tolerance: Tolerance<F>, )
Sourcepub fn assert_within_range<E: ElementOrdered>(&self, range: Range<E>)
pub fn assert_within_range<E: ElementOrdered>(&self, range: Range<E>)
Sourcepub fn assert_within_range_inclusive<E: ElementOrdered>(
&self,
range: RangeInclusive<E>,
)
pub fn assert_within_range_inclusive<E: ElementOrdered>( &self, range: RangeInclusive<E>, )
Source§impl TensorData
impl TensorData
Sourcepub fn new<E: Element, S: Into<Shape>>(value: Vec<E>, shape: S) -> Self
pub fn new<E: Element, S: Into<Shape>>(value: Vec<E>, shape: S) -> Self
Creates a new tensor data structure.
Sourcepub fn quantized<E: Element, S: Into<Shape>>(
value: Vec<E>,
shape: S,
scheme: QuantScheme,
qparams: &[f32],
global: Option<f32>,
) -> Self
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.
Sourcepub fn from_bytes<S: Into<Shape>>(bytes: Bytes, shape: S, dtype: DType) -> Self
pub fn from_bytes<S: Into<Shape>>(bytes: Bytes, shape: S, dtype: DType) -> Self
Creates a new tensor data structure from raw bytes.
Sourcepub fn from_bytes_vec<S: Into<Shape>>(
bytes: Vec<u8>,
shape: S,
dtype: DType,
) -> Self
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.
Sourcepub fn as_slice<E: Element>(&self) -> Result<&[E], DataError>
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.
Sourcepub fn as_mut_slice<E: Element>(&mut self) -> Result<&mut [E], DataError>
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.
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Returns the total number of elements of the tensor data.
Sourcepub fn random<E: Element, R: Rng, S: Into<Shape>>(
shape: S,
distribution: Distribution,
rng: &mut R,
) -> Self
pub fn random<E: Element, R: Rng, S: Into<Shape>>( shape: S, distribution: Distribution, rng: &mut R, ) -> Self
Populates the data with random values.
Sourcepub fn zeros<E: Element, S: Into<Shape>>(shape: S) -> TensorData
pub fn zeros<E: Element, S: Into<Shape>>(shape: S) -> TensorData
Populates the data with zeros.
Sourcepub fn full<E: Element, S: Into<Shape>>(shape: S, fill_value: E) -> TensorData
pub fn full<E: Element, S: Into<Shape>>(shape: S, fill_value: E) -> TensorData
Populates the data with the given value
Sourcepub fn full_dtype<E: Into<Scalar>, S: Into<Shape>>(
shape: S,
fill_value: E,
dtype: DType,
) -> TensorData
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
Sourcepub fn into_bytes(self) -> Bytes
pub fn into_bytes(self) -> Bytes
Returns the bytes representation of the data.
Source§impl TensorData
impl TensorData
Sourcepub fn try_to_vec_as<E: Element>(&self) -> Result<Vec<E>, DataError>
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.
Sourcepub fn try_into_vec_as<E: Element>(self) -> Result<Vec<E>, DataError>
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.
Sourcepub fn to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>
👎Deprecated since 0.22.0: use try_to_vec::<E>()
pub fn to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>
use try_to_vec::<E>()
Copies the stored values to a vector without dtype conversion.
Sourcepub fn into_vec<E: Element>(self) -> Result<Vec<E>, DataError>
👎Deprecated since 0.22.0: use try_into_vec::<E>()
pub fn into_vec<E: Element>(self) -> Result<Vec<E>, DataError>
use try_into_vec::<E>()
Converts the stored values into a vector without dtype conversion.
Sourcepub fn try_to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>
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.
Sourcepub fn try_into_vec<E: Element>(self) -> Result<Vec<E>, DataError>
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.
Sourcepub fn iter<E: Element>(&self) -> Box<dyn Iterator<Item = E> + '_>
pub fn iter<E: Element>(&self) -> Box<dyn Iterator<Item = E> + '_>
Returns an iterator over the values of the tensor data.
Sourcepub fn convert<E: Element>(self) -> Self
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.
Sourcepub fn convert_dtype(self, dtype: DType) -> Self
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.
Sourcepub fn try_cast_as<E: Element>(self) -> Result<TensorData, DataError>
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§impl TensorData
impl TensorData
Sourcepub fn try_view<E: Element>(&self) -> Result<TensorDataView<'_, E>, DataError>
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.
Sourcepub fn view<E: Element>(&self) -> TensorDataView<'_, E>
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.
Sourcepub fn try_mut_view<E: Element>(
&mut self,
) -> Result<TensorDataViewMut<'_, E>, DataError>
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.
Sourcepub fn mut_view<E: Element>(&mut self) -> TensorDataViewMut<'_, E>
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
impl Clone for TensorData
Source§fn clone(&self) -> TensorData
fn clone(&self) -> TensorData
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TensorData
impl Debug for TensorData
Source§impl<'de> Deserialize<'de> for TensorData
impl<'de> Deserialize<'de> for TensorData
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for TensorData
impl Display for TensorData
impl Eq for TensorData
Source§impl From<&[usize]> for TensorData
impl From<&[usize]> for TensorData
Source§impl<E: Element, const A: usize, const B: usize, const C: usize> From<[[[E; C]; B]; A]> for TensorData
impl<E: Element, const A: usize, const B: usize, const C: usize> From<[[[E; C]; B]; A]> for TensorData
Source§impl<E: Element, const A: usize, const B: usize, const C: usize, const D: usize> From<[[[[E; D]; C]; B]; A]> for TensorData
impl<E: Element, const A: usize, const B: usize, const C: usize, const D: usize> From<[[[[E; D]; C]; B]; A]> for TensorData
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
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§impl PartialEq for TensorData
impl PartialEq for TensorData
Source§impl Serialize for TensorData
impl Serialize for TensorData
impl StructuralPartialEq for TensorData
Auto Trait Implementations§
impl !RefUnwindSafe for TensorData
impl !UnwindSafe for TensorData
impl Freeze for TensorData
impl Send for TensorData
impl Sync for TensorData
impl Unpin for TensorData
impl UnsafeUnpin for TensorData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.