-
Notifications
You must be signed in to change notification settings - Fork 14
/
Tokenizer.py
109 lines (84 loc) · 3.26 KB
/
Tokenizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import os
from tokenizers import Tokenizer as TokenizerFast
from logging import getLogger
from typing import List
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../..")
from model_zoo.ModelUtils import __Tokenizer__
logger = getLogger()
class Tokenizer(__Tokenizer__):
def __init__(self, model_path: str):
# reload tokenizer
assert os.path.isfile(model_path), model_path
self.sp_model = TokenizerFast.from_file(model_path)
self.bos_token = "<|endoftext|>"
self.eos_token = "<|endoftext|>"
self.pad_token = "<|endoftext|>"
self.im_start_token = "<|im_start|>"
self.im_end_token = "<|im_end|>"
# BOS / EOS token IDs
self.n_words: int = self.sp_model.get_vocab_size()
self.bos_id: int = self.sp_model.token_to_id(self.bos_token)
self.eos_id: int = self.sp_model.token_to_id(self.eos_token)
self.pad_id: int = self.sp_model.token_to_id(self.pad_token)
self.im_start_id: int = self.sp_model.token_to_id(self.im_start_token)
self.im_end_id: int = self.sp_model.token_to_id(self.im_end_token)
def encode(self, s: str, bos: bool = False, eos: bool = False) -> List[int]:
assert type(s) is str
t = self.sp_model.encode(s).ids
if bos:
t = [self.bos_id] + t
if eos:
t = t + [self.eos_id]
return t
def decode(self, t: List[int]) -> str:
return self.sp_model.decode(t, skip_special_tokens=False)
def vocab_size(self):
return self.n_words
def get_bos_id(self):
return self.bos_id
def get_eos_id(self):
return self.eos_id
def get_pad_id(self):
return self.pad_id
def make_context(
tokenizer: Tokenizer,
query: str,
):
nl_tokens = tokenizer.encode("\n")
def _tokenize_str(role, content):
return f"{role}\n{content}", tokenizer.encode(role) + nl_tokens + tokenizer.encode(content)
system = "You are a helpful assistant."
system_text, system_tokens_part = _tokenize_str("system", system)
system_tokens = [tokenizer.im_start_id] + system_tokens_part + [tokenizer.im_end_id]
raw_text = ""
context_tokens = []
context_tokens = system_tokens + context_tokens
raw_text = f"{tokenizer.im_start_token}{system_text}{tokenizer.im_end_token}" + raw_text
context_tokens += (
nl_tokens
+ [tokenizer.im_start_id]
+ _tokenize_str("user", query)[1]
+ [tokenizer.im_end_id]
+ nl_tokens
+ [tokenizer.im_start_id]
+ tokenizer.encode("assistant")
+ nl_tokens
)
raw_text += f"\n{tokenizer.im_start_token}user\n{query}{tokenizer.im_end_token}\n{tokenizer.im_start_token}assistant\n"
return raw_text, context_tokens
def decode_context(
tokens: List[int],
*,
tokenizer: Tokenizer,
raw_text_len: int,
context_length: int,
):
eod_token_ids=[tokenizer.im_start_id, tokenizer.im_end_id]
eod_token_idx = context_length
for eod_token_idx in range(context_length, len(tokens)):
if tokens[eod_token_idx] in eod_token_ids:
break
trim_decode_tokens = tokenizer.decode(tokens[:eod_token_idx])[raw_text_len:]
trim_decode_tokens = trim_decode_tokens.strip()
return trim_decode_tokens