File size: 37,624 Bytes
da66274 |
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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 |
import json
import os
import re
import torch
import torch.nn.functional as F
from typing import Optional, Sequence, List, Set, Dict, Any, Union
import transformers
import logging
from dataclasses import dataclass
import pathlib
from torch.utils.data import DataLoader
from utils.constants import DEFAULT_BOS_TOKEN, DEFAULT_EOS_TOKEN, IGNORE_INDEX
import pdb
def read_jsonl(path: str):
try:
with open(path) as fh:
return [json.loads(line) for line in fh.readlines() if line]
except:
return json.load(open(path, 'r', encoding= 'utf-8'))
def get_few_shot_prompt(data_dir, prompt_file):
with open(os.path.join(data_dir, prompt_file), 'r') as f:
prompt = f.read()
return prompt.replace('{', '{{').replace('}', '}}').replace('{{test_question}}', '{test_question}')
def handle_jsonfile_psv(json_list):
def get_label(result ,output):
if result['status'] == 'pass':
return -1
elif result['status'] in ['nopass_limit', 'nopass_error']:
return 0
else:
if result['string_pos'] == -1:
return 0
return result['string_pos']
all_results = []
idx = 0
for item in json_list:
dict_item = {
"idx": idx,
"question": item['question'],
"input": item['question'],
"ground_truth_cot": item['answer'],
"ground_truth":item['answer'],
"outputs": [
{
"response": item['total output'][id].split("#align")[0],
"label": get_label(item['results'][id], item['total output'][id].split("#align")[0]),
}
for id in range(len(item['results']))
],
}
idx += 1
all_results.append(dict_item)
pass1 = 0
pass5 = 0
for item in all_results:
pass1 += item['outputs'][0]['label'] == -1
for output in item['outputs']:
if output['label'] == -1:
pass5 += 1
break
print(pass1/len(all_results))
print(pass5/len(all_results))
return all_results
def handle_jsonfile(json_list):
def get_label(result,output):
if result['status'] == 'pass':
return True
else:
for me in json.loads(result['stdout'])['messages']:
if me['severity'] == 'error':
endpos = me['endPos']
return endpos
assert True
all_results = []
idx = 0
for item in json_list:
dict_item = {
"idx": idx,
"question": item['question'],
"input": item['question'],
"ground_truth_cot": item['answer'],
"ground_truth":item['answer'],
"outputs": [
{
"response": item['total output'][id].split("#align")[0],
"label": item['results'][id]['status'] == 'pass',
}
for id in range(len(item['results']))
],
}
idx += 1
all_results.append(dict_item)
return all_results
def get_model_solutions_easy(data_dir, generator_id, target_set, process : bool = False ):
examples = []
for dd in data_dir.split(","):
examples += read_jsonl(dd)['results']
examples = handle_jsonfile(examples)
print(f"{len(examples)} {target_set} examples, each with {len(examples[0]['outputs'])} solutions")
return examples
def get_model_solutions_psv(data_dir, generator_id, target_set, process : bool = False ):
examples = []
for dd in data_dir.split(","):
examples += read_jsonl(dd)['results']
examples = handle_jsonfile_psv(examples)
print(f"{len(examples)} {target_set} examples, each with {len(examples[0]['outputs'])} solutions")
return examples
def get_model_solutions(data_dir, generator_id, target_set, process : bool = False ):
data_dir = os.path.join(data_dir, target_set)
if process:
files_pattern = f'responses_n*_{generator_id}_process.jsonl'
else:
files_pattern = f'responses_n*_{generator_id}.jsonl'
response_files = [str(x) for x in pathlib.Path(data_dir).glob(files_pattern)]
if not response_files:
raise ValueError(f'Fail to find {files_pattern} in {data_dir}')
ordering_and_response_path = []
for response_file in response_files:
regex_match = re.match(r".*responses_n([0-9]+)", response_file)
if regex_match is not None:
ordering_and_response_path.append((int(regex_match.group(1)), response_file))
responses_sorted = sorted(ordering_and_response_path)
responses_sorted = [response[1] for response in responses_sorted]
read_file = responses_sorted[-1]
examples = read_jsonl(read_file)
print(f"{len(examples)} {target_set} examples, each with {len(examples[0]['outputs'])} solutions")
return examples
def get_model_solutions_self(data_dir,data_id, verifier_id ,generator_id, process: bool = False):
# if process:
# files_pattern = f'responses_n*_{generator_id}_process.jsonl'
# else:
# files_pattern = f'responses_n*_{generator_id}.jsonl'
if not len(data_id):
files_pattern = f"responses_v({verifier_id})_g({generator_id})_process_supervision.jsonl"
else:
files_pattern = f"responses_d({data_id})_v({verifier_id})_g({generator_id})_process_supervision.jsonl"
response_files = [str(x) for x in pathlib.Path(data_dir).glob(files_pattern)]
# if not response_files:
# raise ValueError(f'Fail to find {files_pattern} in {data_dir}')
#
# ordering_and_response_path = []
# for response_file in response_files:
# regex_match = re.match(r".*responses_n([0-9]+)", response_file)
# if regex_match is not None:
# ordering_and_response_path.append((int(regex_match.group(1)), response_file))
# responses_sorted = sorted(ordering_and_response_path)
# responses_sorted = [response[1] for response in responses_sorted]
try:
read_file = response_files[-1]
except:
print(f"found no files under {data_dir} for the pattern {files_pattern}")
print(read_file)
examples = read_jsonl(read_file)
print(f"{len(examples)} examples, each with {len(examples[0]['outputs'])} solutions")
return examples
def make_training_dataloaders(
data_module: Dict[str, torch.utils.data.Dataset],
training_args: dataclass = None,
) -> Dict:
train_dataloader = DataLoader(
data_module['train_dataset'],
batch_size=training_args.per_device_train_batch_size,
shuffle=True,
drop_last=False,
collate_fn=data_module['train_dataset'].collate_fn,
)
if data_module['val_dataset'] is not None:
val_dataloader = DataLoader(
data_module['val_dataset'],
batch_size=training_args.per_device_eval_batch_size,
shuffle=False,
drop_last=False,
collate_fn=data_module['val_dataset'].collate_fn,
)
else:
val_dataloader = None
return train_dataloader, val_dataloader
def make_testing_dataloader(
dataset: torch.utils.data.Dataset,
batch_size: int,
):
return DataLoader(dataset, batch_size=batch_size, shuffle=False, drop_last=False, collate_fn=dataset.collate_fn)
def make_training_verifier_data_module(tokenizer: transformers.PreTrainedTokenizer, data_args: dataclass) -> Dict:
if data_args.process == True:
dataset_class = VerifierDataset_test
else:
dataset_class = VerifierDataset
train_dataset = dataset_class(
tokenizer=tokenizer,
data_dir=data_args.data_dir,
target_set=data_args.target_set,
verifier_id=data_args.verifier_id,
data_id=data_args.data_id,
generator_id=data_args.generator_id,
per_problem_sampling_solution=data_args.per_problem_sampling_solution,
loss_level=data_args.loss_level,
loss_on_llm=data_args.loss_on_llm,
dedup=data_args.dedup,
easy=data_args.easy,
)
val_dataset = None
if data_args.val_target_set is not None:
val_dataset = dataset_class(
tokenizer=tokenizer,
data_dir=data_args.data_dir,
target_set=data_args.val_target_set,
generator_id=data_args.generator_id,
per_problem_sampling_solution=-1,
loss_level=data_args.loss_level,
loss_on_llm=data_args.loss_on_llm,
)
return dict(train_dataset=train_dataset, val_dataset=val_dataset)
def make_test_verifier_data_module(tokenizer: transformers.PreTrainedTokenizer, data_args: dataclass) -> Dict:
test_dataset = VerifierDataset(
tokenizer=tokenizer,
data_dir=data_args.data_dir,
target_set=data_args.target_set,
generator_id=data_args.generator_id,
per_problem_sampling_solution=-1,
)
return test_dataset
class ProcessVerifierDataset(torch.utils.data.Dataset):
"""Right Padding"""
def __init__(
self,
tokenizer: transformers.PreTrainedTokenizer = None,
data_dir: str = 'data/gsm8k/model_generation',
target_set: str = None,
generator_id: str = None,
per_problem_sampling_solution: str = None,
loss_level: str = 'token',
loss_on_llm: bool = False,
dedup: bool = False
):
self.tokenizer = tokenizer
self.data_dir = data_dir
self.target_set = target_set
self.generator_id = generator_id
self.loss_level = loss_level
self.loss_on_llm = loss_on_llm
assert loss_level in ('token', 'step')
self.pad_token_id = tokenizer.pad_token_id
self.eos_token_id = tokenizer.eos_token_id
self.examples = get_model_solutions(data_dir, generator_id, target_set, process=True)
assert len(self.examples[0]['outputs']) >= per_problem_sampling_solution
if per_problem_sampling_solution != -1:
for example in self.examples:
example['outputs'] = example['outputs'][:per_problem_sampling_solution]
else:
per_problem_sampling_solution = len(self.examples[0]['outputs'])
if dedup:
for ex in self.examples:
dedup_outputs = []
responses = set()
for output in ex['outputs']:
if output['response'] in responses:
continue
responses.add(output['response'])
dedup_outputs.append(output)
ex['outputs'] = dedup_outputs
indices1 = [[i] * len(ex["outputs"]) for i, ex in enumerate(self.examples)]
indices2 = [[j for j in range(len(ex["outputs"]))] for i, ex in enumerate(self.examples)]
qns_str = [[ex["input"]] * len(ex["outputs"]) for ex in self.examples]
solutions_str = [[outputs["response"] for outputs in ex["outputs"]] for ex in self.examples]
step_labels = [[outputs["step_labels"] for outputs in ex["outputs"]] for ex in self.examples]
v_classes = [[outputs["label"] == True for outputs in ex["outputs"]] for ex in self.examples]
indices1 = self._flatten(indices1)
indices2 = self._flatten(indices2)
qns_str = self._flatten(qns_str)
solutions_str = self._flatten(solutions_str)
step_labels = self._flatten(step_labels)
v_classes = self._flatten(v_classes)
qns_tokens = tokenizer(qns_str, padding=False).input_ids
steps_str = [
list(map(lambda x: x + '\n', solution_str.split('\n')[:-1])) + [solution_str.split('\n')[-1]]
for solution_str in solutions_str
]
solutions_tokens = [
[tokenizer.encode(step_str[0], add_special_tokens=False)]
+ [tokenizer.get_continued_input_ids(step) for step in step_str[1:]]
for step_str in steps_str
]
step_tokens_lens = [
[len(step) for step in tokens]
for tokens in solutions_tokens
]
solutions_tokens = [self._flatten(tokens) for tokens in solutions_tokens]
self.indices1 = indices1
self.indices2 = indices2
self.qns_str = qns_str
self.qns_tokens = qns_tokens
self.solutions_str = solutions_str
self.solutions_tokens = solutions_tokens
self.step_tokens_lens = step_tokens_lens
self.step_labels = step_labels
self.v_classes = v_classes
self.n_question = len(self.examples)
self.per_problem_sampling_solution = per_problem_sampling_solution
print(
f'Number of examples = {len(qns_str)} with #deduplication = {self.n_question * self.per_problem_sampling_solution - len(qns_str)}')
self.max_len = max([
len(self.qns_tokens[i]) + len(self.solutions_tokens[i]) + 1
for i in range(len(self.solutions_tokens))
]
)
print(f"Max tokens: {self.max_len}")
def __len__(self):
return len(self.solutions_tokens)
def _flatten(self, ls):
return [item for sublist in ls for item in sublist]
def __getitem__(self, idx):
qn_tokens = self.qns_tokens[idx]
sol_tokens = self.solutions_tokens[idx]
step_labels = self.step_labels[idx]
step_tokens_lens = self.step_tokens_lens[idx]
input_ids = qn_tokens + sol_tokens + [self.eos_token_id]
masks = (
([0] * len(qn_tokens))
+ ([1] * len(sol_tokens))
+ ([1])
)
# create language modeling labels
if self.loss_on_llm:
labels = input_ids
labels = mask_labels(labels, masks)
# create verifier labels
if self.loss_level == 'token':
v_labels = (
[0] * len(qn_tokens)
+ sum(
[
[1 if step_label else 0] * tokens_len
for tokens_len, step_label in zip(step_tokens_lens, step_labels)
],
[]
)
+ [1 if step_labels[-1] else 0]
)
v_labels = mask_labels(v_labels, masks)
assert len(v_labels) == len(input_ids)
else:
raise NotImplementedError
input_ids = torch.tensor(input_ids)
labels = torch.tensor(labels) if self.loss_on_llm else None
v_labels = torch.tensor(v_labels)
return dict(
idx1=self.indices1[idx], idx2=self.indices2[idx],
input_ids=input_ids, labels=labels, v_labels=v_labels,
qn_str=self.qns_str[idx], qn_tokens=self.qns_tokens[idx], sol_str=self.solutions_str[idx],
sol_tokens=self.solutions_tokens[idx], v_class=self.v_classes[idx],
)
def collate_fn(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels, v_labels = tuple(
[instance[key] for instance in instances] for key in ("input_ids", "labels", "v_labels"))
idx1, idx2, qn_str, qn_tokens, sol_str, sol_tokens, v_class = tuple(
[instance[key] for instance in instances] for key in
("idx1", "idx2", "qn_str", "qn_tokens", "sol_str", "sol_tokens", "v_class"))
input_ids, attention_mask = right_pad_sequences(input_ids, padding_value=self.pad_token_id,
return_attention_mask=True)
labels = right_pad_sequences(labels, padding_value=IGNORE_INDEX,
return_attention_mask=False) if self.loss_on_llm else None
v_labels = right_pad_sequences(v_labels, padding_value=IGNORE_INDEX, return_attention_mask=False)
return dict(
idx1=idx1, idx2=idx2,
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels,
v_labels=v_labels,
qn_str=qn_str, qn_tokens=qn_tokens, sol_str=sol_str, sol_tokens=sol_tokens, v_class=v_class,
)
class VerifierDataset(torch.utils.data.Dataset):
"""Right Padding"""
def __init__(
self,
tokenizer: transformers.PreTrainedTokenizer = None,
data_dir: str = 'data/gsm8k/model_generation',
target_set: str = None,
data_id : str = None,
verifier_id: str = None,
generator_id: str = None,
per_problem_sampling_solution: str = None,
loss_level: str = 'token',
loss_on_llm: bool = False,
dedup: bool = False,
easy: bool = True
):
self.examples = get_model_solutions_easy(data_dir, generator_id, target_set)
assert len(self.examples[0]['outputs']) >= per_problem_sampling_solution
self.tokenizer = tokenizer
self.data_dir = data_dir
self.target_set = target_set
self.generator_id = generator_id
self.loss_level = loss_level
self.loss_on_llm = loss_on_llm
assert loss_level in ('token', 'step')
self.pad_token_id = tokenizer.pad_token_id
self.eos_token_id = tokenizer.eos_token_id
if per_problem_sampling_solution != -1:
for example in self.examples:
example['outputs'] = example['outputs'][:per_problem_sampling_solution]
else:
per_problem_sampling_solution = len(self.examples[0]['outputs'])
if dedup:
for ex in self.examples:
dedup_outputs = []
responses = set()
for output in ex['outputs']:
if output['response'] in responses:
continue
responses.add(output['response'])
dedup_outputs.append(output)
ex['outputs'] = dedup_outputs
indices1 = [[i] * len(ex["outputs"]) for i, ex in enumerate(self.examples)]
indices2 = [[j for j in range(len(ex["outputs"]))] for i, ex in enumerate(self.examples)]
qns_str = [[ex["input"]] * len(ex["outputs"]) for ex in self.examples]
solutions_str = [[outputs["response"] for outputs in ex["outputs"]] for ex in self.examples]
v_classes = [[outputs["label"] == True for outputs in ex["outputs"]] for ex in self.examples]
indices1 = self._flatten(indices1)
indices2 = self._flatten(indices2)
qns_str = self._flatten(qns_str)
solutions_str = self._flatten(solutions_str)
v_classes = self._flatten(v_classes)
qns_tokens = tokenizer(qns_str, padding=False).input_ids
solutions_tokens = tokenizer(solutions_str, padding=False, add_special_tokens=False).input_ids
self.indices1 = indices1
self.indices2 = indices2
self.qns_str = qns_str
self.qns_tokens = qns_tokens
self.solutions_str = solutions_str
self.solutions_tokens = solutions_tokens
self.v_classes = v_classes
self.max_len = max(
[len(qns_tokens[i]) + len(solutions_tokens[i]) + 1 for i in range(len(solutions_tokens))])
print(f"Max tokens: {self.max_len}")
self.per_problem_sampling_solution = per_problem_sampling_solution
print(f'Number of examples = {len(self.qns_str)}')
self.n_question = len(self.examples)
def __len__(self):
return len(self.solutions_tokens)
def _flatten(self, ls):
return [item for sublist in ls for item in sublist]
def __getitem__(self, idx):
qn_tokens = self.qns_tokens[idx]
sol_tokens = self.solutions_tokens[idx]
v_class = self.v_classes[idx]
input_ids = qn_tokens + sol_tokens + [self.eos_token_id]
masks = (
([0] * len(qn_tokens))
+ ([1] * len(sol_tokens))
+ ([1])
)
# create language modeling labels
if self.loss_on_llm:
labels = input_ids
labels = mask_labels(labels, masks)
# create verifier labels
if self.loss_level == 'token':
v_labels = [int(v_class)] * len(input_ids)
v_labels = mask_labels(v_labels, masks)
else:
raise NotImplementedError
input_ids = torch.tensor(input_ids)
labels = torch.tensor(labels) if self.loss_on_llm else None
v_labels = torch.tensor(v_labels)
return dict(
idx1=self.indices1[idx], idx2=self.indices2[idx],
input_ids=input_ids, labels=labels, v_labels=v_labels,
qn_str=self.qns_str[idx], qn_tokens=self.qns_tokens[idx], sol_str=self.solutions_str[idx], sol_tokens=self.solutions_tokens[idx], v_class=self.v_classes[idx],
)
def collate_fn(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels, v_labels = tuple([instance[key] for instance in instances] for key in ("input_ids", "labels", "v_labels"))
idx1, idx2, qn_str, qn_tokens, sol_str, sol_tokens, v_class = tuple([instance[key] for instance in instances] for key in ("idx1", "idx2", "qn_str", "qn_tokens", "sol_str", "sol_tokens", "v_class"))
input_ids, attention_mask = right_pad_sequences(input_ids, padding_value=self.pad_token_id, return_attention_mask=True)
labels = right_pad_sequences(labels, padding_value=IGNORE_INDEX, return_attention_mask=False) if self.loss_on_llm else None
v_labels = right_pad_sequences(v_labels, padding_value=IGNORE_INDEX, return_attention_mask=False)
return dict(
idx1=idx1, idx2=idx2,
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels,
v_labels=v_labels,
qn_str=qn_str, qn_tokens=qn_tokens, sol_str=sol_str, sol_tokens=sol_tokens, v_class=v_class,
)
class VerifierDataset_test(torch.utils.data.Dataset):
"""Right Padding"""
def __init__(
self,
tokenizer: transformers.PreTrainedTokenizer = None,
data_dir: str = 'data/gsm8k/model_generation',
target_set: str = None,
data_id : str = None,
verifier_id: str = None,
generator_id: str = None,
per_problem_sampling_solution: str = None,
loss_level: str = 'token',
loss_on_llm: bool = False,
dedup: bool = False,
easy: bool = True
):
self.examples = get_model_solutions_psv(data_dir, generator_id, target_set)
assert len(self.examples[0]['outputs']) >= per_problem_sampling_solution
print("VerifierDataset_test")
self.tokenizer = tokenizer
self.data_dir = data_dir
self.target_set = target_set
self.generator_id = generator_id
self.loss_level = loss_level
self.loss_on_llm = loss_on_llm
assert loss_level in ('token', 'step')
self.pad_token_id = tokenizer.pad_token_id
self.eos_token_id = tokenizer.eos_token_id
if per_problem_sampling_solution != -1:
for example in self.examples:
example['outputs'] = example['outputs'][:per_problem_sampling_solution]
else:
per_problem_sampling_solution = len(self.examples[0]['outputs'])
if dedup:
for ex in self.examples:
dedup_outputs = []
responses = set()
for output in ex['outputs']:
if output['response'] in responses:
continue
responses.add(output['response'])
dedup_outputs.append(output)
ex['outputs'] = dedup_outputs
indices1 = [[i] * len(ex["outputs"]) for i, ex in enumerate(self.examples)]
indices2 = [[j for j in range(len(ex["outputs"]))] for i, ex in enumerate(self.examples)]
qns_str = [[ex["input"]] * len(ex["outputs"]) for ex in self.examples]
part1_solutions_str = []
part2_solutions_str = []
partition_list= []
for ex in self.examples:
part1_solutions_str_p = []
part2_solutions_str_p = []
partition_list_p = []
for output_id in range(len(ex['outputs'])):
output = ex['outputs'][output_id]["response"]
partition_id = ex['outputs'][output_id]["label"]
partition_list_p.append(partition_id)
if partition_id == -1:
part1_solutions_str_p.append(output)
part2_solutions_str_p.append("")
else:
part1_solutions_str_p.append(output[:partition_id ])
part2_solutions_str_p.append(output[partition_id:])
part1_solutions_str.append(part1_solutions_str_p)
part2_solutions_str.append(part2_solutions_str_p)
partition_list.append(partition_list_p)
# solutions_str = [[outputs["response"] for outputs in ex["outputs"]] for ex in self.examples]
# v_classes = [[outputs["label"] for outputs in ex["outputs"]] for ex in self.examples]
# pass1 = []
# for slices in range(0, len(partition_list)):
# if partition_list[slices][0] == -1:
# pass1.append(1)
# else:
# pass1.append(0)
# print("length:" , len(pass1))
# print("pass1:" , sum(pass1)/len(pass1))
# pdb.set_trace()
indices1 = self._flatten(indices1)
indices2 = self._flatten(indices2)
qns_str = self._flatten(qns_str)
part1_solutions_str = self._flatten(part1_solutions_str)
part2_solutions_str = self._flatten(part2_solutions_str)
partition_list =self._flatten(partition_list)
qns_tokens = tokenizer(qns_str, padding=False).input_ids
part1_solutions_tokens = tokenizer(part1_solutions_str, padding=False, add_special_tokens=False).input_ids
part2_solutions_tokens = tokenizer(part2_solutions_str, padding=False, add_special_tokens=False).input_ids
v_classes = [len(part1_solutions_tokens[id]) if partition_list[id]!= -1 else -1 for id in range(len(part1_solutions_tokens))]
# v_classes = [len(part1_solutions_tokens[id]) if len(part2_solutions_tokens[id]) else -1 for id in range(len(part1_solutions_tokens))]
solutions_tokens = [part1_solutions_tokens[id] + part2_solutions_tokens[id] for id in range(len(part1_solutions_tokens))]
solutions_str = [part1_solutions_str[id] + part2_solutions_str[id] for id in range(len(part1_solutions_str))]
# pass1 = 0
# pass5 = 0
v_class_label = 0
for slices in range(0, len(v_classes)):
if v_classes[slices] == -1:
v_class_label += 1
print("-1 in v_class ratio", v_class_label/len(v_classes))
# for item in range(0, len(self.examples[0]['outputs'])):
# if v_classes[item + slices] == -1:
# pass5 += 1
# break
# print("pass1:" , pass1/len(self.examples))
# print(f"pass{len(self.examples[0]['outputs'])}:" , pass5/len(self.examples))
print("change v_label to 0.5")
one_len = 0
total_len = 0
for id in range(len(partition_list)):
total_len += len(solutions_tokens[id])
if partition_list[id] == -1:
one_len += len(solutions_tokens[id])
else:
one_len += len(part1_solutions_tokens[id])
print("1 label ratio", one_len/total_len)
self.indices1 = indices1
self.indices2 = indices2
self.qns_str = qns_str
self.qns_tokens = qns_tokens
self.solutions_str = solutions_str
self.solutions_tokens = solutions_tokens
self.v_classes = v_classes
self.max_len = max(
[len(qns_tokens[i]) + len(solutions_tokens[i]) + 1 for i in range(len(solutions_tokens))])
print(f"Max tokens: {self.max_len}")
self.per_problem_sampling_solution = per_problem_sampling_solution
print(f'Number of examples = {len(self.qns_str)}')
self.n_question = len(self.examples)
def __len__(self):
return len(self.solutions_tokens)
def _flatten(self, ls):
return [item for sublist in ls for item in sublist]
def __getitem__(self, idx):
qn_tokens = self.qns_tokens[idx]
sol_tokens = self.solutions_tokens[idx]
v_class = self.v_classes[idx]
input_ids = qn_tokens + sol_tokens + [self.eos_token_id]
masks = (
([0] * len(qn_tokens))
+ ([1] * len(sol_tokens))
+ ([1])
)
# create language modeling labels
if self.loss_on_llm:
labels = input_ids
labels = mask_labels(labels, masks)
# create verifier labels
if self.loss_level == 'token':
if v_class == -1:
v_labels = [1] * len(input_ids)
else:
v_labels = [0] * len(input_ids)
v_labels[len(qn_tokens): len(qn_tokens) + v_class] = [0.5] * v_class
v_labels = mask_labels(v_labels, masks)
else:
raise NotImplementedError
input_ids = torch.tensor(input_ids)
labels = torch.tensor(labels) if self.loss_on_llm else None
v_labels = torch.tensor(v_labels)
return dict(
idx1=self.indices1[idx], idx2=self.indices2[idx],
input_ids=input_ids, labels=labels, v_labels=v_labels,
qn_str=self.qns_str[idx], qn_tokens=self.qns_tokens[idx], sol_str=self.solutions_str[idx], sol_tokens=self.solutions_tokens[idx], v_class=self.v_classes[idx],
)
def collate_fn(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels, v_labels = tuple([instance[key] for instance in instances] for key in ("input_ids", "labels", "v_labels"))
idx1, idx2, qn_str, qn_tokens, sol_str, sol_tokens, v_class = tuple([instance[key] for instance in instances] for key in ("idx1", "idx2", "qn_str", "qn_tokens", "sol_str", "sol_tokens", "v_class"))
input_ids, attention_mask = right_pad_sequences(input_ids, padding_value=self.pad_token_id, return_attention_mask=True)
labels = right_pad_sequences(labels, padding_value=IGNORE_INDEX, return_attention_mask=False) if self.loss_on_llm else None
v_labels = right_pad_sequences(v_labels, padding_value=IGNORE_INDEX, return_attention_mask=False)
return dict(
idx1=idx1, idx2=idx2,
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels,
v_labels=v_labels,
qn_str=qn_str, qn_tokens=qn_tokens, sol_str=sol_str, sol_tokens=sol_tokens, v_class=v_class,
)
class VerifierDataset_self(VerifierDataset):
"""Right Padding"""
def __init__(
self,
tokenizer: transformers.PreTrainedTokenizer = None,
data_dir: str = 'data/gsm8k/model_generation',
target_set: str = None,
data_id: str = None,
generator_id: str = None,
verifier_id: str = None,
per_problem_sampling_solution: str = None,
loss_level: str = 'token',
loss_on_llm: bool = False,
dedup: bool = False,
easy: bool = True
):
if easy:
self.examples = get_model_solutions_easy(data_dir, data_id,verifier_id,generator_id)
else:
self.examples = get_model_solutions_self(data_dir, data_id,verifier_id,generator_id)
assert len(self.examples[0]['outputs']) >= per_problem_sampling_solution
self.tokenizer = tokenizer
self.data_dir = data_dir
self.target_set = target_set
self.generator_id = generator_id
self.loss_level = loss_level
self.loss_on_llm = loss_on_llm
assert loss_level in ('token', 'step')
self.pad_token_id = tokenizer.pad_token_id
self.eos_token_id = tokenizer.eos_token_id
if per_problem_sampling_solution != -1:
for example in self.examples:
if "input" not in example:
example['input'] = example['question']
example['outputs'] = example['outputs'][:per_problem_sampling_solution]
else:
per_problem_sampling_solution = len(self.examples[0]['outputs'])
if dedup:
for ex in self.examples:
dedup_outputs = []
responses = set()
for output in ex['outputs']:
if output['response'] in responses:
continue
responses.add(output['response'])
dedup_outputs.append(output)
ex['outputs'] = dedup_outputs
indices1 = [[i] * len(ex["outputs"]) for i, ex in enumerate(self.examples)]
indices2 = [[j for j in range(len(ex["outputs"]))] for i, ex in enumerate(self.examples)]
qns_str = [[ex["input"]] * len(ex["outputs"]) for ex in self.examples]
solutions_str = [[outputs["response"] for outputs in ex["outputs"]] for ex in self.examples]
v_classes = [[outputs["process_vscores"] for outputs in ex["outputs"]] for ex in self.examples]
indices1 = self._flatten(indices1)
indices2 = self._flatten(indices2)
qns_str = self._flatten(qns_str)
solutions_str = self._flatten(solutions_str)
v_classes = self._flatten(v_classes)
qns_tokens = tokenizer(qns_str, padding=False).input_ids
solutions_tokens = tokenizer(solutions_str, padding=False, add_special_tokens=False).input_ids
self.indices1 = indices1
self.indices2 = indices2
self.qns_str = qns_str
self.qns_tokens = qns_tokens
self.solutions_str = solutions_str
self.solutions_tokens = solutions_tokens
self.v_classes = v_classes
self.n_question = len(self.examples)
self.per_problem_sampling_solution = per_problem_sampling_solution
print(
f'Number of examples = {len(qns_str)} with #deduplication = {self.n_question * self.per_problem_sampling_solution - len(qns_str)}')
self.max_len = max([
len(self.qns_tokens[i]) + len(self.solutions_tokens[i]) + 1
for i in range(len(self.solutions_tokens))
]
)
print(f"Max tokens: {self.max_len}")
def __getitem__(self, idx):
qn_tokens = self.qns_tokens[idx]
sol_tokens = self.solutions_tokens[idx]
v_class = self.v_classes[idx]
input_ids = qn_tokens + sol_tokens + [self.eos_token_id]
masks = (
([0] * len(qn_tokens))
+ ([1] * len(sol_tokens))
+ ([1])
)
# create language modeling labels
if self.loss_on_llm:
labels = input_ids
labels = mask_labels(labels, masks)
# create verifier labels
if self.loss_level == 'token':
v_class = [1] * len(qn_tokens)+ v_class
v_labels = mask_labels(v_class, masks)
else:
raise NotImplementedError
input_ids = torch.tensor(input_ids)
labels = torch.tensor(labels) if self.loss_on_llm else None
v_labels = torch.tensor(v_labels)
return dict(
idx1=self.indices1[idx], idx2=self.indices2[idx],
input_ids=input_ids, labels=labels, v_labels=v_labels,
qn_str=self.qns_str[idx], qn_tokens=self.qns_tokens[idx], sol_str=self.solutions_str[idx],
sol_tokens=self.solutions_tokens[idx], v_class=self.v_classes[idx],
)
def left_pad_sequences(sequences: List[torch.LongTensor], padding_value: int, return_attention_mask: bool = False):
max_length = max(len(x) for x in sequences)
padded_sequences = torch.stack([F.pad(seq, (max_length - seq.shape[-1], 0), value=padding_value) for seq in sequences], dim=0)
if return_attention_mask:
attention_mask = padded_sequences.ne(padding_value)
return padded_sequences, attention_mask
return padded_sequences
def right_pad_sequences(sequences: List[torch.LongTensor], padding_value: int, return_attention_mask: bool = False):
padded_sequences = torch.nn.utils.rnn.pad_sequence(
sequences,
batch_first=True,
padding_value=padding_value,
)
if return_attention_mask:
attention_mask = padded_sequences.ne(padding_value)
return padded_sequences, attention_mask
return padded_sequences
def mask_labels(labels: List[int], masks: List[bool]):
"""Mask the corresponding label into IGNORE_INDEX"""
assert len(labels) == len(masks)
return [
token if mask
else IGNORE_INDEX
for token, mask in zip(labels, masks)
]
|