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: DTypeData 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
impl TensorSnapshot
Sourcepub fn from_float<B: Backend, const D: usize>(
tensor: &Tensor<B, D>,
path_stack: Vec<String>,
container_stack: Vec<String>,
tensor_id: ParamId,
) -> Self
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
Sourcepub 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
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
Sourcepub 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
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
Sourcepub fn to_data(&self) -> Result<TensorData, TensorSnapshotError>
pub fn to_data(&self) -> Result<TensorData, TensorSnapshotError>
Convert to TensorData (this is where actual data copy happens)
Sourcepub fn container_path(&self) -> String
pub fn container_path(&self) -> String
Get the full container path by joining the container stack
Sourcepub fn module_type(&self) -> Option<String>
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.weight→Some("Struct:Linear")Vec<Linear>[0].weight→Some("Struct:Linear")Linear.bias(Optional) →Some("Struct:Linear")Vec<Param>[0](no module) →None
Sourcepub fn container_type(&self) -> String
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"
Sourcepub 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
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
Sourcepub fn from_data(
data: TensorData,
path_stack: Vec<String>,
container_stack: Vec<String>,
tensor_id: ParamId,
) -> Self
pub fn from_data( data: TensorData, path_stack: Vec<String>, container_stack: Vec<String>, tensor_id: ParamId, ) -> Self
Create a TensorSnapshot from TensorData directly
Sourcepub fn data_len(&self) -> usize
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)
Sourcepub fn clone_data_fn(
&self,
) -> Rc<dyn Fn() -> Result<TensorData, TensorSnapshotError>>
pub fn clone_data_fn( &self, ) -> Rc<dyn Fn() -> Result<TensorData, TensorSnapshotError>>
Clone the data function for lazy composition