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

add replace spaces flag #9

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn cl100k_base_benchmark(c: &mut Criterion) {
b.iter(|| {
black_box(x.estimate_num_tokens_no_special_tokens_fast(
&t,
false
));
});
});
Expand Down
12 changes: 11 additions & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,17 @@ impl Encoding {
self.core_bpe.encode_ordinary(text)
}

pub fn estimate_num_tokens_no_special_tokens_fast(&self, text: &str) -> usize {
pub fn estimate_num_tokens_no_special_tokens_fast(&self, text: &str, replace_spaces_with_lower_one_eighth_block: bool) -> usize {
let tokens = if replace_spaces_with_lower_one_eighth_block {
self.count_tokens(&text.replace(" ", "\u{2581}"))
} else {
self.count_tokens(text)
};

tokens
}

fn count_tokens(&self, text: &str) -> usize {
let mut token_count = 0;
let mut current_token = Vec::new();
let mut current_token_hash: i64 = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn estimation_is_close() {
.unwrap()
.len();

let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(file);
let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(file, false);

println!("Real count: {}", real_count);
println!("Estimated count: {}", estimated_count);
Expand All @@ -229,7 +229,7 @@ fn simple_estimation_is_close() {
.unwrap()
.len();

let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(&test);
let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(&test, false);

println!("Real count: {}", real_count);
println!("Estimated count: {}", estimated_count);
Expand Down Expand Up @@ -290,7 +290,7 @@ fn estimation_is_close_o200k() {
.unwrap()
.len();

let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(file);
let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(file, false);

println!("Real count: {}", real_count);
println!("Estimated count: {}", estimated_count);
Expand All @@ -314,7 +314,7 @@ fn simple_estimation_is_close_o200k() {
.unwrap()
.len();

let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(&test);
let estimated_count = enc.estimate_num_tokens_no_special_tokens_fast(&test, false);

println!("Real count: {}", real_count);
println!("Estimated count: {}", estimated_count);
Expand Down
Loading