C.W.K.
Stream
Lesson 04 of 05 · published

완전한 QLoRA 파이프라인

~28 min · qlora, end-to-end, wandb, pipeline

Level 0관찰자
0 XP0/43 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

End-to-end 레시피

이전 레슨 모든 것이 하나의 실서비스 모양 스크립트로 결합. 양자화 → 토크나이저 → LoRA → SFTConfig → Trainer → 저장. 진짜 파인튜닝 프로젝트가 어떻게 생겼는지.

Code

Production QLoRA pipeline (Llama 3.1 8B + W&B)·python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig, prepare_model_for_kbit_training
from trl import SFTConfig, SFTTrainer
from datasets import load_dataset

# ── Config ──
model_id = "meta-llama/Llama-3.1-8B-Instruct"
output_dir = "./llama-3.1-8b-finetuned"

# ── 4-bit quantization ──
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)

# ── Load model + tokenizer ──
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=bnb_config,
    device_map="auto",
    attn_implementation="flash_attention_2",
)
model = prepare_model_for_kbit_training(model)

tokenizer = AutoTokenizer.from_pretrained(model_id)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

# ── LoRA ──
peft_config = LoraConfig(
    r=32, lora_alpha=64,
    target_modules="all-linear",
    lora_dropout=0.05, bias="none",
    task_type="CAUSAL_LM",
)

# ── Dataset ──
dataset = load_dataset("json", data_files={
    "train":      "train.jsonl",
    "validation": "val.jsonl",
})

# ── Training config ──
args = SFTConfig(
    output_dir=output_dir,
    num_train_epochs=3,
    per_device_train_batch_size=2,
    gradient_accumulation_steps=8,    # effective batch = 16
    learning_rate=2e-4,
    lr_scheduler_type="cosine",
    warmup_ratio=0.1,
    bf16=True,
    gradient_checkpointing=True,
    gradient_checkpointing_kwargs={"use_reentrant": False},
    logging_steps=10,
    save_strategy="steps",
    save_steps=100,
    eval_strategy="steps",
    eval_steps=100,
    max_seq_length=2048,
    packing=True,
    dataset_kwargs={"assistant_only_loss": True},
    report_to="wandb",                # optional W&B tracking
)

# ── Trainer + go ──
trainer = SFTTrainer(
    model=model,
    args=args,
    train_dataset=dataset["train"],
    eval_dataset=dataset["validation"],
    processing_class=tokenizer,
    peft_config=peft_config,
)

trainer.train()
trainer.save_model(f"{output_dir}/final")
tokenizer.save_pretrained(f"{output_dir}/final")
print("Training complete!")

External links

Exercise

위 풀 파이프라인을 Llama 3.1 8B에 실제(또는 1,000예제 합성) 데이터셋으로 돌려. 트래킹은 W&B 사용. 학습 후 W&B 로그인해서 네 training loss 곡선을 출간된 거랑 비교(wandb.ai에서 'llama 3.1 lora' 검색). 네 곡선이 건강해 보여?

Progress

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

댓글 0

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

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