pub struct Param<T: Parameter> {
pub id: ParamId,
/* private fields */
}Expand description
Parameters are the fundamental building blocks of modules where they serve as containers for tensors that can be updated during training, and loaded during inference. If you don’t want to save the tensors and/or don’t want to update it during training, you don’t need this type to wrap your tensor.
§Core Lazy Initialization Architecture
Param<T> has a dual-state design using OnceCell<T>:
§State Management
Two possible states:
- Initialized:
state: OnceCell<T>contains value,initialization: None - Uninitialized (Lazy):
stateis empty,initialization: Some(RwLock<Option<Uninitialized<T>>>)
Fields§
§id: ParamIdThe unique ID of this parameter. This is used by eg. optimizers to associate a gradient with a specific parameter.
Implementations§
Source§impl<T: Parameter> Param<T>
impl<T: Parameter> Param<T>
Sourcepub fn initialized(id: ParamId, value: T) -> Self
pub fn initialized(id: ParamId, value: T) -> Self
Create a new parameter that is already initialized.
Sourcepub fn uninitialized<F>(
id: ParamId,
init: F,
device: T::Device,
is_require_grad: bool,
shape: Shape,
) -> Self
pub fn uninitialized<F>( id: ParamId, init: F, device: T::Device, is_require_grad: bool, shape: Shape, ) -> Self
Create a new parameter that is not already initialized.
Sourcepub fn val(&self) -> T
pub fn val(&self) -> T
Gets the parameter value, initializing it lazily if needed.
For initialized parameters, this returns a clone of the cached value. For uninitialized parameters, this triggers initialization:
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Check if the parameter has been initialized.
Returns true if the parameter’s value has been computed and cached,
false if it’s still lazy and will be initialized on first access.
Sourcepub fn into_value(self) -> T
pub fn into_value(self) -> T
Gets the parameter’s value while consuming the parameter.
Sourcepub fn consume(self) -> (ParamId, T, ParamMapper<T>)
pub fn consume(self) -> (ParamId, T, ParamMapper<T>)
Gets the parameter id and value while consuming the parameter.
Sourcepub fn map<F: FnOnce(T) -> T>(self, func: F) -> Self
pub fn map<F: FnOnce(T) -> T>(self, func: F) -> Self
Execute the given function on the inner value.
Sourcepub fn from_mapped_value(
id: ParamId,
value: T,
param_mapper: ParamMapper<T>,
) -> Self
pub fn from_mapped_value( id: ParamId, value: T, param_mapper: ParamMapper<T>, ) -> Self
Create an initialized parameter with the given id, value, and param mapper.
This is a helper method for creating parameters while preserving the param mapper, typically used in ModuleMapper implementations.
Sourcepub fn load_mapper<F: Fn(T) -> T + Send + Sync + 'static>(self, func: F) -> Self
pub fn load_mapper<F: Fn(T) -> T + Send + Sync + 'static>(self, func: F) -> Self
Runs a transformation on the parameter when loading.
Sourcepub fn save_mapper<F: Fn(T) -> T + Send + Sync + 'static>(self, func: F) -> Self
pub fn save_mapper<F: Fn(T) -> T + Send + Sync + 'static>(self, func: F) -> Self
Runs a transformation on the parameter when saving.
Sourcepub fn init_mapper<F: FnOnce(T) -> T + Send + 'static>(self, func: F) -> Selfwhere
T: 'static,
pub fn init_mapper<F: FnOnce(T) -> T + Send + 'static>(self, func: F) -> Selfwhere
T: 'static,
Execute the given function on the inner value.
Sourcepub fn lazy_device(&self) -> T::Device
pub fn lazy_device(&self) -> T::Device
The device on which the parameter is or will be initialized, without triggering initialization.
This is critical for the load optimization: when loading tensors into an uninitialized parameter, we need to know the target device to move the loaded tensor appropriately, but we don’t want to trigger the initialization function (which would allocate an unnecessary tensor).
Use this instead of crate::tensor::Tensor::device when you need the device but want to preserve lazy initialization.
Sourcepub fn set_require_grad(self, require_grad: bool) -> Self
pub fn set_require_grad(self, require_grad: bool) -> Self
Override the gradient requirement for the current parameter.
Source§impl<B: Backend, const D: usize> Param<Tensor<B, D>>
impl<B: Backend, const D: usize> Param<Tensor<B, D>>
Sourcepub fn from_tensor(value: Tensor<B, D>) -> Self
pub fn from_tensor(value: Tensor<B, D>) -> Self
Create a new parameter from a float tensor.
§Warnings
We strongly recommend using Param::uninitialized if you are using this method to initialize parameters inside a module, since the tensor initialization will be lazy, making the loading of weights more performant.
Sourcepub fn lazy_shape(&self) -> Shape
pub fn lazy_shape(&self) -> Shape
The shape of the parameter, without triggering initialization.
This is critical for shape validation during loading: when applying tensors to an uninitialized parameter, we need to validate the shape without triggering the initialization function (which would allocate an unnecessary tensor).
Use this instead of crate::tensor::Tensor::shape when you need the shape but want to preserve lazy initialization.
Sourcepub fn from_data<T>(data: T, device: &B::Device) -> Selfwhere
T: Into<TensorData>,
pub fn from_data<T>(data: T, device: &B::Device) -> Selfwhere
T: Into<TensorData>,
Create a new parameter from data.
Sourcepub fn transform_for_load(self, tensor: Tensor<B, D>, param_id: ParamId) -> Self
pub fn transform_for_load(self, tensor: Tensor<B, D>, param_id: ParamId) -> Self
Transform a parameter for loading by applying load transformations.
This method is used to restore a parameter from a tensor (typically during deserialization).
It ensures the tensor is moved to the expected device, applies the param mapper’s
on_load transformation, and preserves the autodiff settings (require_grad).
Sourcepub fn transform_for_save(&self) -> Self
pub fn transform_for_save(&self) -> Self
Transform a parameter for saving by applying save transformations.
This method is used to prepare a parameter for saving (typically during serialization).
It applies the param mapper’s on_save transformation, which can be used
to modify the tensor before serialization (e.g., quantization, precision conversion).
Source§impl<B: Backend, const D: usize> Param<Tensor<B, D, Int>>
impl<B: Backend, const D: usize> Param<Tensor<B, D, Int>>
Sourcepub fn lazy_shape(&self) -> Shape
pub fn lazy_shape(&self) -> Shape
The shape of the parameter, without triggering initialization.
This is critical for shape validation during loading: when applying tensors to an uninitialized parameter, we need to validate the shape without triggering the initialization function (which would allocate an unnecessary tensor).
Use this instead of crate::tensor::Tensor::shape when you need the shape but want to preserve lazy initialization.
Sourcepub fn transform_for_load(
self,
tensor: Tensor<B, D, Int>,
param_id: ParamId,
) -> Self
pub fn transform_for_load( self, tensor: Tensor<B, D, Int>, param_id: ParamId, ) -> Self
Transform a parameter for loading by applying load transformations.
This method is used to restore a parameter from a tensor (typically during deserialization).
It ensures the tensor is moved to the expected device and applies the param mapper’s
on_load transformation.
Sourcepub fn transform_for_save(&self) -> Self
pub fn transform_for_save(&self) -> Self
Transform a parameter for saving by applying save transformations.
This method is used to prepare a parameter for saving (typically during serialization).
It applies the param mapper’s on_save transformation, which can be used
to modify the tensor before serialization (e.g., quantization, precision conversion).
Source§impl<B: Backend, const D: usize> Param<Tensor<B, D, Bool>>
impl<B: Backend, const D: usize> Param<Tensor<B, D, Bool>>
Sourcepub fn lazy_shape(&self) -> Shape
pub fn lazy_shape(&self) -> Shape
The shape of the parameter, without triggering initialization.
This is critical for shape validation during loading: when applying tensors to an uninitialized parameter, we need to validate the shape without triggering the initialization function (which would allocate an unnecessary tensor).
Returns:
- For uninitialized params: the shape from the
Uninitializedstruct - For initialized params: the actual shape from the tensor
Use this instead of crate::tensor::Tensor::shape when you need the shape but want to preserve lazy initialization.
Sourcepub fn transform_for_load(
self,
tensor: Tensor<B, D, Bool>,
param_id: ParamId,
) -> Self
pub fn transform_for_load( self, tensor: Tensor<B, D, Bool>, param_id: ParamId, ) -> Self
Transform a parameter for loading by applying load transformations.
This method is used to restore a parameter from a tensor (typically during deserialization).
It ensures the tensor is moved to the expected device and applies the param mapper’s
on_load transformation.
Sourcepub fn transform_for_save(&self) -> Self
pub fn transform_for_save(&self) -> Self
Transform a parameter for saving by applying save transformations.
This method is used to prepare a parameter for saving (typically during serialization).
It applies the param mapper’s on_save transformation, which can be used
to modify the tensor before serialization (e.g., quantization, precision conversion).
Trait Implementations§
Source§impl<const D: usize, B: AutodiffBackend> AutodiffModule<B> for Param<Tensor<B, D>>
impl<const D: usize, B: AutodiffBackend> AutodiffModule<B> for Param<Tensor<B, D>>
Source§type InnerModule = Param<Tensor<<B as AutodiffBackend>::InnerBackend, D>>
type InnerModule = Param<Tensor<<B as AutodiffBackend>::InnerBackend, D>>
Source§fn valid(&self) -> Self::InnerModule
fn valid(&self) -> Self::InnerModule
Source§impl<const D: usize, B: AutodiffBackend> AutodiffModule<B> for Param<Tensor<B, D, Bool>>
impl<const D: usize, B: AutodiffBackend> AutodiffModule<B> for Param<Tensor<B, D, Bool>>
Source§type InnerModule = Param<Tensor<<B as AutodiffBackend>::InnerBackend, D, Bool>>
type InnerModule = Param<Tensor<<B as AutodiffBackend>::InnerBackend, D, Bool>>
Source§fn valid(&self) -> Self::InnerModule
fn valid(&self) -> Self::InnerModule
Source§impl<const D: usize, B: AutodiffBackend> AutodiffModule<B> for Param<Tensor<B, D, Int>>
impl<const D: usize, B: AutodiffBackend> AutodiffModule<B> for Param<Tensor<B, D, Int>>
Source§type InnerModule = Param<Tensor<<B as AutodiffBackend>::InnerBackend, D, Int>>
type InnerModule = Param<Tensor<<B as AutodiffBackend>::InnerBackend, D, Int>>
Source§fn valid(&self) -> Self::InnerModule
fn valid(&self) -> Self::InnerModule
Source§impl<const D: usize, B: Backend> Module<B> for Param<Tensor<B, D>>
impl<const D: usize, B: Backend> Module<B> for Param<Tensor<B, D>>
Source§fn visit<V: ModuleVisitor<B>>(&self, visitor: &mut V)
fn visit<V: ModuleVisitor<B>>(&self, visitor: &mut V)
Source§fn map<M: ModuleMapper<B>>(self, mapper: &mut M) -> Self
fn map<M: ModuleMapper<B>>(self, mapper: &mut M) -> Self
Source§fn into_record(self) -> Self::Record
fn into_record(self) -> Self::Record
Source§fn load_record(self, record: Self::Record) -> Self
fn load_record(self, record: Self::Record) -> Self
Source§fn to_device(self, device: &Device<B>) -> Self
fn to_device(self, device: &Device<B>) -> Self
Source§fn fork(self, device: &Device<B>) -> Self
fn fork(self, device: &Device<B>) -> Self
Source§fn collect_devices(&self, devices: Vec<Device<B>>) -> Vec<Device<B>> ⓘ
fn collect_devices(&self, devices: Vec<Device<B>>) -> Vec<Device<B>> ⓘ
Source§fn devices(&self) -> Devices<B>
fn devices(&self) -> Devices<B>
Source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
Source§fn save_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
) -> Result<(), RecorderError>
fn save_file<FR, PB>( self, file_path: PB, recorder: &FR, ) -> Result<(), RecorderError>
std only.Source§fn load_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
device: &B::Device,
) -> Result<Self, RecorderError>
fn load_file<FR, PB>( self, file_path: PB, recorder: &FR, device: &B::Device, ) -> Result<Self, RecorderError>
std only.Source§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
Source§impl<const D: usize, B: Backend> Module<B> for Param<Tensor<B, D, Bool>>
impl<const D: usize, B: Backend> Module<B> for Param<Tensor<B, D, Bool>>
Source§fn visit<V: ModuleVisitor<B>>(&self, visitor: &mut V)
fn visit<V: ModuleVisitor<B>>(&self, visitor: &mut V)
Source§fn map<M: ModuleMapper<B>>(self, mapper: &mut M) -> Self
fn map<M: ModuleMapper<B>>(self, mapper: &mut M) -> Self
Source§fn into_record(self) -> Self::Record
fn into_record(self) -> Self::Record
Source§fn load_record(self, record: Self::Record) -> Self
fn load_record(self, record: Self::Record) -> Self
Source§fn to_device(self, device: &Device<B>) -> Self
fn to_device(self, device: &Device<B>) -> Self
Source§fn fork(self, device: &Device<B>) -> Self
fn fork(self, device: &Device<B>) -> Self
Source§fn collect_devices(&self, devices: Vec<Device<B>>) -> Vec<Device<B>> ⓘ
fn collect_devices(&self, devices: Vec<Device<B>>) -> Vec<Device<B>> ⓘ
Source§fn devices(&self) -> Devices<B>
fn devices(&self) -> Devices<B>
Source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
Source§fn save_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
) -> Result<(), RecorderError>
fn save_file<FR, PB>( self, file_path: PB, recorder: &FR, ) -> Result<(), RecorderError>
std only.Source§fn load_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
device: &B::Device,
) -> Result<Self, RecorderError>
fn load_file<FR, PB>( self, file_path: PB, recorder: &FR, device: &B::Device, ) -> Result<Self, RecorderError>
std only.Source§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
Source§impl<const D: usize, B: Backend> Module<B> for Param<Tensor<B, D, Int>>
impl<const D: usize, B: Backend> Module<B> for Param<Tensor<B, D, Int>>
Source§fn visit<V: ModuleVisitor<B>>(&self, visitor: &mut V)
fn visit<V: ModuleVisitor<B>>(&self, visitor: &mut V)
Source§fn map<M: ModuleMapper<B>>(self, mapper: &mut M) -> Self
fn map<M: ModuleMapper<B>>(self, mapper: &mut M) -> Self
Source§fn into_record(self) -> Self::Record
fn into_record(self) -> Self::Record
Source§fn load_record(self, record: Self::Record) -> Self
fn load_record(self, record: Self::Record) -> Self
Source§fn to_device(self, device: &Device<B>) -> Self
fn to_device(self, device: &Device<B>) -> Self
Source§fn fork(self, device: &Device<B>) -> Self
fn fork(self, device: &Device<B>) -> Self
Source§fn collect_devices(&self, devices: Vec<Device<B>>) -> Vec<Device<B>> ⓘ
fn collect_devices(&self, devices: Vec<Device<B>>) -> Vec<Device<B>> ⓘ
Source§fn devices(&self) -> Devices<B>
fn devices(&self) -> Devices<B>
Source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
Source§fn save_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
) -> Result<(), RecorderError>
fn save_file<FR, PB>( self, file_path: PB, recorder: &FR, ) -> Result<(), RecorderError>
std only.Source§fn load_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
device: &B::Device,
) -> Result<Self, RecorderError>
fn load_file<FR, PB>( self, file_path: PB, recorder: &FR, device: &B::Device, ) -> Result<Self, RecorderError>
std only.Source§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
Source§impl<const D: usize, B: Backend> ModuleDisplay for Param<Tensor<B, D>>
impl<const D: usize, B: Backend> ModuleDisplay for Param<Tensor<B, D>>
Source§fn format(&self, passed_settings: DisplaySettings) -> String
fn format(&self, passed_settings: DisplaySettings) -> String
Source§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
Source§impl<const D: usize, B: Backend> ModuleDisplay for Param<Tensor<B, D, Bool>>
impl<const D: usize, B: Backend> ModuleDisplay for Param<Tensor<B, D, Bool>>
Source§fn format(&self, passed_settings: DisplaySettings) -> String
fn format(&self, passed_settings: DisplaySettings) -> String
Source§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
Source§impl<const D: usize, B: Backend> ModuleDisplay for Param<Tensor<B, D, Int>>
impl<const D: usize, B: Backend> ModuleDisplay for Param<Tensor<B, D, Int>>
Source§fn format(&self, passed_settings: DisplaySettings) -> String
fn format(&self, passed_settings: DisplaySettings) -> String
Source§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
Source§impl<B, const D: usize> Record<B> for Param<Tensor<B, D>>where
B: Backend,
impl<B, const D: usize> Record<B> for Param<Tensor<B, D>>where
B: Backend,
Source§type Item<S: PrecisionSettings> = ParamSerde<FloatTensorSerde<S>>
type Item<S: PrecisionSettings> = ParamSerde<FloatTensorSerde<S>>
Source§impl<B, const D: usize> Record<B> for Param<Tensor<B, D, Bool>>where
B: Backend,
impl<B, const D: usize> Record<B> for Param<Tensor<B, D, Bool>>where
B: Backend,
Source§type Item<S: PrecisionSettings> = ParamSerde<BoolTensorSerde>
type Item<S: PrecisionSettings> = ParamSerde<BoolTensorSerde>
Source§impl<B, const D: usize> Record<B> for Param<Tensor<B, D, Int>>where
B: Backend,
impl<B, const D: usize> Record<B> for Param<Tensor<B, D, Int>>where
B: Backend,
Source§type Item<S: PrecisionSettings> = ParamSerde<IntTensorSerde<S>>
type Item<S: PrecisionSettings> = ParamSerde<IntTensorSerde<S>>
Auto Trait Implementations§
impl<T> !Freeze for Param<T>
impl<T> !RefUnwindSafe for Param<T>
impl<T> Send for Param<T>
impl<T> !Sync for Param<T>
impl<T> Unpin for Param<T>
impl<T> !UnwindSafe for Param<T>
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