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

Computing LSTM forward layer without allocating #3351

Merged
merged 11 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 12 additions & 17 deletions components/segmenter/src/complex/lstm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,19 @@ impl<'l> LstmSegmenter<'l> {
return self.dic.len() as u16;
};

// The maximum UTF-8 size of a grapheme cluster seems to be 41 bytes
let mut i = 0;
let mut buf = [0; 41];

#[allow(clippy::unwrap_used)]
// debug_asserting whether my assumption is correct
decode_utf16(grapheme_cluster.iter().copied()).for_each(|c| {
debug_assert!(i < 37);
i += c
.unwrap_or(REPLACEMENT_CHARACTER)
.encode_utf8(&mut buf[i..])
.len()
});

#[allow(clippy::unwrap_used)]
// debug_asserting whether my assumption is correct
self.dic
.get_copied(UnvalidatedStr::from_bytes(&buf[..i]))
.get_copied_by(|key| {
key.as_bytes().iter().copied().cmp(
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion (optional): UTF-8 byte order is equivalent to UTF-32 order; it may be cleaner/smaller/faster code if you compared an iterator of char rather than an iterator of u8.

Separately, I'm starting to get a bit worried that the grapheme cluster code may bloat the code size of the LSTM segmenter. We should probably delete it at some point. Maybe as part of the next round of ML model upgrades.

Copy link
Member Author

Choose a reason for hiding this comment

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

The keys are UnvalidatedStrs aka [u8], we cannot use them as anything that's strongly typed.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't like deps

Copy link
Contributor

Choose a reason for hiding this comment

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

Segmenter already depends on utf8_iter.

utf8_iter = "1.0.3"

decode_utf16(grapheme_cluster.iter().copied()).flat_map(|c| {
let mut buf = [0; 4];
let len = c
.unwrap_or(REPLACEMENT_CHARACTER)
.encode_utf8(&mut buf)
.len();
buf.into_iter().take(len)
}),
)
})
.unwrap_or_else(|| self.dic.len() as u16)
})
.collect()
Expand Down
15 changes: 9 additions & 6 deletions provider/datagen/src/transform/segmenter/lstm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ mod tests {

#[test]
fn thai_word_break_with_grapheme_model() {
const TEST_STR: &str = "ภาษาไทยภาษาไทย";
let provider = crate::DatagenProvider::for_test();
let raw_data = provider
.source
Expand All @@ -249,12 +248,16 @@ mod tests {
),
provider,
);

let segmenter = LineSegmenter::try_new_lstm_with_any_provider(&provider).unwrap();

const TEST_STR: &str = "ภาษาไทยภาษาไทย";
let utf16: Vec<u16> = TEST_STR.encode_utf16().collect();

let breaks: Vec<usize> = segmenter.segment_str(TEST_STR).collect();
assert_eq!(
breaks,
[0, 6, 12, 21, 27, 33, TEST_STR.len()],
"Thai test with grapheme model"
);
assert_eq!(breaks, [0, 6, 12, 21, 27, 33, TEST_STR.len()],);

let breaks: Vec<usize> = segmenter.segment_utf16(&utf16).collect();
assert_eq!(breaks, [0, 2, 4, 7, 9, 11, utf16.len()],);
}
}
6 changes: 6 additions & 0 deletions utils/zerovec/src/map/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ where
self.values.get(index)
}

/// For cases when `V` is fixed-size, obtain a direct copy of `V` instead of `V::ULE`
pub fn get_copied_by(&self, predicate: impl FnMut(&K) -> Ordering) -> Option<V> {
let index = self.keys.zvl_binary_search_by(predicate).ok()?;
self.values.get(index)
}

/// Similar to [`Self::iter()`] except it returns a direct copy of the values instead of references
/// to `V::ULE`, in cases when `V` is fixed-size
pub fn iter_copied_values<'b>(
Expand Down