C.W.K.
Stream
Lesson 01 of 06 · published

LoRA — 더 이상 모델 전체 안 학습시키는 이유

~14 min · lora, adapters, peft

Level 0Curious
0 XP0/51 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Full fine-tuning 을 깨뜨린 수학

70B 모델 full fine-tune 한다는 건 700 억 parameter 의 gradient 계산, 다 위한 optimizer state 보유 (Adam 이 moment 에서 parameter 수의 2× 추가), forward pass 에 걸친 activation 저장. 총 메모리 예산이 모델의 추론 풋프린트의 대략 4-6× — 70B 모델엔 너에게 없는 수백 기가바이트의 GPU 메모리.

LoRA (Low-Rank Adaptation) 가 문제 전체를 sidestep. 모든 원래 weight freeze 하고 select layer 위에 작은 학습 가능 adapter 행렬 추가. Adapter 만 학습; 나머지 무시. 학습 가능 parameter 수가 99% 이상 떨어지고, 그와 함께 메모리 예산 — 16 GB MacBook 에서 7B 모델, office Mac 에서 70B 모델 fine-tune 가능.

분해 트릭, 짧게

shape (d × d) 의 weight 행렬 W 에 대해, LoRA 가 업데이트 ΔW 를 두 깡마른 행렬의 곱으로 근사 — ΔW ≈ A · B, A 가 shape (d × r), B 가 shape (r × d), r (LoRA rank) 가 작음 — 전형적으로 8 또는 16. d² parameter 학습 대신 2 · d · r 학습 — 원래 카운트의 분수.

추론에선 (W + A · B) · x 계산, 원래 W 는 안 변하고 adapter A · B 가 학습된 correction. 두 행렬, 한 rank, 그게 architectural 변경 전부.

왜 작은 adapter 가 full fine-tune 와 경쟁하나

경험적으로, fine-tune 이 사전 학습 모델에 실제로 만들고 싶어 하는 변화들이 저-차원 부분 공간에 살아. 가능한 weight 업데이트의 full d² 공간은 대부분 downstream 작업에 overkill. LoRA 가 주는 rank-16 부분 공간이 fine-tuning 이 표현해야 할 거 대부분 덮어. 흥미로운 거의 모든 벤치마크에서, LoRA 가 parameter 비용의 1% 로 full fine-tune 품질의 90%+ 줘.

그 숫자가 진짜고 load-bearing. 그게 오픈-weight 세상의 누구든 전형 instruction-following 또는 domain-adaptation 작업에 더 이상 full fine-tune 안 하는 이유 — 수학이 그냥 그걸 옹호 안 해.

이 트랙이 할 일

여섯 레슨, Apple Silicon 의 end-to-end LoRA 워크플로 — 데이터 준비 (lesson 2), training-config 노브 고르기 (lesson 3), 정당화될 때 QLoRA / DoRA 로 졸업 (lesson 4), 싼 추론 위해 adapter merge (lesson 5), 그 다음 자기 데이터셋으로 반복 가능한 완전한 walkthrough (lesson 6). 32 GB Mac 의 7B base 모델로 전체 파이프라인 편안하게 돌아.

Code

최소 LoRA 학습 명령·bash
# Train a LoRA adapter on a small base model with a HF-hosted dataset.
# This is the literal minimum to start; lesson 3 walks through every flag.
python -m mlx_lm lora \
  --model mlx-community/Llama-3.2-1B-Instruct-4bit \
  --train \
  --data mlx-community/wikisql \
  --iters 100 \
  --adapter-path ./my-adapter

# Output during training (verbose; mlx-lm reports loss every --steps-per-report iters):
#   Iter 10: Train loss 1.832, Learning Rate 1.000e-05, ...
#   Iter 20: Train loss 1.521, ...
#   ...
학습 후 adapter 디렉토리 살피기·bash
ls ./my-adapter/
# adapters.safetensors    — the trained LoRA weights (small file!)
# adapter_config.json     — rank, alpha, layers, fine-tune type, etc.
# 0000100_adapters.safetensors  — checkpoint at iter 100 (if --save-every fired)

# The adapter file is tiny compared to the base model:
du -sh mlx-community/Llama-3.2-1B-Instruct-4bit/  # ~700 MB base
du -sh ./my-adapter/                              # tens of MB at most

External links

Exercise

아직 안 돌리고 예측 — 기본 16 layer 의 rank-8 LoRA 로 fine-tune 된 Llama-3.2-1B base 모델에 대해, 학습 가능 parameter 가 대략 얼마? 대략 공식 2 · d_model · r · num_layers 사용 (Llama-3.2-1B 의 d_model 은 2048). 그 다음 짧은 학습 후 adapter_config.json 살펴 실제 카운트 체크. 예측 일치했는지 두 문장.

Progress

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

댓글 0

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

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