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
impl TensorData
Sourcepub fn new<E: Element, S: Into<Vec<usize>>>(value: Vec<E>, shape: S) -> Self
pub fn new<E: Element, S: Into<Vec<usize>>>(value: Vec<E>, shape: S) -> Self
Creates a new tensor data structure.
Sourcepub fn quantized<E: Element, S: Into<Vec<usize>>>(
value: Vec<E>,
shape: S,
strategy: QuantizationStrategy,
) -> Self
pub fn quantized<E: Element, S: Into<Vec<usize>>>( value: Vec<E>, shape: S, strategy: QuantizationStrategy, ) -> Self
Creates a new quantized tensor data structure.
Sourcepub fn from_bytes<S: Into<Vec<usize>>>(
bytes: Vec<u8>,
shape: S,
dtype: DType,
) -> Self
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.
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.
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.
§Panics
If the target element type is different from the stored element type.
Sourcepub fn to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>
pub fn to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>
Returns the tensor data as a vector of scalar values.
Sourcepub fn into_vec<E: Element>(self) -> Result<Vec<E>, DataError>
pub fn into_vec<E: Element>(self) -> Result<Vec<E>, DataError>
Returns the tensor data as a vector of scalar values.
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 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: RngCore, S: Into<Vec<usize>>>(
shape: S,
distribution: Distribution,
rng: &mut R,
) -> Self
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.
Sourcepub fn zeros<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData
pub fn zeros<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData
Populates the data with zeros.
Sourcepub fn ones<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData
pub fn ones<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData
Populates the data with ones.
Sourcepub fn full<E: Element, S: Into<Vec<usize>>>(
shape: S,
fill_value: E,
) -> TensorData
pub fn full<E: Element, S: Into<Vec<usize>>>( shape: S, fill_value: E, ) -> TensorData
Populates the data with the given value
Sourcepub fn convert_dtype(self, dtype: DType) -> Self
pub fn convert_dtype(self, dtype: DType) -> Self
Converts the data to a different element type.
Sourcepub fn into_bytes(self) -> Bytes
pub fn into_bytes(self) -> Bytes
Returns the bytes representation of the data.
Sourcepub fn with_quantization(self, quantization: QuantizationStrategy) -> Self
pub fn with_quantization(self, quantization: QuantizationStrategy) -> Self
Applies the data quantization strategy.
§Panics
Panics if the data type is not supported for quantization.
Sourcepub fn dequantize(self) -> Result<Self, DataError>
pub fn dequantize(self) -> Result<Self, DataError>
Dequantizes the data according to its quantization scheme.
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: Element>(&self, range: Range<E>)
pub fn assert_within_range<E: Element>(&self, range: Range<E>)
Sourcepub fn assert_within_range_inclusive<E: Element>(
&self,
range: RangeInclusive<E>,
)
pub fn assert_within_range_inclusive<E: Element>( &self, range: RangeInclusive<E>, )
Trait Implementations§
Source§impl Clone for TensorData
impl Clone for TensorData
Source§fn clone(&self) -> TensorData
fn clone(&self) -> TensorData
1.0.0 · 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
Source§impl From<&[usize]> for TensorData
impl From<&[usize]> 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<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<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 PartialEq for TensorData
impl PartialEq for TensorData
Source§impl Serialize for TensorData
impl Serialize for TensorData
impl Eq for TensorData
impl StructuralPartialEq for TensorData
Auto Trait Implementations§
impl Freeze for TensorData
impl RefUnwindSafe for TensorData
impl Send for TensorData
impl Sync for TensorData
impl Unpin for TensorData
impl UnwindSafe 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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