--- library_name: transformers base_model: Qwen/Qwen2.5-1.5B-Instruct tags: - lora - peft - yoda - style-transfer - qwen2.5 license: apache-2.0 language: - en --- # Qwen2.5-1.5B Yoda-Speak Translator A LoRA fine-tune of Qwen2.5-1.5B-Instruct that translates ordinary English sentences into Yoda-style syntax (object/verb-first reordering, e.g. "Read this you must."). ## Model Details ### Model Description This model takes a plain English sentence and rewrites it in Yoda's speaking style — primarily through object-subject-verb reordering rather than vocabulary changes. It was trained as a hands-on learning project to understand LoRA fine-tuning mechanics end-to-end: data preparation, chat-template formatting, loss masking, hyperparameter tradeoffs, and — most importantly — why validation loss alone isn't sufficient for picking a checkpoint on a small dataset. - **Developed by:** Barath (independent project) - **Model type:** Causal language model, LoRA fine-tune (adapter merged into base weights) - **Language(s):** English - **License:** Apache 2.0 (inherited from base model) - **Finetuned from model:** [Qwen/Qwen2.5-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct) ## Uses ### Direct Use Prompt the model with an instruction to translate a sentence into Yoda-speak. Works best on single, self-contained declarative sentences, questions, negations, and imperatives similar in length to the training data (roughly 25-60 characters). Style transfer only — no factual knowledge was added. ### Out-of-Scope Use - Not intended for factual Q&A, general assistance, or any task beyond stylistic sentence reordering. - Degrades on longer multi-clause paragraphs and dialogue-heavy text (see Evaluation below) — treat output on such inputs as unreliable without manual review. - Not evaluated for languages other than English. ## Bias, Risks, and Limitations - Trained on only 720 source examples (576 train / 72 val / 72 test) from the `dvgodoy/yoda_sentences` dataset — a narrow, templated dataset of short declarative sentences about mundane objects. Generalization to genuinely novel sentence structures (questions, multi-clause sentences, dialogue) is measurably weaker than in-distribution performance. - Occasionally under-transforms harder inputs (falls back toward plain English word order) rather than producing an incorrect reordering — this was the deciding factor in checkpoint selection, since it's a safer failure mode than the token-level corruption seen in more heavily-trained checkpoints. - No safety/toxicity-specific evaluation was performed; inherits the base model's general behavior and limitations. ### Recommendations For inputs meaningfully different from short declarative English sentences (long paragraphs, dialogue, technical text), manually review output quality before relying on it. ## How to Get Started with the Model ```python from transformers import AutoModelForCausalLM, AutoTokenizer import torch model_name = "barath007183/qwen2.5-1.5b-yoda-speak" # replace with actual repo id model = AutoModelForCausalLM.from_pretrained(model_name, dtype=torch.bfloat16, device_map="cuda") tokenizer = AutoTokenizer.from_pretrained(model_name) messages = [ {"role": "system", "content": "You are a Yoda-speak translator."}, {"role": "user", "content": "Translate this into Yoda-speak: The cat sat on the mat."}, ] prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) inputs = tokenizer(prompt, return_tensors="pt").to("cuda") output = model.generate(**inputs, max_new_tokens=60, do_sample=False) print(tokenizer.decode(output[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)) # "Sat on the mat, the cat did." ``` ## Training Details ### Training Data [`dvgodoy/yoda_sentences`](https://huggingface.co/datasets/dvgodoy/yoda_sentences) — 720 paired examples of plain English sentences and their Yoda-syntax translations. Split 80/10/10 (576 train / 72 validation / 72 test), stratified by random shuffle (seed 42). Each row was formatted as a 3-turn chat example (system + user + assistant) using Qwen2.5's native ChatML template via `tokenizer.apply_chat_template()`. A fixed system prompt ("You are a Yoda-speak translator.") was paired with one of 4 randomly-rotated user instruction phrasings per row, to avoid the model overfitting to a single trigger phrase rather than the underlying task. ### Training Procedure - **Method:** LoRA (not QLoRA — base model loaded in bf16, no quantization; unnecessary at this model size on a 24GB GPU) - **LoRA config:** r=8, alpha=16, dropout=0.05, target modules: `q_proj`, `k_proj`, `v_proj`, `o_proj` (attention only — MLP modules were not targeted, since this task is primarily syntactic/relational rather than knowledge-based) - **Trainable parameters:** 2,179,072 / 1,545,893,376 total (0.141%) - **Loss masking:** `assistant_only_loss=True` (trl 1.8.0) — loss computed only on assistant-response tokens, not system/user tokens #### Training Hyperparameters - **Training regime:** bf16 - **Epochs:** 3 (selected after comparing checkpoints at epochs 2, 3, and 8 — see Evaluation) - **Batch size:** 8 (train and eval) - **Learning rate:** 2e-4 - **Optimizer:** AdamW (trl/transformers default) #### Speeds, Sizes, Times - **Hardware:** 1x RTX 4090 (24GB), rented via RunPod - **Training time:** ~26 seconds for 3 epochs (576 examples, 216 steps) - **Checkpoint size:** LoRA adapter ~9MB; merged model ~3GB (bf16) ## Evaluation ### Testing Data, Factors & Metrics Evaluation was done in three stages, deliberately going beyond validation loss alone: 1. **Held-out test set** (72 examples, same distribution as training data) 2. **Out-of-distribution set** (27 hand-written sentences spanning longer multi-clause sentences, questions, negation, first-person, modern/tech vocabulary, cricket-specific sentences, imperatives, and conditionals — deliberately unlike the training distribution) 3. **Paragraph stress test** (a short original fantasy-narrative paragraph with dialogue, proper nouns, and a 3-clause compound sentence) Three checkpoints (epoch 2, 3, and 8) were compared at each stage, alongside the un-fine-tuned base model as a control. ### Results #### Summary - **Base model control:** confirmed fine-tuning was necessary and effective — the base model, even when explicitly instructed, produced correct Yoda-syntax reordering on only ~5/72 test sentences, frequently substituting generic "old-timey" phrasing, hallucinating unrelated content, or leaving sentences unchanged. - **Validation loss was a misleading tie-breaker at this data scale.** Epoch 2 had marginally lower validation loss (0.2385) than epoch 3 (0.2405) — a difference within noise at 72 validation examples — but epoch 3 produced clearly better outputs on both the in-distribution and out-of-distribution test sets (5/9 flagged failure cases from epoch 2 were fully fixed by epoch 3). - **Epoch 8 showed clear overfitting**, though not in the way a naive expectation (worse everywhere) would predict: validation loss more than doubled (0.521 vs. 0.240) and training loss approached zero, with entropy collapsing (0.190 → 0.042), indicating the model became overconfident on training-distribution patterns. In practice this showed up as *inconsistent* behavior on out-of-distribution input — fixing some failure cases epoch 3 couldn't, while introducing new token-level corruption and grammatical regressions elsewhere (e.g., producing a malformed fused token on a 3-clause compound sentence). - **Epoch 3 was selected as the final checkpoint**: across all three evaluation stages, it never produced outright broken/garbled output, only occasional "under-transformation" (falling back toward plain English on the hardest inputs) — judged the safer failure mode compared to epoch 8's occasional token-level corruption. ## Technical Specifications ### Model Architecture and Objective Qwen2.5-1.5B-Instruct architecture (transformer decoder, SwiGLU MLP blocks), causal language modeling objective, fine-tuned via supervised fine-tuning (SFT) with LoRA adapters merged into the base weights post-training. ### Compute Infrastructure #### Hardware 1x NVIDIA RTX 4090 (24GB VRAM), rented via RunPod (PyTorch template, CUDA 12.8 driver) #### Software - `transformers` - `trl` 1.8.0 - `peft` 0.19.1 - `torch` 2.x (cu124 build) - `datasets` ## Model Card Authors Barath C