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 with a record and/or don’t want to update it during training, you don’t need this type to wrap your tensor.
§Laziness
The initialization of parameters can be lazy when created using uninitialized, which can be done using an initializer.
This reduces the amount of allocations done when loading a model for inference without having to create a custom initialization function only for inference.
§Example
let device = Device::default();
let config = ModuleConfig::default();
let record = Recorder::new().load("/path/to/module", &device);
// No tensor allocation
let module = config.init(device);
// Will use the tensor allocated for the record if the same device is used.
let module = module.load_record(record);
Fields§
§id: ParamId
The 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,
) -> Self
pub fn uninitialized<F>( id: ParamId, init: F, device: T::Device, is_require_grad: bool, ) -> Self
Create a new parameter that is not already initialized.
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)
pub fn consume(self) -> (ParamId, 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 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 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.
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>where
T: UnwindSafe,
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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