urdu-words2sentence-17m
A decoder-only transformer with 17.3 million parameters that writes an Urdu sentence from one to four Urdu keywords. The sentence can mix Urdu and English.
Model
| Property | Value |
|---|---|
| Architecture | Decoder-only transformer, pre-norm, RMSNorm, SwiGLU, learned positions, tied output head |
| Layers, width, heads | 8, 384, 6 |
| Parameters | 17,283,456 (14,162,304 outside the embeddings) |
| Vocabulary | 8000 BPE pieces (SentencePiece), trained on the Urdu corpus and the keywords |
| Context | 128 tokens |
| Precision | float32 weights, 69 MB |
Input format
The prompt is the BOS token, the keywords joined by +, and the <sep> token. The model writes the
sentence and ends it with the EOS token. config.json holds the token ids.
Usage
modeling.py holds the model class and generation.py the beam search.
import json
import sentencepiece as spm
from safetensors.torch import load_file
from generation import beam_search
from modeling import Transformer
config = json.load(open("config.json"))
tok = spm.SentencePieceProcessor(model_file="tokenizer.model")
model = Transformer.from_state_dict(load_file("model.safetensors"))
keywords = ["میں", "پانی", "چاہیے"]
prompt = [config["bos_token_id"], *tok.encode(" + ".join(keywords)), config["sep_token_id"]]
sentences = beam_search(model, prompt, config["eos_token_id"], beams=5, max_new=40, length_alpha=0.7)
for tokens in sentences[:3]:
print(tok.decode(tokens))
Output:
مجھے پانی چاہیے۔
مجھے پانی چاہیے ہے۔
میں پانی چاہتا ہوں۔
The evaluation below used beam search with 5 beams and returned the three best sentences.
Training
- Pretraining on 280 million tokens of Urdu text (CC-100, Urdu Wikipedia and Tatoeba, cleaned and packed), with validation loss 3.650.
- Fine-tuning for 3 epochs with learning rate 5e-05 and dropout 0 on 21,055 pairs of keywords and sentence (seed 0), with validation loss 2.881.
The pairs combine 2,575 Tatoeba sentences and 5,290 web sentences that a
language model mapped back to keywords, and 13,191 sentences that a language model
(gemma4:e4b) wrote for sampled keyword selections. Pairs that match an evaluation case were removed.
Evaluation
125 held-out keyword selections (93 Urdu, 32 code-mixed) with reference sentences. Exact match compares normalised sentences.
| Metric | Seed 0 (this model) | Mean of seeds 0 to 2 |
|---|---|---|
| Exact match at 1 | 0.408 | 0.427 |
| Exact match at 3 | 0.728 | 0.728 |
| Urdu cases, exact match at 3 | 0.806 | 0.803 |
| Code-mixed cases, exact match at 3 | 0.500 | 0.510 |
| Judge acceptance of the top sentence | 0.880 | |
| Latency, median of beam search | 18 ms |
The judge is qwen3.5:9b, which accepted 0.952 of the reference sentences and
0.008 of mismatched ones. The mT5-small baseline fine-tuned on the same pairs reaches
exact match at 3 of 0.608.
Intended use and limitations
- The sentences should be read and checked before they are used.
- The evaluation set is small (125 cases), so the numbers carry wide uncertainty.
- Some training sentences come from a language model, so the model reproduces that model's phrasing.
- The model was trained on one to four keywords from a fixed set of Urdu labels. Other inputs are outside what it was trained on.
- Gender and number agreement depend on the keywords.
Data
Pretraining text: CC-100 (Urdu), Urdu Wikipedia (wikimedia/wikipedia, snapshot 20231101.ur) and
Tatoeba. Fine-tuning pairs: keyword selections with a sentence, built from Tatoeba, web sentences and
sentences written by gemma4:e4b.
The fine-tuning pairs and the Tatoeba sentences are in
https://github.com/syedtaha22/goyai-experiments, folder models/inputs/data/ (pairs.tar.xz and
tatoeba_ur.jsonl.xz). The code in models/steps/corpus/ builds the training sets from them.
License
Apache License 2.0. The text is in LICENSE, and the sources of the training text are credited in
NOTICE.