generator.py 4.69 KB
Newer Older
kihoon.lee's avatar
백업    
kihoon.lee committed
1
import logging
kihoon.lee's avatar
kihoon.lee committed
2
3
4
5
import argparse
import os
import pandas as pd
from templates import PROMPT_STRATEGY
kihoon.lee's avatar
백업    
kihoon.lee committed
6

kihoon.lee's avatar
kihoon.lee committed
7
8
9
10
11
12
13
14
15
16
try:
    from aphrodite import LLM, SamplingParams

    print("- Using aphrodite-engine")

except ImportError:
    from vllm import LLM, SamplingParams

    print("- Using vLLM")

kihoon.lee's avatar
백업    
kihoon.lee committed
17
18
19
20
# 로깅 설정
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

kihoon.lee's avatar
kihoon.lee committed
21
22
23
24
25
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--gpu_devices", help=" : CUDA_VISIBLE_DEVICES", default="0")
parser.add_argument(
    "-m",
    "--model",
kihoon.lee's avatar
백업    
kihoon.lee committed
26
27
28
29
30
    help=" : write huggingface model name to evaluate",
    default="LDCC/Chat-Mistral-Nemo-12B-32k",
)
parser.add_argument(
    "-ml", "--model_len", help=" : Maximum Model Length", default=4096, type=int
kihoon.lee's avatar
kihoon.lee committed
31
32
33
)
args = parser.parse_args()

kihoon.lee's avatar
백업    
kihoon.lee committed
34
logger.info(f"Args - {args}")
kihoon.lee's avatar
kihoon.lee committed
35
36
37
38

os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_devices
gpu_counts = len(args.gpu_devices.split(","))

kihoon.lee's avatar
백업    
kihoon.lee committed
39
40
# LLM 초기화
logger.info(f"Initializing LLM with model: {args.model}")
kihoon.lee's avatar
kihoon.lee committed
41
42
43
44
45
llm = LLM(
    model=args.model,
    tensor_parallel_size=gpu_counts,
    max_model_len=args.model_len,
    gpu_memory_utilization=0.8,
Liky98's avatar
Liky98 committed
46
    trust_remote_code=True, 
kihoon.lee's avatar
kihoon.lee committed
47
)
kihoon.lee's avatar
백업    
kihoon.lee committed
48
logger.info("LLM initialized successfully")
kihoon.lee's avatar
kihoon.lee committed
49
50
51
52
53

sampling_params = SamplingParams(
    temperature=0,
    skip_special_tokens=True,
    max_tokens=args.model_len,
kihoon.lee's avatar
백업    
kihoon.lee committed
54
55
56
57
58
59
60
61
62
63
    stop=[
        "<|endoftext|>",
        "[INST]",
        "[/INST]",
        "<|im_end|>",
        "<|end|>",
        "<|eot_id|>",
        "<end_of_turn>",
        "<eos>",
    ],
kihoon.lee's avatar
kihoon.lee committed
64
65
)

kihoon.lee's avatar
백업    
kihoon.lee committed
66
# chat_temlate가 없다면 default로 세팅하는 과정
kihoon.lee's avatar
kihoon.lee committed
67
68
69
tokenizer = llm.llm_engine.tokenizer.tokenizer

if tokenizer.chat_template is None:
kihoon.lee's avatar
백업    
kihoon.lee committed
70
    logger.info("chat template가 없으므로 default로 설정")
kihoon.lee's avatar
kihoon.lee committed
71
72
73
    default_chat_template = "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
    tokenizer.chat_template = default_chat_template

kihoon.lee's avatar
백업    
kihoon.lee committed
74
75
76
77
78
79
# 문제 로드
logger.info("Loading questions from questions.jsonl")
df_questions = pd.read_json(
    "questions.jsonl", orient="records", encoding="utf-8-sig", lines=True
)
logger.info(f"Loaded {len(df_questions)} questions")
kihoon.lee's avatar
kihoon.lee committed
80
81
82
83
84

if not os.path.exists("./generated/" + args.model):
    os.makedirs("./generated/" + args.model)

for strategy_name, prompts in PROMPT_STRATEGY.items():
kihoon.lee's avatar
백업    
kihoon.lee committed
85
    logger.info(f"Processing strategy: {strategy_name}")
kihoon.lee's avatar
kihoon.lee committed
86
87
88
89
90
91
92

    def format_single_turn_question(question):
        return tokenizer.apply_chat_template(
            prompts + [{"role": "user", "content": question[0]}],
            tokenize=False,
            add_generation_prompt=True,
        )
kihoon.lee's avatar
백업    
kihoon.lee committed
93

kihoon.lee's avatar
kihoon.lee committed
94
95
    single_turn_questions = df_questions["questions"].map(format_single_turn_question)
    print(single_turn_questions.iloc[0])
kihoon.lee's avatar
백업    
kihoon.lee committed
96
97
98
    
    # 단일 턴 질문 처리
    logger.info("Generating single-turn outputs")
kihoon.lee's avatar
kihoon.lee committed
99
    single_turn_outputs = [
kihoon.lee's avatar
백업    
kihoon.lee committed
100
101
        output.outputs[0].text.strip()
        for output in llm.generate(single_turn_questions, sampling_params)
kihoon.lee's avatar
kihoon.lee committed
102
    ]
kihoon.lee's avatar
백업    
kihoon.lee committed
103
    logger.info(f"Generated {len(single_turn_outputs)} single-turn outputs")
kihoon.lee's avatar
kihoon.lee committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117

    def format_double_turn_question(question, single_turn_output):
        return tokenizer.apply_chat_template(
            prompts
            + [
                {"role": "user", "content": question[0]},
                {"role": "assistant", "content": single_turn_output},
                {"role": "user", "content": question[1]},
            ],
            tokenize=False,
            add_generation_prompt=True,
        )

    multi_turn_questions = df_questions[["questions", "id"]].apply(
kihoon.lee's avatar
백업    
kihoon.lee committed
118
119
120
        lambda x: format_double_turn_question(
            x["questions"], single_turn_outputs[x["id"] - 1]
        ),
kihoon.lee's avatar
kihoon.lee committed
121
122
        axis=1,
    )
kihoon.lee's avatar
백업    
kihoon.lee committed
123
124
125
    
    # 멀티 턴 질문 처리
    logger.info("Generating multi-turn outputs")
kihoon.lee's avatar
kihoon.lee committed
126
    multi_turn_outputs = [
kihoon.lee's avatar
백업    
kihoon.lee committed
127
128
        output.outputs[0].text.strip()
        for output in llm.generate(multi_turn_questions, sampling_params)
kihoon.lee's avatar
kihoon.lee committed
129
    ]
kihoon.lee's avatar
백업    
kihoon.lee committed
130
    logger.info(f"Generated {len(multi_turn_outputs)} multi-turn outputs")
kihoon.lee's avatar
kihoon.lee committed
131
132
133
134
135
136
137
138
139
140

    df_output = pd.DataFrame(
        {
            "id": df_questions["id"],
            "category": df_questions["category"],
            "questions": df_questions["questions"],
            "outputs": list(zip(single_turn_outputs, multi_turn_outputs)),
            "references": df_questions["references"],
        }
    )
kihoon.lee's avatar
백업    
kihoon.lee committed
141
142
143
144
    
    # 결과 저장
    output_file = f"./generated/{args.model}/{strategy_name}.jsonl"
    logger.info(f"Saving results to {output_file}")
kihoon.lee's avatar
kihoon.lee committed
145
    df_output.to_json(
kihoon.lee's avatar
백업    
kihoon.lee committed
146
        output_file,
kihoon.lee's avatar
kihoon.lee committed
147
148
149
150
        orient="records",
        lines=True,
        force_ascii=False,
    )
kihoon.lee's avatar
백업    
kihoon.lee committed
151
152
153
    logger.info(f"Results saved for strategy: {strategy_name}")

logger.info("Generation process completed")