C.W.K.
Stream
Lesson 07 of 08 · published

QLoRA 와 PEFT 라이브러리

~26 min · training, peft, qlora

Level 0스카우트
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

QLoRA: 양자화된 base 위 LoRA

Base 모델 4-bit 로 양자화 (NF4 specifically — 정규 분포 가중치 위해 디자인된 양자화 scheme), frozen 유지, 위에 LoRA adapter 학습. 결과: 단일 48GB GPU 에 70B 모델 fine-tune 가능. Base 4-bit; 작은 adapter (fp16/bf16) 만 학습.

구성

  • BitsAndBytesConfig — 4-bit quant scheme 선언.
  • prepare_model_for_kbit_training — base freeze, grad checkpointing 활성화, 필요한 cast.
  • LoraConfig — 이전과 같음.
  • get_peft_model — adapter 로 모델 wrap.

PEFT 메서드 zoo

PEFT 가 LoRA 만 X. 지원 다른 메서드: IA3, LoHa, LoKr, OFT, X-LoRA, VeRA. 팀 대부분 LoRA + QLoRA 디폴트. exotic 한 거 specific axis (메모리, expressivity) 에서 이김 — 그래도 typical SFT 의 marginal 품질 차이 작음.

Code

QLoRA 셋업·python
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
import torch

bnb_cfg = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)

base = "meta-llama/Llama-3.1-8B-Instruct"
tok = AutoTokenizer.from_pretrained(base)
model = AutoModelForCausalLM.from_pretrained(base, quantization_config=bnb_cfg, device_map="auto")
model = prepare_model_for_kbit_training(model)

lora_cfg = LoraConfig(
    r=16, lora_alpha=32, lora_dropout=0.05,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    bias="none", task_type="CAUSAL_LM",
)
model = get_peft_model(model, lora_cfg)
model.print_trainable_parameters()
# 이제 Trainer / SFTTrainer 학습

External links

Exercise

7B 모델에 QLoRA fine-tune 셋업. GPU 메모리 비교: 풀 bf16 (fit X), LoRA bf16 (fit 가능), QLoRA. 200 step 학습. Adapter 저장. 인퍼런스: 4-bit base + adapter 로드 후 generate. base SHA 핀해서 결과 재현 가능.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.