TensorBlock

Struct TensorBlock 

Source
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

Source

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 shape
  • dtype - 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);
Source

pub fn with_metadata(data: Bytes, metadata: TensorMetadata) -> Result<Self>

Create a tensor block with metadata

Source

pub fn block(&self) -> &Block

Get the underlying block

Source

pub fn metadata(&self) -> &TensorMetadata

Get tensor metadata

Source

pub fn shape(&self) -> &TensorShape

Get the tensor shape

Source

pub fn dtype(&self) -> TensorDtype

Get the data type

Source

pub fn element_count(&self) -> usize

Get the number of elements

Source

pub fn cid(&self) -> &Cid

Get the CID of this tensor

Source

pub fn data(&self) -> &Bytes

Get the raw tensor data

Source

pub fn into_parts(self) -> (Block, TensorMetadata)

Consume and return the underlying block and metadata

Source

pub fn verify(&self) -> Result<bool>

Verify the tensor block integrity

Source

pub fn reshape(&self, new_shape: TensorShape) -> Result<Self>

Reshape the tensor to a new shape (must have same element count)

Source

pub fn size_bytes(&self) -> usize

Get the size in bytes

Source

pub fn is_scalar(&self) -> bool

Check if this is a scalar tensor (0-dimensional)

Source

pub fn is_vector(&self) -> bool

Check if this is a vector (1-dimensional)

Source

pub fn is_matrix(&self) -> bool

Check if this is a matrix (2-dimensional)

Source§

impl TensorBlock

Utility functions for creating tensors from typed data

Source

pub fn from_f32_slice(data: &[f32], shape: TensorShape) -> Result<Self>

Create a tensor from a slice of f32 values

Source

pub fn from_f64_slice(data: &[f64], shape: TensorShape) -> Result<Self>

Create a tensor from a slice of f64 values

Source

pub fn from_i32_slice(data: &[i32], shape: TensorShape) -> Result<Self>

Create a tensor from a slice of i32 values

Source

pub fn from_i64_slice(data: &[i64], shape: TensorShape) -> Result<Self>

Create a tensor from a slice of i64 values

Source

pub fn from_u8_slice(data: &[u8], shape: TensorShape) -> Result<Self>

Create a tensor from a slice of u8 values

Source

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

Convert tensor data to a Vec of f32 values (if dtype is F32)

Source

pub fn to_f64_vec(&self) -> Result<Vec<f64>>

Convert tensor data to a Vec of f64 values (if dtype is F64)

Source

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

Source§

fn clone(&self) -> TensorBlock

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TensorBlock

Source§

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

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

impl TensorBlockArrowExt for TensorBlock

Source§

fn to_arrow_array(&self) -> Result<ArrayRef>

Convert to an Arrow array (zero-copy when possible)
Source§

fn to_arrow_field(&self, name: &str) -> Field

Convert to an Arrow Field (for schema)
Source§

fn to_arrow_schema(&self, field_name: &str) -> Schema

Convert to an Arrow Schema

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,