ModernCantoneseBert-Large

This model is answerdotai/ModernBERT-large with a Cantonese-extended vocabulary, further pretrained (masked language modeling) for 5 epochs on ~747M tokens of Cantonese text (~3.7B tokens seen in total). It achieves the following results on the evaluation set:
- Loss: 1.4511
- Accuracy: 0.7052
- Perplexity: 4.27
Model description
ModernCantoneseBert is a BERT model specifically designed for Cantonese language understanding. It uses the ModernBERT architecture (alternating local/global attention, RoPE, up to 8192-token context) from HuggingFace Transformers and is trained with masked language modeling (MLM) on Cantonese text.
Unlike ModernCantoneseBert-Base, which was trained from scratch with a WordPiece tokenizer, the Large model starts from the pretrained ModernBERT-large weights:
- Vocabulary extension: ModernBERT's byte-level BPE tokenizer (50,368 tokens) was extended with 16,000 new merges learned on Cantonese text, giving a vocabulary of 66,368 tokens. Common Cantonese characters and words (e.g. 嘅, 係, 唔, 佢, 香港) become single tokens instead of being split into multiple byte pieces.
- Embedding initialisation: each new token's embedding is initialised as the byte-length-weighted mean of the embeddings of the two tokens it was merged from; the tied MLM decoder and its bias are initialised the same way. All other pretrained weights are kept.
- Continued pretraining: the whole model was then trained with MLM on packed 2048-token sequences.
| Parameters | 412,281,664 |
| Layers / hidden / heads | 28 / 1024 / 16 |
| Vocabulary | 66,368 (50,368 ModernBERT + 16,000 Cantonese) |
| Training sequence length | 2048 |
| Max position embeddings | 8192 |
Intended Uses & Limitations
Intended Uses
This model is intended for research and academic purposes in Cantonese natural language processing tasks, including:
- Masked language modeling (fill-mask)
- Feature extraction for downstream NLP tasks
- Fine-tuning for Cantonese text classification, named entity recognition, and other sequence labeling tasks
Limitations
- Research Purpose Only: This project is intended for research and academic use. It may not be suitable for production environments without further evaluation and fine-tuning.
- Language Coverage: The model is specifically trained on Cantonese text and may not perform well on other Chinese dialects or languages. English ability inherited from ModernBERT-large may have degraded during continued pretraining.
- Context Length: Training used 2048-token sequences; behaviour on longer inputs (up to the architectural 8192) has not been evaluated.
Training and Evaluation Data
The model was trained on ~626k Cantonese documents (364,980 packed 2048-token sequences, ~747M tokens per epoch) from open source datasets and web-scraped content, including:
- Cantonese filtered Common Crawl
- Web scraped news and articles
Due to respect for copyright, the training dataset will not be released.
Quick Start
Installation
pip install "transformers>=4.48" torch
Usage
Use the model for fill-mask tasks with the HuggingFace Transformers library:
from transformers import pipeline
mask_filler = pipeline(
"fill-mask",
model="hon9kon9ize/ModernCantoneseBert-Large"
)
mask_filler("雞蛋六隻,糖呢就兩茶匙,仲有[MASK]橙皮添。")
mask_filler("香港特首係李家[MASK]。")
Note: the tokenizer is byte-level BPE, so a [MASK] may stand for a multi-character word rather than a single character.
Training
Data Preprocessing
- Prepare your JSONL/Parquet data files with a
textfield - Tokenize and pack into fixed-length blocks:
python preprocess.py \
--model_path ./models/large \
--data_path ./data/ \
--output_path ./pretrain/data/ \
--max_seq_len 2048 --pack
Extending the Tokenizer and Building the Model
python extend_modernbert_tokenizer.py # learn 16k Cantonese merges on top of ModernBERT's BPE
python build_large_extended.py \
--tokenizer_dir models/tokenizer_modernbert_cantonese \
--out_dir models/large
Training the Model
python run_mlm.py \
--model_name_or_path ./models/large \
--train_file ./pretrain/data/ \
--do_train \
--do_eval \
--bf16 \
--output_dir ./output/
Training procedure
Trained on a single NVIDIA H100 80GB for ~20.4 hours.
Training hyperparameters
The following hyperparameters were used during training:
- learning_rate: 5e-05
- train_batch_size: 8
- eval_batch_size: 8
- seed: 42
- gradient_accumulation_steps: 4
- total_train_batch_size: 32 (65,536 tokens)
- optimizer: AdamW (torch fused) with betas=(0.9,0.999), epsilon=1e-08, weight_decay=0.01
- lr_scheduler_type: cosine
- lr_scheduler_warmup_ratio: 0.05
- num_epochs: 5.0
- precision: bf16 mixed precision
Training results
| Training Loss | Epoch | Step | Validation Loss | Accuracy |
|---|---|---|---|---|
| 2.2382 | 0.1754 | 2000 | 2.4034 | 0.6083 |
| 1.8702 | 0.3507 | 4000 | 2.0356 | 0.6367 |
| 1.8455 | 0.5261 | 6000 | 1.8788 | 0.6519 |
| 1.7987 | 0.7014 | 8000 | 1.7964 | 0.6616 |
| 1.7435 | 0.8768 | 10000 | 1.7430 | 0.6675 |
| 1.6565 | 1.0521 | 12000 | 1.6991 | 0.6732 |
| 1.5574 | 1.2274 | 14000 | 1.6615 | 0.6770 |
| 1.598 | 1.4028 | 16000 | 1.6278 | 0.6820 |
| 1.5757 | 1.5781 | 18000 | 1.6050 | 0.6842 |
| 1.5388 | 1.7535 | 20000 | 1.5860 | 0.6865 |
| 1.5419 | 1.9288 | 22000 | 1.5636 | 0.6906 |
| 1.4715 | 2.1042 | 24000 | 1.5520 | 0.6916 |
| 1.4778 | 2.2795 | 26000 | 1.5360 | 0.6934 |
| 1.4903 | 2.4549 | 28000 | 1.5145 | 0.6974 |
| 1.4338 | 2.6302 | 30000 | 1.5162 | 0.6956 |
| 1.4413 | 2.8056 | 32000 | 1.4981 | 0.6982 |
| 1.4427 | 2.9809 | 34000 | 1.4842 | 0.7007 |
| 1.4414 | 3.1562 | 36000 | 1.4829 | 0.7014 |
| 1.3854 | 3.3316 | 38000 | 1.4763 | 0.7011 |
| 1.3978 | 3.5069 | 40000 | 1.4711 | 0.7022 |
| 1.3956 | 3.6823 | 42000 | 1.4521 | 0.7051 |
| 1.3381 | 3.8576 | 44000 | 1.4570 | 0.7054 |
| 1.4258 | 4.0330 | 46000 | 1.4558 | 0.7044 |
| 1.3968 | 4.2083 | 48000 | 1.4506 | 0.7049 |
| 1.357 | 4.3837 | 50000 | 1.4572 | 0.7060 |
| 1.3969 | 4.5590 | 52000 | 1.4450 | 0.7068 |
| 1.3609 | 4.7344 | 54000 | 1.4491 | 0.7058 |
| 1.3648 | 4.9097 | 56000 | 1.4427 | 0.7056 |
| - | 5.0 | 57030 | 1.4511 | 0.7052 |
Framework versions
- Transformers 4.57.6
- Pytorch 2.8.0+cu128
- Datasets 3.6.0
- Tokenizers 0.22.2