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

fix: ngram_bench #49

Merged
merged 2 commits into from
May 27, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- `ngram` bench use counter lib's function rather than the truly used function.
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure grammatical correctness in the changelog entry.

- `ngram` bench use counter lib's function rather than the truly used function.
+ `ngram` bench uses counter lib's function rather than the truly used function.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
### Fixed
- `ngram` bench use counter lib's function rather than the truly used function.
### Fixed
- `ngram` bench uses counter lib's function rather than the truly used function.

Address formatting issues to improve readability.

+ 
### Fixed
- `ngram` bench uses counter lib's function rather than the truly used function.
+ 
### Changed
- Use AHash in ngram module
+ 
## [0.1.2] - 2024-04-29
+ 
### Changed
- Implement ngram counts with a better way (#33)
- Rust `compute_score` function signature changed to use references (#34)
- Use rayon to run every prediction-references statistics calculation parallely (#38)
- Use ahash for better performance (#39)
+ 
## [0.1.1] - 2024-04-26
+ 
### Changed
- Upgrade `cached` version to `0.50.0`
Change crate-type to `["cdylib", "rlib"]`
+ 
## [0.1.0] - 2024-04-23
+ 
- BLEU score calculation with `Tokenizer13a` as default tokenizer.

Also applies to: 13-14, 18-20, 25-27


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
### Fixed
- `ngram` bench use counter lib's function rather than the truly used function.
### Fixed
- `ngram` bench uses counter lib's function rather than the truly used function.
### Changed
- Use AHash in ngram module
## [0.1.2] - 2024-04-29
### Changed
- Implement ngram counts with a better way (#33)
- Rust `compute_score` function signature changed to use references (#34)
- Use rayon to run every prediction-references statistics calculation parallely (#38)
- Use ahash for better performance (#39)
## [0.1.1] - 2024-04-26
### Changed
- Upgrade `cached` version to `0.50.0`
Change crate-type to `["cdylib", "rlib"]`
## [0.1.0] - 2024-04-23
- BLEU score calculation with `Tokenizer13a` as default tokenizer.


### Changed
- Use AHash in ngram module
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ name = "bleuscore"
crate-type = ["cdylib", "rlib"]

[dependencies]
cached = "0.50.0"
cached = "0.51.3"
regex = "1.10.4"
lazy_static = "1.4.0"
counter = "0.5.7"
rayon = "1.10.0"
ahash = "0.8.11"

Expand Down
54 changes: 9 additions & 45 deletions src/ngram.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ahash::AHashMap;
use counter::Counter;

/// Here the tokens' type is `&[String]` rather than `&Vec<String>`
/// to fix `clippy::not_unsafe_ptr_arg_deref` error.
Expand All @@ -17,24 +16,9 @@ pub fn get_token_ngram_counter(tokens: &[String], max_order: usize) -> AHashMap<
count_map
}

/// TODO: change to use Counter to count ngram
#[allow(dead_code)]
fn get_ngram_counter(line: &str, max_order: usize) -> Counter<&str> {
let mut counts: Counter<&str> = Counter::new();
for order in 1..=max_order {
for start_index in 0..(line.len().saturating_sub(order - 1)) {
// println!("line: {}, start_index: {}, order: {}", line, start_index, order);
let ngram = &line[start_index..(start_index + order)];
// println!("ngram: {}", ngram);
counts[&ngram] += 1;
}
}
counts
}

#[cfg(test)]
mod test {
use crate::ngram::{get_ngram_counter, get_token_ngram_counter};
use crate::ngram::get_token_ngram_counter;

#[test]
fn test_get_token_ngram_short() {
Expand Down Expand Up @@ -69,47 +53,27 @@ mod test {

assert_eq!(counter.len(), 9);
}

#[test]
fn test_get_ngram_short() {
let counter = get_ngram_counter("ab", 4);
assert_eq!(counter[&"a"], 1);
assert_eq!(counter[&"b"], 1);
assert_eq!(counter[&"ab"], 1);
}

#[test]
fn test_get_ngram_long() {
let counter = get_ngram_counter("aabc", 4);
assert_eq!(counter[&"a"], 2);
assert_eq!(counter[&"b"], 1);
assert_eq!(counter[&"c"], 1);
assert_eq!(counter[&"d"], 0);

assert_eq!(counter[&"aa"], 1);
assert_eq!(counter[&"ab"], 1);
assert_eq!(counter[&"bc"], 1);
assert_eq!(counter[&"ac"], 0);

assert_eq!(counter[&"aab"], 1);
assert_eq!(counter[&"aabc"], 1);
}
}

#[cfg(test)]
mod benchmark {
use crate::ngram::get_ngram_counter;
use crate::ngram::get_token_ngram_counter;
use test::Bencher;

#[bench]
fn bench_ngram(b: &mut Bencher) {
let line = "aabc";
let tokens: Vec<String> = vec![
"a".to_string(),
"a".to_string(),
"b".to_string(),
"c".to_string(),
];
let max_order = 4;

let iter_num: usize = 100;
b.iter(|| {
std::hint::black_box(for _ in 1..=iter_num {
get_ngram_counter(line, max_order);
get_token_ngram_counter(&tokens, max_order);
Comment on lines +65 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize the benchmark setup.

Consider pre-allocating the tokens vector outside the benchmark loop to avoid repeated allocations during the benchmarking process.

let tokens: Vec<String> = vec![
    "a".to_string(),
    "a".to_string(),
    "b".to_string(),
    "c".to_string(),
];
let max_order = 4;
let iter_num: usize = 100;
b.iter(|| {
    std::hint::black_box(for _ in 1..=iter_num {
        get_token_ngram_counter(&tokens, max_order);
    });
});

});
});
}
Expand Down
Loading