Repository navigation
Expand file tree
/
Copy pathpython_compare.rs
More file actions
80 lines (72 loc) · 2.97 KB
/
Copy pathpython_compare.rs
File metadata and controls
80 lines (72 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Rust side of the comparison with Python model2vec, driven by
// `scripts/python_compare.py`.
//
// cargo bench --bench python_compare -- corpus=wiki-en-11000000.txt model=minishlab/potion-base-8M \
// reps=5 out=embeddings.f32 quantize=int8 api=flat
//
// Every non-empty line of the corpus is one text, and each timed pass encodes
// all of them with one `encode` call. Prints one `LOAD <seconds>` line, one
// `TEXTS <n>` line and one `SAMPLE <seconds>` line per timed pass. With
// `out=`, writes the embeddings of the warm-up pass as little-endian f32,
// row-major. With `quantize=int8`, quantizes the model to int8 after loading.
// With `api=flat`, encodes with `encode_flat` instead of `encode`.
use std::collections::HashMap;
use std::env;
use std::fs;
use std::hint::black_box;
use std::time::Instant;
use model2vec_rs::model::StaticModel;
fn main() {
let args: HashMap<String, String> = env::args()
.skip(1)
.filter_map(|a| a.split_once('=').map(|(k, v)| (k.to_string(), v.to_string())))
.collect();
let corpus = args
.get("corpus")
.expect("usage: python_compare corpus=<file> [model=..] [reps=N] [out=<file>] [quantize=int8] [api=flat]");
let model_id = args.get("model").map_or("minishlab/potion-base-8M", String::as_str);
let reps: usize = args
.get("reps")
.map_or(5, |r| r.parse().expect("reps must be an integer"));
let text = fs::read_to_string(corpus).unwrap_or_else(|e| panic!("failed to read {corpus}: {e}"));
let texts: Vec<String> = text
.lines()
.filter(|l| !l.trim().is_empty())
.map(String::from)
.collect();
assert!(!texts.is_empty(), "corpus {corpus} has no non-empty lines");
let start = Instant::now();
let mut model = StaticModel::from_pretrained(model_id, None, None, None)
.unwrap_or_else(|e| panic!("failed to load {model_id}: {e}"));
match args.get("quantize").map(String::as_str) {
None => {}
Some("int8") => model = model.quantize_int8(),
Some(other) => panic!("unsupported quantize={other}, expected int8"),
}
println!("LOAD {:.6}", start.elapsed().as_secs_f64());
println!("TEXTS {}", texts.len());
let flat = match args.get("api").map(String::as_str) {
None => false,
Some("flat") => true,
Some(other) => panic!("unsupported api={other}, expected flat"),
};
let warm = if flat {
model.encode_flat(&texts)
} else {
model.encode(&texts).concat()
};
if let Some(out) = args.get("out") {
let bytes: Vec<u8> = warm.iter().flat_map(|x| x.to_le_bytes()).collect();
fs::write(out, bytes).unwrap_or_else(|e| panic!("failed to write {out}: {e}"));
}
drop(warm);
for _ in 0..reps {
let start = Instant::now();
if flat {
black_box(model.encode_flat(&texts));
} else {
black_box(model.encode(&texts));
}
println!("SAMPLE {:.9}", start.elapsed().as_secs_f64());
}
}