Havoc999/tiny-chatbot
04
1---2language:3- en4license: apache-2.05base_model: TinyLlama/TinyLlama-1.1B-Chat-v1.06datasets:7- tatsu-lab/alpaca8tags:9- instruction-tuning10- lora11- peft12- trl13- chatbot14- causal-lm15pipeline_tag: text-generation16---17 18# π€ Tiny Chatbot β LoRA Fine-Tuned on Alpaca19 20A conversational assistant produced by fine-tuning21**[TinyLlama-1.1B-Chat-v1.0](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0)**22on the **[tatsu-lab/alpaca](https://huggingface.co/datasets/tatsu-lab/alpaca)**23instruction dataset (52 K English instructionβresponse pairs) using24LoRA (rank 16) via TRL's SFTTrainer on a Kaggle Dual T4 GPU environment.25 26---27 28## π Quick Start29 30```python31from transformers import AutoModelForCausalLM, AutoTokenizer32import torch33 34model = AutoModelForCausalLM.from_pretrained(35 "Havoc999/tiny-chatbot",36 torch_dtype=torch.float16,37 device_map="auto",38)39tokenizer = AutoTokenizer.from_pretrained("Havoc999/tiny-chatbot")40 41prompt = (42 "Below is an instruction that describes a task. "43 "Write a response that appropriately completes the request.\n\n"44 "### Instruction:\n"45 "Explain the water cycle in simple terms.\n\n"46 "### Response:\n"47)48 49inputs = tokenizer(prompt, return_tensors="pt").to(model.device)50output = model.generate(51 **inputs,52 max_new_tokens=256,53 temperature=0.7,54 top_p=0.9,55 do_sample=True,56 repetition_penalty=1.15,57)58response = tokenizer.decode(output[0, inputs.input_ids.shape[1]:], skip_special_tokens=True)59print(response)60```61 62### Multi-turn (Chat Template)63 64```python65from transformers import pipeline66 67pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)68 69messages = [70 {"role": "user", "content": "What is photosynthesis?"},71]72 73# TinyLlama-Chat supports the built-in chat template74prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)75print(pipe(prompt, max_new_tokens=200)[0]["generated_text"])76```77 78---79 80## π Benchmark Results81 82All benchmarks were evaluated after fine-tuning, using greedy decoding unless otherwise noted.83 84### MMLU β Elementary Mathematics85 86| Metric | Value |87|---|---|88| Samples evaluated | 50 |89| Correct | 15 |90| Invalid outputs | 4 |91| **Accuracy** | **30.00%** |92| Random baseline (4-way) | 25.00% |93 94> **+5 pp above random.** The model demonstrates marginal elementary math ability consistent with the small 1.1 B parameter count and an English instruction dataset that contains limited mathematical content.95 96---97 98### HellaSwag *(commonsense NLI)*99 100| Metric | Score | Samples |101|---|---|---|102| Accuracy | 0.4550 | 200 |103| Accuracy (normalised) | **0.5600** | 200 |104 105> Normalised accuracy above 0.50 indicates better-than-random commonsense sentence completion. HellaSwag is a strong proxy for general language understanding.106 107---108 109### PIQA *(physical intuition QA)*110 111| Metric | Score | Samples |112|---|---|---|113| Accuracy | 0.7450 | 200 |114| Accuracy (normalised) | **0.7400** | 200 |115 116> PIQA tests physical intuition and everyday procedural knowledge. 0.74 is a solid result for a 1.1 B model, suggesting the base pre-training retains good world knowledge even after instruction fine-tuning.117 118---119 120### ARC Challenge *(grade-school science)*121 122| Metric | Score | Samples |123|---|---|---|124| Accuracy | 0.3050 | 200 |125| Accuracy (normalised) | **0.3500** | 200 |126 127> ARC-Challenge targets questions that require reasoning beyond simple retrieval. 0.35 normalised reflects the model's limitations on multi-step reasoning at this scale.128 129---130 131### Summary132 133| Benchmark | Metric | Score |134|---|---|---|135| MMLU Elem. Math | Accuracy | 30.00% |136| HellaSwag | Acc (norm) | 56.00% |137| PIQA | Acc (norm) | 74.00% |138| ARC Challenge | Acc (norm) | 35.00% |139 140---141 142## π Training Details143 144| Setting | Value |145|---|---|146| Base model | TinyLlama/TinyLlama-1.1B-Chat-v1.0 |147| Dataset | tatsu-lab/alpaca |148| Train split | 45,000 examples |149| Eval split | 2,000 examples |150| Fine-tuning method | LoRA (PEFT) |151| LoRA rank | 16 |152| LoRA alpha | 32 |153| LoRA dropout | 0.05 |154| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |155| Trainable parameters | ~17 M / 1.1 B (~1.55%) |156| Precision | float16 (AMP) |157| Epochs | 3 |158| Per-GPU batch size | 4 |159| Gradient accumulation | 4 steps |160| Effective global batch | 32 (4 Γ 2 GPUs Γ 4 accum) |161| Peak learning rate | 2e-4 |162| LR scheduler | Cosine annealing |163| Warmup ratio | 3% |164| Gradient checkpointing | Enabled |165| NEFTune noise alpha | 5 |166| Hardware | Kaggle Dual T4 (2 Γ 16 GiB VRAM) |167| Loss masking | Completion-only (response tokens only) |168| Early stopping patience | 3 evaluations |169 170---171 172## βοΈ Reproduce173 174```python175# Install dependencies176# pip install transformers datasets peft trl accelerate bitsandbytes huggingface_hub177 178from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments179from peft import LoraConfig, get_peft_model, TaskType180from trl import SFTTrainer, DataCollatorForCompletionOnlyLM181from datasets import load_dataset182 183# 1. Load dataset184dataset = load_dataset("tatsu-lab/alpaca", split="train")185 186# 2. Format examples187def format_alpaca(ex):188 input_section = f"### Input:\n{ex['input']}\n\n" if ex["input"].strip() else ""189 return {190 "text": (191 "Below is an instruction that describes a task. "192 "Write a response that appropriately completes the request.\n\n"193 f"### Instruction:\n{ex['instruction']}\n\n"194 f"{input_section}"195 f"### Response:\n{ex['output']}"196 )197 }198 199dataset = dataset.map(format_alpaca, batched=False)200 201# 3. Load model + LoRA202tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")203tokenizer.pad_token = tokenizer.eos_token204 205model = AutoModelForCausalLM.from_pretrained(206 "TinyLlama/TinyLlama-1.1B-Chat-v1.0",207 torch_dtype="auto",208 device_map={"": 0},209)210model.config.use_cache = False211model.enable_input_require_grads()212 213lora_config = LoraConfig(214 r=16, lora_alpha=32, lora_dropout=0.05,215 bias="none", task_type=TaskType.CAUSAL_LM,216 target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"],217)218model = get_peft_model(model, lora_config)219 220# 4. Train221trainer = SFTTrainer(222 model=model, tokenizer=tokenizer,223 train_dataset=dataset,224 dataset_text_field="text",225 max_seq_length=512,226 data_collator=DataCollatorForCompletionOnlyLM("### Response:\n", tokenizer=tokenizer),227 args=TrainingArguments(228 output_dir="./chatbot-lora",229 num_train_epochs=3,230 per_device_train_batch_size=4,231 gradient_accumulation_steps=4,232 learning_rate=2e-4,233 fp16=True,234 gradient_checkpointing=True,235 save_strategy="steps", save_steps=200, save_total_limit=3,236 eval_strategy="no",237 ),238)239trainer.train()240```241 242---243 244## β οΈ Limitations245 246- **English only** β the base model and Alpaca dataset are English-focused; other languages may produce incoherent outputs.247- **Hallucination** β like all generative models, this one can confidently state incorrect facts. Always verify important claims.248- **Limited reasoning** β at 1.1 B parameters, multi-step logical and mathematical reasoning is unreliable (see ARC / MMLU results above).249- **No RLHF safety alignment** β this model has not undergone reinforcement learning from human feedback. It inherits TinyLlama's base alignment only and may produce inappropriate responses to adversarial prompts.250- **Short context** β trained with a maximum sequence length of 512 tokens; very long conversations will be truncated.251- **Not production-ready** β intended as a learning artefact and research baseline, not a deployed consumer product.252 253---254 255## π License256 257This model is released under the **Apache 2.0** license, consistent with the258[TinyLlama base model](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0)259and the260[Alpaca dataset](https://huggingface.co/datasets/tatsu-lab/alpaca).261See [LICENSE](https://www.apache.org/licenses/LICENSE-2.0) for full terms.262 263---264 265*Fine-tuned on Kaggle Dual T4 GPU Β· TRL SFTTrainer Β· LoRA via PEFT*