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

Updated decode() method in GPT4Tokenizer so that it handles special t… #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 12 additions & 3 deletions minbpe/gpt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,18 @@ def _encode_chunk(self, text_bytes):
return ids

def decode(self, ids):
# we have to un-permute the bytes before we decode
text_bytes = b"".join(self.vocab[idx] for idx in ids)
text_bytes = bytes(self.inverse_byte_shuffle[b] for b in text_bytes)
# given ids (list of integers), return Python string
part_bytes = []
for idx in ids:
if idx in self.vocab:
shuffled_bytes = self.vocab[idx]
unshuffled_bytes = bytes([self.inverse_byte_shuffle[b] for b in shuffled_bytes])
part_bytes.append(unshuffled_bytes)
elif idx in self.inverse_special_tokens:
part_bytes.append(self.inverse_special_tokens[idx].encode("utf-8"))
else:
raise ValueError(f"invalid token id: {idx}")
text_bytes = b"".join(part_bytes)
text = text_bytes.decode("utf-8", errors="replace")
return text

Expand Down