Commit
·
151b230
1
Parent(s):
4195ed1
initial release
Browse files- README.md +29 -0
- config.json +46 -0
- maker.py +99 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +37 -0
- tokenizer.json +0 -0
- tokenizer_config.json +64 -0
- vocab.txt +0 -0
README.md
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- "lzh"
|
4 |
+
tags:
|
5 |
+
- "classical chinese"
|
6 |
+
- "literary chinese"
|
7 |
+
- "ancient chinese"
|
8 |
+
- "masked-lm"
|
9 |
+
license: "apache-2.0"
|
10 |
+
pipeline_tag: "fill-mask"
|
11 |
+
mask_token: "[MASK]"
|
12 |
+
widget:
|
13 |
+
- text: "孟子[MASK]梁惠王"
|
14 |
+
---
|
15 |
+
|
16 |
+
# modernbert-small-classical-chinese-traditional
|
17 |
+
|
18 |
+
## Model Description
|
19 |
+
|
20 |
+
This is a ModernBERT model pre-trained on [Kanripo](https://www.kanripo.org) texts. NVIDIA A100-SXM4-40GB×8 took 7 hours 8 minutes for training. You can fine-tune `modernbert-small-classical-chinese-traditional` for downstream tasks, such as sentence-segmentation, POS-tagging, dependency-parsing, and so on.
|
21 |
+
|
22 |
+
## How to Use
|
23 |
+
|
24 |
+
```py
|
25 |
+
from transformers import AutoTokenizer,AutoModelForMaskedLM
|
26 |
+
tokenizer=AutoTokenizer.from_pretrained("KoichiYasuoka/modernbert-small-classical-chinese-traditional")
|
27 |
+
model=AutoModelForMaskedLM.from_pretrained("KoichiYasuoka/modernbert-small-classical-chinese-traditional")
|
28 |
+
```
|
29 |
+
|
config.json
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"ModernBertForMaskedLM"
|
4 |
+
],
|
5 |
+
"attention_bias": false,
|
6 |
+
"attention_dropout": 0.0,
|
7 |
+
"bos_token_id": 0,
|
8 |
+
"classifier_activation": "gelu",
|
9 |
+
"classifier_bias": false,
|
10 |
+
"classifier_dropout": 0.0,
|
11 |
+
"classifier_pooling": "mean",
|
12 |
+
"cls_token_id": 0,
|
13 |
+
"decoder_bias": true,
|
14 |
+
"deterministic_flash_attn": false,
|
15 |
+
"embedding_dropout": 0.0,
|
16 |
+
"eos_token_id": 2,
|
17 |
+
"global_attn_every_n_layers": 3,
|
18 |
+
"global_rope_theta": 160000.0,
|
19 |
+
"gradient_checkpointing": false,
|
20 |
+
"hidden_activation": "gelu",
|
21 |
+
"hidden_size": 256,
|
22 |
+
"initializer_cutoff_factor": 2.0,
|
23 |
+
"initializer_range": 0.02,
|
24 |
+
"intermediate_size": 768,
|
25 |
+
"layer_norm_eps": 1e-05,
|
26 |
+
"local_attention": 128,
|
27 |
+
"local_rope_theta": 10000.0,
|
28 |
+
"max_position_embeddings": 8192,
|
29 |
+
"mlp_bias": false,
|
30 |
+
"mlp_dropout": 0.0,
|
31 |
+
"model_type": "modernbert",
|
32 |
+
"norm_bias": false,
|
33 |
+
"norm_eps": 1e-05,
|
34 |
+
"num_attention_heads": 8,
|
35 |
+
"num_hidden_layers": 16,
|
36 |
+
"pad_token_id": 1,
|
37 |
+
"position_embedding_type": "absolute",
|
38 |
+
"reference_compile": true,
|
39 |
+
"sep_token_id": 2,
|
40 |
+
"sparse_pred_ignore_index": -100,
|
41 |
+
"sparse_prediction": false,
|
42 |
+
"tokenizer_class": "BertTokenizerFast",
|
43 |
+
"torch_dtype": "float32",
|
44 |
+
"transformers_version": "4.51.3",
|
45 |
+
"vocab_size": 23798
|
46 |
+
}
|
maker.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#! /usr/bin/python3
|
2 |
+
#pip3 install transformers accelerate deepspeed triton esupar
|
3 |
+
tgt="KoichiYasuoka/modernbert-small-classical-chinese-traditional"
|
4 |
+
import os
|
5 |
+
os.system("""
|
6 |
+
if test -d Kanripo-data
|
7 |
+
then :
|
8 |
+
else git clone --depth=1 https://github.com/DHSinology/Kanripo-data
|
9 |
+
fi""")
|
10 |
+
if not os.path.isfile("train.txt"):
|
11 |
+
import glob,subprocess
|
12 |
+
from esupar.tradify import tradify
|
13 |
+
with open("train.txt","w",encoding="utf-8") as w:
|
14 |
+
s='''
|
15 |
+
{
|
16 |
+
s=$0;
|
17 |
+
while(s>""){
|
18 |
+
if(f==1){
|
19 |
+
i=index(s,"</hi>");
|
20 |
+
if(i>0){
|
21 |
+
h=h substr(s,1,i-1)"\\n";
|
22 |
+
s=substr(s,i+5);
|
23 |
+
f=0;
|
24 |
+
}
|
25 |
+
else{
|
26 |
+
h=h s;
|
27 |
+
s="";
|
28 |
+
}
|
29 |
+
}
|
30 |
+
else{
|
31 |
+
i=index(s,"<hi>");
|
32 |
+
if(i>0){
|
33 |
+
t=substr(s,1,i-1);
|
34 |
+
gsub(/<[^>]+>/,"",t);
|
35 |
+
gsub(/([ -~]| |\\t)/,"",t);
|
36 |
+
printf("%s",t);
|
37 |
+
s=substr(s,i+4);
|
38 |
+
f=1;
|
39 |
+
}
|
40 |
+
else{
|
41 |
+
gsub(/<[^>]+>/,"",s);
|
42 |
+
gsub(/([ -~]| |\\t)/,"",s);
|
43 |
+
printf("%s",s);
|
44 |
+
s="";
|
45 |
+
}
|
46 |
+
}
|
47 |
+
}
|
48 |
+
printf("\\n");
|
49 |
+
}
|
50 |
+
END{
|
51 |
+
gsub(/<[^>]+>/,"",h);
|
52 |
+
gsub(/([ -~]| |\\t)/,"",h);
|
53 |
+
printf("%s\\n",h);
|
54 |
+
}'''
|
55 |
+
t=subprocess.run(["nawk",s]+glob.glob("Kanripo-data/data/*/*.xml"),stdout=subprocess.PIPE,encoding="utf-8").stdout
|
56 |
+
u=""
|
57 |
+
for s in t.split("\n"):
|
58 |
+
s=s.strip()
|
59 |
+
if len(s)+len(u)>=8200:
|
60 |
+
print(u,file=w)
|
61 |
+
u=""
|
62 |
+
for c in s:
|
63 |
+
u+=tradify[c] if c in tradify else c
|
64 |
+
print(u,file=w)
|
65 |
+
|
66 |
+
from transformers import BertTokenizerFast
|
67 |
+
from tokenizers.pre_tokenizers import Sequence,Whitespace,Split
|
68 |
+
from tokenizers import Regex
|
69 |
+
s=["[CLS]","[PAD]","[SEP]","[UNK]","[MASK]"]
|
70 |
+
if not os.path.isfile("vocab.txt"):
|
71 |
+
with open("train.txt","r",encoding="utf-8") as r:
|
72 |
+
v=set(c for c in r.read() if not c.isspace())
|
73 |
+
with open("vocab.txt","w",encoding="utf-8") as w:
|
74 |
+
print("\n".join(s+sorted(v)),file=w)
|
75 |
+
tkz=BertTokenizerFast(vocab_file="vocab.txt",never_split=s,do_lower_case=False,strip_accents=False,tokenize_chinese_chars=True)
|
76 |
+
tkz.backend_tokenizer.pre_tokenizer=Sequence([Whitespace(),Split(Regex("."),"isolated")])
|
77 |
+
tkz.backend_tokenizer.decoder.prefix=tkz.backend_tokenizer.model.continuing_subword_prefix=""
|
78 |
+
tkz.save_pretrained(tgt)
|
79 |
+
|
80 |
+
with open("train.py","w",encoding="utf-8") as w:
|
81 |
+
print(f'#! /usr/bin/env deepspeed\ntgt="{tgt}"'+'''
|
82 |
+
from transformers import BertTokenizerFast,ModernBertForMaskedLM,AutoConfig,DataCollatorForLanguageModeling,TrainingArguments,Trainer
|
83 |
+
tkz=BertTokenizerFast.from_pretrained(tgt)
|
84 |
+
c={"trust_remote_code":True,"vocab_size":len(tkz),"tokenizer_class":type(tkz).__name__}
|
85 |
+
for k,v in tkz.special_tokens_map.items():
|
86 |
+
c[k+"_id"]=tkz.convert_tokens_to_ids(v)
|
87 |
+
cfg=AutoConfig.from_pretrained("KoichiYasuoka/modernbert-small-japanese-char",**c)
|
88 |
+
arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=1,output_dir=tgt,overwrite_output_dir=True,save_total_limit=2,save_safetensors=False)
|
89 |
+
class ReadLineDS(object):
|
90 |
+
def __init__(self,file,tokenizer):
|
91 |
+
self.tokenizer=tokenizer
|
92 |
+
with open(file,"r",encoding="utf-8") as r:
|
93 |
+
self.lines=[s.strip() for s in r if s.strip()>""]
|
94 |
+
__len__=lambda self:len(self.lines)
|
95 |
+
__getitem__=lambda self,i:self.tokenizer(self.lines[i],truncation=True,add_special_tokens=True,max_length=8190)
|
96 |
+
trn=Trainer(args=arg,data_collator=DataCollatorForLanguageModeling(tkz),model=ModernBertForMaskedLM(cfg),train_dataset=ReadLineDS("train.txt",tkz))
|
97 |
+
trn.train()
|
98 |
+
trn.save_model(tgt)''',file=w)
|
99 |
+
os.system("chmod 755 train.py ; ./train.py")
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1214014c0405b087cf1b82910574c09b9fcacb1d1a0347c0a0ad8648ddee30cb
|
3 |
+
size 79320542
|
special_tokens_map.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": {
|
3 |
+
"content": "[CLS]",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"mask_token": {
|
10 |
+
"content": "[MASK]",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"pad_token": {
|
17 |
+
"content": "[PAD]",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"sep_token": {
|
24 |
+
"content": "[SEP]",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
},
|
30 |
+
"unk_token": {
|
31 |
+
"content": "[UNK]",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": false,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false
|
36 |
+
}
|
37 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[CLS]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"1": {
|
12 |
+
"content": "[PAD]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"2": {
|
20 |
+
"content": "[SEP]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"3": {
|
28 |
+
"content": "[UNK]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"4": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"clean_up_tokenization_spaces": true,
|
45 |
+
"cls_token": "[CLS]",
|
46 |
+
"do_basic_tokenize": true,
|
47 |
+
"do_lower_case": false,
|
48 |
+
"extra_special_tokens": {},
|
49 |
+
"mask_token": "[MASK]",
|
50 |
+
"model_max_length": 1000000000000000019884624838656,
|
51 |
+
"never_split": [
|
52 |
+
"[CLS]",
|
53 |
+
"[PAD]",
|
54 |
+
"[SEP]",
|
55 |
+
"[UNK]",
|
56 |
+
"[MASK]"
|
57 |
+
],
|
58 |
+
"pad_token": "[PAD]",
|
59 |
+
"sep_token": "[SEP]",
|
60 |
+
"strip_accents": false,
|
61 |
+
"tokenize_chinese_chars": true,
|
62 |
+
"tokenizer_class": "BertTokenizerFast",
|
63 |
+
"unk_token": "[UNK]"
|
64 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|