Ali-Forootani
commited on
Commit
•
22684ef
1
Parent(s):
bc14afd
Update README.md
Browse files
README.md
CHANGED
@@ -3,6 +3,149 @@ library_name: transformers
|
|
3 |
tags: []
|
4 |
---
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Model Card for Model ID
|
7 |
|
8 |
<!-- Provide a quick summary of what the model is/does. -->
|
|
|
3 |
tags: []
|
4 |
---
|
5 |
|
6 |
+
|
7 |
+
|
8 |
+
---
|
9 |
+
|
10 |
+
# Fine-Tuning LLaMA-2-7b with QLoRA on Custom Dataset
|
11 |
+
|
12 |
+
This repository provides a setup and script for fine-tuning the LLaMA-2-7b model using QLoRA (Quantized Low-Rank Adaptation) with custom datasets. The script is designed for efficiency and flexibility in training large language models (LLMs) by leveraging advanced techniques such as 4-bit quantization and LoRA.
|
13 |
+
|
14 |
+
## Overview
|
15 |
+
|
16 |
+
The script fine-tunes a pre-trained LLaMA-2-7b model using a custom dataset, applying QLoRA techniques to optimize performance. It utilizes the `transformers`, `datasets`, `peft`, and `trl` libraries for model management, data processing, and training. The setup includes support for mixed precision training, gradient checkpointing, and advanced quantization techniques to enhance the efficiency of the fine-tuning process.
|
17 |
+
|
18 |
+
## Components
|
19 |
+
|
20 |
+
### 1. Dependencies
|
21 |
+
|
22 |
+
Ensure the following libraries are installed:
|
23 |
+
- `torch`
|
24 |
+
- `datasets`
|
25 |
+
- `transformers`
|
26 |
+
- `peft`
|
27 |
+
- `trl`
|
28 |
+
|
29 |
+
Install them using pip if they are not already available:
|
30 |
+
```bash
|
31 |
+
pip install torch datasets transformers peft trl
|
32 |
+
```
|
33 |
+
|
34 |
+
### 2. Model and Dataset
|
35 |
+
|
36 |
+
- **Model**: The base model used is `LLaMA-2-7b`. The script loads this model from a specified local directory.
|
37 |
+
- **Dataset**: The training data is loaded from a specified directory. The dataset should be formatted in a way that the `"text"` field contains the training examples.
|
38 |
+
|
39 |
+
### 3. QLoRA Configuration
|
40 |
+
|
41 |
+
QLoRA parameters are used to configure the quantization and adaptation process:
|
42 |
+
- **LoRA Attention Dimension (`lora_r`)**: 64
|
43 |
+
- **LoRA Alpha Parameter (`lora_alpha`)**: 16
|
44 |
+
- **LoRA Dropout Probability (`lora_dropout`)**: 0.1
|
45 |
+
|
46 |
+
### 4. BitsAndBytes Configuration
|
47 |
+
|
48 |
+
Quantization settings for the model:
|
49 |
+
- **Use 4-bit Precision**: True
|
50 |
+
- **Compute Data Type**: `float16`
|
51 |
+
- **Quantization Type**: `nf4`
|
52 |
+
- **Nested Quantization**: False
|
53 |
+
|
54 |
+
### 5. Training Configuration
|
55 |
+
|
56 |
+
Training parameters are defined as follows:
|
57 |
+
- **Output Directory**: `./results`
|
58 |
+
- **Number of Epochs**: 300
|
59 |
+
- **Batch Size**: 4
|
60 |
+
- **Gradient Accumulation Steps**: 1
|
61 |
+
- **Learning Rate**: 2e-4
|
62 |
+
- **Weight Decay**: 0.001
|
63 |
+
- **Optimizer**: `paged_adamw_32bit`
|
64 |
+
- **Learning Rate Scheduler**: `cosine`
|
65 |
+
- **Gradient Clipping**: 0.3
|
66 |
+
- **Warmup Ratio**: 0.03
|
67 |
+
- **Logging Steps**: 25
|
68 |
+
- **Save Steps**: 0
|
69 |
+
|
70 |
+
### 6. Training and Evaluation
|
71 |
+
|
72 |
+
The script includes preprocessing of the dataset, model initialization with QLoRA, and training using `SFTTrainer` from the `trl` library. It supports mixed precision training and gradient checkpointing to enhance training efficiency.
|
73 |
+
|
74 |
+
### 7. Usage Instructions
|
75 |
+
|
76 |
+
1. **Update File Paths**: Adjust `model_name`, `dataset_name`, and `new_model` paths according to your environment.
|
77 |
+
2. **Run the Script**: Execute the script in your Python environment to start the fine-tuning process.
|
78 |
+
|
79 |
+
```bash
|
80 |
+
python fine_tune_llama.py
|
81 |
+
```
|
82 |
+
|
83 |
+
3. **Monitor Training**: Use TensorBoard or similar tools to monitor the training progress.
|
84 |
+
|
85 |
+
### 8. Model Saving
|
86 |
+
|
87 |
+
After training, the model is saved to the specified directory (`new_model`). This trained model can be loaded for further evaluation or deployment.
|
88 |
+
|
89 |
+
## Example Configuration
|
90 |
+
|
91 |
+
Here’s an example configuration used for fine-tuning:
|
92 |
+
|
93 |
+
```python
|
94 |
+
model_name = "/data/bio-eng-llm/llm_repo/NousResearch/Llama-2-7b-chat-hf"
|
95 |
+
dataset_name = "/data/bio-eng-llm/llm_repo/mlabonne/guanaco-llama2-1k"
|
96 |
+
new_model = "/data/bio-eng-llm/llm_repo/mlabonne/llama-2-7b-miniguanaco"
|
97 |
+
|
98 |
+
lora_r = 64
|
99 |
+
lora_alpha = 16
|
100 |
+
lora_dropout = 0.1
|
101 |
+
|
102 |
+
use_4bit = True
|
103 |
+
bnb_4bit_compute_dtype = "float16"
|
104 |
+
bnb_4bit_quant_type = "nf4"
|
105 |
+
use_nested_quant = False
|
106 |
+
|
107 |
+
output_dir = "./results"
|
108 |
+
num_train_epochs = 300
|
109 |
+
fp16 = False
|
110 |
+
bf16 = False
|
111 |
+
per_device_train_batch_size = 4
|
112 |
+
gradient_accumulation_steps = 1
|
113 |
+
gradient_checkpointing = True
|
114 |
+
max_grad_norm = 0.3
|
115 |
+
learning_rate = 2e-4
|
116 |
+
weight_decay = 0.001
|
117 |
+
optim = "paged_adamw_32bit"
|
118 |
+
lr_scheduler_type = "cosine"
|
119 |
+
max_steps = -1
|
120 |
+
warmup_ratio = 0.03
|
121 |
+
group_by_length = True
|
122 |
+
save_steps = 0
|
123 |
+
logging_steps = 25
|
124 |
+
```
|
125 |
+
|
126 |
+
## License
|
127 |
+
|
128 |
+
This repository is licensed under the [MIT License](LICENSE).
|
129 |
+
|
130 |
+
## Contact
|
131 |
+
|
132 |
+
For questions or issues, please contact [author](mailto:[email protected]).
|
133 |
+
|
134 |
+
---
|
135 |
+
|
136 |
+
This README provides a comprehensive guide to understanding and utilizing the script for fine-tuning the LLaMA-2-7b model using advanced techniques. Adjust file paths and parameters as needed based on your specific requirements.
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
# Model Card for Model ID
|
150 |
|
151 |
<!-- Provide a quick summary of what the model is/does. -->
|