pub struct StateDict { /* private fields */ }Expand description
State dictionary for storing model parameters.
This is similar to PyTorch’s state_dict, mapping parameter names to tensors.
Implementations§
Source§impl StateDict
impl StateDict
Sourcepub fn from_module<M: Module>(module: &M) -> Self
pub fn from_module<M: Module>(module: &M) -> Self
Create a state dictionary from a module.
Sourcepub fn insert(&mut self, name: String, data: TensorData)
pub fn insert(&mut self, name: String, data: TensorData)
Insert a tensor into the state dictionary.
Sourcepub fn insert_entry(&mut self, name: String, entry: StateDictEntry)
pub fn insert_entry(&mut self, name: String, entry: StateDictEntry)
Insert an entry into the state dictionary.
Sourcepub fn get(&self, name: &str) -> Option<&StateDictEntry>
pub fn get(&self, name: &str) -> Option<&StateDictEntry>
Get an entry by name.
Sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut StateDictEntry>
pub fn get_mut(&mut self, name: &str) -> Option<&mut StateDictEntry>
Get a mutable entry by name.
Sourcepub fn entries(&self) -> impl Iterator<Item = (&String, &StateDictEntry)>
pub fn entries(&self) -> impl Iterator<Item = (&String, &StateDictEntry)>
Get all entries.
Sourcepub fn remove(&mut self, name: &str) -> Option<StateDictEntry>
pub fn remove(&mut self, name: &str) -> Option<StateDictEntry>
Remove an entry.
Sourcepub fn filter_prefix(&self, prefix: &str) -> StateDict
pub fn filter_prefix(&self, prefix: &str) -> StateDict
Get a subset of entries matching a prefix.
Sourcepub fn strip_prefix(&self, prefix: &str) -> StateDict
pub fn strip_prefix(&self, prefix: &str) -> StateDict
Strip a prefix from all keys.
Sourcepub fn add_prefix(&self, prefix: &str) -> StateDict
pub fn add_prefix(&self, prefix: &str) -> StateDict
Add a prefix to all keys.
Sourcepub fn set_metadata(&mut self, key: &str, value: &str)
pub fn set_metadata(&mut self, key: &str, value: &str)
Set metadata on the state dictionary.
Sourcepub fn get_metadata(&self, key: &str) -> Option<&String>
pub fn get_metadata(&self, key: &str) -> Option<&String>
Get metadata from the state dictionary.
Sourcepub fn total_params(&self) -> usize
pub fn total_params(&self) -> usize
Get total number of parameters (elements across all tensors).
Sourcepub fn size_bytes(&self) -> usize
pub fn size_bytes(&self) -> usize
Get total size in bytes.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for StateDict
impl<'de> Deserialize<'de> for StateDict
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for StateDict
impl RefUnwindSafe for StateDict
impl Send for StateDict
impl Sync for StateDict
impl Unpin for StateDict
impl UnwindSafe for StateDict
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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