-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRACE.py
More file actions
596 lines (482 loc) · 23.5 KB
/
RACE.py
File metadata and controls
596 lines (482 loc) · 23.5 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import numpy as np
import torch
import torch.nn.functional as F
import spacy
from itertools import combinations
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModelForCausalLM
from modelscope import AutoTokenizer as MSAutoTokenizer
from modelscope import AutoModel as MSAutoModel
from cot_extraction import generate_response
try:
from selfcheckgpt.modeling_selfcheck import SelfCheckNLI
except ImportError:
print("SelfCheckGPT not installed. Some features will be disabled.")
print("Install with: pip install selfcheckgpt")
class RACEScorer:
"""
RACEscorer.
"""
def __init__(self,
embedding_model_path="/path/to/all-MiniLM-L6-v2",
nli_model_path="/path/to/deberta-v3-large-mnli",
llm_model_path="/path/to/llama-model",
use_gpu=True,
sindex_threshold=0.9):
"""
Initialize RACE scorer.
Args:
embedding_model_path: Path to sentence embedding model
nli_model_path: Path to NLI model for contradiction detection
llm_model_path: Path to LLM for uncertainty estimation
use_gpu: Whether to use GPU for computation
sindex_threshold: Threshold for SIndex clustering
"""
self.device = torch.device("cuda" if torch.cuda.is_available() and use_gpu else "cpu")
self.sindex_threshold = sindex_threshold
print("Loading models...")
# Load embedding model
try:
self.embed_tokenizer = MSAutoTokenizer.from_pretrained(embedding_model_path)
self.embed_model = MSAutoModel.from_pretrained(embedding_model_path).to(self.device)
print(f"Embedding model loaded from {embedding_model_path}")
except Exception as e:
print(f"Failed to load embedding model: {e}")
print("Will use fallback for embedding calculations")
self.embed_tokenizer = None
self.embed_model = None
# Load NLI model for SCG
try:
self.selfcheck_nli = SelfCheckNLI(nli_model=nli_model_path, device=self.device)
print(f"NLI model loaded from {nli_model_path}")
except Exception as e:
print(f"Failed to load NLI model: {e}")
print("Will skip contradiction detection")
self.selfcheck_nli = None
# Load LLM for uncertainty
try:
self.llm_tokenizer = AutoTokenizer.from_pretrained(llm_model_path)
self.llm_model = AutoModelForCausalLM.from_pretrained(
llm_model_path,
device_map="auto" if use_gpu else None,
torch_dtype=torch.float32
)
self.llm_model.set_attn_implementation('eager')
print(f"LLM model loaded from {llm_model_path}")
except Exception as e:
print(f"Failed to load LLM model: {e}")
print("Will skip uncertainty calculation")
self.llm_tokenizer = None
self.llm_model = None
# Load spaCy for entity extraction
try:
self.nlp = spacy.load("en_core_web_trf")
print("SpaCy model loaded for entity extraction")
except Exception as e:
print(f"Failed to load spaCy model: {e}")
print("Will skip entity extraction")
self.nlp = None
try:
with open("datasum_prompt.txt") as f:
self.cot_extract_sys_prompt = f.read()
except Exception as e:
print(f"Failed to load datasum_prompt.txt: {e}")
print("Will skip cot extraction")
self.cot_extract_sys_prompt = None
def mean_pooling(self, model_output, attention_mask):
"""Mean pooling for sentence embeddings"""
token_embeddings = model_output[0] # First element contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def get_embedding(self, sentences, q):
"""Get embeddings for sentences with question context"""
if self.embed_model is None or self.embed_tokenizer is None:
# Fallback to simpler approach if model not available
return [[1.0] * 384 for _ in sentences] # Dummy embeddings
consentences = [f"{q} [SEP] {s}" for s in sentences]
encoded_input = self.embed_tokenizer(consentences, padding=True, truncation=True, return_tensors='pt').to(self.device)
with torch.no_grad():
model_output = self.embed_model(**encoded_input)
# Perform pooling
sentence_embeddings = self.mean_pooling(model_output, encoded_input['attention_mask'])
return sentence_embeddings.tolist()
def compute_cosine_similarity(self, emb1, emb2):
"""Compute cosine similarity between two embeddings"""
return np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
def clustering_algorithm(self, sequences, question, thresh=None):
"""
Clusters sequences using average distance and cosine similarity.
Args:
sequences: List of sequences to be clustered
question: Question for context
thresh: Distance threshold for merging clusters (default: self.sindex_threshold)
Returns:
clusters, embeddings dict, number of total sequences
"""
if thresh is None:
thresh = self.sindex_threshold
sequences = sorted(list(set(sequences)))
ebs = self.get_embedding(sequences, question)
embeddings = {si: eb for si, eb in zip(sequences, ebs)}
clusters = []
for si in sequences:
new_cluster = [si]
need_new = True
for i in range(len(clusters)):
cluster = clusters[i]
cluster_dis = 0
for c in cluster:
emb_si = embeddings[si]
emb_c = embeddings[c]
cos_sim = self.compute_cosine_similarity(emb_si, emb_c)
dis = 1 - cos_sim
cluster_dis += dis
avg_dis = cluster_dis / len(cluster)
if avg_dis < 1 - thresh:
clusters[i] = clusters[i] + new_cluster
need_new = False
break
if need_new:
clusters.append(new_cluster)
assert len(sequences) == sum(len(c) for c in clusters)
# print(len(clusters), "clusters")
return clusters, embeddings, len(sequences)
def compute_sindex(self, total_points, clusters, embeddings):
"""
Calculate the SINdex for a given set of clusters and sequences.
Args:
total_points: Total number of sequences
clusters: List of clusters
embeddings: Dictionary mapping sequences to embeddings
Returns:
SINdex value
"""
k = len(clusters)
p_i = {}
cos_sim_ci = {}
for i, cluster in enumerate(clusters):
p_i[i] = len(cluster) / total_points
total_cos_sim = 0
num_pairs = 0
for seq1, seq2 in combinations(cluster, 2):
emb1 = embeddings[seq1]
emb2 = embeddings[seq2]
total_cos_sim += self.compute_cosine_similarity(emb1, emb2)
num_pairs += 1
if num_pairs > 0:
cos_sim_ci[i] = total_cos_sim / num_pairs
else:
cos_sim_ci[i] = 1 # If the cluster has only one element
p_prime_i = {i: p_i[i] * cos_sim_ci[i] for i in range(k)}
s_index = 0
for i in range(k):
s_index += p_prime_i[i] * np.log(p_prime_i[i])
return -s_index
def calculate_sindex(self, main_answer, sample_answers, question):
"""
Calculate SIndex score for a main answer compared to sample answers.
Args:
main_answer: Primary answer to evaluate
sample_answers: List of alternate answers for comparison
question: The question that was answered
Returns:
SIndex score
"""
all_answers = [main_answer] + sample_answers
all_answers = [a.strip() if a.strip() else " " for a in all_answers]
clusters, embeddings, total_points = self.clustering_algorithm(all_answers, question)
# print(f"Clusters: {clusters}")
# print(f"total_points: {total_points}")
score = self.compute_sindex(total_points, clusters, embeddings)
return score
def get_entities(self, text):
"""Extract named entities from text"""
if self.nlp is None:
return []
doc = self.nlp(text)
return [ent.text for ent in doc.ents]
def calculate_entity_consistency(self, main_text, cot_text):
"""
Calculate entity consistency between reasoning and answer.
Args:
main_text: Main reasoning text
cot_texts: Chain of thought texts
Returns:
Entity consistency score
"""
if self.nlp is None:
return 0.0
try:
think_entities = set(self.get_entities(main_text))
cot_entities = set(self.get_entities(cot_text))
# Clean entities (lowercase, remove leading "the")
def clean(entities):
cleaned = set()
for ent in entities:
e = ent.lower()
if e.startswith("the "):
e = e[4:].strip()
cleaned.add(e)
return cleaned
think_entities = clean(think_entities)
cot_entities = clean(cot_entities)
# Calculate consistency as proportion of entities in think not in cot
if think_entities:
return len(think_entities - cot_entities) / len(think_entities)
return 0
except Exception as e:
print(f"Error in entity consistency calculation: {e}")
return 0
def calculate_uncertainty(self, question, cots, final_answer, cross=True):
"""
Calculate uncertainty of the model in its answer.
Args:
question: The question being answered
cots: List of chain-of-thought reasoning steps
final_answer: The final answer provided
Returns:
Dictionary with uncertainty metrics
"""
if self.llm_model is None or self.llm_tokenizer is None or not final_answer:
return {
"final_answer_entropy": None,
"attention": None
}
if not cots:
return {
"final_answer_entropy": None,
"attention": [1.0] if len(cots) > 0 else None
}
try:
if not cross:
messages_only_q = [
{"role": "user", "content": question},
]
q_id_list = self.llm_tokenizer.apply_chat_template(messages_only_q, add_generation_prompt=True)
q_id_list += self.llm_tokenizer.encode(f"Thought: ", add_special_tokens=False)
think_id_list = []
for cot in cots:
think_id_list.append(self.llm_tokenizer.encode(f"{cot}\n", add_special_tokens=False))
think_id_list[-1] = think_id_list[-1] + self.llm_tokenizer.encode("\n\nAnswer: ", add_special_tokens=False)
final_answer_id_list = self.llm_tokenizer.encode(f"{final_answer}", add_special_tokens=False)
new_think_id_list = []
for tl in think_id_list:
new_think_id_list += tl
input_text = q_id_list + new_think_id_list + final_answer_id_list
think_start = len(q_id_list)-1
think_end = len(q_id_list)+len(new_think_id_list)-1
final_answer_start = think_end
input_ids = torch.tensor([input_text]).to(self.llm_model.device)
attention_mask = torch.ones_like(input_ids)
with torch.no_grad():
outputs = self.llm_model(input_ids, attention_mask=attention_mask, output_attentions=True)
attentions = outputs.attentions
logits = outputs.logits.cpu()
final_answer_logits = logits[0, final_answer_start:-1, :]
final_answer_probs = F.softmax(final_answer_logits, dim=-1)
final_answer_entropy = torch.sum(torch.special.entr(final_answer_probs), dim=-1).tolist()
# Calculate attention from answer to each reasoning step
all_layers_attention = torch.stack(attentions, dim=0)
avg_attention = torch.mean(torch.mean(all_layers_attention, dim=0), dim=1)
cot_attentions = []
cot_start_positions = [0]
cum_length = 0
for i in range(len(think_id_list)):
cum_length += len(think_id_list[i])
cot_start_positions.append(cum_length)
for i in range(len(think_id_list)):
start_idx = think_start + cot_start_positions[i]
if i < len(think_id_list) - 1:
end_idx = think_start + cot_start_positions[i+1]
else:
end_idx = think_end
cot_attention = avg_attention[0, final_answer_start:-1, start_idx:end_idx]
avg_cot_attention = torch.mean(torch.mean(cot_attention, dim=1))
cot_attentions.append(avg_cot_attention.item())
if sum(cot_attentions) > 0:
cot_attentions = [att / sum(cot_attentions) for att in cot_attentions]
return {
"final_answer_entropy": final_answer_entropy,
"attention": cot_attentions
}
else:
messages_only_q = [
{"role": "user", "content": question},
]
q_id_list = self.llm_tokenizer.apply_chat_template(messages_only_q, add_generation_prompt=True)
think = "\n".join(cots)
think_id_list = self.llm_tokenizer.encode(f"Thought: {think}\n\nAnswer: ", add_special_tokens=False)
final_answer_id_list = self.llm_tokenizer.encode(f"{final_answer}", add_special_tokens=False)
input_text = q_id_list + think_id_list + final_answer_id_list
think_start = len(q_id_list)-1
think_end = len(q_id_list)+len(think_id_list)-1
final_answer_start = think_end
input_ids = torch.tensor([input_text]).to(self.llm_model.device)
attention_mask = torch.ones_like(input_ids)
with torch.no_grad():
outputs = self.llm_model(input_ids, attention_mask=attention_mask, output_attentions=True)
logits = outputs.logits.cpu()
final_answer_logits = logits[0, final_answer_start:-1, :]
final_answer_probs = F.softmax(final_answer_logits, dim=-1)
final_answer_prob = []
for i in range(len(final_answer_probs)):
tokenid = final_answer_id_list[i]
prob = final_answer_probs[i][tokenid].item()
final_answer_prob.append(prob)
final_answer_entropy = torch.sum(torch.special.entr(final_answer_probs), dim=-1).tolist()
return {
"final_answer_entropy": final_answer_entropy,
"attention": None
}
except Exception as e:
print(f"Error in uncertainty calculation: {e}")
return {
"final_answer_entropy": None,
"attention": None
}
def calculate_cross_uncertainty(self, question, cots, sample_answers):
"""
Calculate uncertainty across different answers using the same reasoning.
Args:
question: The question being answered
cots: List of chain-of-thought reasoning steps
main_answer: The main answer
sample_answers: List of alternative answers
Returns:
Average entropy across answers
"""
if self.llm_model is None or not sample_answers:
return 10.0 # Default high uncertainty
entropies = []
try:
for answer in sample_answers:
if not answer:
continue
result = self.calculate_uncertainty(question, cots, answer)
if result and result["final_answer_entropy"]:
entropies.append(np.mean(result["final_answer_entropy"]))
# print(f"Entropies: {entropies}")
if not entropies:
return 10.0
return np.mean(entropies)
except Exception as e:
print(f"Error in cross uncertainty calculation: {e}")
return 10.0
def calculate_consistency_score(self, main_cots, sample_cots_list, uncertainties):
"""
Calculate consistency scores for chain of thought steps
Args:
cots: Chain of thought reasoning steps
uncertainties: Uncertainty values from LLM
Returns:
Consistency score
"""
if not main_cots or not sample_cots_list:
return 10.0 # Default high uncertainty
try:
if uncertainties and uncertainties["attention"]:
weights = uncertainties["attention"]
else:
weights = [1/len(main_cots) for _ in range(len(main_cots))]
if self.selfcheck_nli:
# Get contradiction scores between CoT steps
cot_consistency = self.selfcheck_nli.predict(
sentences=main_cots,
sampled_passages=["\n".join(sample_cots) for sample_cots in sample_cots_list]
).tolist()
# Weight by attention
score = 0
for w, consistency in zip(weights, cot_consistency):
score += w * consistency
return score
else:
# Fallback if NLI model not available
return 5.0
except Exception as e:
print(f"Error in consistency calculation: {e}")
return 5.0
def calculate_race_score(self, main_data, sample_data):
"""
Calculate the overall RACE score for a given main output and sample outputs.
Args:
main_data: Dictionary with main output data (question, final_answer, think, Option(cots))
sample_data: Dictionary with sample outputs data (final_answer, cots / think)
Returns:
Dictionary with components and final RACE score
"""
question = main_data.get("question", "")
main_answer = main_data.get("final_answer", "")
main_reasoning = main_data.get("think", "")
# main_cots = main_data.get("cots", [])
sample_answers = sample_data.get("final_answer", [])
# sample_cots = sample_data.get("cots", [])
if "cots" in main_data:
main_cots = main_data["cots"]
else:
main_cots = []
if main_answer:
cot = generate_response(
f"## Question\n{question}\n\n## Thought\n{main_reasoning}\n\n## Final Answer\n{main_answer.strip()}",
self.llm_model, self.llm_tokenizer, self.cot_extract_sys_prompt
)
main_cots = cot.split("[STEP]")
main_cots = [c.strip() for c in main_cots if c.strip()]
if "cots" in sample_data:
sample_cots = sample_data["cots"]
else:
assert "think" in sample_data, "think is required in sample_data if cot is not provided"
assert len(sample_answers) == len(sample_data["think"]), "think and final_answer must have the same length"
sample_cots = []
for think, sample_answer in zip(sample_data["think"], sample_answers):
if not sample_answer.strip():
sample_cots.append([])
else:
cot = generate_response(
f"## Question\n{question}\n\n## Thought\n{think}\n\n## Final Answer\n{sample_answer.strip()}",
self.llm_model, self.llm_tokenizer, self.cot_extract_sys_prompt
)
cots = cot.split("[STEP]")
cots = [c.strip() for c in cots if c.strip()]
sample_cots.append(cots)
# Calculate uncertainty component (weight for S_CC)
uncertainty = self.calculate_uncertainty(question, main_cots, main_answer, False)
# Calculate cross-uncertainty (with other answers) (S_CA)
cross_uncertainty = self.calculate_cross_uncertainty(
question, main_cots, sample_answers
)
# Calculate consistency component (S_CC)
consistency = self.calculate_consistency_score(main_cots, sample_cots, uncertainty)
# Calculate entity consistency (S_Coh)
entity_consistency = self.calculate_entity_consistency(main_reasoning, "\n".join(main_cots))
# Calculate SIndex score (S_AA)
sindex = self.calculate_sindex(main_answer, sample_answers, question)
# Combine components into overall RACE score
race_score = consistency + cross_uncertainty + sindex
if entity_consistency > 0:
race_score += entity_consistency
return {
"race_score": race_score,
"components": {
"uncertainty": uncertainty,
"cross_uncertainty": cross_uncertainty,
"consistency": consistency,
"entity_consistency": entity_consistency,
"sindex": sindex
}
}
def batch_calculate_race(self, main_dataset, sample_dataset):
"""
Calculate RACE scores for a batch of data
Args:
main_dataset: List of main output data
sample_dataset: List of sample output data
Returns:
List of RACE scores and components
"""
results = []
for main_item, sample_item in tqdm(zip(main_dataset, sample_dataset),
total=min(len(main_dataset), len(sample_dataset)),
desc="Calculating RACE scores"):
score = self.calculate_race_score(main_item, sample_item)
results.append(score)
return results