Struct tch::nn::VarStore

source ·
pub struct VarStore {
    pub variables_: Arc<Mutex<Variables>>,
    /* private fields */
}
Expand description

A VarStore is used to store variables used by one or multiple layers. It specifies a single device where all variables are stored.

Fields§

§variables_: Arc<Mutex<Variables>>

Implementations§

source§

impl VarStore

source

pub fn read_safetensors<T: AsRef<Path>>(&self, path: T) -> Result<(), TchError>

Read data from safe tensor file, missing tensors will raise a error.

source

pub fn fill_safetensors<P: AsRef<Path>>(&self, path: P) -> Result<(), TchError>

source§

impl VarStore

source

pub fn new(device: Device) -> VarStore

Creates a new var-store located on the specified device.

source

pub fn merge( var_stores: Vec<(VarStore, Option<&str>)> ) -> Result<VarStore, TchError>

source

pub fn device(&self) -> Device

Gets the device for this var-store.

source

pub fn len(&self) -> usize

Returns the number of tensors currently stored on this var-store.

source

pub fn is_empty(&self) -> bool

Returns true if no tensors are currently stored on this var-store.

source

pub fn trainable_variables(&self) -> Vec<Tensor>

Returns all the trainable variables for this var-store.

source

pub fn variables(&self) -> HashMap<String, Tensor>

Returns all variables along with their names.

source

pub fn root(&self) -> Path<'_>

Gets the root path for this variable store.

Variables are named and organized using paths. This function returns the top level path for the var store and can be combined with ‘/’ to create sub-paths.

source

pub fn save<T: AsRef<Path>>(&self, path: T) -> Result<(), TchError>

Saves the var-store variable values to a file.

Weight values for all the tensors currently stored in the var-store are saved in the given file.

source

pub fn save_to_stream<W: Write>(&self, stream: W) -> Result<(), TchError>

Saves the var-store variable values to a stream.

Weight values for all the tensors currently stored in the var-store gets saved in the given stream.

source

pub fn load<T: AsRef<Path>>(&mut self, path: T) -> Result<(), TchError>

Loads the var-store variable values from a file.

Weight values for all the tensors currently stored in the var-store are loaded from the given file. Note that the set of variables stored in the var-store is not changed, only the values for these tensors are modified.

source

pub fn load_from_stream<S: Read + Seek>( &mut self, stream: S ) -> Result<(), TchError>

Loads the var-store variable values from a stream.

Weight values for all the tensors currently stored in the var-store gets loaded from the given stream. Note that the set of variables stored in the var-store is not changed, only the values for these tensors are modified.

source

pub fn load_partial<T: AsRef<Path>>( &mut self, path: T ) -> Result<Vec<String>, TchError>

Loads the var-store variable values from a file if it exists.

Weight values for the tensors currently stored in the var-store and the given file get loaded from the given file. If a variable in the var store is not present in the given file, it is skipped and its values are not updated. This method should be used if pre-trained weight for only parts of the model are available. Note that the set of variables stored in the var-store is not changed, only the values for these tensors are modified.

Returns a String Vector containing the names of missing variables.

source

pub fn freeze(&mut self)

Freezes a var store.

Gradients for the variables in this store are not tracked anymore.

source

pub fn unfreeze(&mut self)

Unfreezes a var store.

Gradients for the variables in this store are tracked again.

source

pub fn set_kind(&mut self, kind: Kind)

Casts all variables in a var store to the target kind .

For floating-point conversion, methods half, bfloat16, float and double should be preferred as they ensure only float-like variables will be converted to the target type.

source

pub fn half(&mut self)

Casts all float-like variable of a var store to half-precision (Half kind).

source

pub fn bfloat16(&mut self)

Casts all float-like variable of a var store to bfloat16-precision (BFloat16 kind).

source

pub fn float(&mut self)

Casts all float-like variable of a var store to single-precision (Float kind).

source

pub fn double(&mut self)

Casts all float-like variable of a var store to single-precision (Double kind).

source

pub fn set_device(&mut self, device: Device)

Migrates a var store and all its tensor to a target device.

source

pub fn copy(&mut self, src: &VarStore) -> Result<(), TchError>

Copies variable values from a source var store to this var store.

All the variables in this var store have to exist with the same name in the source var store, otherwise an error is returned.

Trait Implementations§

source§

impl Debug for VarStore

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V