KoichiYasuoka commited on
Commit
93d929a
1 Parent(s): b0fa4fc

initial release

Browse files
Files changed (9) hide show
  1. README.md +30 -0
  2. config.json +160 -0
  3. maker.py +71 -0
  4. pytorch_model.bin +3 -0
  5. special_tokens_map.json +44 -0
  6. tokenizer.json +0 -0
  7. tokenizer.model +3 -0
  8. tokenizer_config.json +44 -0
  9. upos.py +41 -0
README.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "ja"
4
+ tags:
5
+ - "japanese"
6
+ - "token-classification"
7
+ - "pos"
8
+ base_model: lightblue/karasu-1.1B
9
+ datasets:
10
+ - "universal_dependencies"
11
+ license: "apache-2.0"
12
+ pipeline_tag: "token-classification"
13
+ widget:
14
+ - text: "国境の長いトンネルを抜けると雪国であった。"
15
+ ---
16
+
17
+ # KoichiYasuoka/karasu-1.1B-upos
18
+
19
+ ## Model Description
20
+
21
+ This is a LLaMA model for POS-tagging, derived from [karasu-1.1B](https://huggingface.co/lightblue/karasu-1.1B). Every short-unit-word is tagged by [UPOS](https://universaldependencies.org/u/pos/) (Universal Part-Of-Speech) and [FEATS](https://universaldependencies.org/u/feat/).
22
+
23
+ ## How to Use
24
+
25
+ ```py
26
+ from transformers import pipeline
27
+ nlp=pipeline("upos","KoichiYasuoka/karasu-1.1B-upos",trust_remote_code=True,aggregation_strategy="simple")
28
+ print(nlp("国境の長いトンネルを抜けると雪国であった。"))
29
+ ```
30
+
config.json ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "LlamaForTokenClassification"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 1,
8
+ "custom_pipelines": {
9
+ "upos": {
10
+ "impl": "upos.BellmanFordTokenClassificationPipeline",
11
+ "pt": "AutoModelForTokenClassification"
12
+ }
13
+ },
14
+ "eos_token_id": 2,
15
+ "hidden_act": "silu",
16
+ "hidden_size": 2048,
17
+ "id2label": {
18
+ "0": "ADJ",
19
+ "1": "B-ADJ",
20
+ "2": "I-ADJ",
21
+ "3": "ADJ|Polarity=Neg",
22
+ "4": "B-ADJ|Polarity=Neg",
23
+ "5": "I-ADJ|Polarity=Neg",
24
+ "6": "ADP",
25
+ "7": "B-ADP",
26
+ "8": "I-ADP",
27
+ "9": "ADV",
28
+ "10": "B-ADV",
29
+ "11": "I-ADV",
30
+ "12": "AUX",
31
+ "13": "B-AUX",
32
+ "14": "I-AUX",
33
+ "15": "AUX|Polarity=Neg",
34
+ "16": "B-AUX|Polarity=Neg",
35
+ "17": "I-AUX|Polarity=Neg",
36
+ "18": "CCONJ",
37
+ "19": "B-CCONJ",
38
+ "20": "I-CCONJ",
39
+ "21": "DET",
40
+ "22": "B-DET",
41
+ "23": "I-DET",
42
+ "24": "INTJ",
43
+ "25": "B-INTJ",
44
+ "26": "I-INTJ",
45
+ "27": "NOUN",
46
+ "28": "B-NOUN",
47
+ "29": "I-NOUN",
48
+ "30": "NOUN|Polarity=Neg",
49
+ "31": "B-NOUN|Polarity=Neg",
50
+ "32": "I-NOUN|Polarity=Neg",
51
+ "33": "NUM",
52
+ "34": "B-NUM",
53
+ "35": "I-NUM",
54
+ "36": "PART",
55
+ "37": "B-PART",
56
+ "38": "I-PART",
57
+ "39": "PRON",
58
+ "40": "B-PRON",
59
+ "41": "I-PRON",
60
+ "42": "PROPN",
61
+ "43": "B-PROPN",
62
+ "44": "I-PROPN",
63
+ "45": "PUNCT",
64
+ "46": "B-PUNCT",
65
+ "47": "I-PUNCT",
66
+ "48": "SCONJ",
67
+ "49": "B-SCONJ",
68
+ "50": "I-SCONJ",
69
+ "51": "SYM",
70
+ "52": "B-SYM",
71
+ "53": "I-SYM",
72
+ "54": "VERB",
73
+ "55": "B-VERB",
74
+ "56": "I-VERB",
75
+ "57": "X",
76
+ "58": "B-X",
77
+ "59": "I-X"
78
+ },
79
+ "initializer_range": 0.02,
80
+ "intermediate_size": 5632,
81
+ "label2id": {
82
+ "ADJ": 0,
83
+ "ADJ|Polarity=Neg": 3,
84
+ "ADP": 6,
85
+ "ADV": 9,
86
+ "AUX": 12,
87
+ "AUX|Polarity=Neg": 15,
88
+ "B-ADJ": 1,
89
+ "B-ADJ|Polarity=Neg": 4,
90
+ "B-ADP": 7,
91
+ "B-ADV": 10,
92
+ "B-AUX": 13,
93
+ "B-AUX|Polarity=Neg": 16,
94
+ "B-CCONJ": 19,
95
+ "B-DET": 22,
96
+ "B-INTJ": 25,
97
+ "B-NOUN": 28,
98
+ "B-NOUN|Polarity=Neg": 31,
99
+ "B-NUM": 34,
100
+ "B-PART": 37,
101
+ "B-PRON": 40,
102
+ "B-PROPN": 43,
103
+ "B-PUNCT": 46,
104
+ "B-SCONJ": 49,
105
+ "B-SYM": 52,
106
+ "B-VERB": 55,
107
+ "B-X": 58,
108
+ "CCONJ": 18,
109
+ "DET": 21,
110
+ "I-ADJ": 2,
111
+ "I-ADJ|Polarity=Neg": 5,
112
+ "I-ADP": 8,
113
+ "I-ADV": 11,
114
+ "I-AUX": 14,
115
+ "I-AUX|Polarity=Neg": 17,
116
+ "I-CCONJ": 20,
117
+ "I-DET": 23,
118
+ "I-INTJ": 26,
119
+ "I-NOUN": 29,
120
+ "I-NOUN|Polarity=Neg": 32,
121
+ "I-NUM": 35,
122
+ "I-PART": 38,
123
+ "I-PRON": 41,
124
+ "I-PROPN": 44,
125
+ "I-PUNCT": 47,
126
+ "I-SCONJ": 50,
127
+ "I-SYM": 53,
128
+ "I-VERB": 56,
129
+ "I-X": 59,
130
+ "INTJ": 24,
131
+ "NOUN": 27,
132
+ "NOUN|Polarity=Neg": 30,
133
+ "NUM": 33,
134
+ "PART": 36,
135
+ "PRON": 39,
136
+ "PROPN": 42,
137
+ "PUNCT": 45,
138
+ "SCONJ": 48,
139
+ "SYM": 51,
140
+ "VERB": 54,
141
+ "X": 57
142
+ },
143
+ "max_position_embeddings": 2048,
144
+ "mlp_bias": false,
145
+ "model_type": "llama",
146
+ "num_attention_heads": 32,
147
+ "num_hidden_layers": 22,
148
+ "num_key_value_heads": 4,
149
+ "pad_token_id": 0,
150
+ "pretraining_tp": 1,
151
+ "rms_norm_eps": 1e-05,
152
+ "rope_scaling": null,
153
+ "rope_theta": 10000.0,
154
+ "tie_word_embeddings": false,
155
+ "tokenizer_class": "LlamaTokenizerFast",
156
+ "torch_dtype": "float32",
157
+ "transformers_version": "4.42.4",
158
+ "use_cache": true,
159
+ "vocab_size": 32000
160
+ }
maker.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/python3
2
+ src="lightblue/karasu-1.1B"
3
+ tgt="KoichiYasuoka/karasu-1.1B-upos"
4
+
5
+ import os
6
+ from transformers import AutoTokenizer,AutoConfig,LlamaForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
7
+ from tokenizers.normalizers import Replace
8
+ os.system("test -f ja_gsd_modern.conllu || curl -LO https://github.com/KoichiYasuoka/SuPar-UniDic/raw/main/suparunidic/suparmodels/ja_gsd_modern.conllu")
9
+
10
+ class UPOSFileDataset(object):
11
+ def __init__(self,conllu,tokenizer):
12
+ self.conllu=open(conllu,"r",encoding="utf-8")
13
+ self.tokenizer=tokenizer
14
+ self.seeks=[0]
15
+ label=set(["SYM"])
16
+ s=self.conllu.readline()
17
+ while s!="":
18
+ if s=="\n":
19
+ self.seeks.append(self.conllu.tell())
20
+ else:
21
+ w=s.split("\t")
22
+ if len(w)==10:
23
+ if w[0].isdecimal():
24
+ label.add(w[3] if w[5]=="_" else w[3]+"|"+w[5])
25
+ s=self.conllu.readline()
26
+ lid={}
27
+ for i,l in enumerate(sorted(label)):
28
+ lid[l],lid["B-"+l],lid["I-"+l]=i*3,i*3+1,i*3+2
29
+ self.label2id=lid
30
+ def __call__(*args):
31
+ lid={l:i for i,l in enumerate(sorted(set(sum([list(t.label2id) for t in args],[]))))}
32
+ for t in args:
33
+ t.label2id=lid
34
+ return lid
35
+ def __del__(self):
36
+ self.conllu.close()
37
+ __len__=lambda self:len(self.seeks)-1
38
+ def __getitem__(self,i):
39
+ self.conllu.seek(self.seeks[i])
40
+ form,upos,sp=[],[],False
41
+ while self.conllu.tell()<self.seeks[i+1]:
42
+ w=self.conllu.readline().split("\t")
43
+ if len(w)==10:
44
+ form.append(" "+w[1] if sp else w[1])
45
+ if w[0].isdecimal():
46
+ upos.append(w[3] if w[5]=="_" else w[3]+"|"+w[5])
47
+ sp=w[9].find("SpaceAfter=No")<0
48
+ v=self.tokenizer(form,add_special_tokens=False)
49
+ i,u=[self.tokenizer.cls_token_id],["SYM"]
50
+ for j,(x,y) in enumerate(zip(v["input_ids"],upos)):
51
+ if x!=[]:
52
+ i+=x
53
+ u+=[y] if len(x)==1 else ["B-"+y]+["I-"+y]*(len(x)-1)
54
+ if len(i)<self.tokenizer.model_max_length-3:
55
+ ids=i
56
+ upos=u
57
+ else:
58
+ ids=i[0:self.tokenizer.model_max_length-2]
59
+ upos=u[0:self.tokenizer.model_max_length-2]
60
+ return {"input_ids":ids,"labels":[self.label2id[t] for t in upos]}
61
+
62
+ tkz=AutoTokenizer.from_pretrained(src,cls_token="<s>",sep_token="<s>",pad_token="</s>",model_max_length=2048)
63
+ tkz.backend_tokenizer.normalizer=Replace(" ","\u2581")
64
+ trainDS=UPOSFileDataset("ja_gsd_modern.conllu",tkz)
65
+ lid=trainDS.label2id
66
+ cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()},ignore_mismatched_sizes=True)
67
+ arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=32,output_dir=tgt,overwrite_output_dir=True,save_total_limit=2,learning_rate=5e-05,warmup_ratio=0.1,save_safetensors=False)
68
+ trn=Trainer(args=arg,data_collator=DataCollatorForTokenClassification(tkz),model=LlamaForTokenClassification.from_pretrained(src,config=cfg,ignore_mismatched_sizes=True),train_dataset=trainDS)
69
+ trn.train()
70
+ trn.save_model(tgt)
71
+ tkz.save_pretrained(tgt)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:22fb62f88c2c6d54a7e48354a75f5ae861a0d624360cb996f35606bc4dc01ea2
3
+ size 4138609458
special_tokens_map.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "cls_token": {
10
+ "content": "<s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "eos_token": {
17
+ "content": "</s>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "pad_token": {
24
+ "content": "</s>",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "sep_token": {
31
+ "content": "<s>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ },
37
+ "unk_token": {
38
+ "content": "<unk>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false
43
+ }
44
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
3
+ size 499723
tokenizer_config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": null,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": false,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ }
30
+ },
31
+ "bos_token": "<s>",
32
+ "clean_up_tokenization_spaces": false,
33
+ "cls_token": "<s>",
34
+ "eos_token": "</s>",
35
+ "legacy": false,
36
+ "model_max_length": 2048,
37
+ "pad_token": "</s>",
38
+ "padding_side": "right",
39
+ "sep_token": "<s>",
40
+ "sp_model_kwargs": {},
41
+ "tokenizer_class": "LlamaTokenizerFast",
42
+ "unk_token": "<unk>",
43
+ "use_default_system_prompt": false
44
+ }
upos.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TokenClassificationPipeline
2
+
3
+ class BellmanFordTokenClassificationPipeline(TokenClassificationPipeline):
4
+ def __init__(self,**kwargs):
5
+ import numpy
6
+ super().__init__(**kwargs)
7
+ x=self.model.config.label2id
8
+ y=[k for k in x if not k.startswith("I-")]
9
+ self.transition=numpy.full((len(x),len(x)),numpy.nan)
10
+ for k,v in x.items():
11
+ for j in ["I-"+k[2:]] if k.startswith("B-") else [k]+y if k.startswith("I-") else y:
12
+ self.transition[v,x[j]]=0
13
+ def check_model_type(self,supported_models):
14
+ pass
15
+ def postprocess(self,model_outputs,**kwargs):
16
+ import numpy
17
+ if "logits" not in model_outputs:
18
+ return self.postprocess(model_outputs[0],**kwargs)
19
+ m=model_outputs["logits"][0].numpy()
20
+ e=numpy.exp(m-numpy.max(m,axis=-1,keepdims=True))
21
+ z=e/e.sum(axis=-1,keepdims=True)
22
+ for i in range(m.shape[0]-1,0,-1):
23
+ m[i-1]+=numpy.nanmax(m[i]+self.transition,axis=1)
24
+ k=[numpy.nanargmax(m[0]+self.transition[0])]
25
+ for i in range(1,m.shape[0]):
26
+ k.append(numpy.nanargmax(m[i]+self.transition[k[-1]]))
27
+ w=[{"entity":self.model.config.id2label[j],"start":s,"end":e,"score":z[i,j]} for i,((s,e),j) in enumerate(zip(model_outputs["offset_mapping"][0].tolist(),k)) if s<e]
28
+ if "aggregation_strategy" in kwargs and kwargs["aggregation_strategy"]!="none":
29
+ for i,t in reversed(list(enumerate(w))):
30
+ p=t.pop("entity")
31
+ if p.startswith("I-"):
32
+ w[i-1]["score"]=min(w[i-1]["score"],t["score"])
33
+ w[i-1]["end"]=w.pop(i)["end"]
34
+ elif p.startswith("B-"):
35
+ t["entity_group"]=p[2:]
36
+ else:
37
+ t["entity_group"]=p
38
+ for t in w:
39
+ t["text"]=model_outputs["sentence"][t["start"]:t["end"]]
40
+ return w
41
+