QLoRA: Efficient Finetuning of Quantized LLMs
Paper โข 2305.14314 โข Published โข 61
How to use Beybars/mistral-7b-qlora-sft with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1")
model = PeftModel.from_pretrained(base_model, "Beybars/mistral-7b-qlora-sft")How to use Beybars/mistral-7b-qlora-sft with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="Beybars/mistral-7b-qlora-sft") # Load model directly
from transformers import AutoModel
model = AutoModel.from_pretrained("Beybars/mistral-7b-qlora-sft", dtype="auto")How to use Beybars/mistral-7b-qlora-sft with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "Beybars/mistral-7b-qlora-sft"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Beybars/mistral-7b-qlora-sft",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/Beybars/mistral-7b-qlora-sft
How to use Beybars/mistral-7b-qlora-sft with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "Beybars/mistral-7b-qlora-sft" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Beybars/mistral-7b-qlora-sft",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "Beybars/mistral-7b-qlora-sft" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Beybars/mistral-7b-qlora-sft",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use Beybars/mistral-7b-qlora-sft with Docker Model Runner:
docker model run hf.co/Beybars/mistral-7b-qlora-sft
A QLoRA adapter for Mistral-7B-v0.1, fine-tuned on the Stanford Alpaca dataset using Supervised Fine-Tuning (SFT).
Author: Beybars
| Parameter | Value |
|---|---|
| Base model | mistralai/Mistral-7B-v0.1 |
| Method | QLoRA (4-bit NF4 quantization) |
| LoRA rank (r) | 16 |
| LoRA alpha | 16 |
| LoRA dropout | 0.1 |
| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
| Dataset | tatsu-lab/alpaca (~51K examples) |
| Epochs | 3 |
| Learning rate | 1e-4 |
| Batch size | 4 (gradient accumulation 4, effective 16) |
| Optimizer | paged_adamw_8bit |
| Precision | bfloat16 |
| GPU | NVIDIA RTX 3090 (24GB) |
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch
# Load base model in 4-bit
from transformers import BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
base_model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-v0.1",
quantization_config=bnb_config,
device_map="auto",
)
# Load tokenizer and resize embeddings
tokenizer = AutoTokenizer.from_pretrained("Beybars/mistral-7b-qlora-sft")
base_model.resize_token_embeddings(len(tokenizer))
# Load adapter
model = PeftModel.from_pretrained(base_model, "Beybars/mistral-7b-qlora-sft")
# Generate
prompt = "### Instruction:\nExplain quantum computing in simple terms.\n\n### Response:\n"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.7, top_p=0.9)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
| Metric | Value |
|---|---|
| Final training loss | ~1.05 |
| Final eval loss | ~1.10 |
| Gradient norm | Stable ~0.5โ0.6 |
| Training time | ~15 hours (3 epochs) |
This is a learning project adapter. It demonstrates QLoRA fine-tuning on a consumer GPU. Not intended for production use.
Training code: github.com/beybars1/llm-tuning
Base model
mistralai/Mistral-7B-v0.1