Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache for Encoding - Runtime Boosted by 12% #319

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,25 @@ impl CoreBPE {
fn _encode_ordinary_native(&self, text: &str) -> Vec<Rank> {
// This is the core of the encoding logic; the other functions in here
// just make things complicated :-)

// The cache is a HashMap, populated first with encoder items, but keys are strings.
// The 0.05 ratio is experimental
let initial_capacity = (text.len() as f64 * 0.05) as usize;
let mut cache: HashMap<&str, Vec<Rank>> = HashMap::with_capacity_and_hasher(initial_capacity, Default::default());

for (k, &v) in self.encoder.iter() {
if let Ok(key_str) = std::str::from_utf8(k) {
cache.insert(key_str, vec![v]);
}
}

//lookup piece in cache, and add it if it is a nonekey
let regex = self._get_tl_regex();
let mut ret = vec![];
for mat in regex.find_iter(text) {
let piece = mat.unwrap().as_str().as_bytes();
match self.encoder.get(piece) {
Some(token) => ret.push(*token),
None => ret.extend(&byte_pair_encode(piece, &self.encoder)),
}
let piece = mat.unwrap().as_str();
let encoded_tokens = cache.entry(piece).or_insert_with(|| byte_pair_encode(&piece.as_bytes(), &self.encoder));
ret.extend(&*encoded_tokens);
}
ret
}
Expand Down