-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathutils.py
289 lines (250 loc) · 9.74 KB
/
utils.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import re
from copy import deepcopy
from typing import Dict, Optional
import fishfarm
import torch
import torch.utils
import vllm
def load_hf_params_to_vllm(param: Dict, llm: vllm.LLM) -> None:
"""Load weights from HF transformer model to vLLM model."""
model = llm.llm_engine.model_executor.driver_worker.model_runner.model
num_layers = model.config.num_hidden_layers
# Load embeddings layer weights.
model_param = model.get_parameter("model.embed_tokens.weight")
model_param.copy_(
param["model.embed_tokens.weight"][: model_param.shape[0]]
.to(model_param.dtype)
.to(model_param.device)
)
model_param = model.get_parameter("lm_head.weight")
model_param.copy_(
param["lm_head.weight"][: model_param.shape[0]]
.to(model_param.dtype)
.to(model_param.device)
)
# Load the final layernorm weights.
model_param = model.get_parameter("model.norm.weight")
model_param.copy_(
param["model.norm.weight"].to(model_param.dtype).to(model_param.device)
)
for i in range(num_layers):
# Load qkv_proj weights.
model_param = model.get_parameter(f"model.layers.{i}.self_attn.qkv_proj.weight")
model_param.copy_(
torch.cat(
[
param[f"model.layers.{i}.self_attn.q_proj.weight"],
param[f"model.layers.{i}.self_attn.k_proj.weight"],
param[f"model.layers.{i}.self_attn.v_proj.weight"],
],
dim=0,
)
.to(model_param.dtype)
.to(model_param.device)
)
# Load gate_up_proj weights.
model_param = model.get_parameter(f"model.layers.{i}.mlp.gate_up_proj.weight")
model_param.copy_(
torch.cat(
[
param[f"model.layers.{i}.mlp.gate_proj.weight"],
param[f"model.layers.{i}.mlp.up_proj.weight"],
],
dim=0,
)
.to(model_param.dtype)
.to(model_param.device)
)
# Load o_proj and down_proj weights.
model_param = model.get_parameter(f"model.layers.{i}.self_attn.o_proj.weight")
model_param.copy_(
param[f"model.layers.{i}.self_attn.o_proj.weight"]
.to(model_param.dtype)
.to(model_param.device)
)
model_param = model.get_parameter(f"model.layers.{i}.mlp.down_proj.weight")
model_param.copy_(
param[f"model.layers.{i}.mlp.down_proj.weight"]
.to(model_param.dtype)
.to(model_param.device)
)
# Load layer_norm weights.
model_param = model.get_parameter(f"model.layers.{i}.input_layernorm.weight")
model_param.copy_(
param[f"model.layers.{i}.input_layernorm.weight"]
.to(model_param.dtype)
.to(model_param.device)
)
model_param = model.get_parameter(
f"model.layers.{i}.post_attention_layernorm.weight"
)
model_param.copy_(
param[f"model.layers.{i}.post_attention_layernorm.weight"]
.to(model_param.dtype)
.to(model_param.device)
)
def eval_model(vllm_model, evaluator, ix=None):
result = evaluator.evaluate(vllm_model, sample_ids=ix)
return result
def compose_new_params(
policy,
param_name,
decomposed_params,
learnable_params,
):
"""Compose new parameters from decomposed parameters."""
mm = policy.get_mask(learnable_params[param_name])
return (
decomposed_params[f"{param_name}.U"]
@ torch.diag_embed(decomposed_params[f"{param_name}.S"] * mm)
@ decomposed_params[f"{param_name}.V"].T
) * (
decomposed_params[f"{param_name}.S"].sum()
/ (decomposed_params[f"{param_name}.S"] * mm).sum()
)
@torch.no_grad()
def forward(policy, model, base_params, decomposed_params, learnable_params):
"""Forward pass."""
new_params = {}
for k in base_params:
if "mlp" in k:
new_params[k] = compose_new_params(
policy, k, decomposed_params, learnable_params
)
model.get_parameter(k).copy_(new_params[k])
else:
new_params[k] = base_params[k]
return new_params
@torch.no_grad()
def load_base_params(
model,
base_params,
):
for k in base_params:
if "mlp" in k:
model.get_parameter(k).copy_(base_params[k].cuda())
def backward(
policy,
model,
base_params,
decomposed_params,
learnable_params,
):
"""Backward pass."""
keys_to_backprop = [k for k in base_params if "mlp" in k]
last_key = keys_to_backprop[-1]
for k in keys_to_backprop[:-1]:
compose_new_params(policy, k, decomposed_params, learnable_params).backward(
model.get_parameter(k).grad, retain_graph=True
)
# release graph
compose_new_params(policy, last_key, decomposed_params, learnable_params).backward(
model.get_parameter(last_key).grad, retain_graph=False
)
def classify_samples(vllm_model, test_eval):
"""Classify samples."""
CLASSIFICATION_PROMPT = """
# Analyze the given question and classify it into one of four categories: 'code', 'math', 'reasoning' or 'other'. Follow these guidelines:
1. Code: Questions asking for programming solutions, functions, algorithms. Often includes specific programming terms, language syntax, or data structures.
2. Math: Questions involving mathematical calculations, formulas, statistics. Often includes numbers, equations, or mathematical operations.
3. Reasoning: Questions requiring logical thinking, application of scientific knowledge, or critical analysis of information. Often presents statements that need evaluation based on general understanding.
4. Other: Questions not clearly fit into above categories.
Instructions:
- Consider the primary focus, skills, and knowledge required to answer the question.
- If a question spans multiple categories, choose the most dominant one.
- Provide your final classification within \\boxed{} notation. Example: \\boxed{reasoning}
Format your response as follows:
Classification: \\boxed{category}
"""
def extract_classification(text: str) -> Optional[str]:
"""
Extract the classification from the model's output using regex.
"""
match = re.search(r"\\boxed{([^}]*)}", text)
return match.group(1) if match else None
# Identify the key in the samples that contains the problem text
problem_key = None
for key in ("problem", "question", "instruction"):
if (
hasattr(test_eval.samples[0], key)
and getattr(test_eval.samples[0], key) is not None
):
problem_key = key
break
assert problem_key is not None, "Could not find problem text in the samples"
# Prepare classification requests
classification_requests = [
fishfarm.models.GenerationRequest(
messages=[
fishfarm.Message("system", CLASSIFICATION_PROMPT),
fishfarm.Message("user", getattr(sample, problem_key)),
]
)
for sample in test_eval.samples
]
# Generate classifications using the model
model_outputs = vllm_model.generate(classification_requests)
# Process results and update samples
classified_samples = []
for sample, result in zip(test_eval.samples, model_outputs):
prediction = extract_classification(result.generation)
if prediction not in ["code", "math", "reasoning"]:
prediction = "other"
sample.expert_label = prediction
classified_samples.append(sample)
return classified_samples
def eval_model_experts_prompt_based(
vllm_model,
evaluator,
experts_path_dict,
policy,
model,
base_params,
decomposed_params,
task_metric,
):
"""Evaluate the model using expert models and prompt-based classification."""
results_by_expert: Dict[str, Dict] = {}
# Classify all test samples
classified_samples = classify_samples(vllm_model, evaluator)
# Evaluate samples for each expert model
for expert_label, expert_model_path in experts_path_dict.items():
# Filter samples for current expert
expert_samples = [
sample
for sample in classified_samples
if sample.expert_label == expert_label
]
if not expert_samples:
continue
# Update test evaluation with filtered samples
evaluator.samples = expert_samples
# Load and apply expert model parameters if available
if expert_model_path:
policy.load_state_dict(torch.load(expert_model_path))
expert_params = policy.get_learnable_params()
updated_params = forward(
policy=policy,
model=model,
base_params=base_params,
decomposed_params=decomposed_params,
learnable_params=expert_params,
)
load_hf_params_to_vllm(updated_params, vllm_model.llm)
# Evaluate current expert model
evaluation_results = eval_model(vllm_model, evaluator)
# Store results for current expert
results_by_expert[expert_label] = {
"num_samples": len(expert_samples),
"test_acc": evaluation_results.aggregate_metrics[task_metric],
}
# Compute the overall accuracy.
data_dict = deepcopy(results_by_expert)
data_dict["final_test_acc"] = 0.0
for label in results_by_expert.keys():
data_dict["final_test_acc"] += (
results_by_expert[label]["test_acc"]
* results_by_expert[label]["num_samples"]
)
data_dict["final_test_acc"] /= len(classified_samples)
return data_dict