init
Browse files
README.md
CHANGED
@@ -1,202 +1,81 @@
|
|
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 |
-
[More Information Needed]
|
83 |
-
|
84 |
-
### Training Procedure
|
85 |
-
|
86 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
87 |
-
|
88 |
-
#### Preprocessing [optional]
|
89 |
-
|
90 |
-
[More Information Needed]
|
91 |
-
|
92 |
-
|
93 |
-
#### Training Hyperparameters
|
94 |
-
|
95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
-
|
97 |
-
#### Speeds, Sizes, Times [optional]
|
98 |
-
|
99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
-
|
101 |
-
[More Information Needed]
|
102 |
-
|
103 |
-
## Evaluation
|
104 |
-
|
105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
-
|
107 |
-
### Testing Data, Factors & Metrics
|
108 |
-
|
109 |
-
#### Testing Data
|
110 |
-
|
111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
112 |
-
|
113 |
-
[More Information Needed]
|
114 |
-
|
115 |
-
#### Factors
|
116 |
-
|
117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
-
|
119 |
-
[More Information Needed]
|
120 |
-
|
121 |
-
#### Metrics
|
122 |
-
|
123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
-
|
125 |
-
[More Information Needed]
|
126 |
-
|
127 |
-
### Results
|
128 |
-
|
129 |
-
[More Information Needed]
|
130 |
-
|
131 |
-
#### Summary
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
## Model Examination [optional]
|
136 |
-
|
137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
138 |
-
|
139 |
-
[More Information Needed]
|
140 |
-
|
141 |
-
## Environmental Impact
|
142 |
-
|
143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
-
|
145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
-
|
147 |
-
- **Hardware Type:** [More Information Needed]
|
148 |
-
- **Hours used:** [More Information Needed]
|
149 |
-
- **Cloud Provider:** [More Information Needed]
|
150 |
-
- **Compute Region:** [More Information Needed]
|
151 |
-
- **Carbon Emitted:** [More Information Needed]
|
152 |
-
|
153 |
-
## Technical Specifications [optional]
|
154 |
-
|
155 |
-
### Model Architecture and Objective
|
156 |
-
|
157 |
-
[More Information Needed]
|
158 |
-
|
159 |
-
### Compute Infrastructure
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
-
|
163 |
-
#### Hardware
|
164 |
-
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Software
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
## Citation [optional]
|
172 |
-
|
173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
-
|
175 |
-
**BibTeX:**
|
176 |
-
|
177 |
-
[More Information Needed]
|
178 |
-
|
179 |
-
**APA:**
|
180 |
-
|
181 |
-
[More Information Needed]
|
182 |
-
|
183 |
-
## Glossary [optional]
|
184 |
-
|
185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
-
|
187 |
-
[More Information Needed]
|
188 |
-
|
189 |
-
## More Information [optional]
|
190 |
-
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## Model Card Authors [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
-
|
197 |
-
## Model Card Contact
|
198 |
-
|
199 |
-
[More Information Needed]
|
200 |
-
### Framework versions
|
201 |
-
|
202 |
-
- PEFT 0.14.0
|
|
|
1 |
+
### README
|
2 |
+
|
3 |
+
#### Project Overview
|
4 |
+
Yo! You're looking at a sick project where we've finetuned the Qwen 2.5 3B model using LoRA with a dirty language corpus. Yeah, you heard it right, we're taking this language model to a whole new level of sass!
|
5 |
+
|
6 |
+
#### What's LoRA?
|
7 |
+
LoRA, or Low-Rank Adaptation, is like a magic trick for large language models. Instead of finetuning the entire massive model, which is as expensive as buying a spaceship, LoRA only tweaks a small part of it. It's like fixing a small engine in a big plane.
|
8 |
+
|
9 |
+
The core formula of LoRA is:
|
10 |
+
|
11 |
+
$\Delta W = BA$
|
12 |
+
|
13 |
+
Here, $W$ is the original weight matrix of the model. $\Delta W$ is the low-rank update to $W$. $B$ and $A$ are two low-rank matrices. By training these two small matrices, we can achieve a similar effect as finetuning the whole $W$. It's efficient, it's fast, and it's like a cheat code for model finetuning!
|
14 |
+
|
15 |
+
#### Code Explanation
|
16 |
+
Let's break down the provided code:
|
17 |
+
|
18 |
+
1. **Model and Tokenizer Loading**:
|
19 |
+
```python
|
20 |
+
import torch
|
21 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
22 |
+
from peft import PeftModel
|
23 |
+
|
24 |
+
# Check for GPU availability
|
25 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
+
|
27 |
+
# Model name
|
28 |
+
model_name = "Qwen/Qwen2.5-3B-Instruct"
|
29 |
+
|
30 |
+
# Load the tokenizer
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
32 |
+
|
33 |
+
# Load the base model
|
34 |
+
base_model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True).to(device)
|
35 |
+
# Load the LoRA model
|
36 |
+
lora_model = PeftModel.from_pretrained(base_model, "./qwen25_3b_instruct_lora_vulgarity_finetuned")
|
37 |
+
```
|
38 |
+
This part loads the Qwen 2.5 3B model and its tokenizer. Then it applies the LoRA adaptation to the base model using the finetuned LoRA weights.
|
39 |
+
|
40 |
+
2. **Inference Example**:
|
41 |
+
```python
|
42 |
+
input_text = "Hello"
|
43 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(lora_model.device)
|
44 |
+
output = lora_model.generate(input_ids, max_new_tokens=50, do_sample=True, top_p=0.95, temperature=0.35)
|
45 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
46 |
+
print(output_text)
|
47 |
+
```
|
48 |
+
This is a simple inference example. It takes an input text, converts it to input IDs, generates an output using the finetuned model, and then decodes the output to text.
|
49 |
+
|
50 |
+
3. **Gradio Interface**:
|
51 |
+
```python
|
52 |
+
import gradio as gr
|
53 |
+
|
54 |
+
def chatbot(input_text, history):
|
55 |
+
# Chatbot logic here
|
56 |
+
...
|
57 |
+
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=chatbot,
|
60 |
+
inputs=[gr.Textbox(label="输入你的问题"), gr.State()],
|
61 |
+
outputs=[gr.Chatbot(label="聊天历史"), gr.State()],
|
62 |
+
title="Qwen2.5-finetune-骂人专家",
|
63 |
+
description="Qwen2.5-finetune-骂人专家"
|
64 |
+
)
|
65 |
+
|
66 |
+
iface.launch(share=True, inbrowser=False, debug=True)
|
67 |
+
```
|
68 |
+
This creates a Gradio interface for the chatbot. Users can input text, and the chatbot will respond based on the finetuned model.
|
69 |
+
|
70 |
+
#### How to Run
|
71 |
+
1. Make sure you have all the necessary libraries installed. You can install them using `pip`:
|
72 |
+
```bash
|
73 |
+
pip install torch transformers peft gradio
|
74 |
+
```
|
75 |
+
2. Place your finetuned LoRA weights in the `./qwen25_3b_instruct_lora_vulgarity_finetuned` directory.
|
76 |
+
3. Run the Python script. It will start the Gradio server, and you can access the chatbot through the provided link.
|
77 |
+
|
78 |
+
#### Warning
|
79 |
+
This project uses a dirty language corpus for finetuning. Please use it responsibly and don't let it loose in a polite society!
|
80 |
+
|
81 |
+
That's it, folks! You're now ready to unleash the power of this finetuned Qwen 2.5 model. Have fun!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|