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/extend re replacement seq #948

Merged
merged 15 commits into from
Jun 12, 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
5 changes: 4 additions & 1 deletion outlines/fsm/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,10 @@ def create_fsm_index_end_to_end(


re_llama_byte_token = re.compile(r"^<0x[0-9A-F]{2}>$")
re_replacement_seq = re.compile(r"^▁*�+$")

# The "▁*" prefix is required to handle Gemma and GPT-SW3 tokenizers, and the "\.*"
# suffix is required to handle the NorwAI tokenizer.
re_replacement_seq = re.compile(r"^▁*�+\.*$")


# Copied from transformers.models.gpt2.tokenization_gpt2.bytes_to_unicode
Expand Down
29 changes: 29 additions & 0 deletions tests/fsm/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
reduced_vocabulary,
walk_fsm,
)
from outlines.integrations.utils import adapt_tokenizer
from outlines.models.transformers import TransformerTokenizer


Expand Down Expand Up @@ -686,3 +687,31 @@ def test_numba_leading_null_byte_unicode_type_sane(input_key):
d = numba.typed.typeddict.Dict.empty(numba.types.unicode_type, numba.int64)
d["一"] = 10 # \xe4\xb8\x80
str(d) # assert successfully interprets


@pytest.mark.parametrize(
"rare_token",
[
"�",
"��",
"�.",
"�..",
"▁�",
"▁▁�",
"▁�.",
"▁�.",
"▁▁�..",
],
)
def test_reduced_vocabulary_with_rare_tokens(rare_token):
"""Assert reduced_vocabulary works with rare tokens.

See [1] and [2] for context.

[1]: https://github.com/outlines-dev/outlines/pull/763
[2]: https://github.com/outlines-dev/outlines/pull/948
"""
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
tokenizer = adapt_tokenizer(tokenizer=tokenizer)
tokenizer.vocabulary[rare_token] = max(tokenizer.vocabulary.values()) + 1
reduced_vocabulary(tokenizer)
Loading