-
Notifications
You must be signed in to change notification settings - Fork 1
/
combine_tok_before_mpt.py
77 lines (63 loc) · 2.47 KB
/
combine_tok_before_mpt.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
from transformers import AutoModel, AutoTokenizer, AutoConfig
import numpy as np
import torch
import json
import argparse
parser = argparse.ArgumentParser()
# parser.add_argument("--wordEmbTrain", default="false", help = "[true | false]")
parser.add_argument("--indic_tok", default="indiMPT_tokenizer_64k")
args = parser.parse_args()
target_tokenizer = AutoTokenizer.from_pretrained("./tokenizer_mpt_7b_model/")
source_tokenizer = AutoTokenizer.from_pretrained(f"indic_tokenizer/{args.indic_tok}")
target_vocab = target_tokenizer.vocab
source_vocab = source_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(f"indic_tokenizer/{args.indic_tok}/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']
print("length of merge_source ", len(merge_source))
print("length of merge_target ", len(merge_target))
# count = 50254
count = len(target_tokenizer)
print("length of target tokenizer ", len(target_tokenizer))
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("merge type is ", merge_source[0])
merge_intersect = 0
count_rev = 0
for merge_pair in merge_target:
if merge_pair in merge_source:
merge_intersect += 1
else:
data_source['model']['merges'].append(merge_pair)
# #### insert at begining ###
# data_target['model']['merges'].insert(count_rev, merge_pair)
# count_rev+= 1
# if merge_intersect == 1:
# break
data_target['model']['merges'] = data_source['model']['merges']
print("intersected merge is ", merge_intersect)
# print(len(merge_source))
# print(len(data['model']['merges']))
print("should be same length of target merges is after: ", len(data_target['model']['merges']), len(merge_source))
# print(data_source.keys())
with open("tokenizer.json", "w",encoding='utf-8') as outfile:
json.dump(data_target, outfile, indent = 3,ensure_ascii=False)
# print(vocab_dict)
# print(len(vocab_dict))