forked from ssbuild/chatglm_finetuning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_processer.py
150 lines (130 loc) · 5.91 KB
/
data_processer.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
140
141
142
143
144
145
146
147
148
149
150
# @Time : 2023/3/25 18:36
# @Author : tk
import random
import typing
from enum import Enum
import numpy as np
from models import ChatGLMTokenizer
class DataStrategy(Enum):
truncation = 1
singlesliding = 2
doublesliding = 3
class TokenIdsFinal:
@classmethod
def process(cls,input_ids: typing.List,sptoken,max_seq_length,tokenizer):
ctxlen = input_ids.index(sptoken[-1])
mask_position = ctxlen - 1
labels = [-100] * ctxlen + input_ids[mask_position + 1:]
seqlen = np.asarray(len(input_ids), dtype=np.int32)
pad_len = max_seq_length - seqlen
input_ids = np.asarray(input_ids, dtype=np.int32)
labels = np.asarray(labels, dtype=np.int32)
ctxlen = np.asarray(ctxlen, dtype=np.int32)
if pad_len:
pad_val = tokenizer.pad_token_id
input_ids = np.pad(input_ids, (0, pad_len), 'constant', constant_values=(pad_val, pad_val))
labels = np.pad(labels, (0, pad_len), 'constant', constant_values=(-100, -100))
d = {
'input_ids': input_ids,
'labels': labels,
'seqlen': seqlen,
'ctxlen': ctxlen
}
return d
#对prompt 截断
class TokenTruncation:
@classmethod
def process(cls, tokenizer: ChatGLMTokenizer,config, a_ids, b_ids, max_seq_length, sptoken: typing.List,ensure_answer_min_length=1):
ds = []
assert ensure_answer_min_length > 0
input_ids_qa = a_ids[:max_seq_length-len(sptoken)-ensure_answer_min_length] + sptoken + b_ids + [config.eos_token_id] * 2
pos = 0
while pos < len(input_ids_qa):
if sptoken[0] in input_ids_qa[pos:pos + max_seq_length]:
val = input_ids_qa[pos:pos + max_seq_length][-1]
if val == sptoken[-1]:
input_ids = input_ids_qa[pos+1:pos + max_seq_length+1]
pos += max_seq_length + 1
elif val == sptoken[0]:
input_ids = input_ids_qa[pos + 2:pos + max_seq_length + 2]
pos += max_seq_length + 2
else:
input_ids = input_ids_qa[pos:pos + max_seq_length]
pos += max_seq_length
else:
input_ids = sptoken + input_ids_qa[pos:pos + max_seq_length - 2]
pos += max_seq_length - 2
d = TokenIdsFinal.process(input_ids,sptoken,max_seq_length,tokenizer)
ds.append(d)
return ds
#对prompt sliding
class TokenSingleSliding:
@classmethod
def process(cls,tokenizer: ChatGLMTokenizer,config,a_ids,b_ids,max_seq_length,sptoken: typing.List,sliding_size,p=1):
ds = []
input_ids_qa = a_ids + sptoken + b_ids + [config.eos_token_id] * 2
a_length = len(a_ids)
pos = 0
assert sliding_size < max_seq_length - 2
while pos < len(input_ids_qa):
if pos + max_seq_length <= a_length:
input_ids = input_ids_qa[pos:pos + max_seq_length-2]
if p > 0:
input_ids = input_ids[0:-p] + sptoken + input_ids[-p:]
else:
p = random.randint(0,max_seq_length-2)
input_ids = input_ids[0:p] + sptoken + input_ids[p:]
pos += sliding_size
elif sptoken[0] in input_ids_qa[pos:pos + max_seq_length]:
val = input_ids_qa[pos:pos + max_seq_length][-1]
if val == sptoken[-1]:
input_ids = input_ids_qa[pos + 1:pos + max_seq_length + 1]
pos += max_seq_length + 1
elif val == sptoken[0]:
input_ids = input_ids_qa[pos + 2:pos + max_seq_length + 2]
pos += max_seq_length + 2
else:
input_ids = input_ids_qa[pos:pos + max_seq_length]
pos += max_seq_length
else:
input_ids = sptoken + input_ids_qa[pos:pos + max_seq_length - 2]
pos += max_seq_length - 2
d = TokenIdsFinal.process(input_ids, sptoken, max_seq_length, tokenizer)
ds.append(d)
return ds
# 对prompt sliding
class TokenDoubleSliding:
@classmethod
def process(cls, tokenizer: ChatGLMTokenizer,config, a_ids, b_ids, max_seq_length, sptoken: typing.List, sliding_size,
p=1):
ds = []
input_ids_qa = a_ids + sptoken + b_ids + [config.eos_token_id] * 2
a_length = len(a_ids)
pos = 0
assert sliding_size < max_seq_length - 2
while pos < len(input_ids_qa):
if pos + max_seq_length <= a_length:
input_ids = input_ids_qa[pos:pos + max_seq_length - 2]
if p > 0:
input_ids = input_ids[0:-p] + sptoken + input_ids[-p:]
else:
p = random.randint(0, max_seq_length - 2)
input_ids = input_ids[0:p] + sptoken + input_ids[p:]
pos += sliding_size
elif sptoken[0] in input_ids_qa[pos:pos + max_seq_length]:
val = input_ids_qa[pos:pos + max_seq_length][-1]
if val == sptoken[-1]:
input_ids = input_ids_qa[pos + 1:pos + max_seq_length + 1]
pos += max_seq_length + 1
elif val == sptoken[0]:
input_ids = input_ids_qa[pos + 2:pos + max_seq_length + 2]
pos += max_seq_length + 2
else:
input_ids = input_ids_qa[pos:pos + max_seq_length]
pos += sliding_size
else:
input_ids = sptoken + input_ids_qa[pos:pos + max_seq_length - 2]
pos += sliding_size
d = TokenIdsFinal.process(input_ids, sptoken, max_seq_length, tokenizer)
ds.append(d)
return ds