Update README.md
Browse files
README.md
CHANGED
@@ -1,202 +1,235 @@
|
|
1 |
---
|
2 |
-
|
|
|
|
|
3 |
library_name: peft
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
## Model
|
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 |
## Training Details
|
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 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
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.15.1
|
|
|
1 |
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- en
|
5 |
library_name: peft
|
6 |
+
base_model: TinyLlama/TinyLlama-1.1B-Chat-v1.0
|
7 |
+
tags:
|
8 |
+
- tinyllama
|
9 |
+
- llama
|
10 |
+
- qlora
|
11 |
+
- fine-tuning
|
12 |
+
- motorcycle-repair
|
13 |
+
- question-answering
|
14 |
+
- generated:text-generation-code:pipeline_example
|
15 |
+
pipeline_tag: text-generation
|
16 |
+
datasets:
|
17 |
+
- cahlen/cdg-motorcycle-repair-qa-data-85x10
|
18 |
+
model_index:
|
19 |
+
- name: cahlen/tinyllama-motorcycle-repair-qa-adapter
|
20 |
+
results: []
|
21 |
---
|
22 |
|
23 |
+
# LoRA Adapter for TinyLlama-1.1B-Chat specialized on Motorcycle Repair QA
|
24 |
+
|
25 |
+
This repository contains LoRA adapter weights fine-tuned from the base model `TinyLlama/TinyLlama-1.1B-Chat-v1.0`. The goal was to enhance the model's knowledge and question-answering capabilities specifically within the domain of motorcycle repair and maintenance, while leveraging the efficiency of the compact TinyLlama architecture.
|
26 |
+
|
27 |
+
This adapter was trained using QLoRA for memory efficiency.
|
28 |
+
|
29 |
+
## Model Description
|
30 |
+
|
31 |
+
* **Base Model:** [TinyLlama/TinyLlama-1.1B-Chat-v1.0](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0)
|
32 |
+
* **Adapter Task:** Question Answering / Instruction Following on Motorcycle Repair topics.
|
33 |
+
* **Fine-tuning Method:** QLoRA (4-bit quantization) via `trl`'s `SFTTrainer`.
|
34 |
+
* **Dataset:** [cahlen/cdg-motorcycle-repair-qa-data-85x10](https://huggingface.co/datasets/cahlen/cdg-motorcycle-repair-qa-data-85x10) (880 synthetically generated QA pairs).
|
35 |
+
|
36 |
+
## Key Features
|
37 |
+
|
38 |
+
* **Domain Specialization:** Improved performance on questions related to motorcycle repair compared to the base model.
|
39 |
+
* **Efficiency:** Builds upon the small and efficient TinyLlama (1.1B parameters). The adapter itself is only ~580MB.
|
40 |
+
* **QLoRA Trained:** Enables loading the base model in 4-bit precision for reduced memory footprint during inference.
|
41 |
+
|
42 |
+
## How to Use
|
43 |
+
|
44 |
+
You need to load the base model (`TinyLlama/TinyLlama-1.1B-Chat-v1.0`) and then apply this LoRA adapter on top. Ensure you have `transformers`, `peft`, `accelerate`, and `bitsandbytes` installed.
|
45 |
+
|
46 |
+
```python
|
47 |
+
import torch
|
48 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
|
49 |
+
from peft import PeftModel
|
50 |
+
import os
|
51 |
+
|
52 |
+
# --- Configuration ---
|
53 |
+
base_model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
54 |
+
adapter_id = "cahlen/tinyllama-motorcycle-repair-qa-adapter" # This is the adapter you are using
|
55 |
+
device_map = "auto"
|
56 |
+
|
57 |
+
# --- Load Tokenizer ---
|
58 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)
|
59 |
+
if tokenizer.pad_token is None:
|
60 |
+
tokenizer.pad_token = tokenizer.eos_token
|
61 |
+
tokenizer.padding_side = "left"
|
62 |
+
|
63 |
+
# --- Configure Quantization ---
|
64 |
+
use_4bit = True # Set to False if not using 4-bit
|
65 |
+
compute_dtype = getattr(torch, "float16") # Default compute dtype
|
66 |
+
quantization_config = None
|
67 |
+
|
68 |
+
if use_4bit and torch.cuda.is_available():
|
69 |
+
print("Using 4-bit quantization")
|
70 |
+
bnb_config = BitsAndBytesConfig(
|
71 |
+
load_in_4bit=True,
|
72 |
+
bnb_4bit_quant_type="nf4",
|
73 |
+
bnb_4bit_compute_dtype=compute_dtype,
|
74 |
+
bnb_4bit_use_double_quant=False,
|
75 |
+
)
|
76 |
+
quantization_config = bnb_config
|
77 |
+
else:
|
78 |
+
print("Not using 4-bit quantization or CUDA not available")
|
79 |
+
compute_dtype = torch.float32 # Use default float32 on CPU
|
80 |
+
|
81 |
+
# --- Load Base Model ---
|
82 |
+
print(f"Loading base model: {base_model_id}")
|
83 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
84 |
+
base_model_id,
|
85 |
+
quantization_config=quantization_config,
|
86 |
+
device_map=device_map,
|
87 |
+
trust_remote_code=True,
|
88 |
+
torch_dtype=compute_dtype # Set appropriate dtype
|
89 |
+
)
|
90 |
+
base_model.config.use_cache = True
|
91 |
+
|
92 |
+
# --- Load LoRA Adapter ---
|
93 |
+
print(f"Loading LoRA adapter: {adapter_id}")
|
94 |
+
model = PeftModel.from_pretrained(base_model, adapter_id)
|
95 |
+
model.eval()
|
96 |
+
print("Adapter loaded successfully.")
|
97 |
+
|
98 |
+
# --- Prepare Prompt ---
|
99 |
+
# Example prompt
|
100 |
+
topic = "Brake System"
|
101 |
+
question = "What are the signs of worn brake pads?"
|
102 |
+
system_prompt = "You are a helpful assistant knowledgeable about motorcycle repair."
|
103 |
+
|
104 |
+
user_query = f"Topic: {topic}\nQuestion: {question}"
|
105 |
+
messages = [
|
106 |
+
{"role": "system", "content": system_prompt},
|
107 |
+
{"role": "user", "content": user_query},
|
108 |
+
]
|
109 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
110 |
+
messages,
|
111 |
+
tokenize=False,
|
112 |
+
add_generation_prompt=True
|
113 |
+
)
|
114 |
+
print(f"--- Prompt ---\n{formatted_prompt}")
|
115 |
+
|
116 |
+
# --- Generate Response ---
|
117 |
+
print("Generating...")
|
118 |
+
pipe = pipeline(
|
119 |
+
task="text-generation",
|
120 |
+
model=model,
|
121 |
+
tokenizer=tokenizer,
|
122 |
+
max_new_tokens=100,
|
123 |
+
do_sample=True,
|
124 |
+
temperature=0.7,
|
125 |
+
top_p=0.9,
|
126 |
+
pad_token_id=tokenizer.eos_token_id,
|
127 |
+
eos_token_id=tokenizer.eos_token_id
|
128 |
+
)
|
129 |
+
result = pipe(formatted_prompt)
|
130 |
+
|
131 |
+
# --- Print Response ---
|
132 |
+
print("\n--- Output ---")
|
133 |
+
print(result[0]['generated_text'])
|
134 |
+
|
135 |
+
# Extract only the assistant's response
|
136 |
+
assistant_response = result[0]['generated_text'][len(formatted_prompt):].strip()
|
137 |
+
print("\n--- Assistant Only ---")
|
138 |
+
print(assistant_response)
|
139 |
+
|
140 |
+
```
|
141 |
|
142 |
## Training Details
|
143 |
|
144 |
+
* **Base Model:** `TinyLlama/TinyLlama-1.1B-Chat-v1.0`
|
145 |
+
* **Dataset:** `cahlen/cdg-motorcycle-repair-qa-data-85x10` (880 examples)
|
146 |
+
* **Training Method:** QLoRA using `trl.SFTTrainer`.
|
147 |
+
* **QLoRA Config:** 4-bit NF4 quantization, `float16` compute dtype.
|
148 |
+
* **LoRA Config:** `r=64`, `lora_alpha=16`, `lora_dropout=0.1`, target modules: `["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]`.
|
149 |
+
* **Training Arguments:**
|
150 |
+
* Epochs: 1
|
151 |
+
* Batch Size (per device): 4
|
152 |
+
* Gradient Accumulation: 2 (Effective Batch Size: 8)
|
153 |
+
* Optimizer: PagedAdamW (32-bit)
|
154 |
+
* Learning Rate: 2e-4
|
155 |
+
* LR Scheduler: Cosine
|
156 |
+
* Sequence Length: 512
|
157 |
+
* Precision: Mixed (FP16)
|
158 |
+
|
159 |
+
## Example Usage & Responses
|
160 |
+
|
161 |
+
Here are some examples of prompts and the responses generated by this fine-tuned adapter:
|
162 |
+
|
163 |
+
**Example 1: Tire Pressure**
|
164 |
+
```
|
165 |
+
<|system|>
|
166 |
+
You are a helpful assistant knowledgeable about motorcycle repair.</s>
|
167 |
+
<|user|>
|
168 |
+
Topic: Tire Maintenance
|
169 |
+
Question: What is the first step in checking tire pressure?</s>
|
170 |
+
<|assistant|>
|
171 |
+
The first step is to check the tire pressure, then check the tire pressure gauge, and finally check the tire tread depth.
|
172 |
+
```
|
173 |
+
|
174 |
+
**Example 2: Oil Change Frequency**
|
175 |
+
```
|
176 |
+
<|system|>
|
177 |
+
You are a helpful assistant knowledgeable about motorcycle repair.</s>
|
178 |
+
<|user|>
|
179 |
+
Topic: Oil Maintenance
|
180 |
+
Question: How often should I change my motorcycle oil?</s>
|
181 |
+
<|assistant|>
|
182 |
+
It is recommended to change your motorcycle oil every 5,000 to 10,000 miles, or as recommended by the manufacturer.
|
183 |
+
```
|
184 |
+
|
185 |
+
**Example 3: Worn Brake Pads**
|
186 |
+
```
|
187 |
+
<|system|>
|
188 |
+
You are a helpful assistant knowledgeable about motorcycle repair.</s>
|
189 |
+
<|user|>
|
190 |
+
Topic: Brake System
|
191 |
+
Question: What are the signs of worn brake pads?</s>
|
192 |
+
<|assistant|>
|
193 |
+
Worn brake pads can be felt in the brake pedal, resulting in a rough or jerky braking action, or a noticeable decrease in braking performance.
|
194 |
+
```
|
195 |
+
|
196 |
+
**Example 4: General Knowledge Retention**
|
197 |
+
```
|
198 |
+
<|system|>
|
199 |
+
You are a helpful assistant knowledgeable about motorcycle repair.</s>
|
200 |
+
<|user|>
|
201 |
+
Topic: Geography
|
202 |
+
Question: What is the capital of France?</s>
|
203 |
+
<|assistant|>
|
204 |
+
The capital of France is Paris.
|
205 |
+
```
|
206 |
+
|
207 |
+
## Limitations and Bias
|
208 |
+
|
209 |
+
* This model inherits limitations and potential biases from the base `TinyLlama` model and the synthetically generated dataset.
|
210 |
+
* The motorcycle repair knowledge, while enhanced, is based on AI-generated data and has **not** been verified by human experts. **Do not rely solely on this model for critical repair decisions.** Always consult official service manuals and qualified mechanics.
|
211 |
+
* Performance on topics outside of motorcycle repair may be degraded compared to the base model.
|
212 |
+
|
213 |
+
## Citation
|
214 |
+
|
215 |
+
If you use this adapter, please cite the base model and consider citing this repository:
|
216 |
+
|
217 |
+
```bibtex
|
218 |
+
@misc{cahlen_tinyllama_motorcycle_repair_qa_adapter,
|
219 |
+
author = {Cahlen},
|
220 |
+
title = {LoRA Adapter for TinyLlama-1.1B-Chat specialized on Motorcycle Repair QA},
|
221 |
+
year = {2024},
|
222 |
+
publisher = {Hugging Face},
|
223 |
+
journal = {Hugging Face Hub},
|
224 |
+
howpublished = {\url{https://huggingface.co/cahlen/tinyllama-motorcycle-repair-qa-adapter}}
|
225 |
+
}
|
226 |
+
|
227 |
+
@misc{zhang2024tinyllama,
|
228 |
+
title={TinyLlama: An Open-Source Small Language Model},
|
229 |
+
author={Peiyuan Zhang and Guangxuan Xiao and Ning Tuan Anh Tran and Xin (Notus) Li and Hao Tan and Yaowen Zhang and Philipp F. Hoefer and Hong Mo Kuan and Benn Tan and Ponnuchamy Muthu Ilakkuvan and Associated Professor Nan Yang and Dr. Si-Qing Qin and Dr. Bin Lin and Dr. Zhengin Li and Dr. Ramesha Karunasena and Dr. Ajay Kumar Jha and Mohamed Ahmed Hassan and ARIES AI},
|
230 |
+
year={2024},
|
231 |
+
eprint={2401.02385},
|
232 |
+
archivePrefix={arXiv},
|
233 |
+
primaryClass={cs.CL}
|
234 |
+
}
|
235 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|