pub struct Tensor { /* private fields */ }Expand description
A multi-dimensional tensor with reference-counted buffer ownership.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn zeros(shape: impl Into<Shape>, dtype: DType) -> Result<Self>
pub fn zeros(shape: impl Into<Shape>, dtype: DType) -> Result<Self>
Create a zero-filled tensor on the CPU.
Sourcepub fn from_f32(data: &[f32], shape: impl Into<Shape>) -> Result<Self>
pub fn from_f32(data: &[f32], shape: impl Into<Shape>) -> Result<Self>
Create a tensor from a flat f32 slice (CPU, row-major).
Sourcepub fn from_bf16_bytes(data: &[u8], shape: impl Into<Shape>) -> Result<Self>
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.
Sourcepub fn from_f16_bytes(data: &[u8], shape: impl Into<Shape>) -> Result<Self>
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.
Sourcepub fn from_quant_bytes(
data: &[u8],
shape: impl Into<Shape>,
dtype: DType,
) -> Result<Self>
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.
Sourcepub fn scalar_f32(v: f32) -> Result<Self>
pub fn scalar_f32(v: f32) -> Result<Self>
Create a scalar tensor from a single f32.
Sourcepub fn from_buffer(
shape: impl Into<Shape>,
dtype: DType,
buffer: BufferHandle,
offset: usize,
) -> Result<Self>
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).
pub fn shape(&self) -> &Shape
pub fn dtype(&self) -> DType
pub fn ndim(&self) -> usize
pub fn numel(&self) -> usize
pub fn strides(&self) -> &[usize]
pub fn buffer(&self) -> &BufferHandle
pub fn offset(&self) -> usize
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
True if the buffer is row-major contiguous (normal case).
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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.
Sourcepub fn as_quant_blocks(&self) -> &[u8] ⓘ
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.
Sourcepub fn as_f32_slice(&self) -> &[f32]
pub fn as_f32_slice(&self) -> &[f32]
Typed f32 view — panics if dtype is not F32.
Sourcepub fn to_f32_cow(&self) -> Cow<'_, [f32]>
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>.
Sourcepub fn to_f32_vec(&self) -> Vec<f32>
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.
Sourcepub fn to_f32_tensor(&self) -> Result<Tensor>
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.
Sourcepub fn as_f32_slice_mut(&mut self) -> Result<&mut [f32]>
pub fn as_f32_slice_mut(&mut self) -> Result<&mut [f32]>
Mutable typed f32 view — fails if buffer is shared or not F32.
Sourcepub fn reshape(&self, new_shape: impl Into<Shape>) -> Result<Tensor>
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.