pub struct TensorBlock { /* private fields */ }Expand description
A content-addressed tensor block
Combines a regular Block with tensor-specific metadata like shape and dtype.
This allows storing neural network weights, activations, and gradients in a
content-addressed manner.
Implementations§
Source§impl TensorBlock
impl TensorBlock
Sourcepub fn new(data: Bytes, shape: TensorShape, dtype: TensorDtype) -> Result<Self>
pub fn new(data: Bytes, shape: TensorShape, dtype: TensorDtype) -> Result<Self>
Create a new tensor block
§Arguments
data- Raw tensor data (should be in native endian format)shape- Tensor shapedtype- Data type
§Errors
Returns an error if:
- Data size doesn’t match shape * dtype size
- Block creation fails
§Example
use ipfrs_core::tensor::{TensorBlock, TensorDtype, TensorShape};
use bytes::Bytes;
let shape = TensorShape::new(vec![2, 2]);
let data = Bytes::from(vec![1.0f32, 2.0, 3.0, 4.0]
.iter()
.flat_map(|f| f.to_le_bytes())
.collect::<Vec<u8>>());
let tensor = TensorBlock::new(data, shape, TensorDtype::F32).unwrap();
assert_eq!(tensor.element_count(), 4);Sourcepub fn with_metadata(data: Bytes, metadata: TensorMetadata) -> Result<Self>
pub fn with_metadata(data: Bytes, metadata: TensorMetadata) -> Result<Self>
Create a tensor block with metadata
Sourcepub fn metadata(&self) -> &TensorMetadata
pub fn metadata(&self) -> &TensorMetadata
Get tensor metadata
Sourcepub fn shape(&self) -> &TensorShape
pub fn shape(&self) -> &TensorShape
Get the tensor shape
Sourcepub fn dtype(&self) -> TensorDtype
pub fn dtype(&self) -> TensorDtype
Get the data type
Sourcepub fn element_count(&self) -> usize
pub fn element_count(&self) -> usize
Get the number of elements
Sourcepub fn into_parts(self) -> (Block, TensorMetadata)
pub fn into_parts(self) -> (Block, TensorMetadata)
Consume and return the underlying block and metadata
Sourcepub fn reshape(&self, new_shape: TensorShape) -> Result<Self>
pub fn reshape(&self, new_shape: TensorShape) -> Result<Self>
Reshape the tensor to a new shape (must have same element count)
Sourcepub fn size_bytes(&self) -> usize
pub fn size_bytes(&self) -> usize
Get the size in bytes
Source§impl TensorBlock
Utility functions for creating tensors from typed data
impl TensorBlock
Utility functions for creating tensors from typed data
Sourcepub fn from_f32_slice(data: &[f32], shape: TensorShape) -> Result<Self>
pub fn from_f32_slice(data: &[f32], shape: TensorShape) -> Result<Self>
Create a tensor from a slice of f32 values
Sourcepub fn from_f64_slice(data: &[f64], shape: TensorShape) -> Result<Self>
pub fn from_f64_slice(data: &[f64], shape: TensorShape) -> Result<Self>
Create a tensor from a slice of f64 values
Sourcepub fn from_i32_slice(data: &[i32], shape: TensorShape) -> Result<Self>
pub fn from_i32_slice(data: &[i32], shape: TensorShape) -> Result<Self>
Create a tensor from a slice of i32 values
Sourcepub fn from_i64_slice(data: &[i64], shape: TensorShape) -> Result<Self>
pub fn from_i64_slice(data: &[i64], shape: TensorShape) -> Result<Self>
Create a tensor from a slice of i64 values
Sourcepub fn from_u8_slice(data: &[u8], shape: TensorShape) -> Result<Self>
pub fn from_u8_slice(data: &[u8], shape: TensorShape) -> Result<Self>
Create a tensor from a slice of u8 values
Sourcepub fn to_f32_vec(&self) -> Result<Vec<f32>>
pub fn to_f32_vec(&self) -> Result<Vec<f32>>
Convert tensor data to a Vec of f32 values (if dtype is F32)
Sourcepub fn to_f64_vec(&self) -> Result<Vec<f64>>
pub fn to_f64_vec(&self) -> Result<Vec<f64>>
Convert tensor data to a Vec of f64 values (if dtype is F64)
Sourcepub fn to_i32_vec(&self) -> Result<Vec<i32>>
pub fn to_i32_vec(&self) -> Result<Vec<i32>>
Convert tensor data to a Vec of i32 values (if dtype is I32)
Trait Implementations§
Source§impl Clone for TensorBlock
impl Clone for TensorBlock
Source§fn clone(&self) -> TensorBlock
fn clone(&self) -> TensorBlock
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TensorBlock
impl Debug for TensorBlock
Source§impl TensorBlockArrowExt for TensorBlock
impl TensorBlockArrowExt for TensorBlock
Source§fn to_arrow_array(&self) -> Result<ArrayRef>
fn to_arrow_array(&self) -> Result<ArrayRef>
Source§fn to_arrow_field(&self, name: &str) -> Field
fn to_arrow_field(&self, name: &str) -> Field
Source§fn to_arrow_schema(&self, field_name: &str) -> Schema
fn to_arrow_schema(&self, field_name: &str) -> Schema
Auto Trait Implementations§
impl !Freeze for TensorBlock
impl RefUnwindSafe for TensorBlock
impl Send for TensorBlock
impl Sync for TensorBlock
impl Unpin for TensorBlock
impl UnwindSafe for TensorBlock
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