PEFT
Safetensors
English
code
python
lora
qwen2
code-generation
File size: 5,456 Bytes
acc11b8
c7ace90
 
5be539f
bb6690f
 
 
c7ace90
5be539f
 
 
bb6690f
 
 
 
 
c7ace90
 
5be539f
c7ace90
bb6690f
 
 
c7ace90
5be539f
c7ace90
bb6690f
 
 
 
 
 
 
 
 
 
 
 
b6df816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb6690f
c7ace90
bb6690f
27922f7
acc11b8
 
 
 
 
 
 
27922f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acc11b8
27922f7
acc11b8
 
 
 
27922f7
 
 
 
acc11b8
 
 
 
27922f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acc11b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

---
base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct
library_name: peft
license: apache-2.0
language:
- en
tags:
- code
- python
- lora
- peft
- qwen2
- code-generation
datasets:
- iamtarun/python_code_instructions_18k_alpaca
---

# my-python-coder

A LoRA fine-tune of [Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) specialized for Python code generation.

This model was fine-tuned as a learning project to demonstrate the full workflow of taking a base model, training it on a custom dataset, and publishing it to the Hugging Face Hub.

## Training Details

| Parameter | Value |
|---|---|
| **Base model** | `Qwen/Qwen2.5-Coder-1.5B-Instruct` |
| **Dataset** | `iamtarun/python_code_instructions_18k_alpaca` (first 1,500 examples) |
| **Method** | LoRA (r=16, alpha=32, target_modules=`all-linear`) |
| **Training steps** | 200 |
| **Learning rate** | 2e-4 |
| **Effective batch size** | 8 (batch=2 Γ— grad_accum=4) |
| **Max sequence length** | 1024 |
| **Hardware** | Google Colab (NVIDIA T4, 16 GB VRAM) |
| **Training time** | ~33 minutes |

## What Is This β€” A Model or an Adapter?

This repository contains a **LoRA adapter**, not a standalone model. Understanding the difference matters for how you load and use it.

### The Two Artifacts

| | **Base Model** | **LoRA Adapter (this repo)** |
|---|---|---|
| **What it is** | The full pretrained neural network | A small set of trained weights that modify the base |
| **Size** | ~3 GB | ~74 MB |
| **Who made it** | The Qwen team | Me (SathishKumar89) |
| **Repo** | `Qwen/Qwen2.5-Coder-1.5B-Instruct` | `SathishKumar89/my-python-coder` |
| **Contains** | All model weights, tokenizer, config | Only adapter weights + config + tokenizer copy |
| **Loadable alone?** | βœ… Yes | ❌ No β€” needs the base model |

### Why This Design?

Instead of retraining all ~1.5 billion parameters of the base model, **LoRA (Low-Rank Adaptation)** freezes the base model and only trains a tiny number of new parameters. This gives several advantages:

- **Tiny file size** β€” 74 MB vs. ~3 GB (a ~40Γ— reduction)
- **Fast training** β€” minutes to hours instead of days
- **Runs on modest hardware** β€” a free Google Colab T4 GPU is enough
- **Easy to swap** β€” you can keep the same base model and load different adapters for different tasks

### How to Load It Correctly

Because this repo is an adapter, you must load **two** things β€” the base model first, then the adapter on top:

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch

# Step 1: Load the base model
base = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen2.5-Coder-1.5B-Instruct",
    dtype=torch.float16,
    device_map="auto",
)

# Step 2: Attach the LoRA adapter
model = PeftModel.from_pretrained(base, "SathishKumar89/my-python-coder")

# Step 3: Load the tokenizer (included in this repo)
tokenizer = AutoTokenizer.from_pretrained("SathishKumar89/my-python-coder")

## Prompt Format

This model was trained with the following instruction format. Using the same format at inference time will give the best results:

```
### Instruction:
<your task description>

### Response:
<model's answer>
```

## Usage

```python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel

# Load base model and LoRA adapter
base_model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen2.5-Coder-1.5B-Instruct",
    dtype=torch.float16,
    device_map="auto",
)
model = PeftModel.from_pretrained(base_model, "SathishKumar89/my-python-coder")
tokenizer = AutoTokenizer.from_pretrained("SathishKumar89/my-python-coder")

# Prepare a prompt
prompt = """### Instruction:
Write a Python function that checks if a number is prime.

### Response:
"""

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=False)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```

## Example Output

**Prompt:**
```
### Instruction:
Write a Python function that checks if a number is prime.

### Response:
```

**Model output:**
```python
def is_prime(num):
    # Check for 0 and 1
    if num <= 1:
        return False

    # Check for even numbers greater than 2
    elif num == 2:
        return True
    elif num % 2 == 0:
        return False

    # Check for odd numbers greater than 3
    else:
        for i in range(3, int(num**0.5) + 1, 2):
            if num % i == 0:
                return False
        return True
```

## Limitations

- Trained on a **small subset** (1,500 of 18,612 examples) for only 200 steps β€” this is a proof-of-concept, not a production model.
- May not generalize well to complex Python tasks (large refactors, multi-file projects, advanced libraries).
- Inherits any biases or limitations present in the base model and training dataset.
- Not evaluated against standard benchmarks.

## Future Improvements

- Train on the full dataset for multiple epochs
- Increase LoRA rank for greater capacity
- Evaluate on HumanEval or MBPP benchmarks

## Acknowledgements

- Base model: [Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) by the Qwen team
- Dataset: [iamtarun/python_code_instructions_18k_alpaca](https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca)
- Training framework: Hugging Face `transformers`, `peft`, `trl`
```