Skip to main content

RlxEmbed

Struct RlxEmbed 

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

High-level embedding model — auto-detects BERT / NomicBERT / NomicVision.

Implementations§

Source§

impl RlxEmbed

Source

pub fn from_dir(dir: &Path, pooling: Pooling) -> Result<Self>

Load from a local directory (config.json + model.safetensors) on CPU.

Source

pub fn from_dir_on(dir: &Path, pooling: Pooling, device: Device) -> Result<Self>

Load from a local directory on the given device.

Examples found in repository?
examples/bench_latency.rs (line 20)
9fn main() -> anyhow::Result<()> {
10    let dir = std::env::var("MINILM_DIR").unwrap_or_else(|_| "/tmp/minilm6".into());
11    let devname = std::env::var("DEVICE").unwrap_or_else(|_| "cpu".into());
12    let device = match devname.as_str() {
13        "metal" => Device::Metal,
14        "mlx" => Device::Mlx,
15        "gpu" | "wgpu" => Device::Gpu,
16        "vulkan" => Device::Vulkan,
17        "ane" | "coreml" => Device::Ane,
18        _ => Device::Cpu,
19    };
20    let mut model = RlxEmbed::from_dir_on(Path::new(&dir), Pooling::Mean, device)?;
21    let label = format!("rlx-{devname}");
22    let seq: usize = std::env::var("SEQ")
23        .ok()
24        .and_then(|s| s.parse().ok())
25        .unwrap_or(128);
26    let (runs, warmup) = (50usize, 10usize);
27    println!("framework,batch,p50_ms");
28    for &b in &[1usize, 4, 8, 16, 32] {
29        let n = b * seq;
30        let ids: Vec<f32> = (0..n).map(|i| (i * 131 % 30000) as f32).collect();
31        let mask = vec![1.0f32; n];
32        let tt = vec![0.0f32; n];
33        let pos: Vec<f32> = (0..b).flat_map(|_| (0..seq).map(|i| i as f32)).collect();
34        let inputs = [
35            ("input_ids", ids.as_slice()),
36            ("attention_mask", mask.as_slice()),
37            ("token_type_ids", tt.as_slice()),
38            ("position_ids", pos.as_slice()),
39        ];
40        for _ in 0..warmup {
41            let _ = model.forward(&inputs, b, seq)?;
42        }
43        let mut t: Vec<f64> = Vec::with_capacity(runs);
44        for _ in 0..runs {
45            let s = Instant::now();
46            let _ = model.forward(&inputs, b, seq)?;
47            t.push(s.elapsed().as_secs_f64() * 1e3);
48        }
49        t.sort_by(|a, c| a.partial_cmp(c).unwrap());
50        let p50 = t[t.len() / 2];
51        eprintln!("{label}  b={b:>3}  p50={p50:7.2} ms");
52        println!("{label},{b},{p50:.2}");
53    }
54    Ok(())
55}
Source

pub fn from_weights(path: &Path, pooling: Pooling) -> Result<Self>

Load from a .gguf file or a directory containing one (optional sidecar config.json).

Source

pub fn from_weights_on( path: &Path, pooling: Pooling, device: Device, ) -> Result<Self>

Load weights path on the given device.

Source

pub fn device(&self) -> Device

Execution device for this instance.

Source

pub fn dim(&self) -> usize

Source

pub fn arch(&self) -> Arch

Source

pub fn forward( &mut self, inputs: &[(&str, &[f32])], batch: usize, seq: usize, ) -> Result<Vec<f32>>

Forward on pre-tokenized inputs; returns flattened hidden states.

Examples found in repository?
examples/bench_latency.rs (line 41)
9fn main() -> anyhow::Result<()> {
10    let dir = std::env::var("MINILM_DIR").unwrap_or_else(|_| "/tmp/minilm6".into());
11    let devname = std::env::var("DEVICE").unwrap_or_else(|_| "cpu".into());
12    let device = match devname.as_str() {
13        "metal" => Device::Metal,
14        "mlx" => Device::Mlx,
15        "gpu" | "wgpu" => Device::Gpu,
16        "vulkan" => Device::Vulkan,
17        "ane" | "coreml" => Device::Ane,
18        _ => Device::Cpu,
19    };
20    let mut model = RlxEmbed::from_dir_on(Path::new(&dir), Pooling::Mean, device)?;
21    let label = format!("rlx-{devname}");
22    let seq: usize = std::env::var("SEQ")
23        .ok()
24        .and_then(|s| s.parse().ok())
25        .unwrap_or(128);
26    let (runs, warmup) = (50usize, 10usize);
27    println!("framework,batch,p50_ms");
28    for &b in &[1usize, 4, 8, 16, 32] {
29        let n = b * seq;
30        let ids: Vec<f32> = (0..n).map(|i| (i * 131 % 30000) as f32).collect();
31        let mask = vec![1.0f32; n];
32        let tt = vec![0.0f32; n];
33        let pos: Vec<f32> = (0..b).flat_map(|_| (0..seq).map(|i| i as f32)).collect();
34        let inputs = [
35            ("input_ids", ids.as_slice()),
36            ("attention_mask", mask.as_slice()),
37            ("token_type_ids", tt.as_slice()),
38            ("position_ids", pos.as_slice()),
39        ];
40        for _ in 0..warmup {
41            let _ = model.forward(&inputs, b, seq)?;
42        }
43        let mut t: Vec<f64> = Vec::with_capacity(runs);
44        for _ in 0..runs {
45            let s = Instant::now();
46            let _ = model.forward(&inputs, b, seq)?;
47            t.push(s.elapsed().as_secs_f64() * 1e3);
48        }
49        t.sort_by(|a, c| a.partial_cmp(c).unwrap());
50        let p50 = t[t.len() / 2];
51        eprintln!("{label}  b={b:>3}  p50={p50:7.2} ms");
52        println!("{label},{b},{p50:.2}");
53    }
54    Ok(())
55}

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

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

Source§

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

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

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

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

Source§

fn vzip(self) -> V