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
15 changes: 14 additions & 1 deletion gemma/gm/text/_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@


_WHITESPACE_CHAR = '▁' # Note this is NOT a undescore (▁ != _)
_ESCAPE_CHAR = '\uE000' # Private Use Area character for escaping special tokens


class _DisplayEnumType(enum.EnumType):
Expand Down Expand Up @@ -213,6 +214,12 @@ def from_version(cls, version: int | str) -> Tokenizer:
else:
raise ValueError(f'Unsupported tokenizer version: {version}')

def escape(self, text: str) -> str:
"""Escapes special sequences in the text so they are tokenized as text."""
text = text.replace('<', f'<{_ESCAPE_CHAR}')
text = text.replace('[', f'[{_ESCAPE_CHAR}')
return text

def encode(
self,
text: str | list[str],
Expand Down Expand Up @@ -242,7 +249,13 @@ def encode(
if isinstance(text, str):
if self.FORMAT_TO_CONVERT:
text = self.FORMAT_TO_CONVERT.from_gemma4(text)
token_ids = self._sp.EncodeAsIds(text)
if _ESCAPE_CHAR in text:
token_ids = []
for part in text.split(_ESCAPE_CHAR):
if part:
token_ids.extend(self._sp.EncodeAsIds(part))
else:
token_ids = self._sp.EncodeAsIds(text)
else:
text = [t.replace(' ', _WHITESPACE_CHAR) for t in text]
if self.FORMAT_TO_CONVERT:
Expand Down
6 changes: 6 additions & 0 deletions test_spm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import urllib.request

def test():
print("Test passed")

test()