CoolFace
Modelpublic

nassimjp/MiniCPM5-2B-SFT-Pashto

sourceHugging Faceapache-2.0updated 14d agoView on Hugging Face
0likes1.8kdownloads
Model Card

🧬 MiniCPM5-2B-Pashto

Pashto + Urdu Tokenizer Surgery Model


📋 Model Overview

This is a tokenizer-modified version of openbmb/MiniCPM5-2B with 46 Pashto and Urdu-specific characters added as single tokens to the vocabulary.

PropertyValue
Base Modelopenbmb/MiniCPM5-2B
Original Vocab Size130,560
New Vocab Size130,606
Atoms Added46
ArchitectureLlamaForCausalLM
Hidden Size2,048
Layers42
Context Length131,072
EmbeddingsNOT tied (independent input/output)

🔍 Why This Model Exists

MiniCPM5-2B's tokenizer does not natively support many Pashto and Urdu characters as single tokens. This causes:

  • ❌ Characters being split into multiple tokens
  • ❌ Inefficient encoding
  • ❌ Poor representation for Pashto/Urdu text

This model fixes that by surgically adding 46 missing atoms as independent tokens.


📊 Complete Audit & Modification Report

✅ Existing Single-Token Atoms (20)

AtomIDAtomID
ا20541د57692
ب79922ر37101
پ120234ز124105
ت55761س75848
ح118797ش107172
ع84439ک77297
ف79312ل29673
ق74411م36367
ن35964ه64732
و41636ی46703

⚠️ Split Atoms — FIXED via Surgery (46)

#AtomOld IDsNew ID
1ښ[172, 270]130560
2څ[172, 249]130561
3ځ[172, 245]130562
4ڼ[172, 142]130563
5ږ[172, 266]130564
6ډ[172, 253]130565
7ټ[171, 142]130566
8ړ[172, 263]130567
9ې[173, 260]130568
10ۍ[173, 257]130569
11ګ[172, 126]130570
12ث[170, 126]130571
13ج[170, 127]130572
14چ[172, 250]130573
15خ[170, 128]130574
16ذ[170, 130]130575
17ص[170, 135]130576
18ض[170, 136]130577
19ط[170, 137]130578
20ظ[170, 138]130579
21غ[170, 140]130580
22ئ[170, 121]130581
23ے[173, 262]130582
24ۀ[173, 244]130583
25ٹ[171, 139]130584
26ڈ[172, 252]130585
27ڑ[172, 261]130586
28ں[172, 140]130587
29ھ[172, 144]130588
30گ[172, 129]130589
31أ[170, 118]130590
32إ[170, 120]130591
33آ[170, 117]130592
34ؤ[170, 119]130593
35ء[170, 116]130594
36ٱ[171, 131]130595
37۰[173, 130]130596
38۱[173, 131]130597
39۲[173, 132]130598
40۳[173, 133]130599
41۴[173, 134]130600
42۵[173, 135]130601
43۶[173, 136]130602
44۷[173, 137]130603
45۸[173, 138]130604
46۹[173, 139]130605

🔬 Embedding Forensics

Model Configuration

python
{
    "architectures": ["LlamaForCausalLM"],
    "vocab_size": 130606,
    "hidden_size": 2048,
    "num_hidden_layers": 42,
    "max_position_embeddings": 131072,
    "tie_word_embeddings": False,
    "initializer_range": 0.02,
}

Input Embedding Statistics (New Atoms)

MetricValue
Mean embedding norm0.882395
Max cosine similarity0.27078322

Most Similar Pair (New Atoms)

Atom 1Atom 2Cosine Similarity
بم0.27078322

No exact duplicate embeddings detected


🚀 Usage

Installation

bash
pip install transformers torch accelerate

Basic Loading

python
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load model and tokenizer
model_id = "nassimjp/MiniCPM5-2B-Pashto"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

Tokenization Check

python
# Test Pashto characters
pashto_text = "سلام ښه راغلئ"

# Tokenize
tokens = tokenizer.encode(pashto_text, add_special_tokens=False)
print(f"Token IDs: {tokens}")

# Decode back
decoded = tokenizer.decode(tokens)
print(f"Decoded: {decoded}")

# Check individual atoms
for char in "ښڅځڼږډټړېۍګ":
    ids = tokenizer.encode(char, add_special_tokens=False)
    print(f"'{char}' → {ids}")

Text Generation

python
# Simple generation
prompt = "ښه راغلئ"
inputs = tokenizer.encode(prompt, return_tensors="pt")

# Generate
with torch.no_grad():
    outputs = model.generate(
        inputs,
        max_new_tokens=50,
        temperature=0.7,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id
    )

print(tokenizer.decode(outputs[0], skip_special_tokens=True))

⚙️ Technical Details

Surgery Methodology

  1. 1.Audit Phase
  2. 2.Iterated through 66 Pashto/Urdu characters
  3. 3.Detected which characters split into multiple tokens
  4. 4.Identified 46 characters needing surgery
  1. 1.Token Addition
  2. 2.Used tokenizer.add_tokens() to add 46 missing atoms
  3. 3.Resized model embeddings with model.resize_token_embeddings()
  4. 4.New tokens assigned IDs: 130560 → 130605
  1. 1.Independent Initialization
  2. 2.Input embeddings initialized with normal distribution
  3. 3.Output embeddings initialized independently
  4. 4.Both use initializer_range = 0.02
  5. 5.Not tied — maintains separate input and output spaces
  1. 1.Verification
  2. 2.Tokenization gate confirmed all 66 atoms as single tokens
  3. 3.No exact duplicate embeddings detected
  4. 4.Embedding norms and covariances verified

⚠️ Important Notes

NoteDescription
No FinetuningThis model has NOT been finetuned on Pashto/Urdu data
EmbeddingsInput and output embeddings are NOT tied
New TokensAdded tokens are at the end of vocabulary (IDs 130560+)
PerformanceRequires finetuning on Pashto/Urdu data for optimal performance
TokenizerWorks out-of-the-box with all Pashto/Urdu atoms

📦 Model Files

MiniCPM5-2B-Pashto/
├── chat_template.jinja             0.01 MB
├── config.json                     0.00 MB
├── generation_config.json          0.00 MB
├── ipashto_surgery_info.txt        0.00 MB
├── model-00001-of-00003.safetensors  1,903.45 MB
├── model-00002-of-00003.safetensors  1,907.19 MB
├── model-00003-of-00003.safetensors    990.10 MB
├── model.safetensors.index.json    0.03 MB
├── tokenizer.json                  9.44 MB
└── tokenizer_config.json           0.00 MB

Total Size: 4.70 GB

🔗 Related Resources


📝 Citation

If you use this model, please cite the original MiniCPM paper:

bibtex
@article{minicpm2024,
  title={MiniCPM: Unveiling the Potential of Small Language Models},
  author={Hu, Shengding and Ding, Ning and others},
  journal={arXiv preprint arXiv:2404.06395},
  year={2024}
}

📄 License

This model is released under the Apache License 2.0.


🤝 Acknowledgements

  • OpenBMB for developing and releasing MiniCPM5-2B
  • Hugging Face for the transformers library and model hosting
  • Kaggle for providing the compute environment

🧪 Testing Results

All 66 Pashto/Urdu atoms successfully tokenize as single tokens:

CategoryCountStatus
Pashto-Specific11✅ All single tokens
Shared Arabic/Persian30✅ All single tokens
Positional Forms3✅ All single tokens
Urdu/South Asian6✅ All single tokens
Arabic Variants6✅ All single tokens
Eastern Arabic Digits10✅ All single tokens
TOTAL66✅ 100% Coverage

Created with ❤️ for the Pashto and Urdu communities


---