File size: 3,810 Bytes
7e2433d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
library_name: peft
license: apache-2.0
base_model: Qwen/Qwen2.5-7B
tags:
  - code-generation
  - lora
  - fine-tuned
  - qwen2
  - python
  - transformers
  - peft
  - trl
datasets:
  - TokenBender/code_instructions_122k_alpaca_style
language:
  - en
pipeline_tag: text-generation
---

# Qwen2.5-7B Code LoRA

LoRA adapter fine-tuned on [Qwen/Qwen2.5-7B](https://huggingface.co/Qwen/Qwen2.5-7B) for Python code generation.

## Training Summary

| Detail | Value |
|---|---|
| Base Model | Qwen/Qwen2.5-7B (7.6B params) |
| Method | LoRA (rank 128, alpha 64) |
| Trainable Params | 323M / 7.9B (4.07%) |
| Dataset | [122K code instructions (Alpaca format)](https://huggingface.co/datasets/TokenBender/code_instructions_122k_alpaca_style) |
| Train / Eval Split | 115,861 / 6,098 |
| Epochs | 3 |
| Effective Batch Size | 32 (16 x 2 gradient accumulation) |
| Learning Rate | 2e-4 (cosine schedule, 3% warmup) |
| Max Sequence Length | 2048 (with packing) |
| Precision | bf16 |
| Training Time | ~4 hrs 26 min |
| GPU | NVIDIA RTX PRO 6000 Blackwell (96 GB) |
| Best Eval Loss | **0.7324** (step 600) |
| Eval Token Accuracy | **82.7%** |

## LoRA Configuration

- **Rank**: 128
- **Alpha**: 64
- **Dropout**: 0.05
- **Target Modules**: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
- **Adapter Size**: ~1.2 GB

## Usage

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

base = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen2.5-7B", dtype=torch.bfloat16, device_map="auto"
)
model = PeftModel.from_pretrained(base, "koushikkb12/Qwen2.5-7B-Code-LoRA")
model = model.merge_and_unload()

tokenizer = AutoTokenizer.from_pretrained("koushikkb12/Qwen2.5-7B-Code-LoRA")

messages = [
    {"role": "system", "content": "You are a highly skilled Python programmer. Write clean, efficient, and correct code."},
    {"role": "user", "content": "Write a function to flatten a nested list."},
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)

with torch.no_grad():
    out = model.generate(**inputs, max_new_tokens=256, temperature=0.7, top_p=0.9)

print(tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
```

## Example Outputs

**Prompt**: *Write a Python function that checks if a given string is a palindrome.*
```python
def is_palindrome(s):
    return s == s[::-1]
```

**Prompt**: *Implement a binary search algorithm in Python.*
```python
def binary_search(arr, target):
    low, high = 0, len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1
```

**Prompt**: *Write a Python class for a stack with push, pop, peek, and is_empty.*
```python
class Stack:
    def __init__(self):
        self.items = []
    def push(self, item):
        self.items.append(item)
    def pop(self):
        if not self.is_empty():
            return self.items.pop()
    def peek(self):
        if not self.is_empty():
            return self.items[-1]
    def is_empty(self):
        return len(self.items) == 0
```

## Training Curves

| Step | Train Loss | Eval Loss | Eval Token Acc |
|---|---|---|---|
| 100 | 0.758 | 0.741 | 82.6% |
| 200 | 0.749 | 0.737 | 82.6% |
| 300 | 0.739 | 0.734 | 82.7% |
| 400 | 0.731 | 0.734 | 82.7% |
| 500 | 0.720 | 0.733 | 82.7% |
| 600 | 0.718 | **0.732** | **82.7%** |
| 700 | 0.725 | 0.734 | 82.7% |
| 800 | 0.710 | 0.734 | 82.7% |
| 900 | 0.739 | 0.734 | 82.7% |

## License

This adapter inherits the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0) from Qwen2.5-7B.