File size: 10,742 Bytes
ecadbd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
# import wandb
import os
import yaml
from peft import LoraConfig, get_peft_model_state_dict
from torch.utils.data import DataLoader
import time

from typing import List, Tuple

import json
import re
import string
import copy
from dataclasses import field, dataclass, asdict
from typing import Sequence, Literal, Dict

import transformers
from transformers import AutoModelForCausalLM, AutoConfig, AutoTokenizer
from transformers import Trainer
from transformers.modeling_utils import *
from transformers.trainer import _is_peft_model
from transformers.models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from transformers.data.data_collator import DataCollator

from transformers.training_args import TrainingArguments
from transformers.tokenization_utils_base import PreTrainedTokenizerBase
from transformers.trainer_callback import TrainerCallback
from transformers.trainer_utils import EvalPrediction
from torch.utils.data import Dataset, IterableDataset
from datasets import load_dataset
##
#from ..pipeline.flux_omini import transformer_forward, encode_images
# from ...omini.rotation import RotationTuner, RotationConfig
# from smpeft.sama import RotationTuner, RotationConfig
from smpeft import PeftModel
from .config import MainConfig, convert_to_trainer_args
import draccus
import argparse
# from omegaconf import OmegaConf
import numpy as np
import random
import transformers

import argparse
from vllm import LLM, SamplingParams
from datetime import datetime

from .utils import set_seed_all

from multiprocessing import Process, Queue

IGNORE_INDEX = -100
DEFAULT_PAD_TOKEN = "[PAD]"
DEFAULT_EOS_TOKEN = "</s>"
DEFAULT_BOS_TOKEN = "</s>"
DEFAULT_UNK_TOKEN = "</s>"

MAX_NEW_TOKENS = 50
PROMPT_TEMPLATE = (
    "Below is an passage followed by a coresponding question that describes a task "
    "Write a response that appropriately completes the request with your answer.\n\n"
    "### Instruction:\n{instruction}\n\n### Response:"
)

# def parse_args():
#     parser = argparse.ArgumentParser()
#     # parser.add_argument('--dataset', choices=["boolq", "piqa", "social_i_qa", "hellaswag", "winogrande", "ARC-Challenge", "ARC-Easy", "openbookqa", "all"],
#     #                     required=True)
    
#     parser.add_argument('--do_merge', type=bool, default=True)
#     return parser.parse_args()

def set_deterministic_seed(seed=42):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    transformers.set_seed(seed)
    # torch.backends.cudnn.deterministic = True
    # torch.backends.cudnn.benchmark = False

def normalize_answer(s):
    """Lower text and remove punctuation, articles and extra whitespace."""
    def remove_articles(text):
        return re.sub(r'\b(a|an|the)\b', ' ', text)
    def white_space_fix(text):
        return ' '.join(text.split())
    def remove_punc(text):
        exclude = set(string.punctuation)
        return ''.join(ch for ch in text if ch not in exclude)
    return white_space_fix(remove_articles(remove_punc(s.lower())))

def f1_score(prediction, ground_truth):
    pred_tokens = normalize_answer(prediction).split()
    truth_tokens = normalize_answer(ground_truth).split()
    common = collections.Counter(pred_tokens) & collections.Counter(truth_tokens)
    num_same = sum(common.values())
    if num_same == 0: return 0
    precision = 1.0 * num_same / len(pred_tokens)
    recall = 1.0 * num_same / len(truth_tokens)
    return (2 * precision * recall) / (precision + recall)

def exact_match_score(prediction, ground_truth):
    return (normalize_answer(prediction) == normalize_answer(ground_truth))

def metric_max_over_ground_truths(metric_fn, prediction, ground_truths):
    """DROP the highest scores."""
    return max([metric_fn(prediction, gt) for gt in ground_truths])

def score_outputs(outputs, test_dataset, ids, all_ground_truths):
    results = []
    total_em = 0
    total_f1 = 0

    print("Calculating scores...")
    for i, output in enumerate(outputs):
        # vLLM only output, no prompt anymore. No need to clean whitespace 
        prediction = output.outputs[0].text.strip()
        ground_truths = all_ground_truths[i]
        
        # Grade
        em = metric_max_over_ground_truths(exact_match_score, prediction, ground_truths)
        f1 = metric_max_over_ground_truths(f1_score, prediction, ground_truths)
        
        total_em += em
        total_f1 += f1
        
        results.append({
            "id": ids[i],
            "prediction": prediction,
            "ground_truths": ground_truths,
            "em": em,
            "f1": f1
        })

    # G. Final Statistics
    avg_em = 100.0 * total_em / len(test_dataset)
    avg_f1 = 100.0 * total_f1 / len(test_dataset)

    print("\n" + "="*40)
    print("FINAL RESULTS (vLLM)")
    print("="*40)
    print(f"Total Samples: {len(test_dataset)}")
    print(f"Exact Match (EM): {avg_em:.2f}%")
    print(f"F1 Score      : {avg_f1:.2f}%")
    print("="*40)
    return results, avg_em, avg_f1
    
def merge_process(queue, mainCfg: MainConfig, force_to_merge: bool = False):
    try:
        model_name = mainCfg.model.model_name
        if mainCfg.model.merge_adapter_path is not None:
            adapter = mainCfg.model.merge_adapter_path + "/ft2"
            print(f'Merging... from mainCfg.model.merge_adapter_path {adapter}')
        elif mainCfg.model.adapter_path is not None:
            adapter = mainCfg.model.adapter_path + "/ft2"
            print(f'From mainCfg.model.adapter_path {adapter}')
        else:
            raise KeyError('No adapter path')
        
        if mainCfg.model.merge_output_path is not None:
            output_path = mainCfg.model.merge_output_path + "/merge"
            out_json = mainCfg.model.merge_output_path
        else:
            output_path = mainCfg.model.adapter_path + "/merge" ## this case
            out_json = mainCfg.model.adapter_path
        
        
        if os.path.exists(output_path):
            has_weights = any(f.endswith(".bin") or f.endswith(".safetensors") for f in os.listdir(output_path))
        else:
            has_weights = False
        # if not has_weights: # merge
        if not has_weights or force_to_merge:
            # model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto",)
            # tokenizer = AutoTokenizer.from_pretrained(model_name, device_map='auto')
            model = AutoModelForCausalLM.from_pretrained(model_name, device_map="cpu",low_cpu_mem_usage=True)
            tokenizer = AutoTokenizer.from_pretrained(model_name, device_map='cpu')

            # config = PeftConfig.from_pretrained(args.adapter)
            model = PeftModel.from_pretrained(model, adapter)
            model = model.merge_and_unload()
            model.save_pretrained(output_path, safe_serialization=False, max_shard_size="10GB")
            tokenizer.save_pretrained(output_path)
            del model
            del tokenizer
            gc.collect()
            gc.collect()
            torch.cuda.empty_cache()
            # print(model)
            
            print(f'The end of merging,    from {adapter},\n \t \t      to {output_path}')
        else:
            print("No need to merge")
        queue.put((output_path, out_json))
    except Exception as e:
        import traceback
        error_msg = traceback.format_exc()
        print(error_msg)
        queue.put(error_msg)
        print(f"Error in merge_process: {e}")

@draccus.wrap()
def main(mainCfg: MainConfig):
    print('='*120)
    set_seed_all(mainCfg.seed)

    queue = Queue()
    p = Process(target=merge_process, args=(queue, mainCfg, False))
    p.start()
    result = queue.get() # Wait for the process to finish
    p.join()

    if result is None:
        raise RuntimeError("Model merging failed.")
    model_path, out_json = result
    
    # model_path, out_json = merge(mainCfg, force_to_merge=False) # recommended to use this setting
    if not os.path.exists(model_path):
        raise FileNotFoundError(f"Model directory does not exist: {model_path}")
    print(f"Verified model path: {os.path.abspath(model_path)}")
    print("Loading dataset...")
    test_dataset = load_dataset(mainCfg.data.path, split="validation").select(range(mainCfg.data.total_test_samples))
    
    def prepare_test_data(batch):
        # 1. Handle Instructions and Prompts
        # Because batched=True, batch['passage'] and batch['question'] are lists
        prompts = []
        for passage, question in zip(batch['passage'], batch['question']):
            instr = f"Passage: {passage}\nQuestion: {question}"
            # Format the prompt string
            full_prompt = PROMPT_TEMPLATE.format(instruction=instr, input_section="")
            prompts.append(full_prompt)
        
        # 2. Handle Answer Spans
        # batch['answers_spans'] is a list of dictionaries. 
        # We extract 'spans' from each dictionary in the list.
        target_spans = [ans['spans'] for ans in batch['answers_spans']]
        
        # Return a dictionary where each value is a list of the same length
        return {
            "prompt": prompts,
            "target_spans": target_spans
        }
    
    test_dataset = test_dataset.map(prepare_test_data,
                                   batched=True, batch_size=2000, num_proc=8)
    prompts = test_dataset['prompt']
    ids = test_dataset['query_id']
    all_ground_truths = test_dataset['target_spans']
    
    print('out', model_path)
    # exit()
    llm = LLM(
        model=model_path,
        dtype="bfloat16",
        gpu_memory_utilization=0.90, # 90% VRAM
        max_model_len=mainCfg.model.model_max_seq_length # Context window
    )
    stop_tokens = ["Instruction:", "Response:", "\n", "###", "Passage:", "Question:"]
    sampling_params = SamplingParams(temperature=0, top_p=1, max_tokens=MAX_NEW_TOKENS, stop=stop_tokens)
    print(f"Generating for {len(prompts)} samples...")
    
    start_time = datetime.now()
    outputs = llm.generate(prompts, sampling_params)
    end_time = datetime.now()
    print('end time: ', end_time.strftime("%Y-%m-%d %H:%M:%S"), '| duration: ', end_time - start_time)
        
    results, avg_em, avg_f1 = score_outputs(outputs=outputs,
                                    test_dataset=test_dataset, ids=ids, all_ground_truths=all_ground_truths)

    # Save
    save_file = out_json + '/drop_vllm_results.json'
    with open(save_file, "w", encoding="utf-8") as f:
        json.dump({
            "metrics": {"EM": avg_em, "F1": avg_f1},
            "details": results
        }, f, indent=2, ensure_ascii=False)
    print("Results saved to drop_vllm_results.json")

if __name__ == "__main__":
    main()