-
Notifications
You must be signed in to change notification settings - Fork 1
/
combine_tokenizer.py
60 lines (51 loc) · 1.93 KB
/
combine_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
from transformers import AutoModel, AutoTokenizer, AutoConfig
import numpy as np
import torch
import json
target_tokenizer = AutoTokenizer.from_pretrained("./tokenizer_mpt_7b_model/")
source_tokenizer = AutoTokenizer.from_pretrained("indic_tokenizer/indiMPT_tokenizer_64k")
source_vocab = source_tokenizer.vocab
target_vocab = target_tokenizer.vocab
########## contain list of all intersecting token
intersect= []
# f = open('indic_tokenizer/IndicMPTexplicit/tokenizer.json')
# data = json.load(f)
# for index, token in enumerate(target_vocab):
# if token in source_vocab:
# intersect.append(token)
f_source = open('indic_tokenizer/indiMPT_tokenizer_64k/tokenizer.json')
data_source = json.load(f_source)
f_target = open('tokenizer_mpt_7b_model/tokenizer.json')
data_target = json.load(f_target)
# vocab_dict = data['model']['vocab']
merge_source = data_source['model']['merges']
merge_target = data_target['model']['merges']
count = 50254
for index, token in enumerate(source_vocab):
if token in target_vocab:
intersect.append(token)
else:
data_target['model']['vocab'][token] = count
count = count + 1
# if count == 50256:
# break
print("length of source merges is ", len(merge_source))
print("length of target merges is before: ", len(merge_target))
print("merge type is ", merge_source[0])
merge_intersect = 0
for merge_pair in merge_source:
if merge_pair in merge_target:
merge_intersect += 1
else:
data_target['model']['merges'].append(merge_pair)
# if merge_intersect == 1:
# break
print("intersected merge is ", merge_intersect)
# print(len(merge_source))
# print(len(data['model']['merges']))
print("length of target merges is after: ", len(merge_target))
# print(data_source.keys())
with open("sample_correct.json", "w",encoding='utf-8') as outfile:
json.dump(data_target, outfile, indent = 3,ensure_ascii=False)
# print(vocab_dict)
# print(len(vocab_dict))