-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_processor.py
198 lines (153 loc) · 5.63 KB
/
data_processor.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
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
import subprocess
import ast
from args import printd
def run_parser(parser: str, func_name: str, code_only: bool) -> str:
parser_args = [parser, "-func", func_name]
if code_only:
parser_args.append("-code")
result = subprocess.run(parser_args,
text=True, # Output will be treated as string.
capture_output=True) # Capture stdout and stderr.
# Check the return code of the binary.
if result.returncode != 0:
# If the exit code is non-zero, raise an exception with stdout and stderr.
raise Exception(f"Parser error: {result.stderr}")
source_code = result.stdout.strip()
if source_code == "":
raise Exception("Parser returned empty result")
return source_code # Return the captured output
# Processor for fine-tuned models
class FineTuneProcessor:
def __init__(self, tokenizer, seq2seq, split: str, max_encode_length: int):
self.tokenizer = tokenizer
self.seq2seq = seq2seq
self.split_tokens = tokenizer.encode(split, add_special_tokens=False)
self.max_encode_length = max_encode_length
if seq2seq == "t5" or seq2seq == "t5-ft":
self.max_encode_length -= 2
def encode(self, source_code: str):
suffix_tokens = self.split_tokens
if self.seq2seq:
suffix_tokens = [] # Do not add split tokens for seq2seq models
input_ids = encode(
self.tokenizer, self.max_encode_length, source_code,
suffix_tokens=suffix_tokens,
)
if self.seq2seq == "t5" or self.seq2seq == "t5-ft":
input_ids = [self.tokenizer.bos_token_id] + input_ids + [self.tokenizer.eos_token_id]
return input_ids, [] # Leave system empty
def stop_token(self):
# return self.tokenizer.eos_token
return '\n' # Workaround FT
def extract(self, output: str) -> list[str]:
if len(output) > 0:
return [output]
return []
# Processor for prompt-tuning
class PromptTuneProcessor:
def __init__(
self, tokenizer, seq2seq, max_encode_length: int, count: int,
prefix: str, suffix: str, multi_vals: bool, code_only: bool, stop: str = ""
):
self.tokenizer = tokenizer
self.seq2seq = seq2seq
self.max_encode_length = max_encode_length
if seq2seq == "t5" or seq2seq == "t5-ft":
self.max_encode_length -= 3
self.multi_vals = multi_vals
prefix = prefix.replace("<count>", str(count))
suffix = suffix.replace("<count>", str(count))
self.prefix_tokens = tokenizer.encode(prefix, add_special_tokens=False)
self.suffix_tokens = tokenizer.encode(suffix, add_special_tokens=False)
if stop == "":
stop = tokenizer.eos_token
self.stop = stop
self.start_with_string = False
if len(suffix) > 0 and (suffix[-1] == '"' or suffix[-1] == "`"):
self.start_with_string = suffix[-1]
self.check_suffix_tokens = []
if not code_only:
self.check_suffix_tokens = tokenizer.encode("\n```", add_special_tokens=False)
def encode(self, source_code: str):
input_ids = encode(
self.tokenizer, self.max_encode_length, source_code,
prefix_tokens=self.prefix_tokens,
suffix_tokens=self.suffix_tokens,
check_suffix_tokens=self.check_suffix_tokens,
)
if self.seq2seq == "t5" or self.seq2seq == "t5-ft":
input_ids = [self.tokenizer.bos_token_id] + input_ids + [self.tokenizer.extra_token_id, self.tokenizer.eos_token_id]
return input_ids, self.prefix_tokens
def stop_token(self):
return self.stop
def extract(self, raw: str) -> list[str]:
if len(raw) == 0:
return []
output = raw
if self.start_with_string and output[0] != self.start_with_string:
output = self.start_with_string + output
seeds = []
for line in output.splitlines():
values = extract_values(line, self.multi_vals)
if len(values) == 0:
continue
for seed in values:
# The output contains a string in source code, try to un-escape it
seed = parse_escaped(seed)
if len(seed) > 0:
seeds.append(seed)
if len(seeds) == 0: # Make sure to always return something when extraction failed
output = parse_escaped(raw)
return [output]
return seeds
def encode(tokenizer, max_encode_length: int, text: str, prefix_tokens = [], suffix_tokens = [], check_suffix_tokens = []):
max_length = max_encode_length - len(prefix_tokens) - len(suffix_tokens) - len(check_suffix_tokens)
if max_length <= 0:
raise Exception("Encode length too small")
encoded = tokenizer.encode(text, truncation=True, max_length=max_length, add_special_tokens=False)
if len(encoded) >= max_length:
print(" Warning: input length >= max encode length, prompt truncated")
if len(check_suffix_tokens) > 0 and encoded[-len(check_suffix_tokens):] != check_suffix_tokens:
suffix_tokens = check_suffix_tokens + suffix_tokens
encoded = prefix_tokens + encoded + suffix_tokens
if len(encoded) > max_encode_length:
raise Exception("Encoded length too large")
return encoded
def extract_values(line: str, multi_vals: bool) -> list[str]:
values = []
value, i = extract_value(line)
if value != "":
values.append(value)
if not multi_vals or i == -1:
return values
# Handle one-line cases like []string{"value1", "value2"}
while True:
line = line[i+1:]
value, i = extract_value(line)
if value != "":
values.append(value)
if i == -1:
break
return values
def extract_value(line: str) -> str:
stringType = '"'
start = 0
start = line.find('"')
startBT = line.find('`')
if start == -1 or (startBT != -1 and startBT < start):
start = startBT
stringType = '`'
if start == -1:
return "", -1
start += 1
# iterate from the start to the end
for i in range(start, len(line)):
if line[i] == stringType and (i == 0 or line[i-1] != '\\'):
return line[start:i], i
return "", -1
def parse_escaped(value):
str_val = '"'+value+'"'
try:
return ast.literal_eval(str_val)
except SyntaxError: # Just return the original value on failure
return value