Instructions to use Shivenys/phi3-mini-lora-tutorial with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Shivenys/phi3-mini-lora-tutorial with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct") model = PeftModel.from_pretrained(base_model, "Shivenys/phi3-mini-lora-tutorial") - Transformers
How to use Shivenys/phi3-mini-lora-tutorial with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Shivenys/phi3-mini-lora-tutorial", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Initial upload of Phi-3 LoRA adapter
Browse files- README.md +160 -0
- adapter_config.json +43 -0
- adapter_model.safetensors +3 -0
- chat_template.jinja +8 -0
- tokenizer.json +0 -0
- tokenizer_config.json +16 -0
- training_args.bin +3 -0
README.md
CHANGED
|
@@ -1,3 +1,163 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
+
base_model: microsoft/Phi-3-mini-4k-instruct
|
| 4 |
+
library_name: peft
|
| 5 |
+
tags:
|
| 6 |
+
- phi-3
|
| 7 |
+
- lora
|
| 8 |
+
- qlora
|
| 9 |
+
- peft
|
| 10 |
+
- transformers
|
| 11 |
+
- instruction-tuning
|
| 12 |
+
- fine-tuning
|
| 13 |
+
- pytorch
|
| 14 |
---
|
| 15 |
+
|
| 16 |
+
# Phi-3 Mini 4K Instruct - LoRA Fine-Tuned Model
|
| 17 |
+
|
| 18 |
+
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.
|
| 19 |
+
|
| 20 |
+
> **Note:** This model was created as a learning project to understand the complete LLM fine-tuning workflow.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## Base Model
|
| 25 |
+
|
| 26 |
+
- **Base Model:** `microsoft/Phi-3-mini-4k-instruct`
|
| 27 |
+
- **Fine-Tuning Method:** QLoRA (4-bit Quantization + LoRA)
|
| 28 |
+
- **Framework:** Hugging Face Transformers
|
| 29 |
+
- **PEFT Library:** PEFT
|
| 30 |
+
- **Trainer:** TRL SFTTrainer
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## Model Details
|
| 35 |
+
|
| 36 |
+
| Parameter | Value |
|
| 37 |
+
|-----------|-------|
|
| 38 |
+
| Base Model | Phi-3 Mini 4K Instruct |
|
| 39 |
+
| Fine-Tuning Method | QLoRA |
|
| 40 |
+
| LoRA Rank (r) | 8 |
|
| 41 |
+
| LoRA Alpha | 16 |
|
| 42 |
+
| LoRA Dropout | 0.05 |
|
| 43 |
+
| Task | Causal Language Modeling |
|
| 44 |
+
| Quantization | 4-bit NF4 |
|
| 45 |
+
| Compute Type | FP16 |
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
## Training Configuration
|
| 50 |
+
|
| 51 |
+
- Epochs: **5**
|
| 52 |
+
- Batch Size: **1**
|
| 53 |
+
- Learning Rate: **2e-4**
|
| 54 |
+
- Max Sequence Length: **256**
|
| 55 |
+
- Optimizer: AdamW (default TRL optimizer)
|
| 56 |
+
|
| 57 |
+
---
|
| 58 |
+
|
| 59 |
+
## Training Dataset
|
| 60 |
+
|
| 61 |
+
The adapter was fine-tuned on a small custom instruction-response dataset containing sample examples such as:
|
| 62 |
+
|
| 63 |
+
- Who created Python?
|
| 64 |
+
- What is AI?
|
| 65 |
+
- What is LoRA?
|
| 66 |
+
- What is the capital of France?
|
| 67 |
+
|
| 68 |
+
This dataset was intentionally kept small for educational purposes to demonstrate the end-to-end fine-tuning process.
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
## Usage
|
| 73 |
+
|
| 74 |
+
Install the required libraries:
|
| 75 |
+
|
| 76 |
+
```bash
|
| 77 |
+
pip install transformers peft bitsandbytes accelerate torch
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
Load the adapter:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
import torch
|
| 84 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 85 |
+
from peft import PeftModel
|
| 86 |
+
|
| 87 |
+
BASE_MODEL = "microsoft/Phi-3-mini-4k-instruct"
|
| 88 |
+
|
| 89 |
+
bnb_config = BitsAndBytesConfig(
|
| 90 |
+
load_in_4bit=True,
|
| 91 |
+
bnb_4bit_quant_type="nf4",
|
| 92 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 93 |
+
bnb_4bit_use_double_quant=True,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 97 |
+
|
| 98 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 99 |
+
BASE_MODEL,
|
| 100 |
+
quantization_config=bnb_config,
|
| 101 |
+
device_map="auto",
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
model = PeftModel.from_pretrained(
|
| 105 |
+
base_model,
|
| 106 |
+
"YOUR_USERNAME/YOUR_REPOSITORY_NAME"
|
| 107 |
+
)
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
Generate text:
|
| 111 |
+
|
| 112 |
+
```python
|
| 113 |
+
prompt = """### Instruction:
|
| 114 |
+
Who created Python?
|
| 115 |
+
|
| 116 |
+
### Response:
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 120 |
+
|
| 121 |
+
outputs = model.generate(
|
| 122 |
+
**inputs,
|
| 123 |
+
max_new_tokens=100,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
## Repository Structure
|
| 132 |
+
|
| 133 |
+
```
|
| 134 |
+
adapter_config.json
|
| 135 |
+
adapter_model.safetensors
|
| 136 |
+
README.md
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
## Limitations
|
| 142 |
+
|
| 143 |
+
- This adapter was trained on a very small dataset.
|
| 144 |
+
- It is intended for educational and demonstration purposes.
|
| 145 |
+
- It should not be considered a production-ready fine-tuned model.
|
| 146 |
+
|
| 147 |
+
---
|
| 148 |
+
|
| 149 |
+
## Acknowledgements
|
| 150 |
+
|
| 151 |
+
- Microsoft for the Phi-3 Mini 4K Instruct model
|
| 152 |
+
- Hugging Face Transformers
|
| 153 |
+
- Hugging Face TRL
|
| 154 |
+
- PEFT
|
| 155 |
+
- BitsAndBytes
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
## License
|
| 160 |
+
|
| 161 |
+
This repository contains only the LoRA adapter.
|
| 162 |
+
|
| 163 |
+
Please follow the license terms of the original Phi-3 Mini 4K Instruct model when using the base model.
|
adapter_config.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"alora_invocation_tokens": null,
|
| 3 |
+
"alpha_pattern": {},
|
| 4 |
+
"arrow_config": null,
|
| 5 |
+
"auto_mapping": null,
|
| 6 |
+
"base_model_name_or_path": "microsoft/Phi-3-mini-4k-instruct",
|
| 7 |
+
"bias": "none",
|
| 8 |
+
"corda_config": null,
|
| 9 |
+
"ensure_weight_tying": false,
|
| 10 |
+
"eva_config": null,
|
| 11 |
+
"exclude_modules": null,
|
| 12 |
+
"fan_in_fan_out": false,
|
| 13 |
+
"inference_mode": true,
|
| 14 |
+
"init_lora_weights": true,
|
| 15 |
+
"layer_replication": null,
|
| 16 |
+
"layers_pattern": null,
|
| 17 |
+
"layers_to_transform": null,
|
| 18 |
+
"loftq_config": {},
|
| 19 |
+
"lora_alpha": 16,
|
| 20 |
+
"lora_bias": false,
|
| 21 |
+
"lora_dropout": 0.05,
|
| 22 |
+
"lora_ga_config": null,
|
| 23 |
+
"megatron_config": null,
|
| 24 |
+
"megatron_core": "megatron.core",
|
| 25 |
+
"modules_to_save": null,
|
| 26 |
+
"peft_type": "LORA",
|
| 27 |
+
"peft_version": "0.19.1",
|
| 28 |
+
"qalora_group_size": 16,
|
| 29 |
+
"r": 8,
|
| 30 |
+
"rank_pattern": {},
|
| 31 |
+
"revision": null,
|
| 32 |
+
"target_modules": [
|
| 33 |
+
"qkv_proj",
|
| 34 |
+
"o_proj"
|
| 35 |
+
],
|
| 36 |
+
"target_parameters": null,
|
| 37 |
+
"task_type": "CAUSAL_LM",
|
| 38 |
+
"trainable_token_indices": null,
|
| 39 |
+
"use_bdlora": null,
|
| 40 |
+
"use_dora": false,
|
| 41 |
+
"use_qalora": false,
|
| 42 |
+
"use_rslora": false
|
| 43 |
+
}
|
adapter_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:46410ac5be1667fdddfcc5aa28e64d5bc182b67bd10bc0497e7e342e00a9ffac
|
| 3 |
+
size 9454304
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% for message in messages %}{% if message['role'] == 'system' %}{{'<|system|>
|
| 2 |
+
' + message['content'] + '<|end|>
|
| 3 |
+
'}}{% elif message['role'] == 'user' %}{{'<|user|>
|
| 4 |
+
' + message['content'] + '<|end|>
|
| 5 |
+
'}}{% elif message['role'] == 'assistant' %}{{'<|assistant|>
|
| 6 |
+
' + message['content'] + '<|end|>
|
| 7 |
+
'}}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>
|
| 8 |
+
' }}{% else %}{{ eos_token }}{% endif %}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<s>",
|
| 4 |
+
"clean_up_tokenization_spaces": false,
|
| 5 |
+
"eos_token": "<|endoftext|>",
|
| 6 |
+
"is_local": false,
|
| 7 |
+
"legacy": false,
|
| 8 |
+
"local_files_only": false,
|
| 9 |
+
"model_max_length": 4096,
|
| 10 |
+
"pad_token": "<|endoftext|>",
|
| 11 |
+
"padding_side": "left",
|
| 12 |
+
"sp_model_kwargs": {},
|
| 13 |
+
"tokenizer_class": "TokenizersBackend",
|
| 14 |
+
"unk_token": "<unk>",
|
| 15 |
+
"use_default_system_prompt": false
|
| 16 |
+
}
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9b83fc75ff391b29c0d29e57dd7512afa27fc43cb5af79c94338622050d9202f
|
| 3 |
+
size 5649
|