안녕하세요! 저는 오픈 모델(open models)을 미세 조정(fine-tune)할 수 있는 플랫폼을 구축했습니다. GPU 비용을 절감하기 위해 전체 정밀도 미세 조정 대신 4비트 양자화(quantized) 모델을 QLoRA로 미세 조정하는 데 중점을 두고 있습니다. 이 미세 조정에 사용되는 양자화 방식은 CQ(Coded Quantization)라고 불리며, 제가 직접 구축한 보정 과정이 필요 없는(calibration-free) 양자화 기법입니다. 이는 최고 수준의 양자화 방식만큼 성능을 발휘하며, 미세 조정에 좋은 기반을 제공합니다.
예를 들어, Wikitext-2 데이터셋을 사용하여 Qwen 3.8 27B 모델에서 CQ 4비트와 unsloth-bnb-4bit를 비교한 KLD(Kullback–Leibler Divergence) 값은 다음과 같습니다:
| Quant Disk Size (GB) | Mean KLD | Median KLD | 99% KLD | Top 1% Agreement |
|---|
| CQ 4-bit | 18 | 0.02823666 | 0.01282929 | 0.97606068 | 92.419% |
| unsloth-bnb-4bit | 22.4 | 0.03297115 | 0.01437706 | 1.18053893 | 91.954% |
따라서 저는 더 나은 양자화 기반을 확보하게 되었습니다. 이후 Qwen3.8-27B 모델에 대한 CQ-4bit와 unsloth-bnb-4bit의 QLoRA 학습 성능을 벤치마킹했습니다.
벤치마크 설정:
- 모델: Qwen3.8-27B
- 방법: Alpaca 기반 QLoRA 미세 조정 비교 (ThousandML CQ 4비트 vs. Unsloth bitsandbytes NF4)
- 공통 설정: LoRA r=32, α=32, 배치 크기(batch size) 2, 기울기 누적(gradient accumulation) 4, 시퀀스 길이(sequence length) 512, 학습률(learning rate) 2e-5, 시드(seed) 42, NVIDIA A40 48 GB GPU에서 최적화 단계(optimizer steps) 500
CQ는 보고된 최종 검증 손실(validation loss)에서 17.4% 낮은 값(1.0315 대 1.2484)을 달성했습니다. 이는 손실 비교일 뿐, 아직 검증된 다운스트림 작업 품질 비교는 아닙니다. 검증 손실 및 학습 손실에 대한 그림이 첨부되어 있습니다.
https://preview.redd.it/uzaf8s8mypxh1.png?width=2750&format=png&auto=webp&s=65715b0e4e23b91ba88a1fb7b574d151e170b886 https://preview.redd.it/jcif1uar1qxh1.png?width=2750&format=png&auto=webp&s=e35d4603ed6cdda3262ae0e9cd0e72ecbe19b850
Qwen3.8-27B — Alpaca 기반 QLoRA 미세 조정 (500 스텝) | 지표 | CQ 4-bit | Unsloth NF4 | 장점 |
:--|--:|--:|:--|
| 최고 검증 손실 | 0.8385 | 1.2484 | CQ |
| 최종 검증 손실 | 1.0315 | 1.2484 | CQ |
| 평균 학습 손실 (스텝 401–500) | 1.0505 | 1.2457 | CQ |
| 중앙값 안정화 최적화 스텝 시간 | 27.60 s | 3.20 s | Unsloth |
| 최대 PyTorch 할당 메모리 | 24.39 GiB | 23.83 GiB | Unsloth |
두 실행 모두 NVIDIA A40 48 GB GPU에서 일치하는 명목 하이퍼파라미터로 Alpaca에 QLoRA를 사용했습니다. 토큰화 및 손실 마스크 동등성은 독립적으로 검증되지 않았습니다.
저는 속도 최적화 작업을 진행하고 있습니다. 왜냐하면 이 분야에서 CQ가 Unsloth보다 상당히 뒤처져 있기 때문입니다. 반면에 검증 손실과 VRAM 사용량은 훌륭합니다! 실제로 CQ는 훨씬 적은 스텝에서 수렴하는 것 같아 큰 도움이 될 수 있을 것 같습니다.
Training Factory 시작하기: 사용 방법 안내
여기에 간단한 사용 단계가 있습니다:
- 설치:
git clone https://github.com/thousandml/training-factory.git
cd training-factory
mchmod +x install.sh
./install.sh
- CQ 4-bit 기본 모델 다운로드:
hf download thousandml/Qwen3.8-27B-CQ-4bit --local-dir ./models/Qwen3.8-27B-CQ-4bit
- LoRA 어댑터 학습:
python training/train.py --model ./models/Qwen3.8-27B-CQ-4bit --data training/examples/train.jsonl --output ./outputs/my-lora --targets q_proj,v_proj --kernel-module cq.cq_kernels --text-only --max-steps 2 --max-length 128 --batch-size 1 --grad-accum 1
- 기본 모델 + 어댑터 서빙:
vllm serve ./models/Qwen3.8-27B-CQ-4bit --quantization cq --enable-lora --max-lora-rank 16 --lora-modules my-lora=./outputs/my-lora
미세 조정된 어댑터 호출하기: # 서버가 준비되면: # 해당 어댑터가 "my-lora"로 등록되었는지 확인합니다
curl http://localhost:8000/v1/models
기본 체크포인트가 아닌 미세 조정된 모델로부터 응답을 요청하려면 다음 명령어를 사용하세요:
curl http://localhost:8000/v1/chat/completions
-H "Content-Type: application/json"
-d '{ "model": "my-lora", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain how LoRA fine-tuning works in one sentence." } ], "max_tokens": 128, "temperature": 0.2 }'
현재 기본 모델로 사용 가능한 4비트 양자화된 모델은 다음과 같습니다 (추후 더 추가 예정): thousandml/Qwen3.8-27B-CQ-4bit, thousandml/Llama-3.1-8B-Instruct-CQ-4bit, thousandml/Llama-3.3-70B-Instruct-CQ-4bit, thousandml/Qwen3-Coder-Next-CQ-4bit
위에서 언급된 벤치마크 결과를 재현하려면 다음 스크립트를 사용하십시오:
CQ의 경우:
hf download thousandml/Qwen3.8-27B-CQ-4bit
--local-dir ./models/Qwen3.8-27B-CQ-4bit
python training/train.py
--model ./models/Qwen3.8-27B-CQ-4bit
--data training/examples/alpaca_train.jsonl
--eval-data training/examples/alpaca_val.jsonl
--output ./outputs/qwen38-27b-cq-4bit
--kernel-module cq.cq_kernels
--targets q_proj,k_proj,v_proj,o_proj,gate_proj,up_proj,down_proj
--rank 32
--alpha 32
--lora-dropout 0
--batch-size 2
--grad-accum 4
--max-length 512
--max-steps 500
--learning-rate 2e-5
--seed 42
--eval-steps 50
--comparison-logs
--benchmark
--benchmark-warmup-steps 10
Unsloth의 경우:
python training/train_unsloth_nf4.py
--model unsloth/Qwen3.8-27B-unsloth-bnb-4bit
--train-data training/examples/alpaca_train.jsonl
--eval-data training/examples/alpaca_val.jsonl
--output ./outputs/qwen38-27b-unsloth-bnb-4bit
--max-steps 500
--max-length 512
--eval-steps 50
--save-steps 100
--seed 42
--benchmark-warmup-steps 10
직접 시도해 보시고 작동하는지 알려주시면 감사하겠습니다. 피드백은 매우 환영합니다! submitted by /u/textclf [link] [comments]