TensorSnapshot

Struct TensorSnapshot 

Source
pub struct TensorSnapshot {
    pub dtype: DType,
    pub shape: Vec<usize>,
    pub path_stack: Option<Vec<String>>,
    pub container_stack: Option<Vec<String>>,
    pub tensor_id: Option<ParamId>,
    /* private fields */
}
Expand description

A lightweight snapshot of a tensor that can lazily produce TensorData.

TensorSnapshot stores a cloned tensor internally (which is cheap due to reference counting) and only materializes the actual data when to_data() is called. This allows efficient inspection of module structure without the overhead of copying all tensor data.

The dtype and shape are cached for efficient access without requiring data materialization, which is particularly useful for serialization formats that need metadata upfront.

Fields§

§dtype: DType

Data type of the tensor (cached for efficient access)

§shape: Vec<usize>

Shape of the tensor (cached for efficient access)

§path_stack: Option<Vec<String>>

Path stack representing the module hierarchy

§container_stack: Option<Vec<String>>

Container stack representing the container types at each level

§tensor_id: Option<ParamId>

Unique identifier for the tensor parameter

Implementations§

Source§

impl TensorSnapshot

Source

pub fn from_float<B: Backend, const D: usize>( tensor: &Tensor<B, D>, path_stack: Vec<String>, container_stack: Vec<String>, tensor_id: ParamId, ) -> Self

Create a new tensor snapshot from a float tensor

Source

pub fn from_int<B: Backend, const D: usize>( tensor: &Tensor<B, D, Int>, path_stack: Vec<String>, container_stack: Vec<String>, tensor_id: ParamId, ) -> Self

Create a new tensor snapshot from an int tensor

Source

pub fn from_bool<B: Backend, const D: usize>( tensor: &Tensor<B, D, Bool>, path_stack: Vec<String>, container_stack: Vec<String>, tensor_id: ParamId, ) -> Self

Create a new tensor snapshot from a bool tensor

Source

pub fn to_data(&self) -> Result<TensorData, TensorSnapshotError>

Convert to TensorData (this is where actual data copy happens)

Source

pub fn full_path(&self) -> String

Get the full path by joining the path stack

Source

pub fn container_path(&self) -> String

Get the full container path by joining the container stack

Source

pub fn module_type(&self) -> Option<String>

Get the module type (last Struct/Enum in the hierarchy)

Returns the last user-defined module type, skipping primitive containers like “Vec”, “Array”. This is useful for determining which user-defined module a tensor belongs to.

§Examples
  • Linear.weightSome("Struct:Linear")
  • Vec<Linear>[0].weightSome("Struct:Linear")
  • Linear.bias (Optional) → Some("Struct:Linear")
  • Vec<Param>[0] (no module) → None
Source

pub fn container_type(&self) -> String

Get the immediate container type (last in the container stack)

Returns the last element in the container stack, which could be a user-defined type (“Struct:”, “Enum:”) or a collection type (“Vec”, “Array”). This is useful for understanding the full container hierarchy.

§Examples
  • Linear.weight"Struct:Linear"
  • Vec<Linear>[0].weight"Struct:Linear" (the Linear, not the Vec)
  • Vec<Param>[0]"Vec"
Source

pub fn from_closure( data_fn: Rc<dyn Fn() -> Result<TensorData, TensorSnapshotError>>, dtype: DType, shape: Vec<usize>, path_stack: Vec<String>, container_stack: Vec<String>, tensor_id: ParamId, ) -> Self

Create a TensorSnapshot from a closure that produces TensorData This is used internally for lazy loading

Source

pub fn from_data( data: TensorData, path_stack: Vec<String>, container_stack: Vec<String>, tensor_id: ParamId, ) -> Self

Create a TensorSnapshot from TensorData directly

Source

pub fn data_len(&self) -> usize

Get the size of the tensor data in bytes without materializing it.

For regular (non-quantized) types, this is simply shape.product() * dtype.size().

For quantized types (QFloat), this accounts for:

  • The quantized values (packed according to the quantization scheme)
  • Alignment padding (values are aligned to 4-byte boundary)
  • Quantization parameters (scale values appended to the data)
Source

pub fn clone_data_fn( &self, ) -> Rc<dyn Fn() -> Result<TensorData, TensorSnapshotError>>

Clone the data function for lazy composition

Trait Implementations§

Source§

impl Clone for TensorSnapshot

Source§

fn clone(&self) -> Self

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 TensorSnapshot

Source§

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

Formats the value using the given formatter. 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> 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