Skip to content
Open
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
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,14 @@ impl CoreBPE {
let decoder: HashMap<Rank, Vec<u8>> =
encoder.iter().map(|(k, v)| (*v, k.clone())).collect();

assert!(
encoder.len() == decoder.len(),
"Encoder and decoder must be of equal length. Encoder length: {}, decoder length: {}.\nMaybe you had duplicate token indices in your encoder?",
encoder.len(),
decoder.len()
);
if encoder.len() != decoder.len() {
return Err(format!(
"Encoder and decoder must be of equal length. Encoder length: {}, decoder length: {}.\nMaybe you had duplicate token indices in your encoder?",
encoder.len(),
decoder.len()
)
.into());
}

let special_tokens_decoder: HashMap<Rank, Vec<u8>> = special_tokens_encoder
.iter()
Expand Down
13 changes: 13 additions & 0 deletions tests/test_duplicate_ranks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

import tiktoken


def test_duplicate_mergeable_ranks_raise_value_error():
with pytest.raises(ValueError):
tiktoken.Encoding(
name="duplicate_ranks",
pat_str=r".",
mergeable_ranks={b"a": 0, b"b": 0, b"c": 1},
special_tokens={},
)
14 changes: 14 additions & 0 deletions tests/test_unstable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tiktoken


def test_encode_with_unstable_surrogate_pairs():
enc = tiktoken.Encoding(
name="test",
pat_str=r"(?s:.)",
mergeable_ranks={bytes([i]): i for i in range(256)},
special_tokens={},
)

for text in ["py\ud83d\udc4d", "py\ud83d"]:
stable_tokens, _ = enc.encode_with_unstable(text)
assert stable_tokens == enc.encode(text)[: len(stable_tokens)]
7 changes: 6 additions & 1 deletion tiktoken/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ def encode_with_unstable(
if match := _special_token_regex(disallowed_special).search(text):
raise_disallowed_special_token(match.group())

return self._core_bpe.encode_with_unstable(text, allowed_special)
try:
return self._core_bpe.encode_with_unstable(text, allowed_special)
except UnicodeEncodeError:
# See comment in encode
text = text.encode("utf-16", "surrogatepass").decode("utf-16", "replace")
return self._core_bpe.encode_with_unstable(text, allowed_special)
Comment on lines +247 to +248

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Recheck disallowed specials after surrogate repair

For custom encodings with a non-BMP special token, a surrogate-pair spelling of that token skips the disallowed-special guard: the regex check above runs on the unrepaired string (e.g. "\ud83d\udc4d"), but this retry tokenizes the repaired scalar (e.g. "👍") without running the guard again. In that case encode_with_unstable no longer raises for a default-disallowed special token even though the normal scalar spelling still would; rerun the special-token check after repair before calling into Rust.

Useful? React with 👍 / 👎.


def encode_single_token(self, text_or_bytes: str | bytes) -> int:
"""Encodes text corresponding to a single token to its token value.
Expand Down