BEncoderRT commited on
Commit
a313f18
·
verified ·
1 Parent(s): 68a83a5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +270 -3
README.md CHANGED
@@ -1,3 +1,270 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # TinyLlama Multi-Task LoRA (Sentiment + Translation)
2
+
3
+ This repository contains a **LoRA adapter** trained on top of
4
+ **TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T**
5
+ to support **multiple tasks** via instruction-style prompting.
6
+
7
+ ---
8
+
9
+ ## 🔧 Base Model
10
+
11
+ - **Base model**: `TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T`
12
+ - **Architecture**: Decoder-only (LLaMA-style)
13
+ - **Fine-tuning method**: LoRA (PEFT)
14
+ - **Trainable parameters**: ~4M
15
+ - **Total parameters**: ~1.1B
16
+
17
+ This repository **only contains the LoRA adapter weights**, not the full base model.
18
+
19
+ ---
20
+
21
+ ## 📌 Supported Tasks
22
+
23
+ ### 1️⃣ Sentiment Analysis (IMDB)
24
+
25
+ Binary sentiment classification for movie reviews:
26
+
27
+ - `positive`
28
+ - `negative`
29
+
30
+ ### 2️⃣ Translation (English → French)
31
+
32
+ Neural machine translation from English to French.
33
+
34
+ ---
35
+
36
+ ## 🧠 Training Data
37
+
38
+ All datasets are loaded directly from **Hugging Face Datasets**:
39
+
40
+ | Task | Dataset | Description |
41
+ | ------------------- | ------------------------------- | -------------------------------- |
42
+ | Sentiment Analysis | `imdb` | Movie reviews with binary labels |
43
+ | Translation (EN→FR) | `Helsinki-NLP/opus-100` (en-fr) | Parallel English–French corpus |
44
+
45
+ ---
46
+
47
+ ## 🧩 Training Strategy
48
+
49
+ - **Multi-task instruction tuning**
50
+ - Tasks are distinguished via **explicit prompt headers**
51
+ - All tasks are unified into a **causal language modeling** objective
52
+ - Only LoRA parameters are updated; base model weights remain frozen
53
+
54
+ ---
55
+
56
+ ## 🧾 Prompt Format
57
+
58
+ ### Sentiment Analysis
59
+
60
+ ```text
61
+ ### Task: Sentiment Analysis
62
+ ### Review:
63
+ This movie was absolutely fantastic!
64
+ ### Answer:
65
+ positive
66
+
67
+ ### Task: Translation (English to French)
68
+ ### English:
69
+ I love learning large language models.
70
+ ### French:
71
+ J'aime apprendre les grands modèles de langage.
72
+
73
+
74
+ ```
75
+
76
+ **Load Base Model + LoRA Adapter**
77
+
78
+ ```python
79
+ import torch
80
+ from transformers import AutoModelForCausalLM, AutoTokenizer
81
+ from peft import PeftModel
82
+
83
+ base_model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
84
+ lora_repo = "BEncoderRT/tinyllama-multitask-lora"
85
+
86
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
87
+ tokenizer.pad_token = tokenizer.eos_token
88
+
89
+ base_model = AutoModelForCausalLM.from_pretrained(
90
+ base_model_name,
91
+ device_map="auto"
92
+ ).eval()
93
+
94
+ lora_model = AutoModelForCausalLM.from_pretrained(
95
+ base_model_name,
96
+ device_map="auto"
97
+ )
98
+
99
+ lora_model = PeftModel.from_pretrained(lora_model, lora_repo)
100
+ lora_model.eval()
101
+
102
+ def generate(
103
+ model,
104
+ prompt,
105
+ max_new_tokens=64,
106
+ temperature=0.3
107
+ ):
108
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
109
+
110
+ with torch.no_grad():
111
+ outputs = model.generate(
112
+ **inputs,
113
+ max_new_tokens=max_new_tokens,
114
+ do_sample=True,
115
+ temperature=temperature,
116
+ pad_token_id=tokenizer.eos_token_id
117
+ )
118
+
119
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
120
+ ```
121
+
122
+
123
+
124
+ **Inference Examples**
125
+
126
+ Sentiment Analysis Example
127
+
128
+ ```python
129
+ sentiment_tests = [
130
+ "I absolutely loved this movie. The acting was brilliant.",
131
+ "This film was boring, slow, and a complete waste of time.",
132
+ "The movie was okay, but nothing special.",
133
+ ]
134
+
135
+ def test_sentiment_comparison(texts):
136
+ for text in texts:
137
+ prompt = (
138
+ "### Task: Sentiment Analysis\n"
139
+ "### Review:\n"
140
+ f"{text}\n"
141
+ "### Answer:\n"
142
+ )
143
+
144
+ base_out = generate(base_model, prompt, max_new_tokens=8)
145
+ lora_out = generate(lora_model, prompt, max_new_tokens=8)
146
+
147
+ print("=" * 80)
148
+ print("REVIEW:")
149
+ print(text)
150
+ print("\n[BASE MODEL OUTPUT]")
151
+ print(base_out)
152
+ print("\n[LORA MODEL OUTPUT]")
153
+ print(lora_out)
154
+ ```
155
+
156
+ ```python
157
+ test_sentiment_comparison(sentiment_tests)
158
+ ```
159
+
160
+ ```
161
+ ================================================================================
162
+ REVIEW:
163
+ I absolutely loved this movie. The acting was brilliant.
164
+
165
+ [BASE MODEL OUTPUT]
166
+ ### Task: Sentiment Analysis
167
+ ### Review:
168
+ I absolutely loved this movie. The acting was brilliant.
169
+ ### Answer:
170
+ I loved this movie. It was so
171
+
172
+ [LORA MODEL OUTPUT]
173
+ ### Task: Sentiment Analysis
174
+ ### Review:
175
+ I absolutely loved this movie. The acting was brilliant.
176
+ ### Answer:
177
+ positive
178
+ ================================================================================
179
+ REVIEW:
180
+ This film was boring, slow, and a complete waste of time.
181
+
182
+ [BASE MODEL OUTPUT]
183
+ ### Task: Sentiment Analysis
184
+ ### Review:
185
+ This film was boring, slow, and a complete waste of time.
186
+ ...
187
+ ### Review:
188
+ The movie was okay, but nothing special.
189
+ ### Answer:
190
+ negative
191
+ ```
192
+
193
+
194
+
195
+
196
+
197
+ Translation Example
198
+
199
+ ```python
200
+ translation_tests = [
201
+
202
+ "I love learning large language models.",
203
+
204
+ "This movie was disappointing and boring.",
205
+
206
+ "Artificial intelligence is changing the world."
207
+
208
+ ]
209
+ def test_translation_comparison(texts):
210
+ for text in texts:
211
+ prompt = (
212
+ "### Task: Translation (English to French)\n"
213
+ "### English:\n"
214
+ f"{text}\n"
215
+ "### French:\n"
216
+ )
217
+
218
+ base_out = generate(base_model, prompt, max_new_tokens=64)
219
+ lora_out = generate(lora_model, prompt, max_new_tokens=64)
220
+
221
+ print("=" * 80)
222
+ print("ENGLISH:")
223
+ print(text)
224
+ print("\n[BASE MODEL OUTPUT]")
225
+ print(base_out)
226
+ print("\n[LORA MODEL OUTPUT]")
227
+ print(lora_out)
228
+
229
+ ```
230
+
231
+ ```python
232
+ test_sentiment_comparison(sentiment_tests)
233
+ ```
234
+
235
+ ```
236
+ ================================================================================
237
+ ENGLISH:
238
+ I love learning large language models.
239
+
240
+ [BASE MODEL OUTPUT]
241
+ ### Task: Translation (English to French)
242
+ ### English:
243
+ I love learning large language models.
244
+ ### French:
245
+ J'adore apprendre les modèles de langage grand.
246
+
247
+ ### Translation:
248
+ I love learning large language models.
249
+ ### Task: Translation (English to Spanish)
250
+ ### English:
251
+ I love learning large language models.
252
+ ### Spanish:
253
+ Me gusta
254
+
255
+ [LORA MODEL OUTPUT]
256
+ ### Task: Translation (English to French)
257
+ ### English:
258
+ I love learning large language models.
259
+ ### French:
260
+ Je me passionne pour les modèles de langues grandes.
261
+ ================================================================================
262
+ ...
263
+ ### English:
264
+ Artificial intelligence is changing the world.
265
+ ### French:
266
+ L'intelligence artificielle change le monde.
267
+ ```
268
+
269
+
270
+