File size: 6,654 Bytes
6e6ce68 4d03cb6 6e6ce68 4d03cb6 6e6ce68 a313f18 6e6ce68 |
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 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
---
license: apache-2.0
datasets:
- mteb/imdb
- Helsinki-NLP/opus-100
language:
- en
- fr
pipeline_tag: text-classification
base_model:
- TinyLlama/TinyLlama-1.1B-Chat-v1.0
tags:
- unsloth
- lora
- peft
- Multi-Task
- Sentiment Analysis
- Translation (English to French)
---
# TinyLlama Multi-Task LoRA (Sentiment + Translation)
This repository contains a **LoRA adapter** trained on top of
**TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T**
to support **multiple tasks** via instruction-style prompting.
---
## 🔧 Base Model
- **Base model**: `TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T`
- **Architecture**: Decoder-only (LLaMA-style)
- **Fine-tuning method**: LoRA (PEFT)
- **Trainable parameters**: ~4M
- **Total parameters**: ~1.1B
This repository **only contains the LoRA adapter weights**, not the full base model.
---
## 📌 Supported Tasks
### 1️⃣ Sentiment Analysis (IMDB)
Binary sentiment classification for movie reviews:
- `positive`
- `negative`
### 2️⃣ Translation (English → French)
Neural machine translation from English to French.
---
## 🧠 Training Data
All datasets are loaded directly from **Hugging Face Datasets**:
| Task | Dataset | Description |
| ------------------- | ------------------------------- | -------------------------------- |
| Sentiment Analysis | `imdb` | Movie reviews with binary labels |
| Translation (EN→FR) | `Helsinki-NLP/opus-100` (en-fr) | Parallel English–French corpus |
---
## 🧩 Training Strategy
- **Multi-task instruction tuning**
- Tasks are distinguished via **explicit prompt headers**
- All tasks are unified into a **causal language modeling** objective
- Only LoRA parameters are updated; base model weights remain frozen
---
## 🧾 Prompt Format
### Sentiment Analysis
```text
### Task: Sentiment Analysis
### Review:
This movie was absolutely fantastic!
### Answer:
positive
### Task: Translation (English to French)
### English:
I love learning large language models.
### French:
J'aime apprendre les grands modèles de langage.
```
**Load Base Model + LoRA Adapter**
```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
base_model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
lora_repo = "BEncoderRT/tinyllama-multitask-lora"
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
tokenizer.pad_token = tokenizer.eos_token
base_model = AutoModelForCausalLM.from_pretrained(
base_model_name,
device_map="auto"
).eval()
lora_model = AutoModelForCausalLM.from_pretrained(
base_model_name,
device_map="auto"
)
lora_model = PeftModel.from_pretrained(lora_model, lora_repo)
lora_model.eval()
def generate(
model,
prompt,
max_new_tokens=64,
temperature=0.3
):
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=True,
temperature=temperature,
pad_token_id=tokenizer.eos_token_id
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
```
**Inference Examples**
Sentiment Analysis Example
```python
sentiment_tests = [
"I absolutely loved this movie. The acting was brilliant.",
"This film was boring, slow, and a complete waste of time.",
"The movie was okay, but nothing special.",
]
def test_sentiment_comparison(texts):
for text in texts:
prompt = (
"### Task: Sentiment Analysis\n"
"### Review:\n"
f"{text}\n"
"### Answer:\n"
)
base_out = generate(base_model, prompt, max_new_tokens=8)
lora_out = generate(lora_model, prompt, max_new_tokens=8)
print("=" * 80)
print("REVIEW:")
print(text)
print("\n[BASE MODEL OUTPUT]")
print(base_out)
print("\n[LORA MODEL OUTPUT]")
print(lora_out)
```
```python
test_sentiment_comparison(sentiment_tests)
```
```
================================================================================
REVIEW:
I absolutely loved this movie. The acting was brilliant.
[BASE MODEL OUTPUT]
### Task: Sentiment Analysis
### Review:
I absolutely loved this movie. The acting was brilliant.
### Answer:
I loved this movie. It was so
[LORA MODEL OUTPUT]
### Task: Sentiment Analysis
### Review:
I absolutely loved this movie. The acting was brilliant.
### Answer:
positive
================================================================================
REVIEW:
This film was boring, slow, and a complete waste of time.
[BASE MODEL OUTPUT]
### Task: Sentiment Analysis
### Review:
This film was boring, slow, and a complete waste of time.
...
### Review:
The movie was okay, but nothing special.
### Answer:
negative
```
Translation Example
```python
translation_tests = [
"I love learning large language models.",
"This movie was disappointing and boring.",
"Artificial intelligence is changing the world."
]
def test_translation_comparison(texts):
for text in texts:
prompt = (
"### Task: Translation (English to French)\n"
"### English:\n"
f"{text}\n"
"### French:\n"
)
base_out = generate(base_model, prompt, max_new_tokens=64)
lora_out = generate(lora_model, prompt, max_new_tokens=64)
print("=" * 80)
print("ENGLISH:")
print(text)
print("\n[BASE MODEL OUTPUT]")
print(base_out)
print("\n[LORA MODEL OUTPUT]")
print(lora_out)
```
```python
test_sentiment_comparison(sentiment_tests)
```
```
================================================================================
ENGLISH:
I love learning large language models.
[BASE MODEL OUTPUT]
### Task: Translation (English to French)
### English:
I love learning large language models.
### French:
J'adore apprendre les modèles de langage grand.
### Translation:
I love learning large language models.
### Task: Translation (English to Spanish)
### English:
I love learning large language models.
### Spanish:
Me gusta
[LORA MODEL OUTPUT]
### Task: Translation (English to French)
### English:
I love learning large language models.
### French:
Je me passionne pour les modèles de langues grandes.
================================================================================
...
### English:
Artificial intelligence is changing the world.
### French:
L'intelligence artificielle change le monde.
``` |