pub struct RlxBertModel { /* private fields */ }Expand description
RLX-compiled BERT model ready for inference.
Implementations§
Source§impl RlxBertModel
impl RlxBertModel
pub fn load_sized( config_path: &Path, weights_path: &str, batch: usize, seq: usize, ) -> Result<Self>
pub fn load_sized_on( config_path: &Path, weights_path: &str, batch: usize, seq: usize, device: Device, ) -> Result<Self>
pub fn load_sized_with_policy( config_path: &Path, weights_path: &str, batch: usize, seq: usize, device: Device, precision: Precision, policy: Option<PrecisionPolicy>, ) -> Result<Self>
Sourcepub fn load(config_path: &Path, weights_path: &str) -> Result<Self>
pub fn load(config_path: &Path, weights_path: &str) -> Result<Self>
Examples found in repository?
examples/semantic_check.rs (lines 27-30)
15fn main() -> anyhow::Result<()> {
16 let dir = std::env::args()
17 .nth(1)
18 .expect("usage: semantic_check <model_dir>");
19 let dir = Path::new(&dir);
20 let pooling = if dir.to_string_lossy().to_lowercase().contains("bge") {
21 Pooling::Cls
22 } else {
23 Pooling::Mean
24 };
25
26 let tok = BertTokenizer::from_dir(dir, 64)?;
27 let mut model = RlxBertModel::load(
28 &dir.join("config.json"),
29 dir.join("model.safetensors").to_str().unwrap(),
30 )?;
31
32 let texts = [
33 "A man is playing a guitar.", // 0
34 "Someone is strumming an acoustic guitar.", // 1 ~ 0
35 "It is freezing outside and snow is falling.", // 2
36 "The weather is cold and it is snowing.", // 3 ~ 2
37 ];
38 let vecs = embed_with_rlx(&mut model, &tok, &texts, pooling)?;
39 println!("dim={} pooling={:?}", vecs[0].len(), pooling);
40
41 println!("\ncosine matrix:");
42 print!(" ");
43 for j in 0..texts.len() {
44 print!(" s{j:<5}");
45 }
46 println!();
47 for i in 0..texts.len() {
48 print!("s{i} ");
49 for j in 0..texts.len() {
50 print!(" {:.3} ", cosine(&vecs[i], &vecs[j]));
51 }
52 println!();
53 }
54
55 let sim_related = (cosine(&vecs[0], &vecs[1]) + cosine(&vecs[2], &vecs[3])) / 2.0;
56 let sim_unrelated = (cosine(&vecs[0], &vecs[2])
57 + cosine(&vecs[0], &vecs[3])
58 + cosine(&vecs[1], &vecs[2])
59 + cosine(&vecs[1], &vecs[3]))
60 / 4.0;
61 println!("\nmean related (0-1, 2-3): {sim_related:.3}");
62 println!("mean unrelated (cross) : {sim_unrelated:.3}");
63 if sim_related > sim_unrelated + 0.05 {
64 println!("PASS: related pairs are clearly more similar than unrelated pairs.");
65 } else {
66 println!("FAIL: semantic ordering not observed.");
67 std::process::exit(1);
68 }
69 Ok(())
70}pub fn recompile(&mut self, batch: usize, seq: usize) -> Result<()>
pub fn forward( &mut self, input_ids: &[f32], attention_mask: &[f32], token_type_ids: &[f32], position_ids: &[f32], ) -> Vec<f32>
Auto Trait Implementations§
impl !RefUnwindSafe for RlxBertModel
impl !Sync for RlxBertModel
impl !UnwindSafe for RlxBertModel
impl Freeze for RlxBertModel
impl Send for RlxBertModel
impl Unpin for RlxBertModel
impl UnsafeUnpin for RlxBertModel
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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