-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapipe.py
139 lines (122 loc) · 3.85 KB
/
datapipe.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from typing import Iterable, Callable
from tiktoken_ext import openai_public
import tensorflow as tf
import tiktoken
import threading
_tokenizer_lock = threading.RLock()
TOKENIZER = None
def get_tokenizer(vocab_size: int) -> tiktoken.Encoding:
"""Returns a smaller version of cl100k_base tokenizer of `vocab_size`."""
global TOKENIZER
if TOKENIZER:
return TOKENIZER
with _tokenizer_lock:
if TOKENIZER:
return TOKENIZER
config = openai_public.cl100k_base()
mergeable_ranks = {
token: rank
for token, rank in config['mergeable_ranks'].items()
if rank < vocab_size
}
config = {
**config,
'name': 'cl4k_base',
'mergeable_ranks': mergeable_ranks,
'special_tokens': {
'<|endoftext|>':
len(mergeable_ranks) +
1, # I don't know why rank `len(mergeable_ranks)` is not used but following the same pattern.
'<|endofprompt|>': len(mergeable_ranks) + 2,
},
}
TOKENIZER = tiktoken.Encoding(**config)
return TOKENIZER
def build_char_tokenizer(
chars: Iterable[str]) -> tuple[Callable, Callable, int]:
vocab = sorted(list(set(chars)))
vocab = ['[PAD]', '[UNK]', '[START]', '[END]'] + vocab
idx_to_word = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(
keys=range(len(vocab)),
values=vocab,
),
default_value='[UNK]',
)
word_to_idx = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(
keys=vocab,
values=range(len(vocab)),
),
default_value=1,
)
def tokenize_batch(inputs):
return tf.map_fn(
lambda s: word_to_idx.lookup(
tf.concat([
['[START]'],
s,
['[END]'],
], axis=0)),
inputs,
fn_output_signature=tf.RaggedTensorSpec(shape=[None], dtype=tf.int32),
)
def detokenize_batch(inputs):
return tf.map_fn(
lambda idxs: idx_to_word.lookup(tf.boolean_mask(idxs, idxs >= 4)),
inputs,
fn_output_signature=tf.TensorSpec(shape=[None], dtype=tf.string),
)
return tokenize_batch, detokenize_batch, len(vocab)
def build_simple_tokenizers(examples: Iterable[str],
separator=' ') -> tuple[Callable, Callable, int]:
"""Builds tokenizers for a given set of examples.
Returns:
tokenize: string tensor -> int tensor.
detokenize: int tensor -> string tensor.
vocab_size: The size of the vocabulary.
"""
vocab = set()
for example in examples:
if isinstance(example, tf.Tensor):
example = example.numpy()
vocab.update(example.decode('utf-8').split(separator))
vocab = ['[PAD]', '[UNK]', '[START]', '[END]'] + list(sorted(vocab))
idx_to_word = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(
keys=range(len(vocab)),
values=vocab,
),
default_value='[UNK]',
)
word_to_idx = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(
keys=vocab,
values=range(len(vocab)),
),
default_value=1,
)
def tokenize_batch(inputs):
return tf.map_fn(
lambda s: word_to_idx.lookup(
tf.concat(
[
['[START]'],
tf.strings.split(s, sep=separator),
['[END]'],
],
axis=0,
)),
inputs,
fn_output_signature=tf.RaggedTensorSpec(shape=[None], dtype=tf.int32),
)
def detokenize_batch(inputs):
return tf.map_fn(
lambda idxs: tf.strings.join(
idx_to_word.lookup(tf.boolean_mask(idxs, idxs >= 4)),
separator=separator,
),
inputs,
fn_output_signature=tf.TensorSpec(shape=[], dtype=tf.string),
)
return tokenize_batch, detokenize_batch, len(vocab)