16
"""Tokenization classes for OpenAI GPT."""
18
from __future__ import absolute_import, division, print_function, unicode_literals
28
from ppfleetx.utils.download import cached_path
31
from functools import lru_cache
37
return lambda func: func
40
from ppfleetx.utils.log import logger
44
from paddlenlp.transformers.gpt.tokenizer import GPTChineseTokenizer
46
raise ImportError("Please import paddlenlp before running the GPT tasks.")
48
PRETRAINED_VOCAB_ARCHIVE_MAP = {
49
"gpt2": "http://fleet.bj.bcebos.com/datasets/gpt/gpt2-vocab.json",
51
PRETRAINED_MERGES_ARCHIVE_MAP = {
52
"gpt2": "http://fleet.bj.bcebos.com/datasets/gpt/gpt2-merges.txt",
54
PRETRAINED_VOCAB_POSITIONAL_EMBEDDINGS_SIZE_MAP = {
57
VOCAB_NAME = "vocab.json"
58
MERGES_NAME = "merges.txt"
59
SPECIAL_TOKENS_NAME = "special_tokens.txt"
63
def bytes_to_unicode():
65
Returns list of utf-8 byte and a corresponding list of unicode strings.
66
The reversible bpe codes work on unicode strings.
67
This means you need a large # of unicode characters in your vocab if you want to avoid UNKs.
68
When you're at something like a 10B token dataset you end up needing around 5K for decent coverage.
69
This is a signficant percentage of your normal, say, 32K bpe vocab.
70
To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
71
And avoids mapping to whitespace/control characters the bpe code barfs on.
73
_chr = unichr if sys.version_info[0] == 2 else chr
75
list(range(ord("!"), ord("~") + 1)) + list(range(ord("¡"), ord("¬") + 1)) + list(range(ord("®"), ord("ÿ") + 1))
84
cs = [_chr(n) for n in cs]
85
return dict(zip(bs, cs))
89
"""Return set of symbol pairs in a word.
90
Word is represented as tuple of symbols (symbols being variable-length strings).
95
pairs.add((prev_char, char))
100
class GPTTokenizer(object):
102
GPT-2 BPE tokenizer. Peculiarities:
106
padding_side = "right"
107
truncation_side = "right"
108
model_input_names = ["input_ids", "token_type_ids", "attention_mask"]
109
pad_token_type_id = 0
113
def from_pretrained(cls, pretrained_model_name_or_path, cache_dir=None, *inputs, **kwargs):
115
Instantiate a PreTrainedBertModel from a pre-trained model file.
116
Download and cache the pre-trained model file if needed.
118
if pretrained_model_name_or_path in PRETRAINED_VOCAB_ARCHIVE_MAP:
119
vocab_file = PRETRAINED_VOCAB_ARCHIVE_MAP[pretrained_model_name_or_path]
120
merges_file = PRETRAINED_MERGES_ARCHIVE_MAP[pretrained_model_name_or_path]
121
special_tokens_file = None
123
vocab_file = os.path.join(pretrained_model_name_or_path, VOCAB_NAME)
124
merges_file = os.path.join(pretrained_model_name_or_path, MERGES_NAME)
125
special_tokens_file = os.path.join(pretrained_model_name_or_path, SPECIAL_TOKENS_NAME)
126
if not os.path.exists(special_tokens_file):
127
special_tokens_file = None
129
logger.info("loading special tokens file {}".format(special_tokens_file))
132
resolved_vocab_file = cached_path(vocab_file, cache_dir=cache_dir)
133
resolved_merges_file = cached_path(merges_file, cache_dir=cache_dir)
134
except Exception as e:
137
"Model name '{}' was not found in model name list ({}). "
138
"We assumed '{}' was a path or url but couldn't find files {} and {} "
139
"at this path or url.".format(
140
pretrained_model_name_or_path,
141
", ".join(PRETRAINED_VOCAB_ARCHIVE_MAP.keys()),
142
pretrained_model_name_or_path,
148
if resolved_vocab_file == vocab_file and resolved_merges_file == merges_file:
149
logger.info("loading vocabulary file {}".format(vocab_file))
150
logger.info("loading merges file {}".format(merges_file))
152
logger.info("loading vocabulary file {} from cache at {}".format(vocab_file, resolved_vocab_file))
153
logger.info("loading merges file {} from cache at {}".format(merges_file, resolved_merges_file))
154
if pretrained_model_name_or_path in PRETRAINED_VOCAB_POSITIONAL_EMBEDDINGS_SIZE_MAP:
157
max_len = PRETRAINED_VOCAB_POSITIONAL_EMBEDDINGS_SIZE_MAP[pretrained_model_name_or_path]
158
kwargs["max_len"] = min(kwargs.get("max_len", int(1e12)), max_len)
160
if special_tokens_file and "special_tokens" not in kwargs:
161
special_tokens = open(special_tokens_file, encoding="utf-8").read().split("\n")[:-1]
163
special_tokens = kwargs.pop("special_tokens", [])
164
tokenizer = cls(resolved_vocab_file, resolved_merges_file, special_tokens=special_tokens, *inputs, **kwargs)
167
def __init__(self, vocab_file, merges_file, errors="replace", special_tokens=None, max_len=None, **kwargs):
169
self.padding_side = kwargs.pop("padding_side", self.padding_side)
170
if self.padding_side not in ["right", "left"]:
172
f"Padding side should be selected between 'right' and 'left', current value: {self.padding_side}"
175
self.truncation_side = kwargs.pop("truncation_side", self.truncation_side)
176
if self.truncation_side not in ["right", "left"]:
178
f"Padding side should be selected between 'right' and 'left', current value: {self.truncation_side}"
181
self.max_len = max_len if max_len is not None else int(1e12)
182
self.encoder = json.load(open(vocab_file))
183
self.decoder = {v: k for k, v in self.encoder.items()}
185
self.byte_encoder = bytes_to_unicode()
186
self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
187
bpe_data = open(merges_file, encoding="utf-8").read().split("\n")[1:-1]
188
bpe_merges = [tuple(merge.split()) for merge in bpe_data]
189
self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
194
self.eod_id = self.encoder["<|endoftext|>"]
195
self.pat = re.compile(r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""")
197
self.special_tokens = {}
198
self.special_tokens_decoder = {}
199
self.set_special_tokens(special_tokens)
205
add_special_tokens=True,
209
pad_to_multiple_of=None,
210
return_token_type_ids=None,
211
return_attention_mask=None,
212
return_overflowing_tokens=False,
215
assert padding in [True, False, "longest", "max_length", "do_not_pad"]
217
if max_length is not None and padding is False and truncation is False:
218
truncation = "longest_first"
222
elif padding is False:
223
padding = "do_not_pad"
225
assert truncation in [True, False, "only_first", "only_second", "longest_first", "do_not_truncate"]
226
if truncation is True:
227
truncation = "longest_first"
228
elif truncation is False:
229
truncation = "do_not_truncate"
233
truncation != "do_not_truncate"
234
and padding != "do_not_pad"
235
and pad_to_multiple_of is not None
236
and max_length is not None
237
and (max_length % pad_to_multiple_of != 0)
240
"Truncation and padding are both activated but "
241
f"truncation length ({max_length}) is not a multiple of pad_to_multiple_of ({pad_to_multiple_of})."
244
is_batched = isinstance(text, (list, tuple))
246
raise NotImplementedError
248
return self.encode_plus(
251
add_special_tokens=add_special_tokens,
253
truncation=truncation,
254
max_length=max_length,
255
pad_to_multiple_of=pad_to_multiple_of,
256
return_token_type_ids=return_token_type_ids,
257
return_attention_mask=return_attention_mask,
258
return_overflowing_tokens=return_overflowing_tokens,
259
return_length=return_length,
266
add_special_tokens=True,
267
padding="do_not_pad",
268
truncation="do_not_truncate",
270
pad_to_multiple_of=None,
271
return_token_type_ids=None,
272
return_attention_mask=None,
273
return_overflowing_tokens=False,
277
def get_input_ids(text):
278
if isinstance(text, str):
279
tokens = self.tokenize(text, **kwargs)
280
return self.convert_tokens_to_ids(tokens)
281
elif isinstance(text, (list, tuple)) and len(text) > 0 and isinstance(text[0], str):
282
if is_split_into_words:
284
itertools.chain(*(self.tokenize(t, is_split_into_words=True, **kwargs) for t in text))
286
return self.convert_tokens_to_ids(tokens)
288
return self.convert_tokens_to_ids(text)
289
elif isinstance(text, (list, tuple)) and len(text) > 0 and isinstance(text[0], int):
292
raise NotImplementedError
294
first_ids = get_input_ids(text)
295
second_ids = get_input_ids(text_pair) if text_pair is not None else None
297
pair = bool(second_ids is not None)
298
len_ids = len(first_ids)
299
len_pair_ids = len(second_ids) if pair else 0
301
if return_token_type_ids and not add_special_tokens:
303
"Asking to return token_type_ids while setting add_special_tokens to False "
304
"results in an undefined behavior. Please set add_special_tokens to True or "
305
"set return_token_type_ids to None."
309
if return_token_type_ids is None:
310
return_token_type_ids = "token_type_ids" in self.model_input_names
311
if return_attention_mask is None:
312
return_attention_mask = "attention_mask" in self.model_input_names
316
total_len = len_ids + len_pair_ids + (self.num_special_tokens_to_add(pair=pair) if add_special_tokens else 0)
319
overflowing_tokens = []
320
if truncation != "do_not_truncate" and max_length and total_len > max_length:
321
first_ids, second_ids, overflowing_tokens = self.truncate_sequences(
324
num_tokens_to_remove=total_len - max_length,
325
truncation=truncation,
327
if return_overflowing_tokens:
328
encoded_inputs["overflowing_tokens"] = overflowing_tokens
329
encoded_inputs["num_truncated_tokens"] = total_len - max_length
332
if add_special_tokens:
333
sequence = self.build_inputs_with_special_tokens(first_ids, second_ids)
334
token_type_ids = self.create_token_type_ids_from_sequences(first_ids, second_ids)
336
sequence = first_ids + second_ids if pair else first_ids
337
token_type_ids = [0] * len(first_ids) + ([0] * len(second_ids) if pair else [])
340
encoded_inputs["input_ids"] = sequence
341
if return_token_type_ids:
342
encoded_inputs["token_type_ids"] = token_type_ids
345
if padding != "do_not_pad" or return_attention_mask:
346
encoded_inputs = self.pad(
348
max_length=max_length,
350
pad_to_multiple_of=pad_to_multiple_of,
351
return_attention_mask=return_attention_mask,
355
encoded_inputs["length"] = len(encoded_inputs["input_ids"])
357
return encoded_inputs
359
def num_special_tokens_to_add(self, pair: bool = False) -> int:
362
return len(self.build_inputs_with_special_tokens(token_ids_0, token_ids_1 if pair else None))
364
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
365
if token_ids_1 is None:
367
return token_ids_0 + token_ids_1
369
def create_token_type_ids_from_sequences(self, token_ids_0, token_ids_1=None):
370
if token_ids_1 is None:
371
return len(token_ids_0) * [0]
372
return [0] * len(token_ids_0) + [1] * len(token_ids_1)
374
def truncate_sequences(
378
num_tokens_to_remove=0,
379
truncation="longest_first",
382
if num_tokens_to_remove <= 0:
383
return ids, pair_ids, []
385
overflowing_tokens = []
386
if truncation == "only_first" or (truncation == "longest_first" and pair_ids is None):
387
if len(ids) > num_tokens_to_remove:
388
window_len = min(len(ids), stride + num_tokens_to_remove)
389
if self.truncation_side == "left":
390
overflowing_tokens = ids[:window_len]
391
ids = ids[num_tokens_to_remove:]
392
elif self.truncation_side == "right":
393
overflowing_tokens = ids[-window_len:]
394
ids = ids[:-num_tokens_to_remove]
396
raise ValueError(f"invalid truncation strategy: {self.truncation_side}, use 'left' or 'right'.")
400
f"We need to remove {num_tokens_to_remove} to truncate the input "
401
f"but the first sequence has a length {len(ids)}. "
403
if truncation == "only_first":
405
error_msg + "Please select another truncation strategy than "
406
f"{truncation}, for instance 'longest_first' or 'only_second'."
408
logger.error(error_msg)
409
elif truncation == "longest_first":
411
"Be aware, overflowing tokens are not returned for the setting you have chosen,"
412
f" i.e. sequence pairs with the '{truncation}' "
413
"truncation strategy. So the returned list will always be empty even if some "
414
"tokens have been removed."
416
for _ in range(num_tokens_to_remove):
417
if pair_ids is None or len(ids) > len(pair_ids):
418
if self.truncation_side == "right":
420
elif self.truncation_side == "left":
423
raise ValueError("invalid truncation strategy:" + str(self.truncation_side))
425
if self.truncation_side == "right":
426
pair_ids = pair_ids[:-1]
427
elif self.truncation_side == "left":
428
pair_ids = pair_ids[1:]
430
raise ValueError("invalid truncation strategy:" + str(self.truncation_side))
431
elif truncation == "only_second" and pair_ids is not None:
432
if len(pair_ids) > num_tokens_to_remove:
433
window_len = min(len(pair_ids), stride + num_tokens_to_remove)
434
if self.truncation_side == "right":
435
overflowing_tokens = pair_ids[-window_len:]
436
pair_ids = pair_ids[:-num_tokens_to_remove]
437
elif self.truncation_side == "left":
438
overflowing_tokens = pair_ids[:window_len]
439
pair_ids = pair_ids[num_tokens_to_remove:]
441
raise ValueError("invalid truncation strategy:" + str(self.truncation_side))
444
f"We need to remove {num_tokens_to_remove} to truncate the input "
445
f"but the second sequence has a length {len(pair_ids)}. "
446
f"Please select another truncation strategy than {truncation}, "
447
"for instance 'longest_first' or 'only_first'."
450
return (ids, pair_ids, overflowing_tokens)
457
pad_to_multiple_of=None,
458
return_attention_mask=None,
464
if self.model_input_names[0] not in encoded_inputs:
466
"You should supply an encoding or a list of encodings to this method "
467
f"that includes {self.model_input_names[0]}, but you provided {list(encoded_inputs.keys())}"
470
required_input = encoded_inputs[self.model_input_names[0]]
472
if not required_input:
473
if return_attention_mask:
474
encoded_inputs["attention_mask"] = []
475
return encoded_inputs
477
required_input = encoded_inputs[self.model_input_names[0]]
479
if required_input and not isinstance(required_input[0], (list, tuple)):
480
encoded_inputs = self._pad(
482
max_length=max_length,
484
pad_to_multiple_of=pad_to_multiple_of,
485
return_attention_mask=return_attention_mask,
487
return encoded_inputs
489
batch_size = len(required_input)
491
len(v) == batch_size for v in encoded_inputs.values()
492
), "Some items in the output dictionary have a different batch size than others."
494
if padding == "longest":
495
max_length = max(len(inputs) for inputs in required_input)
496
padding = "max_length"
499
for i in range(batch_size):
500
inputs = dict((k, v[i]) for k, v in encoded_inputs.items())
503
max_length=max_length,
505
pad_to_multiple_of=pad_to_multiple_of,
506
return_attention_mask=return_attention_mask,
509
for key, value in outputs.items():
510
if key not in batch_outputs:
511
batch_outputs[key] = []
512
batch_outputs[key].append(value)
514
return encoded_inputs
520
padding="do_not_pad",
521
pad_to_multiple_of=None,
522
return_attention_mask=None,
525
if return_attention_mask is None:
526
return_attention_mask = "attention_mask" in self.model_input_names or "attention_mask" in encoded_inputs
528
required_input = encoded_inputs[self.model_input_names[0]]
530
if padding == "longest":
531
max_length = len(required_input)
533
if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
534
max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
536
needs_to_be_padded = padding != "do_not_pad" and len(required_input) != max_length
539
if return_attention_mask and "attention_mask" not in encoded_inputs:
540
encoded_inputs["attention_mask"] = [1] * len(required_input)
542
if needs_to_be_padded:
543
difference = max_length - len(required_input)
545
if self.padding_side == "right":
546
if return_attention_mask:
547
encoded_inputs["attention_mask"] = encoded_inputs["attention_mask"] + [0] * difference
548
if "token_type_ids" in encoded_inputs:
549
encoded_inputs["token_type_ids"] = (
550
encoded_inputs["token_type_ids"] + [self.pad_token_type_id] * difference
552
if "special_tokens_mask" in encoded_inputs:
553
encoded_inputs["special_tokens_mask"] = encoded_inputs["special_tokens_mask"] + [1] * difference
554
if "offset_mapping" in encoded_inputs:
555
encoded_inputs["offset_mapping"] = encoded_inputs["offset_mapping"] + [(0, 0)] * difference
556
if "position_ids" in encoded_inputs:
557
encoded_inputs["position_ids"] = encoded_inputs["position_ids"] + [0] * difference
558
encoded_inputs[self.model_input_names[0]] = required_input + [self.pad_token_id] * difference
559
elif self.padding_side == "left":
560
if return_attention_mask:
561
encoded_inputs["attention_mask"] = [0] * difference + encoded_inputs["attention_mask"]
562
if "token_type_ids" in encoded_inputs:
563
encoded_inputs["token_type_ids"] = [self.pad_token_type_id] * difference + encoded_inputs[
566
if "special_tokens_mask" in encoded_inputs:
567
encoded_inputs["special_tokens_mask"] = [1] * difference + encoded_inputs["special_tokens_mask"]
568
if "offset_mapping" in encoded_inputs:
569
encoded_inputs["offset_mapping"] = [(0, 0)] * difference + encoded_inputs["offset_mapping"]
570
if "position_ids" in encoded_inputs:
571
encoded_inputs["position_ids"] = [0] * difference + encoded_inputs["position_ids"]
572
encoded_inputs[self.model_input_names[0]] = [self.pad_token_id] * difference + required_input
574
raise ValueError("Invalid padding strategy:" + str(self.padding_side))
576
return encoded_inputs
579
return len(self.encoder) + len(self.special_tokens)
581
def set_special_tokens(self, special_tokens):
582
"""Add a list of additional tokens to the encoder.
583
The additional tokens are indexed starting from the last index of the
584
current vocabulary in the order of the `special_tokens` list.
586
if not special_tokens:
587
self.special_tokens = {}
588
self.special_tokens_decoder = {}
590
self.special_tokens = dict((tok, len(self.encoder) + i) for i, tok in enumerate(special_tokens))
591
self.special_tokens_decoder = {v: k for k, v in self.special_tokens.items()}
592
logger.info("Special tokens {}".format(self.special_tokens))
594
def bpe(self, token):
595
if token in self.cache:
596
return self.cache[token]
598
pairs = get_pairs(word)
604
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
605
if bigram not in self.bpe_ranks:
607
first, second = bigram
612
j = word.index(first, i)
613
new_word.extend(word[i:j])
615
except BaseException:
616
new_word.extend(word[i:])
619
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
620
new_word.append(first + second)
623
new_word.append(word[i])
625
new_word = tuple(new_word)
630
pairs = get_pairs(word)
631
word = " ".join(word)
632
self.cache[token] = word
635
def tokenize(self, text):
636
"""Tokenize a string."""
638
for token in re.findall(self.pat, text):
639
if sys.version_info[0] == 2:
640
token = "".join(self.byte_encoder[ord(b)] for b in token)
642
token = "".join(self.byte_encoder[b] for b in token.encode("utf-8"))
643
bpe_tokens.extend(bpe_token for bpe_token in self.bpe(token).split(" "))
646
def convert_tokens_to_ids(self, tokens):
647
"""Converts a sequence of tokens into ids using the vocab."""
649
if isinstance(tokens, str) or (sys.version_info[0] == 2 and isinstance(tokens, unicode)):
650
if tokens in self.special_tokens:
651
return self.special_tokens[tokens]
653
return self.encoder.get(tokens, 0)
655
if token in self.special_tokens:
656
ids.append(self.special_tokens[token])
658
ids.append(self.encoder.get(token, 0))
659
if len(ids) > self.max_len:
661
"Token indices sequence length is longer than the specified maximum "
662
" sequence length for this OpenAI GPT model ({} > {}). Running this"
663
" sequence through the model will result in indexing errors".format(len(ids), self.max_len)
667
def convert_ids_to_string(self, ids):
669
Converts a single index or a sequence of indices to texts.
672
The token id (or token ids) to be converted to text.
674
str: The decoded text.
677
from paddlenlp.transformers import GPTTokenizer
678
tokenizer = GPTTokenizer.from_pretrained('gpt2-medium-en')
679
print(tokenizer.convert_ids_to_string(tokenizer.convert_ids_to_string([14618, 284, 779, 350, 37382, 47, 37382, 290, 350, 37382, 45, 19930]))
680
# 'Welcome to use PaddlePaddle and PaddleNLP'
683
text = "".join([self.decoder[id] for id in ids])
684
text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors)
687
def convert_ids_to_tokens(self, ids, skip_special_tokens=False):
688
"""Converts a sequence of ids in BPE tokens using the vocab."""
691
if i in self.special_tokens_decoder:
692
if not skip_special_tokens:
693
tokens.append(self.special_tokens_decoder[i])
695
tokens.append(self.decoder[i])
698
def encode(self, text):
699
return self.convert_tokens_to_ids(self.tokenize(text))
701
def decode(self, tokens):
702
text = "".join([self.decoder[token] if token in self.decoder.keys() else "" for token in tokens])
703
text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors)
706
def save_vocabulary(self, vocab_path):
707
"""Save the tokenizer vocabulary and merge files to a directory."""
708
if not os.path.isdir(vocab_path):
709
logger.error("Vocabulary path ({}) should be a directory".format(vocab_path))
711
vocab_file = os.path.join(vocab_path, VOCAB_NAME)
712
merge_file = os.path.join(vocab_path, MERGES_NAME)
713
special_tokens_file = os.path.join(vocab_path, SPECIAL_TOKENS_NAME)
715
with open(vocab_file, "w", encoding="utf-8") as f:
716
f.write(json.dumps(self.encoder, ensure_ascii=False))
719
with open(merge_file, "w", encoding="utf-8") as writer:
720
writer.write("#version: 0.2\n")
721
for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]):
722
if index != token_index:
724
"Saving vocabulary to {}: BPE merge indices are not consecutive."
725
" Please check that the tokenizer is not corrupted!".format(merge_file)
728
writer.write(" ".join(bpe_tokens) + "\n")
731
index = len(self.encoder)
732
with open(special_tokens_file, "w", encoding="utf-8") as writer:
733
for token, token_index in sorted(self.special_tokens.items(), key=lambda kv: kv[1]):
734
if index != token_index:
736
"Saving special tokens vocabulary to {}: BPE indices are not consecutive."
737
" Please check that the tokenizer is not corrupted!".format(special_tokens_file)
740
writer.write(token + "\n")
743
return vocab_file, merge_file, special_tokens_file
746
def vocab_size(self):
747
return len(self.encoder)
758
def eos_token_id(self):