Abhaykoul commited on
Commit
2252622
1 Parent(s): c9a1b98

Upload tokenization_HelpingAI_fast.py

Browse files
Files changed (1) hide show
  1. tokenization_HelpingAI_fast.py +144 -0
tokenization_HelpingAI_fast.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import json
3
+ from typing import List, Optional, Tuple
4
+
5
+ from tokenizers import pre_tokenizers, processors
6
+
7
+ from transformers.tokenization_utils_fast import PreTrainedTokenizerFast
8
+ from transformers.utils import logging
9
+
10
+
11
+ logger = logging.get_logger(__name__)
12
+
13
+ VOCAB_FILES_NAMES = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}
14
+
15
+
16
+ class HelpingAITokenizerFast(PreTrainedTokenizerFast):
17
+ vocab_files_names = VOCAB_FILES_NAMES
18
+ model_input_names = ["input_ids", "attention_mask"]
19
+
20
+ def __init__(
21
+ self,
22
+ vocab_file=None,
23
+ merges_file=None,
24
+ tokenizer_file=None,
25
+ unk_token="<|endoftext|>",
26
+ bos_token="<|im_start|>",
27
+ eos_token="<|im_end|>",
28
+ pad_token="<|im_end|>",
29
+ add_bos_token=False,
30
+ add_eos_token=False,
31
+ add_prefix_space=False,
32
+ **kwargs,
33
+ ):
34
+ super().__init__(
35
+ vocab_file,
36
+ merges_file,
37
+ tokenizer_file=tokenizer_file,
38
+ unk_token=unk_token,
39
+ bos_token=bos_token,
40
+ eos_token=eos_token,
41
+ pad_token=pad_token,
42
+ add_bos_token=add_bos_token,
43
+ add_eos_token=add_eos_token,
44
+ add_prefix_space=add_prefix_space,
45
+ **kwargs,
46
+ )
47
+
48
+ self._add_bos_token = add_bos_token
49
+ self._add_eos_token = add_eos_token
50
+ self.update_post_processor()
51
+
52
+ pre_tok_state = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__())
53
+ if pre_tok_state.get("add_prefix_space", add_prefix_space) != add_prefix_space:
54
+ pre_tok_class = getattr(pre_tokenizers, pre_tok_state.pop("type"))
55
+ pre_tok_state["add_prefix_space"] = add_prefix_space
56
+ self.backend_tokenizer.pre_tokenizer = pre_tok_class(**pre_tok_state)
57
+
58
+ self.add_prefix_space = add_prefix_space
59
+
60
+ @property
61
+ def add_eos_token(self):
62
+ return self._add_eos_token
63
+
64
+ @property
65
+ def add_bos_token(self):
66
+ return self._add_bos_token
67
+
68
+ @add_eos_token.setter
69
+ def add_eos_token(self, value):
70
+ self._add_eos_token = value
71
+ self.update_post_processor()
72
+
73
+ @add_bos_token.setter
74
+ def add_bos_token(self, value):
75
+ self._add_bos_token = value
76
+ self.update_post_processor()
77
+
78
+ def update_post_processor(self):
79
+ bos = self.bos_token
80
+ bos_token_id = self.bos_token_id
81
+ if bos is None and self.add_bos_token:
82
+ raise ValueError("add_bos_token = True but bos_token = None")
83
+
84
+ eos = self.eos_token
85
+ eos_token_id = self.eos_token_id
86
+ if eos is None and self.add_eos_token:
87
+ raise ValueError("add_eos_token = True but eos_token = None")
88
+
89
+ single = f"{(bos+':0 ') if self.add_bos_token else ''}$A:0{(' '+eos+':0') if self.add_eos_token else ''}"
90
+ pair = f"{single}{(' '+bos+':1') if self.add_bos_token else ''} $B:1{(' '+eos+':1') if self.add_eos_token else ''}"
91
+
92
+ special_tokens = []
93
+ if self.add_bos_token:
94
+ special_tokens.append((bos, bos_token_id))
95
+ if self.add_eos_token:
96
+ special_tokens.append((eos, eos_token_id))
97
+ self._tokenizer.post_processor = processors.TemplateProcessing(
98
+ single=single, pair=pair, special_tokens=special_tokens
99
+ )
100
+
101
+ def get_special_tokens_mask(
102
+ self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
103
+ ) -> List[int]:
104
+ if already_has_special_tokens:
105
+ return super().get_special_tokens_mask(
106
+ token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
107
+ )
108
+
109
+ bos_token_id = [1] if self.add_bos_token else []
110
+ eos_token_id = [1] if self.add_eos_token else []
111
+
112
+ if token_ids_1 is None:
113
+ return bos_token_id + ([0] * len(token_ids_0)) + eos_token_id
114
+ return (
115
+ bos_token_id
116
+ + ([0] * len(token_ids_0))
117
+ + eos_token_id
118
+ + bos_token_id
119
+ + ([0] * len(token_ids_1))
120
+ + eos_token_id
121
+ )
122
+
123
+ def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
124
+ bos_token_id = [self.bos_token_id] if self.add_bos_token else []
125
+ eos_token_id = [self.eos_token_id] if self.add_eos_token else []
126
+
127
+ output = bos_token_id + token_ids_0 + eos_token_id
128
+
129
+ if token_ids_1 is not None:
130
+ output = output + bos_token_id + token_ids_1 + eos_token_id
131
+
132
+ return output
133
+
134
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
135
+ files = self._tokenizer.model.save(save_directory, name=filename_prefix)
136
+ return tuple(files)
137
+
138
+ @property
139
+ def default_chat_template(self):
140
+ return "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
141
+
142
+ """
143
+ Tool call template not added
144
+ """