ModernCantoneseBert-Large-Char

This model is a character-level version of hon9kon9ize/ModernCantoneseBert-Large: the same ModernBERT-large body with a new one-token-per-character tokenizer, further pretrained (masked language modeling) for 5 epochs on ~637M tokens of Cantonese and written-Chinese text (~3.2B tokens seen in total). It achieves the following results on the evaluation set:
- Loss: 0.9725
- Accuracy: 0.7807
- Perplexity: 2.64
These numbers are not comparable with ModernCantoneseBert-Large (loss 1.4511, accuracy 0.7052). That model predicts BPE tokens that are often whole words; this one predicts single characters, usually with the rest of the word visible, and is evaluated on a filtered version of the corpus.
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.
ModernCantoneseBert-Large uses byte-level BPE, whose merges follow frequency rather than word or entity boundaries (for example 佢喺 is one token and 保安局局長 splits as 保安 + 局局長). That makes token-aligned tasks such as NER and span extraction unreliable. This model replaces the tokenizer so that every Chinese character is exactly one token:
-
Character tokenizer: 12,160 tokens — 10,950 Han characters plus punctuation, symbols, kana and Greek, 5 class tokens and 256 reserved
[unusedN]slots for task-specific tokens. Traditional and Simplified characters are both kept and never unified. Chinese numerals (二零二零,一千,廿) stay as characters. -
Class tokens: non-Chinese material is normalised inside the tokenizer to one token per item:
Token Replaces Example [URL]URLs https://example.com/a?b=1[EMAIL]e-mail addresses someone@example.com[LATIN]one Latin-script word, including words containing digits HSBC,COVID-19,A380[NUM]Arabic-digit numbers 2019,1,000.5,10:30[FOREIGN]runs of other scripts (Hangul, Cyrillic, Arabic, Thai, ...) 한국어 -
Normalisation: NFKC, so full-width forms fold to half-width (
,!?()ABC123→,!?()ABC123). CJK-only punctuation (。、「」《》) is unchanged. Whitespace is dropped. -
Embedding initialisation: the 28-layer body of ModernCantoneseBert-Large is kept. Each character's embedding is initialised as the mean of the BPE token embeddings its text mapped to; the tied MLM decoder and its bias are initialised the same way.
-
Continued pretraining: the whole model was then trained with MLM on packed 2048-token sequences.
| Parameters | 356,718,464 |
| Layers / hidden / heads | 28 / 1024 / 16 |
| Vocabulary | 12,160 (character-level) |
| 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 token-aligned tasks: named entity recognition, word segmentation, span extraction and other sequence labeling, where one label per character is needed
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.
- No downstream evaluation yet: Only MLM loss and accuracy have been measured. The model has not been fine-tuned or benchmarked on NER, classification or any other task.
- Class tokens hide the surface form: every Latin word is the same
[LATIN]token and every number the same[NUM], so the model cannot tellHSBCfromMTRor2019from2020. It is not suitable for English or code-switched content where the Latin words matter. Recover the original string from character offsets when you need it. - Tokenization is lossy: decoding token ids does not reproduce the input (whitespace is dropped, class tokens replace their text).
- Offsets of class tokens: the
offset_mappingof a class token covers only the last character it replaced, not the whole word or number. Offsets of ordinary characters are exact. - 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 a filtered subset of the ModernCantoneseBert-Large corpus: ~384k of the ~626k documents (311,221 packed 2048-token sequences, ~637M tokens per epoch; 20,000 sequences held out) from open source datasets and web-scraped content, including:
- Cantonese filtered Common Crawl
- Web scraped news and articles
Documents were dropped if more than 70% of their non-whitespace characters were Latin letters or digits, fewer than 15% were Han characters, or more than 10% were letters of other scripts. This kept 97% of the Han characters and raised their share of the text from 30% to 74%. Written Chinese in both Traditional and Simplified script is deliberately kept alongside Cantonese.
Due to respect for copyright, the training dataset will not be released.
Quick Start
Installation
pip install transformers torch
Trained and tested with Transformers 5.23.0. The tokenizer and config also load with Transformers 4.52; the model weights have not been run on 4.x.
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-Char"
)
mask_filler("雞蛋六隻,糖呢就兩茶匙,仲有[MASK]橙皮添。")
mask_filler("香港特首係李家[MASK]。") # -> 超 (0.96)
Each [MASK] stands for exactly one character (or one class token).
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("hon9kon9ize/ModernCantoneseBert-Large-Char")
tok.tokenize("我今日去咗HSBC攞咗3,000蚊。")
# ['我', '今', '日', '去', '咗', '[LATIN]', '攞', '咗', '[NUM]', '蚊', '。']
Training procedure
Trained for 3 h 37 min on 4 × NVIDIA H100 80GB (DDP). The first 400 of the 45,505 optimizer steps ran on a single 12 GB H100 MIG slice with the same effective batch size before the run moved to the 4-GPU node.
Training hyperparameters
The following hyperparameters were used during training:
- learning_rate: 5e-05
- train_batch_size: 8 per GPU × 4 GPUs
- eval_batch_size: 8
- seed: 42
- 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, torch.compile
Training results
Evaluation uses 500 held-out sequences with masking re-sampled each time, so repeated evaluations differ by about ±0.01.
| Training Loss | Epoch | Step | Validation Loss | Accuracy |
|---|---|---|---|---|
| 1.3127 | 0.2198 | 2000 | 1.2889 | 0.7260 |
| 1.2424 | 0.4395 | 4000 | 1.2126 | 0.7396 |
| 1.1748 | 0.6593 | 6000 | 1.1728 | 0.7460 |
| 1.1203 | 0.8790 | 8000 | 1.1420 | 0.7514 |
| 1.1267 | 1.0988 | 10000 | 1.1198 | 0.7550 |
| 1.1086 | 1.3185 | 12000 | 1.1010 | 0.7577 |
| 1.0904 | 1.5383 | 14000 | 1.0848 | 0.7602 |
| 1.0756 | 1.7580 | 16000 | 1.0803 | 0.7611 |
| 1.0760 | 1.9778 | 18000 | 1.0522 | 0.7653 |
| 1.0680 | 2.1976 | 20000 | 1.0469 | 0.7667 |
| 1.0381 | 2.4173 | 22000 | 1.0320 | 0.7699 |
| 1.0049 | 2.6371 | 24000 | 1.0322 | 0.7698 |
| 0.9945 | 2.8568 | 26000 | 1.0235 | 0.7705 |
| 1.0428 | 3.0766 | 28000 | 1.0035 | 0.7743 |
| 0.9982 | 3.2963 | 30000 | 1.0042 | 0.7743 |
| 0.9985 | 3.5161 | 32000 | 0.9992 | 0.7757 |
| 0.9874 | 3.7359 | 34000 | 0.9943 | 0.7758 |
| 1.0006 | 3.9556 | 36000 | 0.9828 | 0.7784 |
| 1.0222 | 4.1754 | 38000 | 0.9770 | 0.7799 |
| 0.9914 | 4.3951 | 40000 | 0.9730 | 0.7807 |
| 0.9901 | 4.6149 | 42000 | 0.9714 | 0.7798 |
| 0.9675 | 4.8346 | 44000 | 0.9833 | 0.7781 |
| - | 5.0 | 45505 | 0.9725 | 0.7807 |
Framework versions
- Transformers 5.23.0
- Pytorch 2.8.0+cu128
- Datasets 3.6.0
- Tokenizers 0.23.3