-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
209 lines (183 loc) · 6.64 KB
/
export.py
File metadata and controls
209 lines (183 loc) · 6.64 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
WWHO — SGPE Export.
"""
import argparse
import json
import os
try:
import tiktoken
_TIKTOKEN_OK = True
except ImportError:
_TIKTOKEN_OK = False
def export_hf_tokenizer(
vocab: dict[str, int],
merges: list,
special_tokens: list[str],
output_path: str,
script_mode: str = "mixed",
):
added = []
for st in special_tokens:
added.append({
"id": vocab[st],
"content": st,
"single_word": False,
"lstrip": False,
"rstrip": False,
"normalized": False,
"special": True,
})
hf_merges = [f"{a} {b}" for a, b in merges]
cls_id = vocab.get("[CLS]", 2)
sep_id = vocab.get("[SEP]", 3)
tokenizer_json = {
"version": "1.0",
"wwho_version": "1.0.0",
"script_mode": script_mode,
"truncation": None,
"padding": None,
"added_tokens": added,
"normalizer": None,
"pre_tokenizer": None,
"post_processor": {
"type": "TemplateProcessing",
"single": [
{"SpecialToken": {"id": "[CLS]", "type_id": 0}},
{"Sequence": {"id": "A", "type_id": 0}},
{"SpecialToken": {"id": "[SEP]", "type_id": 0}},
],
"pair": [
{"SpecialToken": {"id": "[CLS]", "type_id": 0}},
{"Sequence": {"id": "A", "type_id": 0}},
{"SpecialToken": {"id": "[SEP]", "type_id": 0}},
{"Sequence": {"id": "B", "type_id": 1}},
{"SpecialToken": {"id": "[SEP]", "type_id": 1}},
],
"special_tokens": {
"[CLS]": {"id": str(cls_id), "ids": [cls_id], "tokens": ["[CLS]"]},
"[SEP]": {"id": str(sep_id), "ids": [sep_id], "tokens": ["[SEP]"]},
},
},
"decoder": None,
"model": {
"type": "BPE",
"dropout": None,
"unk_token": "[UNK]",
"continuing_subword_prefix": "",
"end_of_word_suffix": "",
"fuse_unk": False,
"byte_fallback": False,
"vocab": vocab,
"merges": hf_merges,
},
}
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(tokenizer_json, f, ensure_ascii=False, indent=2)
size_mb = os.path.getsize(output_path) / (1024 * 1024)
print(f" [SGPE HF] exported: {output_path} ({size_mb:.2f} MB)")
def export_meta_tokenizer(
sgpe_vocab: dict[str, int],
sgpe_merges: list,
special_tokens: list[str],
output_dir: str,
tiktoken_model: str = "o200k_base",
script_mode: str = "mixed",
):
if not _TIKTOKEN_OK:
print(" [META] tiktoken not available, skipping meta export.")
return
tik = tiktoken.get_encoding(tiktoken_model)
tik_size = tik.n_vocab
sgpe_size = len(sgpe_vocab)
offset_vocab: dict[str, int] = {}
for tok_str, raw_id in sgpe_vocab.items():
offset_vocab[tok_str] = raw_id + tik_size
hf_merges_offset = [f"{a} {b}" for a, b in sgpe_merges]
added = []
for st in special_tokens:
if st in offset_vocab:
added.append({
"id": offset_vocab[st],
"content": st,
"single_word": False,
"lstrip": False, "rstrip": False,
"normalized": False, "special": True,
})
meta_hf = {
"version": "1.0",
"sgpe_version": "2.0.0",
"tiktoken_model": tiktoken_model,
"tiktoken_vocab_size": tik_size,
"sgpe_vocab_size": sgpe_size,
"total_vocab_size": tik_size + sgpe_size,
"sgpe_id_offset": tik_size,
"script_mode": script_mode,
"note": (
"IDs [0, tiktoken_vocab_size) are tiktoken tokens. "
"IDs [tiktoken_vocab_size, total_vocab_size) are SGPE tokens "
"(SGPE raw_id + tiktoken_vocab_size)."
),
"added_tokens": added,
"model": {
"type": "BPE",
"unk_token": "[UNK]",
"vocab": offset_vocab,
"merges": hf_merges_offset,
},
}
meta_path = os.path.join(output_dir, "tokenizer_meta.json")
with open(meta_path, "w", encoding="utf-8") as f:
json.dump(meta_hf, f, ensure_ascii=False, indent=2)
meta_mb = os.path.getsize(meta_path) / (1024 * 1024)
print(f" [META HF] exported: {meta_path} ({meta_mb:.2f} MB)")
print(f" vocab = tiktoken({tik_size:,}) + SGPE({sgpe_size:,}) "
f"= {tik_size + sgpe_size:,} total IDs")
cfg_path = os.path.join(output_dir, "meta_config.json")
meta_config = {
"tiktoken_model": tiktoken_model,
"tiktoken_vocab_size": tik_size,
"sgpe_vocab_size": sgpe_size,
"sgpe_id_offset": tik_size,
"script_mode": script_mode,
"sgpe_vocab_path": "vocab.json",
}
with open(cfg_path, "w", encoding="utf-8") as f:
json.dump(meta_config, f, indent=2)
print(f" [META CFG] exported: {cfg_path}")
def main():
parser = argparse.ArgumentParser(
description="WWHO — Export SGPE vocab to HF tokenizer format"
)
parser.add_argument("--vocab", type=str, default="output/vocab.json")
parser.add_argument("--out_dir", type=str, default=None,
help="Output directory (default: same dir as vocab)")
parser.add_argument("--tiktoken_model", type=str, default="o200k_base",
help="Tiktoken model for meta-vocab offset calculation")
parser.add_argument("--no_meta", action="store_true",
help="Skip meta-vocab export (export raw SGPE only)")
args = parser.parse_args()
if not os.path.exists(args.vocab):
print(f"Error: vocab file not found: {args.vocab}")
return
with open(args.vocab, "r", encoding="utf-8") as f:
data = json.load(f)
vocab = data["vocab"]
merges = [tuple(m) for m in data["merges"]]
special_tokens = data["special_tokens"]
script_mode = data.get("script_mode", "mixed")
out_dir = args.out_dir or os.path.dirname(args.vocab) or "."
os.makedirs(out_dir, exist_ok=True)
# 1. Raw SGPE HF tokenizer
hf_path = os.path.join(out_dir, "tokenizer.json")
export_hf_tokenizer(vocab, merges, special_tokens, hf_path,
script_mode=script_mode)
# 2. Meta-vocab tokenizer + config (for unified ID space)
if not args.no_meta:
export_meta_tokenizer(
vocab, merges, special_tokens, out_dir,
tiktoken_model=args.tiktoken_model,
script_mode=script_mode,
)
if __name__ == "__main__":
main()