-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrobustft.py
More file actions
221 lines (182 loc) · 6.46 KB
/
Copy pathrobustft.py
File metadata and controls
221 lines (182 loc) · 6.46 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
210
211
212
213
214
215
216
217
218
219
220
import random
import pandas as pd
import os
import copy
import numpy as np
from common import *
from eval import Eval
import threading
from config import *
from rag import NearestReference
import argparse
def calculate_entropy(probs):
prob_list = np.array(probs)
entropy = - np.sum(prob_list) / len(prob_list)
return entropy
# Add argument parser
parser = argparse.ArgumentParser(description='Noisy Free Fine-tuning')
parser.add_argument('--task', type=str, default='mmlu', help='Task name')
parser.add_argument('--model', type=str, default='llama3.1-8b', help='Model name')
parser.add_argument('--noise_ratio', type=int, default=50, help='Noise ratio')
parser.add_argument('--base_url', type=str, default='http://localhost:8002/v1', help='Base URL')
args = parser.parse_args()
task = args.task
model = args.model
noise_ratio = args.noise_ratio
base_url = args.base_url
if model not in MODELS_CONFIG:
raise ValueError(f"Model {model} not found in MODELS_CONFIG")
if task not in TASK_CONFIG:
raise ValueError(f"Task {task} not found in TASK_CONFIG")
model_config = MODELS_CONFIG[model]
os.environ['LLM_BASE_URL'] = base_url
if 'OPENAI_API_KEY' in model_config:
os.environ['OPENAI_API_KEY'] = model_config['OPENAI_API_KEY']
infer_config = {
'type': model_config["method"],
'task': task,
'config': {
"model": model_config['name'],
"temperature": 1,
"max_tokens": 512,
"logprobs": True
}
}
eval_config = TASK_CONFIG[task]
question_type = eval_config['question_type']
check_fn = eval_config['check_fn']
# Noisy Path
noisy_dir = f'./data/{task}/noisy'
noisy_labeled_path = f'{noisy_dir}/noisy{noise_ratio}.csv'
# Denoise Path
denoise_dir = f'./data/{task}-{model}/denoise'
os.makedirs(denoise_dir, exist_ok=True)
denoise_path = f'{denoise_dir}/denoise{noise_ratio}.csv'
# labeled data path
labeled_path = f'./data/{task}/labeled.csv'
# Read Noisy Data
if not os.path.exists(noisy_labeled_path):
raise FileNotFoundError(f"Noisy labeled file not found: {noisy_labeled_path}")
try:
all_data_df = pd.read_csv(noisy_labeled_path)
except Exception as e:
raise Exception(f"Error reading noisy labeled file: {e}")
all_data = []
for row1 in all_data_df.iterrows():
d = row1[1].to_dict()
d['question_type'] = question_type.lower()
d['additional_prompt'] = eval_config['additional_prompt']
all_data.append(d)
## Only for Debug
# num_samples = 10
# all_data = random.Random(0).sample(all_data, num_samples)
"""
Noise Detection
"""
vanilla_predictions = []
reasoning_enhanced_predictions = []
"""
Vanilla inference
"""
inference_data = copy.deepcopy(all_data)
vanilla_eval = Eval(samples=inference_data, **infer_config)
_ = vanilla_eval.eval(
format_fn=format_question_vanilla,
check_fn=eval_config['check_fn'],
extract_fn=extract_result
)
vanilla_predictions = vanilla_eval.get_results()
"""
Reasoning-enhanced inference
"""
inference_data = copy.deepcopy(all_data)
reasoning_enhanced_eval = Eval(samples=inference_data, **infer_config)
_ = reasoning_enhanced_eval.eval(
format_fn=format_reasoning_enhance_question,
check_fn=eval_config['check_fn'],
extract_fn=extract_result
)
reasoning_enhanced_predictions = reasoning_enhanced_eval.get_results()
reasoning_enhanced_entropy_list = [calculate_entropy(infer['logprobs']) for infer in reasoning_enhanced_predictions]
"""
Data Split
"""
clean_samples = []
noisy_samples = []
data_packed = []
for i in range(len(vanilla_predictions)):
data_ = all_data[i]
potential_answer = data_['answer']
vanilla_pred = vanilla_predictions[i]['PredAnswer']
reasoning_pred = reasoning_enhanced_predictions[i]['PredAnswer']
reasoning_entropy = reasoning_enhanced_entropy_list[i]
data_['vanilla_prediction'] = vanilla_pred
data_['reasoning_prediction'] = reasoning_pred
data_['potential_answer'] = potential_answer
data_['reasoning_entropy'] = reasoning_entropy
if check_fn(vanilla_pred, potential_answer) and check_fn(reasoning_pred, potential_answer):
data_['PseudoLabel'] = potential_answer
data_['clean_flag'] = 1
clean_samples.append(data_)
else:
data_['clean_flag'] = 0
noisy_samples.append(data_)
"""
Noise Data Re-labeling
Context-enhanced relabeling
"""
nr = NearestReference(k=3)
nr.embed_data(clean_samples, f'tmp/{task}_noisy{noise_ratio}_clean')
def format_context_enhance_question_with_reference(data):
ref_str = nr.fewshot(data)
data['reference'] = ref_str
data['additional_prompt'] = data['additional_prompt']
data['question_type'] = data['question_type']
context_user_prompt = format_context_enhance_question(data)
return context_user_prompt
context_data = copy.deepcopy(noisy_samples)
context_enhance_eval = Eval(samples=context_data, **infer_config)
context_enhance_acc = context_enhance_eval.eval(
format_fn=format_context_enhance_question_with_reference,
check_fn=eval_config['check_fn'],
extract_fn=extract_result
)
context_enhance_res_list = context_enhance_eval.get_results()
context_enhance_entropy_list = [calculate_entropy(infer['logprobs']) for infer in context_enhance_res_list]
"""
Review
"""
data_for_review = []
for i in range(len(context_enhance_res_list)):
data_ = noisy_samples[i]
data_['context_prediction'] = context_enhance_res_list[i]['PredAnswer']
data_for_review.append(data_)
review_data = copy.deepcopy(data_for_review)
review_eval = Eval(samples=review_data, **infer_config)
_ = review_eval.eval(
format_fn=format_review_question,
check_fn=check_answer,
extract_fn=extract_result
)
"""
Data Selection
"""
review_res_list = review_eval.get_results()
review_entropy_list = [calculate_entropy(infer['logprobs']) for infer in review_res_list]
data_to_sft = []
for i in range(len(review_res_list)):
data_ = data_for_review[i]
data_['context_entropy'] = context_enhance_entropy_list[i]
data_['reasoning_entropy'] = reasoning_enhanced_entropy_list[i]
data_['review_entropy'] = review_entropy_list[i]
data_['PredAnswer'] = review_res_list[i]['PredAnswer']
data_['PseudoLabel'] = review_res_list[i]['PredAnswer']
if data_['context_entropy'] < np.percentile(context_enhance_entropy_list, 50):
data_to_sft.append(data_)
combined_samples = clean_samples + data_to_sft
combined_df = pd.DataFrame(combined_samples)
combined_df.to_csv(denoise_path, index=False)
print(f'=== Noise Data Re-labeling ===')
print(f'Combined Samples: {len(combined_samples)}')
print(f'Save to {denoise_path}')
print(f'========================')