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 container_type(&self) -> String
pub fn container_type(&self) -> String
Get the immediate container type (last in the container stack)
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
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