-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte_pair_encoding.rs
More file actions
72 lines (60 loc) · 1.96 KB
/
Copy pathbyte_pair_encoding.rs
File metadata and controls
72 lines (60 loc) · 1.96 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
use std::collections::HashMap;
use std::env;
use std::fs;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
eprintln!("Usage: {} <filename> <vocab_size>", args[0]);
return;
}
let filename = &args[1];
let target_vocab_size: u32 = args[2].parse().expect("Vocab size must be a number");
let text = fs::read_to_string(filename).expect("Could not read file");
let mut tokens: Vec<u32> = Vec::with_capacity(text.len());
for byte in text.bytes() {
tokens.push(byte as u32);
}
let mut current_vocab: u32 = 256;
println!("Starting token count: {}", tokens.len().to_string());
println!("Starting encoding in Rust...");
println!("|----------------------|");
loop {
let mut lookup: HashMap<u64, u32> = HashMap::new();
let mut max_freq: u32 = 0;
let mut max_freq_token: u64 = 0;
for token_index in 0..(tokens.len() - 1) {
let key: u64 = ((tokens[token_index] as u64) << 32) | tokens[token_index+1] as u64;
*lookup.entry(key).or_insert(0) += 1;
}
for (key, count) in &lookup {
if *count > max_freq {
max_freq = *count;
max_freq_token = *key;
}
}
if max_freq < 2 {break};
let new_token_id = current_vocab;
current_vocab += 1;
let tokenA: u32 = (max_freq_token >> 32) as u32;
let tokenB: u32 = (max_freq_token & 0xFFFFFFFF) as u32;
let mut new_tokens: Vec<u32> = Vec::with_capacity(tokens.len());
println!(
"vocab: {:<5} / {} | merging: ({}, {}) -> {} | count: {}",
current_vocab, target_vocab_size, tokenA, tokenB, new_token_id, max_freq
);
let mut i: usize = 0;
while i < tokens.len() {
if i < tokens.len() - 1 && tokens[i] == tokenA && tokens[i+1] == tokenB {
new_tokens.push(new_token_id);
i += 2;
} else {
new_tokens.push(tokens[i]);
i += 1;
}
}
tokens = new_tokens;
if current_vocab > target_vocab_size {break};
}
println!("Encoding Complete.");
println!("Final token count: {}", tokens.len());
}