Fine-tuning
Fine-Tuning
Additional training of a pretrained model for a specific task. Made efficient with LoRA.
Fine-Tuning
Additional training of a pretrained model for a specific task. Made efficient with LoRA.
Fine-tuning is a technique that further trains a large-scale pre-trained model to fit a specific domain or task. It is used to specialize Foundation Models such as GPT-4 or Llama for professional fields such as medicine, law, and finance.
Full Fine-tuning, which retrains all parameters, is costly, so efficient techniques such as LoRA (Low-Rank Adaptation) and QLoRA have emerged. LoRA freezes the original weights and trains only small adapter matrices, reducing GPU memory by more than 90%.
OpenAI supports fine-tuning of GPT-3.5 Turbo and GPT-4o mini, and the training cost is $0.008~$0.025 per token. In open source, Llama, Mistral, and others can be fine-tuned with the Hugging Face Transformers + PEFT library.
Choosing between RAG and Fine-tuning is important. If real-time knowledge updates are needed, RAG is suitable; if learning a specific style/format is needed, Fine-tuning is suitable. In practice, the two are often used together.
# LoRA를 이용한 Llama 파인튜닝 (PEFT 라이브러리)
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from datasets import load_dataset
from trl import SFTTrainer
# 모델 로드 (4비트 양자화로 메모리 절약)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
load_in_4bit=True,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
# LoRA 설정: rank 16, alpha 32가 일반적인 시작점
lora_config = LoraConfig(
r=16, # LoRA rank
lora_alpha=32, # 스케일링 팩터
target_modules=["q_proj", "v_proj"], # 어텐션 레이어만 학습
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM"
)
model = prepare_model_for_kbit_training(model)
model = get_peft_model(model, lora_config)
# 학습 가능 파라미터 확인
model.print_trainable_parameters()
# Output: trainable params: 4,194,304 || all params: 6,738,415,616 || 0.06%
# 데이터셋 & 학습
dataset = load_dataset("your-dataset")
trainer = SFTTrainer(model=model, train_dataset=dataset["train"], ...)
trainer.train()
# 어댑터만 저장 (수 MB)
model.save_pretrained("./lora-adapter")
This is OpenAI fine-tuning cost information as of January 2025.
| Model | Training cost (1M tokens) | Inference input | Inference output |
|---|---|---|---|
| GPT-4o fine-tuning | $25.00 | $3.75 | $15.00 |
| GPT-4o-mini fine-tuning | $3.00 | $0.30 | $1.20 |
| GPT-3.5 Turbo fine-tuning | $8.00 | $3.00 | $6.00 |
Cost calculation: Total training tokens = training data tokens x number of epochs. For 10,000 data entries (about 500K tokens) over 3 epochs, GPT-4o-mini costs about $4.5. An inference discount applies when the data sharing option is enabled.
"To match the client's tone and manner, we need fine-tuning. Fine-tuning Llama 7B with QLoRA is possible on a single A100, and 10,000 training data entries take 2–3 hours. The cost is about $30 on cloud GPUs."
"LoRA works by adding a low-rank matrix BA to the original weights W. With rank 16, the trainable parameters drop to under 0.1%, which maximizes memory efficiency. In an actual legal QA chatbot project, LoRA fine-tuning improved domain accuracy by 40%."
"It's best to separate real-time updated product information into RAG and customer-service style learning into fine-tuning. If you attach RAG to a fine-tuned model, you can get the benefits of both."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
"Garbage in, garbage out" - Noisy data actually degrades model performance. 10,000 cleaned data entries are better than 100,000 noisy data entries.
Excessive fine-tuning makes the model forget existing knowledge. Keep the learning rate low (1e-5~5e-5) and the number of epochs small (2~3).
Invest in building a high-quality dataset, and tune the LoRA rank and learning rate through experiments. Comparing benchmarks before and after fine-tuning is essential.