Skip to main content

Model

Struct Model 

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

A compiled, runnable model with by-name weight binding. Available with the eval feature. A compiled graph plus its parameter roster — a runnable model.

Bind weights with set / load by name (for an rlx! graph that name is the param’s @ "…" key or, absent that, its binding ident), then run. Weight sources that carry extra tensors (a whole checkpoint) are fine — only the params this model declares are bound, and unbound reports any it still needs.

ⓘ
let mut m = Model::on(graph, Device::Cpu);
m.load(weights.iter().map(|(k, v)| (k.as_str(), v.as_slice())));
assert!(m.unbound().is_empty(), "missing weights: {:?}", m.unbound());
let out = m.run(&[("input_ids", &ids)]);

Implementations§

Source§

impl Model

Source

pub fn new(graph: Graph) -> Self

Compile on the fastest backend this build can run the graph on.

Source

pub fn on(graph: Graph, device: Device) -> Self

Compile on an explicit device.

Source

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

Every parameter name the model expects.

Source

pub fn unbound(&self) -> Vec<&str>

Parameters not yet bound (in arbitrary order) — check this is empty after load to catch a missing / misnamed weight loudly.

Source

pub fn set(&mut self, name: &str, data: &[f32]) -> &mut Self

Bind one parameter by name. A name the model doesn’t declare is ignored (so binding straight from a superset checkpoint is safe).

Source

pub fn load<'a, I>(&mut self, weights: I) -> &mut Self
where I: IntoIterator<Item = (&'a str, &'a [f32])>,

Bind every parameter the model declares from a name → data source (a HashMap, a slice of pairs, …); extra entries are ignored.

Source

pub fn run(&mut self, inputs: &[(&str, &[f32])]) -> Vec<Vec<f32>>

Run with named inputs, returning one Vec<f32> per graph output.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Model

§

impl !Sync for Model

§

impl !UnwindSafe for Model

§

impl Freeze for Model

§

impl Send for Model

§

impl Unpin for Model

§

impl UnsafeUnpin for Model

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
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.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.