Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor { /* private fields */ }
Expand description

A multi-dimensional tensor with reference-counted buffer ownership.

Implementations§

Source§

impl Tensor

Source

pub fn zeros(shape: impl Into<Shape>, dtype: DType) -> Result<Self>

Create a zero-filled tensor on the CPU.

Source

pub fn from_f32(data: &[f32], shape: impl Into<Shape>) -> Result<Self>

Create a tensor from a flat f32 slice (CPU, row-major).

Source

pub fn from_bf16_bytes(data: &[u8], shape: impl Into<Shape>) -> Result<Self>

Create a tensor from raw BF16 bytes, storing them natively without conversion. Use to_f32_vec() or to_f32_tensor() to convert for computation.

Source

pub fn from_f16_bytes(data: &[u8], shape: impl Into<Shape>) -> Result<Self>

Create a tensor from raw F16 bytes, storing them natively without conversion.

Source

pub fn from_quant_bytes( data: &[u8], shape: impl Into<Shape>, dtype: DType, ) -> Result<Self>

Create a quantized tensor from raw block bytes (Q4_0 / Q8_0).

data must contain exactly dtype.byte_count(shape.numel()) bytes, i.e. the packed ggml block bytes with no expansion. The shape describes the logical element count; shape.numel() must be a multiple of 32.

Source

pub fn scalar_f32(v: f32) -> Result<Self>

Create a scalar tensor from a single f32.

Source

pub fn from_buffer( shape: impl Into<Shape>, dtype: DType, buffer: BufferHandle, offset: usize, ) -> Result<Self>

Create from a pre-built BufferHandle (used by backends).

Source

pub fn shape(&self) -> &Shape

Source

pub fn dtype(&self) -> DType

Source

pub fn ndim(&self) -> usize

Source

pub fn numel(&self) -> usize

Source

pub fn strides(&self) -> &[usize]

Source

pub fn buffer(&self) -> &BufferHandle

Source

pub fn offset(&self) -> usize

Source

pub fn is_scalar(&self) -> bool

True if the tensor has a single element.

Source

pub fn is_contiguous(&self) -> bool

True if the buffer is row-major contiguous (normal case).

Source

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

Raw byte view. For non-quantized tensors returns the full buffer slice from offset onwards (preserving the original behavior that stride-based kernels rely on). For quantized tensors (Q4_0/Q8_0) returns exactly the packed block bytes for this tensor’s logical shape.

Source

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

For quantized tensors (Q4_0, Q8_0): returns the packed block bytes as a row-major slice where each logical row of k elements occupies dtype.byte_count(k) bytes. Panics if the tensor is not quantized.

Source

pub fn as_f32_slice(&self) -> &[f32]

Typed f32 view — panics if dtype is not F32.

Source

pub fn to_f32_cow(&self) -> Cow<'_, [f32]>

Returns a Cow<[f32]>. Borrows if the tensor is already F32, otherwise allocates a new Vec<f32>.

Source

pub fn to_f32_vec(&self) -> Vec<f32>

Convert this tensor to a Vec<f32>, handling all dtypes including quantized. For F32: cheap copy. For F16/BF16: convert. For Q4_0/Q8_0: dequantize all blocks.

Source

pub fn to_f32_tensor(&self) -> Result<Tensor>

Returns an F32 tensor, converting BF16/F16 if necessary. For already-F32 tensors, clones the buffer. For native types, converts.

Source

pub fn as_f32_slice_mut(&mut self) -> Result<&mut [f32]>

Mutable typed f32 view — fails if buffer is shared or not F32.

Source

pub fn reshape(&self, new_shape: impl Into<Shape>) -> Result<Tensor>

Returns a new tensor with a different shape but the same buffer. The total number of elements must be unchanged.

Source

pub fn t(&self) -> Result<Tensor>

Transpose a 2-D tensor (swap axes 0 and 1).

Source

pub fn slice_axis( &self, axis: usize, start: usize, end: usize, ) -> Result<Tensor>

Return a view of the tensor sliced along the given axis.

Source

pub fn byte_size(&self) -> usize

Byte count for all elements.

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

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 Tensor

Source§

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

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

impl<'de> Deserialize<'de> for Tensor

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

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

impl Display for Tensor

Source§

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

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

impl From<&Tensor> for TensorMeta

Source§

fn from(t: &Tensor) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Tensor

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

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