base_model: Qwen/Qwen2.5-Coder-3B-Instruct library_name: peft pipeline_tag: text-generation tags:
A parameter-efficient fine-tuning project that adapts Qwen2.5-Coder-3B-Instruct using QLoRA and coding instruction datasets. The project explores how additional supervised fine-tuning affects code generation, instruction following, and performance on simple programming tasks.
Large language models can generate code, explain algorithms, and assist with software development. However, fine-tuning can adapt their behavior to specific datasets and task requirements.
In this project, I fine-tuned an existing coding model using 4-bit QLoRA, trained it on coding instruction-response examples, saved the learned LoRA adapter, and tested inference and preliminary coding performance against the original base model.
Important: This project fine-tunes an existing pretrained model; it does not train a language model from scratch.
| Parameter | Configuration |
|---|---|
| Base model | Qwen/Qwen2.5-Coder-3B-Instruct |
| Fine-tuning method | QLoRA |
| Quantization | 4-bit NF4 |
| Compute dtype | BF16 during training |
| LoRA rank | 16 |
| LoRA alpha | 32 |
| LoRA dropout | 0.05 |
| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
| Maximum sequence length | 2,048 tokens |
| Training examples | 45,000 |
| Validation examples | 2,994 |
| Epochs | 2 |
| Learning rate | 2e-4 |
| Optimizer | Paged AdamW 8-bit |
| Gradient accumulation | 16 |
| Training hardware | AWS EC2 G5.xlarge with NVIDIA A10G GPU |
QLoRA loads the base model in quantized form and trains additional low-rank adapter parameters. The original base weights remain frozen during training.
The training dataset was constructed from two sources:
| Split | Examples |
|---|---|
| Training | 45,000 |
| Validation | 2,994 |
| Total prepared | 47,994 |
The combined dataset includes programming instructions and examples covering multiple programming languages. Dataset metadata and language labels were retained where available.
The production training run completed successfully after two epochs.
| Metric | Result |
|---|---|
| Training loss | 0.4730 |
| Validation loss | 0.6010 |
| Mean token accuracy | 82.81% |
| Training duration | Approximately 14.85 hours |
| Final optimizer steps | 5,626 |
| Adapter size | Approximately 60 MB |
These metrics describe the training run and its validation behavior. Mean token accuracy is not equivalent to the percentage of coding problems solved correctly.
The original model and fine-tuned model were tested using the same prompts and predefined expected outputs on small, custom Python coding test suites.
| Evaluation Set | Original Qwen | Fine-Tuned Qwen |
|---|---|---|
| Initial 5-problem pilot | 12/15 (80.0%) | 15/15 (100.0%) |
| Additional 20-problem pilot | 52/60 (86.7%) | 54/60 (90.0%) |
The second evaluation showed a difference of 3.3 percentage points in favor of the fine-tuned model on this particular test suite.
These results are preliminary, not a definitive benchmark. The test suites are small and custom-built, and some problems overlap with earlier experiments. They do not establish general coding superiority or prove that the observed difference will generalize to unseen tasks.
A more rigorous evaluation should use a larger set of independent programming problems, validated test cases, and an isolated execution environment.
The trained adapter can be loaded alongside the original base model to generate code.
The inference process is:
Write a Python function to implement binary search
on a sorted list. Return the target index or -1
if the target is not present.
The adapter contains the learned parameter updates; it is not a standalone replacement for the base model.
qwen-coder/
├── data/
│ ├── final/
│ ├── train/
│ └── validation/
├── scripts/
│ └── train_qlora.py
├── checkpoints/
│ └── qwen-coder-qlora/
├── outputs/
│ └── qwen-coder-qlora/
│ ├── adapter_config.json
│ ├── adapter_model.safetensors
│ ├── eval_metrics.json
│ └── training_metrics.json
└── README.md
The exact contents may vary depending on which training checkpoints and generated artifacts are included in the repository.
Install the required dependencies in a compatible Python environment:
pip install torch transformers peft accelerate bitsandbytes
Load the base model and attach the adapter:
import torch
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
BitsAndBytesConfig,
)
from peft import PeftModel
BASE_MODEL = "Qwen/Qwen2.5-Coder-3B-Instruct"
ADAPTER_PATH = "./outputs/qwen-coder-qlora"
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
)
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
quantization_config=quant_config,
device_map="auto",
)
model = PeftModel.from_pretrained(
base_model,
ADAPTER_PATH,
)
model.eval()
messages = [
{
"role": "user",
"content": (
"Write a Python function to check whether "
"a string is a palindrome."
),
}
]
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt",
).to(model.device)
with torch.inference_mode():
outputs = model.generate(
inputs,
max_new_tokens=256,
do_sample=False,
pad_token_id=tokenizer.eos_token_id,
)
generated_tokens = outputs[0][inputs.shape[1]:]
print(
tokenizer.decode(
generated_tokens,
skip_special_tokens=True,
)
)
The code assumes the adapter files are available locally and that the installed libraries and hardware support the selected quantization configuration. BF16 was used for the original training run; FP16 is used here for broader inference compatibility.
This project demonstrates an end-to-end QLoRA fine-tuning workflow for an existing code-generation model: dataset preparation, quantized training, adapter saving, inference, and preliminary evaluation.
The initial experiments show promising results on selected custom test cases, while further independent evaluation is needed to determine the generality and significance of any performance improvement.
Review the base model's license and the licenses and terms of both datasets before redistributing model artifacts or datasets. Preserve required attribution and comply with the applicable terms.
checkpoint-32/optimizer.pt 61 MBadapter_model.safetensors 60 MBcheckpoint-32/adapter_model.safetensors 60 MBcheckpoint-32/rng_state.pth 14.6 kBcheckpoint-32/training_args.bin 5.7 kBtraining_args.bin 5.7 kBcheckpoint-32/scheduler.pt 1.5 kBQwen/Qwen2.5-Coder-3B-Instruct