Skip to main content

Weights

Struct Weights 

Source
pub struct Weights { /* private fields */ }
Expand description

Named weights, such as the parameters of a Flax model with names like params/Dense_0/kernel (the keys of the parameter tree joined by /).

§Examples

use ruxla_ir::HostArray;
use ruxla_nn::Weights;

let mut w = Weights::default();
w.insert("params/Dense_0/bias", HostArray::from_vec(vec![0.5f32, -1.0], [2]));
let bias = w.array::<f32>("params/Dense_0/bias", &[2]).unwrap();
assert_eq!(bias.to_vec(), [0.5, -1.0]);

Implementations§

Source§

impl Weights

Source

pub fn from_safetensors(bytes: &[u8]) -> Result<Self, WeightsError>

Reads a safetensors file (little-endian, as written by safetensors.numpy.save_file or safetensors.flax.save_file).

§Errors

Returns an error if the data is not valid safetensors or has an element type that ruxla does not have.

Source

pub fn from_flax_msgpack(bytes: &[u8]) -> Result<Self, WeightsError>

Reads weights serialized by Flax (flax.serialization.to_bytes of a parameter tree): nested maps whose leaves are arrays.

§Errors

Returns an error if the data is not valid MessagePack, is not a tree of maps and arrays, or has an element type that ruxla does not have.

Source

pub fn insert(&mut self, name: impl Into<String>, array: HostArray)

Adds or replaces a weight.

Source

pub fn names(&self) -> impl Iterator<Item = &str>

The names of the weights, in sorted order.

Source

pub fn contains(&self, name: &str) -> bool

Whether there is a weight with a name.

Source

pub fn get(&self, name: &str) -> Result<&HostArray, WeightsError>

The weight with a name.

§Errors

Returns an error if there is no such weight.

Source

pub fn array<T: Element>( &self, name: &str, shape: &[usize], ) -> Result<Array<Native, T>, WeightsError>

The weight with a name as a native array of type T and the given shape.

§Errors

Returns an error if there is no such weight or it has another type or shape.

Trait Implementations§

Source§

impl Clone for Weights

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Weights

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Weights

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.