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 add_bos=False, add_eos=False to SentencePieceTokenizer.__init__() #1811

Merged
merged 4 commits into from
Sep 9, 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
11 changes: 11 additions & 0 deletions keras_nlp/src/tokenizers/sentence_piece_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class SentencePieceTokenizer(tokenizer.Tokenizer):
for more details on the format.
sequence_length: If set, the output will be converted to a dense
tensor and padded/trimmed so all outputs are of `sequence_length`.
add_bos: Add beginning of sentence token to the result.
add_eos: Add end of sentence token to the result. Token is always
truncated if output is longer than specified `sequence_length`.

References:
- [Kudo and Richardson, 2018](https://arxiv.org/abs/1808.06226)
Expand Down Expand Up @@ -116,6 +119,8 @@ def __init__(
proto=None,
sequence_length=None,
dtype="int32",
add_bos=False,
mattdangerw marked this conversation as resolved.
Show resolved Hide resolved
add_eos=False,
**kwargs,
) -> None:
if not is_int_dtype(dtype) and not is_string_dtype(dtype):
Expand All @@ -128,6 +133,8 @@ def __init__(

self.proto = None
self.sequence_length = sequence_length
self.add_bos = add_bos
self.add_eos = add_eos
self.set_proto(proto)
self.file_assets = [VOCAB_FILENAME]

Expand Down Expand Up @@ -172,6 +179,8 @@ def set_proto(self, proto):
self._sentence_piece = tf_text.SentencepieceTokenizer(
model=proto_bytes,
out_type=self.compute_dtype,
add_bos=self.add_bos,
mattdangerw marked this conversation as resolved.
Show resolved Hide resolved
add_eos=self.add_eos,
)
# Keras cannot serialize a bytestring, so we base64 encode the model
# byte array as a string for saving.
Expand Down Expand Up @@ -212,6 +221,8 @@ def get_config(self):
{
"proto": None, # Save vocabulary via an asset!
"sequence_length": self.sequence_length,
"add_bos": self.add_bos,
"add_eos": self.add_eos,
}
)
return config
Expand Down
23 changes: 23 additions & 0 deletions keras_nlp/src/tokenizers/sentence_piece_tokenizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ def test_string_tokenize(self):
[["▁the", "▁quick", "▁brown", "▁fox."]],
)

def test_scalar_bos_eos(self):
input_data = "the quick brown fox."
tokenizer = SentencePieceTokenizer(
proto=self.proto,
add_bos=True,
add_eos=True,
)
output_data = tokenizer(input_data)
self.assertAllEqual(output_data, [1, 6, 5, 3, 4, 2])

def test_string_bos_eos(self):
input_data = ["the quick brown fox."]
tokenizer = SentencePieceTokenizer(
proto=self.proto,
dtype="string",
add_bos=True,
add_eos=True,
)
output_data = tokenizer(input_data)
self.assertAllEqual(
output_data, [["<s>", "▁the", "▁quick", "▁brown", "▁fox.", "</s>"]]
)

def test_detokenize(self):
tokenizer = SentencePieceTokenizer(proto=self.proto)
outputs = tokenizer.detokenize([6, 5, 3, 4])
Expand Down
Loading