File size: 3,239 Bytes
5ecc74e
 
1979f80
 
 
 
 
 
 
 
 
 
 
5ecc74e
1979f80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: apache-2.0
base_model: microsoft/Phi-3-mini-4k-instruct
library_name: peft
tags:
- phi-3
- lora
- qlora
- peft
- transformers
- instruction-tuning
- fine-tuning
- pytorch
---

# Phi-3 Mini 4K Instruct - LoRA Fine-Tuned Model

This repository contains a **QLoRA (4-bit LoRA)** adapter fine-tuned from **Microsoft Phi-3 Mini 4K Instruct** using the Hugging Face Transformers, PEFT, and TRL libraries.

> **Note:** This model was created as a learning project to understand the complete LLM fine-tuning workflow.

---

## Base Model

- **Base Model:** `microsoft/Phi-3-mini-4k-instruct`
- **Fine-Tuning Method:** QLoRA (4-bit Quantization + LoRA)
- **Framework:** Hugging Face Transformers
- **PEFT Library:** PEFT
- **Trainer:** TRL SFTTrainer

---

## Model Details

| Parameter | Value |
|-----------|-------|
| Base Model | Phi-3 Mini 4K Instruct |
| Fine-Tuning Method | QLoRA |
| LoRA Rank (r) | 8 |
| LoRA Alpha | 16 |
| LoRA Dropout | 0.05 |
| Task | Causal Language Modeling |
| Quantization | 4-bit NF4 |
| Compute Type | FP16 |

---

## Training Configuration

- Epochs: **5**
- Batch Size: **1**
- Learning Rate: **2e-4**
- Max Sequence Length: **256**
- Optimizer: AdamW (default TRL optimizer)

---

## Training Dataset

The adapter was fine-tuned on a small custom instruction-response dataset containing sample examples such as:

- Who created Python?
- What is AI?
- What is LoRA?
- What is the capital of France?

This dataset was intentionally kept small for educational purposes to demonstrate the end-to-end fine-tuning process.

---

## Usage

Install the required libraries:

```bash
pip install transformers peft bitsandbytes accelerate torch
```

Load the adapter:

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

BASE_MODEL = "microsoft/Phi-3-mini-4k-instruct"

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_use_double_quant=True,
)

tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)

base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    quantization_config=bnb_config,
    device_map="auto",
)

model = PeftModel.from_pretrained(
    base_model,
    "YOUR_USERNAME/YOUR_REPOSITORY_NAME"
)
```

Generate text:

```python
prompt = """### Instruction:
Who created Python?

### Response:
"""

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=100,
)

print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```

---

## Repository Structure

```
adapter_config.json
adapter_model.safetensors
README.md
```

---

## Limitations

- This adapter was trained on a very small dataset.
- It is intended for educational and demonstration purposes.
- It should not be considered a production-ready fine-tuned model.

---

## Acknowledgements

- Microsoft for the Phi-3 Mini 4K Instruct model
- Hugging Face Transformers
- Hugging Face TRL
- PEFT
- BitsAndBytes

---

## License

This repository contains only the LoRA adapter.

Please follow the license terms of the original Phi-3 Mini 4K Instruct model when using the base model.