Skip to content

BOS and EOS #195

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

Merged
merged 5 commits into from
Jul 26, 2023
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignore __pycache__ folder
__pycache__/
19 changes: 17 additions & 2 deletions tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ def __init__(self, tokenizer_model_path):
self.pad_token_id = 0 # self.tokenizer.pad_id()
self.newline_token_id = 13


# Encode string

def encode(self, text, return_mask = False, max_seq_len = 2048):
def encode(self, text, return_mask = False, max_seq_len = 2048, add_bos = False, add_eos = False):

if isinstance(text, list):

# text is a list of strings

list_ids = self.tokenizer.EncodeAsIds(text)

# pad bos and eos

if add_bos:
for ids in list_ids: ids.insert(0, self.bos_token_id)
if add_eos:
for ids in list_ids: ids.append(self.eos_token_id)

max_length = max([len(ids) for ids in list_ids])

needs_mask = False
Expand Down Expand Up @@ -56,6 +63,14 @@ def encode(self, text, return_mask = False, max_seq_len = 2048):
# text is a single string

ids = self.tokenizer.EncodeAsIds(text)

# pad bos and eos

if add_bos:
ids = [self.bos_token_id] + ids
if add_eos:
ids = ids + [self.eos_token_id]

stacked_ids = torch.tensor(ids).unsqueeze(0)

if return_mask:
Expand Down