07_finetuning.py

Download
python 304 lines 7.2 KB
  1"""
  207. νŒŒμΈνŠœλ‹ 예제
  3
  4HuggingFace Trainerλ₯Ό μ‚¬μš©ν•œ λͺ¨λΈ νŒŒμΈνŠœλ‹
  5"""
  6
  7print("=" * 60)
  8print("νŒŒμΈνŠœλ‹")
  9print("=" * 60)
 10
 11
 12# ============================================
 13# 1. κΈ°λ³Έ νŒŒμΈνŠœλ‹ (μ½”λ“œ μ˜ˆμ‹œ)
 14# ============================================
 15print("\n[1] κΈ°λ³Έ νŒŒμΈνŠœλ‹")
 16print("-" * 40)
 17
 18basic_finetuning = '''
 19from transformers import (
 20    AutoTokenizer,
 21    AutoModelForSequenceClassification,
 22    TrainingArguments,
 23    Trainer
 24)
 25from datasets import load_dataset
 26
 27# 데이터 λ‘œλ“œ
 28dataset = load_dataset("imdb")
 29
 30# ν† ν¬λ‚˜μ΄μ €
 31tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
 32
 33def tokenize(batch):
 34    return tokenizer(batch["text"], truncation=True, padding="max_length", max_length=256)
 35
 36tokenized = dataset.map(tokenize, batched=True)
 37
 38# λͺ¨λΈ
 39model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)
 40
 41# ν•™μŠ΅ μ„€μ •
 42args = TrainingArguments(
 43    output_dir="./results",
 44    num_train_epochs=3,
 45    per_device_train_batch_size=16,
 46    learning_rate=2e-5,
 47    evaluation_strategy="epoch",
 48)
 49
 50# Trainer
 51trainer = Trainer(
 52    model=model,
 53    args=args,
 54    train_dataset=tokenized["train"],
 55    eval_dataset=tokenized["test"],
 56)
 57
 58# ν•™μŠ΅
 59trainer.train()
 60'''
 61print(basic_finetuning)
 62
 63
 64# ============================================
 65# 2. LoRA νŒŒμΈνŠœλ‹
 66# ============================================
 67print("\n[2] LoRA νŒŒμΈνŠœλ‹")
 68print("-" * 40)
 69
 70lora_code = '''
 71from peft import LoraConfig, get_peft_model, TaskType
 72
 73# LoRA μ„€μ •
 74lora_config = LoraConfig(
 75    r=8,                           # 랭크
 76    lora_alpha=32,                 # μŠ€μΌ€μΌλ§
 77    target_modules=["query", "value"],  # 적용 λͺ¨λ“ˆ
 78    lora_dropout=0.1,
 79    bias="none",
 80    task_type=TaskType.SEQ_CLS
 81)
 82
 83# λͺ¨λΈμ— LoRA 적용
 84model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)
 85model = get_peft_model(model, lora_config)
 86
 87# ν•™μŠ΅ κ°€λŠ₯ν•œ νŒŒλΌλ―Έν„° 확인
 88model.print_trainable_parameters()
 89# trainable: 0.27% (μ•½ 300K / 110M)
 90
 91# 일반 Trainer둜 ν•™μŠ΅
 92trainer = Trainer(model=model, args=args, ...)
 93trainer.train()
 94'''
 95print(lora_code)
 96
 97
 98# ============================================
 99# 3. QLoRA (μ–‘μžν™” + LoRA)
100# ============================================
101print("\n[3] QLoRA")
102print("-" * 40)
103
104qlora_code = '''
105from transformers import BitsAndBytesConfig
106import torch
107
108# 4λΉ„νŠΈ μ–‘μžν™” μ„€μ •
109bnb_config = BitsAndBytesConfig(
110    load_in_4bit=True,
111    bnb_4bit_use_double_quant=True,
112    bnb_4bit_quant_type="nf4",
113    bnb_4bit_compute_dtype=torch.bfloat16
114)
115
116# μ–‘μžν™”λœ λͺ¨λΈ λ‘œλ“œ
117model = AutoModelForCausalLM.from_pretrained(
118    "meta-llama/Llama-2-7b-hf",
119    quantization_config=bnb_config,
120    device_map="auto"
121)
122
123# LoRA 적용
124model = get_peft_model(model, lora_config)
125
126# ν•™μŠ΅
127trainer = Trainer(model=model, ...)
128'''
129print(qlora_code)
130
131
132# ============================================
133# 4. μ»€μŠ€ν…€ λ©”νŠΈλ¦­
134# ============================================
135print("\n[4] μ»€μŠ€ν…€ λ©”νŠΈλ¦­")
136print("-" * 40)
137
138try:
139    import evaluate
140    import numpy as np
141
142    # λ©”νŠΈλ¦­ λ‘œλ“œ
143    accuracy = evaluate.load("accuracy")
144    f1 = evaluate.load("f1")
145
146    def compute_metrics(eval_pred):
147        logits, labels = eval_pred
148        predictions = np.argmax(logits, axis=-1)
149        return {
150            "accuracy": accuracy.compute(predictions=predictions, references=labels)["accuracy"],
151            "f1": f1.compute(predictions=predictions, references=labels, average="weighted")["f1"]
152        }
153
154    print("μ»€μŠ€ν…€ λ©”νŠΈλ¦­ ν•¨μˆ˜ μ •μ˜ μ™„λ£Œ")
155
156    # ν…ŒμŠ€νŠΈ
157    mock_pred = (np.array([[0.9, 0.1], [0.2, 0.8]]), np.array([0, 1]))
158    result = compute_metrics(mock_pred)
159    print(f"ν…ŒμŠ€νŠΈ κ²°κ³Ό: {result}")
160
161except ImportError:
162    print("evaluate λ―Έμ„€μΉ˜ (pip install evaluate)")
163
164
165# ============================================
166# 5. NER νŒŒμΈνŠœλ‹
167# ============================================
168print("\n[5] NER νŒŒμΈνŠœλ‹")
169print("-" * 40)
170
171ner_code = '''
172from transformers import AutoModelForTokenClassification
173
174# λ ˆμ΄λΈ”
175label_names = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"]
176
177# λͺ¨λΈ
178model = AutoModelForTokenClassification.from_pretrained(
179    "bert-base-uncased",
180    num_labels=len(label_names)
181)
182
183# 토큰 μ •λ ¬ (μ„œλΈŒμ›Œλ“œ 처리)
184def tokenize_and_align_labels(examples):
185    tokenized = tokenizer(examples["tokens"], truncation=True, is_split_into_words=True)
186
187    labels = []
188    for i, label in enumerate(examples["ner_tags"]):
189        word_ids = tokenized.word_ids(batch_index=i)
190        label_ids = []
191        for word_idx in word_ids:
192            if word_idx is None:
193                label_ids.append(-100)  # 특수 토큰
194            else:
195                label_ids.append(label[word_idx])
196        labels.append(label_ids)
197
198    tokenized["labels"] = labels
199    return tokenized
200'''
201print(ner_code)
202
203
204# ============================================
205# 6. QA νŒŒμΈνŠœλ‹
206# ============================================
207print("\n[6] QA νŒŒμΈνŠœλ‹")
208print("-" * 40)
209
210qa_code = '''
211from transformers import AutoModelForQuestionAnswering
212
213# λͺ¨λΈ
214model = AutoModelForQuestionAnswering.from_pretrained("bert-base-uncased")
215
216# μ „μ²˜λ¦¬ (μ‹œμž‘/끝 μœ„μΉ˜ μ°ΎκΈ°)
217def prepare_train_features(examples):
218    tokenized = tokenizer(
219        examples["question"],
220        examples["context"],
221        truncation="only_second",
222        max_length=384,
223        stride=128,
224        return_overflowing_tokens=True,
225        return_offsets_mapping=True,
226        padding="max_length",
227    )
228
229    # λ‹΅λ³€ μœ„μΉ˜λ₯Ό 토큰 μœ„μΉ˜λ‘œ λ³€ν™˜
230    tokenized["start_positions"] = []
231    tokenized["end_positions"] = []
232
233    for i, offsets in enumerate(tokenized["offset_mapping"]):
234        # λ‹΅λ³€ μ‹œμž‘/끝 문자 μœ„μΉ˜ β†’ 토큰 μœ„μΉ˜
235        ...
236
237    return tokenized
238'''
239print(qa_code)
240
241
242# ============================================
243# 7. ν•™μŠ΅ μ΅œμ ν™” 팁
244# ============================================
245print("\n[7] ν•™μŠ΅ μ΅œμ ν™” 팁")
246print("-" * 40)
247
248optimization_tips = '''
249# Gradient Checkpointing (λ©”λͺ¨λ¦¬ μ ˆμ•½)
250model.gradient_checkpointing_enable()
251
252# Mixed Precision (속도 ν–₯상)
253args = TrainingArguments(
254    ...,
255    fp16=True,  # λ˜λŠ” bf16=True
256)
257
258# Gradient Accumulation (큰 배치 효과)
259args = TrainingArguments(
260    per_device_train_batch_size=4,
261    gradient_accumulation_steps=8,  # μ‹€νš¨ 배치 = 32
262)
263
264# DeepSpeed (λΆ„μ‚° ν•™μŠ΅)
265args = TrainingArguments(
266    ...,
267    deepspeed="ds_config.json"
268)
269
270# Learning Rate Scheduler
271args = TrainingArguments(
272    learning_rate=2e-5,
273    warmup_ratio=0.1,
274    lr_scheduler_type="cosine",
275)
276'''
277print(optimization_tips)
278
279
280# ============================================
281# 정리
282# ============================================
283print("\n" + "=" * 60)
284print("νŒŒμΈνŠœλ‹ 정리")
285print("=" * 60)
286
287summary = """
288νŒŒμΈνŠœλ‹ 선택 κ°€μ΄λ“œ:
289    - μΆ©λΆ„ν•œ GPU: Full Fine-tuning
290    - μ œν•œλœ λ©”λͺ¨λ¦¬: LoRA / QLoRA
291    - 맀우 적은 데이터: Prompt Tuning
292
293핡심 μ½”λ“œ:
294    # Trainer
295    trainer = Trainer(model=model, args=args, train_dataset=dataset)
296    trainer.train()
297
298    # LoRA
299    from peft import LoraConfig, get_peft_model
300    config = LoraConfig(r=8, target_modules=["query", "value"])
301    model = get_peft_model(model, config)
302"""
303print(summary)