A decoder-only transformer with 18.8 million parameters that writes an Urdu sentence from one to four Urdu keywords, and then the English sentence with the same meaning. One model and one set of weights write both languages. It extends urdu-words2sentence-17m.
| Property | Value |
|---|---|
| Architecture | Decoder-only transformer, pre-norm, RMSNorm, SwiGLU, learned positions, tied output head for Urdu, separate output head for English |
| Layers, width, heads | 8, 384, 6 |
| Parameters | 18,819,456 (14,930,304 outside the embeddings) |
| Vocabulary | 10,000 pieces: 8000 Urdu pieces (SentencePiece, tokenizer.model) and 2000 English pieces (english_tokenizer.model) |
| Context | 128 tokens |
| Precision | float32 weights, 75 MB |
The English pieces take the ids 8000 to 9999 (English piece id plus english_token_offset). The Urdu
pieces and the end token use the tied embedding matrix as output matrix. The English pieces use the
separate matrix english_head.weight.
The prompt is the BOS token, the keywords joined by +, and the <sep> token. The model writes the
Urdu sentence, then the marker token (marker_token_id, the English BOS piece), then the English
sentence, then the EOS token. config.json holds the token ids.
modeling.py holds the base model, joint.py the model with the English output matrix and
generation.py the beam search. The beam search ends each sentence at the marker, and the English
sentence is then written greedily.
import json
import sentencepiece as spm
from safetensors.torch import load_file
from generation import beam_search
from joint import JointTransformer, write_english
config = json.load(open("config.json"))
urdu = spm.SentencePieceProcessor(model_file="tokenizer.model")
english = spm.SentencePieceProcessor(model_file="english_tokenizer.model")
model = JointTransformer.from_state_dict(load_file("model.safetensors"))
offset, marker = config["english_token_offset"], config["marker_token_id"]
keywords = ["میں", "پانی", "چاہیے"]
prompt = [config["bos_token_id"], *urdu.encode(" + ".join(keywords)), config["sep_token_id"]]
beams = beam_search(model, prompt, marker, beams=5, max_new=40, length_alpha=0.7)
for tokens in beams[:3]:
sentence = urdu.decode([t for t in tokens if t < offset])
ids = write_english(model, prompt + urdu.encode(sentence) + [marker], config["eos_token_id"], 30)
print(sentence, "|", english.decode([t - offset for t in ids]))
Output:
مجھے پانی چاہیے۔ | I need water.
مجھے پانی چاہیے، میں پانی چاہتا ہوں۔ | I need water, I want water.
مجھے پانی چاہیے ہے۔ | I need water.
The evaluation below used beam search with 5 beams and returned the three best sentences.
The folder onnx/ holds the model for use in a web browser with ONNX Runtime Web.
| File | Content |
|---|---|
model.int8.onnx |
One decoding step with 8-bit weights in the matrix multiplications (22.8 MB) |
meta.json |
Number of layers, heads, positions and vocabulary pieces, the id of <sep> and the file sizes |
tokenizer.json, english_tokenizer.json |
The pieces and scores of the two SentencePiece models, for a tokenizer in JavaScript |
The graph takes the new tokens, the row of the cache that each beam continues (beam_idx) and the
key and value tensors of the eight blocks (past_k_0, past_v_0 and so on). It returns the logits of
the last position and the extended cache. The beam search and the English continuation run outside the
graph. On the 125 cases the 8-bit graph reaches exact match at 3 of 0.736, and a request takes about
70 to 83 ms in WebAssembly on a laptop processor (two runs). The graph with float32 weights gave the
same candidates as the PyTorch model on 250 requests.
The English sentences were written by gemma4:e4b for the Urdu sentences of the training pairs
(19,423 of 19,479 distinct sentences, and pairs without an English sentence were not used for English). The model does not call gemma4:e4b when it runs.
The same 125 held-out keyword selections (93 Urdu, 32 code-mixed) as for the Urdu model. English exact match and word F1 compare the English sentence of the top Urdu sentence with the reference English sentences.
| Metric | Seed 1 (this model) | Mean of seeds 0 to 3 |
|---|---|---|
| Urdu exact match at 1 | 0.360 | |
| Urdu exact match at 3 | 0.736 | 0.730 (sd 0.014) |
| Urdu cases, exact match at 3 | 0.806 | 0.801 |
| Code-mixed cases, exact match at 3 | 0.531 | 0.523 |
| English exact match | 0.504 | 0.504 |
| English word F1 | 0.788 | 0.772 |
| Latency, median of beam search and English | 39 ms |
The Urdu-only model reaches exact match at 3 of 0.728. For the English sentence of the reference
Urdu sentence, gemma4:e4b reaches exact match 0.520 and word F1 0.795 at a median of 589 ms.
Pretraining text: CC-100 (Urdu), Urdu Wikipedia (wikimedia/wikipedia, snapshot 20231101.ur) and
Tatoeba. Fine-tuning pairs: keyword selections with an Urdu sentence from Tatoeba, web sentences and
sentences written by gemma4:e4b, and English sentences written by gemma4:e4b. The data and the
code are in https://github.com/syedtaha22/goyai-experiments, folder models/.
Apache License 2.0. The text is in LICENSE, and the sources of the training text are credited in
NOTICE.
model.safetensors 72 MiBonnx/model.int8.onnx 22 MiB