-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.py
1073 lines (811 loc) · 36.2 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
import time
from dataclasses import dataclass, field
from timeit import repeat
from typing import Optional,Dict,Union,Any,Tuple,List
import fitlog
import nltk
import numpy as np
import datasets
import torch
import torch.nn as nn
import torch.distributed as dist
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
import transformers
from transformers import (
DataCollatorForLanguageModeling,
DataCollatorForSeq2Seq,
AutoConfig,
AutoTokenizer,
HfArgumentParser,
TrainingArguments,
Seq2SeqTrainingArguments,
set_seed,
)
from transformers import Trainer, Seq2SeqTrainer
from transformers import TrainingArguments
from transformers import trainer_utils, training_args
from transformers.trainer_pt_utils import nested_detach
from transformers import BertForMaskedLM
from transformers.file_utils import PaddingStrategy
from transformers.modeling_utils import PreTrainedModel
from transformers.tokenization_utils_base import BatchEncoding, PreTrainedTokenizerBase
from data.DatasetLoadingHelper import (
load_ctc2021,
load_ctc2021_mT5,
load_sighan,
load_sighan_enchanted,
load_sighan_gector,
load_sighan_mask,
load_sighan_expand,
load_lattice_sighan,
load_abs_pos_sighan,
load_abs_pos_sighan_lang8,
load_abs_pos_sighan_plus,
load_abs_pos_and_spe_token_sighan,
load_sighan13_test,
load_sighan14_test,
load_sighan15_test,
load_sighan_chinesebert,
load_sighan_chinesebert_mask,
load_sighan_chinesebert_holy,
load_sighan_holy,
load_sighan_holy_mask,
load_sighan_pure,
)
def ddp_exec(command):
"""
"""
if os.environ["LOCAL_RANK"] != '0':
return
else:
exec(command)
def ddp_print(*something):
"""
out of time
"""
if os.environ["LOCAL_RANK"] != '0':
return
else:
for thing in something:
print(thing)
return
def fitlogging(training_args):
for attr in dir(training_args):
if not re.match("__.*__", attr) and isinstance(getattr(training_args, attr), (int, str, bool, float)):
fitlog.add_hyper(value=getattr(training_args, attr), name=attr)
return
@dataclass
class MySeq2SeqTrainingArguments(Seq2SeqTrainingArguments):
# hack for bug
disable_tqdm: Optional[bool] = field(
default=None, metadata={"help": "Whether or not to disable the tqdm progress bars."}
)
load_best_model_at_end: Optional[bool] = field(
default=False,
metadata={"help": "Whether or not to load the best model found during training at the end of training."},
)
# extra args
model_name: str=field(default="MaskedLM", metadata={"help":"which bert model "})
dataset: str = field(default="sighan", metadata={"help":"dataset"})
eval_dataset:str = field(default="15", metadata={"help":"dataset for eval"})
max_length: int = field(default=128, metadata={"help": "max length"})
num_beams: int = field(default=4, metadata={"help": "num beams"})
use_extra_dataset:bool = field(default=False, metadata={"help":"Only work for ctc2021, using larger v2"})
fix_cls:bool = field(default=False, metadata={"help":"whether or not fix the cls layer of BertMaskedLM"})
multi_task:bool = field(default=False, metadata={"help":"whether or not use multi-task loss"})
cl_weight:float = field(default=0.2, metadata={"help": "contrastive learning loss weight"})
repeat_weight:float = field(default=0.2, metadata={"help": "distill repeat loss"})
copy_weight:float = field(default=0.5, metadata={"help":"copy weight"})
multi_task_weight:float = field(default=0.05, metadata={"help":"multi task loss weight"})
num_gpus:int = field(default=4, metadata={"help":"num_gpus"})
pretrained_name:str = field(default="roberta", metadata={"help":"pretrained_name"})
log_path:str = field(default="Recent_train.log", metadata={"help":"log path or name"})
class mydataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
def argument_init(trainingarguments=Seq2SeqTrainingArguments):
"""
"""
parser = HfArgumentParser(trainingarguments)
training_args = parser.parse_args_into_dataclasses()[0]
return training_args
def get_model(model_name="MaskedLM", pretrained_model_name_or_path="hfl/chinese-roberta-wwm-ext", training_args=None):
"""
Just get model
MLP:
bert->mlp->loss
Dot:
bert->dot product with embeddings->loss
MaskedLM_v2:
lexcions ( flat
CL:
Model with Contrastive Learning Loss
MaskedLM:
bert->lmhead->loss
T5:
generation
Proto:
Prototype
"""
model = None
print("[Core] Hint: Loading Model " + "*"*5 + model_name + "*"*5)
if model_name == "MLP":
from models.bert.modeling_bert_v3 import BertModelForCSC as ProtoModel
elif model_name == "Dot":
from models.bert.modeling_bert_v3 import BetterBertModelForCSC as ProtoModel
elif model_name == "MaskedLM_v2":
from models.bert.modeling_bert_v3 import BertForMaskedLM_v2 as ProtoModel
elif model_name == "CL":
from models.bert.modeling_bert_v4 import BertForMaskedLM_CL as ProtoModel
elif model_name == "CPT_NLG":
from models.bart.modeling_bart_v2 import BartForConditionalGeneration as ProtoModel
pretrained_model_name_or_path="fnlp/cpt-base" # '/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/models/bart/bart-zh/arch12-2-new-iter8w'
#pretrained_model_name_or_path = '/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/models/bart/bart-zh/arch12-2-new-iter8w'
elif model_name == "CPT_NLU":
from models.bart.modeling_bart_v2 import BartForMaskedLM as ProtoModel
pretrained_model_name_or_path="fnlp/cpt-large" # '/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/models/bart/bart-zh/arch12-2-new-iter8w'
elif model_name == "BART-base":
from models.bart.modeling_bart_v2 import BartForConditionalGeneration as ProtoModel
pretrained_model_name_or_path="fnlp/bart-base-chinese"# '/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/models/bart/bart-zh/arch12-2-new-iter8w'
elif model_name == "BART-large":
from models.bart.modeling_bart_v2 import BartForConditionalGeneration as ProtoModel
pretrained_model_name_or_path="fnlp/bart-large-chinese"# '/remote-home/xtzhang/CTC/CTC2021/SpecialEdition/models/bart/bart-zh/arch12-2-new-iter8w'
elif model_name == "T5-base":
print("[Core] Warning: T5-base is not implemented yet")
exit()
from transformers import T5ForConditionalGeneration as ProtoModel
pretrained_model_name_or_path="uer/t5-base-chinese-cluecorpussmall"
elif model_name.find("mt5") != -1:
from transformers import AutoModelForSeq2SeqLM as ProtoModel
pretrained_model_name_or_path = "google/" + model_name
elif model_name == "Proto":
if training_args.copy_weight == 0:
print("[Core] Hint: Load Proto self-Distill Contrastive Bert (NAACL2022)")
from models.bert.modeling_bert_v4 import ProtoModel_v3 as ProtoModel
else:
print("[Core] Hint: Load Proto COCO-LM (NIPS2022)")
from models.bert.modeling_bert_v4 import ProtoModel_copy as ProtoModel
elif model_name == "Gector":
from models.bert.modeling_bert_v3 import GectorModel as ProtoModel
elif model_name == "GPT":
from transformers import GPT2LMHeadModel as ProtoModel
elif model_name == "mBART-50":
print("[Core] Warning: mBART-50 is too large to train even in GTX3090[24G] (ctc task seq2seq 30w limit batch_size 16 cost 30+ hours)!")
print("[Core] There is no base : https://github.com/facebookresearch/fairseq/issues/3252")
exit()
from transformers import MBartForConditionalGeneration as ProtoModel
pretrained_model_name_or_path = "facebook/mbart-large-50"
elif model_name is None or model_name == "MaskedLM":
if training_args.pretrained_name == "chinesebert":
print("[Core] Hint: Load ChineseBert MaskedLM")
from chinesebert import ChineseBertConfig, ChineseBertForMaskedLM
config = ChineseBertConfig.from_pretrained(pretrained_model_name_or_path)
model = ChineseBertForMaskedLM.from_pretrained(pretrained_model_name_or_path, config=config)
return model
elif training_args.pretrained_name == "roformer":
from roformer import RoFormerForMaskedLM
model = RoFormerForMaskedLM.from_pretrained( pretrained_model_name_or_path )
return model
print("[Core] Hint: Load Default BertForMaskedLM.")
from transformers import BertForMaskedLM as ProtoModel
else:
print("[Core] Error: No such " + model_name)
exit(0)
print("[Core] MODEL_NAME:", pretrained_model_name_or_path)
if model_name != "Proto":
model = ProtoModel.from_pretrained(pretrained_model_name_or_path=pretrained_model_name_or_path)
else:
#model = ProtoModel.from_pretrained(pretrained_model_name_or_path=pretrained_model_name_or_path)
model = ProtoModel(
pretrained_model_name_or_path=pretrained_model_name_or_path,
training_args=training_args,
)
if not model:
print("[Core] Warning: wrong model name ! Check the core.py ")
exit()
return model
def get_dataset(training_args, path_head):
"""
Deprecation Warning !
preprocess wrapped in load_ctc2021
return : mydataset
torch.LongTensor
Good day!
"""
print("[Core] Loading Dataset !")
exec("os.system('date')")
if training_args.dataset == "ctc2021":
train_data, eval_data, test_data = load_ctc2021(args=training_args)
elif training_args.dataset == "sighan":
train_data, eval_data, test_data = load_sighan(path_head=path_head)
else:
print("[Core] Error: No such dataset ")
print(training_args.dataset)
exit(0)
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def get_dataset_plus(training_args):
"""
preprocess wrapped in load_ctc2021
return : mydate
torch.LongTensor
Good day!
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if training_args.dataset == "ctc2021":
if training_args.model_name.find("mT5") != -1:
train_data, eval_data, test_data = load_ctc2021_mT5(args=training_args)
else:
train_data, eval_data, test_data = load_ctc2021(args=training_args)
elif "sighan" in training_args.dataset:
#train_data, eval_data, test_data = load_sighan(path_head)
if training_args.model_name == "Gector":
return _get_Gector_dataset(args=training_args)
if training_args.pretrained_name == "chinesebert":
if "mask" in training_args.dataset:
return _get_chinesebert_mask_dataset(args=training_args)
elif "holy" in training_args.dataset:
return _get_chinesebert_holy_dataset(args=training_args)
else:
return _get_chinesebert_dataset(args=training_args)
elif "holy" in training_args.dataset:
if "mask" in training_args.dataset:
return _get_holy_mask_dataset(args=training_args)
else:
return _get_holy_dataset(args=training_args)
elif "mask" in training_args.dataset:
return _get_mask_dataset(args=training_args)
elif "holy" in training_args.dataset:
return _get_holy_dataset(args=training_args)
elif "enchanted" in training_args.dataset:
return _get_enchanted_dataset(args=training_args)
elif "raw" in training_args.dataset:
return _get_raw_dataset(args=training_args)
elif 'ReaLiSe' in training_args.dataset:
if training_args.multi_task:
print("[Core] Hint: Using Multi-Task Training")
return _get_ReaLiSe_multi_task_dataset(args=training_args)
else:
return _get_ReaLiSe_dataset(args=training_args)
elif 'expand' in training_args.dataset:
return _get_expand_dataset(args=training_args)
elif 'pure' in training_args.dataset:
return _get_pure_dataset(args=training_args)
else:
print("[Core] Unclear data type, load default raw sighan")
return _get_raw_dataset(args=training_args)
else:
print("[Core] Error: No such dataset ")
print(training_args.dataset)
exit(0)
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_enchanted_dataset(args, which="15"):
"""
Gector for sighan
"""
print("[Core] Loading Enchanted Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_enchanted(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_raw_dataset(args, which="15"):
"""
Gector for sighan
"""
print("[Core] Loading Raw Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_pure_dataset(args, which="15"):
"""
Gector for sighan
"""
print("[Core] Loading Pure SIGHAN Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_pure(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_holy_dataset(args, which="15"):
"""
Holy for sighan
"""
print("[Core] Loading Holy Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_holy(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_holy_mask_dataset(args, which="15"):
"""
Holy Mask for sighan
"""
print("[Core] Loading Holy Mask Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_holy_mask(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_mask_dataset(args, which="15"):
"""
Gector for sighan
"""
print("[Core] Loading MASK Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_mask(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_Gector_dataset(args, which="15"):
"""
Gector for sighan
"""
print("[Core] Loading GECTOR Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_gector(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_chinesebert_holy_dataset(args, which="15"):
"""
ChineseBert for sighan
Mainly diff in no max_length and 'pinyin_idx' must be 8 * len('input_ids')
"""
print("[Core] Loading ChineseBert Holy Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_chinesebert_holy(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_chinesebert_dataset(args, which="15"):
"""
ChineseBert for sighan
Mainly diff in no max_length and 'pinyin_idx' must be 8 * len('input_ids')
"""
print("[Core] Loading ChineseBert Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_chinesebert(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_chinesebert_mask_dataset(args, which="15"):
"""
ChineseBert for sighan
Mainly diff in no max_length and 'pinyin_idx' must be 8 * len('input_ids')
"""
print("[Core] Loading Masked ChineseBert Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_chinesebert_mask(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_ReaLiSe_dataset(args, which="15"):
"""
For its
"""
print("[Core] Loading ReaLiSe Dataset !")
print("[Core] Hint: The Data You Loading now is the preprocessed sighan from ReaLise, ")
ddp_exec("os.system('date')")
path = "../SE_tmp_back/milestone/ReaLiSe/data/"
import pickle
train_dataset = pickle.load(open(path + "trainall.times2.pkl", "rb"))
eval_dataset = pickle.load(open(path + "test.sighan" + args.eval_dataset + ".pkl", "rb"))
test_dataset = pickle.load(open(path + "test.sighan" + args.eval_dataset + ".pkl", "rb"))
print("[Core] Hint: Using **SIGHAN" + which + "** for eval & test !")
def trans2mydataset(features):
new = []
for feature in features:
tmp = {}
tmp["input_ids"] = feature["src_idx"][:128]
tmp["labels"] = feature["tgt_idx"][:128]
tmp["attention_mask"] = ([1] * len(tmp["input_ids"]))[:128]#feature["lengths"])[:128]
new.append(tmp)
return mydataset(new)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return trans2mydataset(train_dataset), trans2mydataset(eval_dataset), trans2mydataset(test_dataset)
def _get_ReaLiSe_multi_task_dataset(args, which="15"):
"""
For its
"""
print("[Core] Loading ReaLiSe with Multi-Task Dataset !")
print("[Core] Hint: The Data You Loading now is the Multi-Task preprocessed sighan from ReaLise, ")
#ddp_exec("os.system('date')")
path = "./data/rawdata/sighan/realise/data/"
import pickle
train_dataset = pickle.load(open(path + "trainall.times2_plus_1.pkl", "rb"))
eval_dataset = pickle.load(open(path + "test.sighan" + which + "_plus_1.pkl", "rb"))
test_dataset = pickle.load(open(path + "test.sighan" + which + "_plus_1.pkl", "rb"))
print("[Core] Hint: Using **SIGHAN" + which + "** for eval & test !")
def trans2mydataset(features):
new = []
for feature in features:
tmp = {}
tmp["input_ids"] = feature["src_idx"][:128]
tmp["labels"] = feature["tgt_idx"][:128]
tmp["seg_labels"] = feature["seg"][:128]
tmp["pos_labels"] = feature["pos"][:128]
tmp["idx_labels"] = feature["error"][:128]
if "error_type" not in feature.keys():
print(feature.keys())
exit()
tmp["error_labels"] = feature["error_type"][:128]
tmp["attention_mask"] = ([1] * len(tmp["input_ids"]))[:128]#feature["lengths"])[:128]
new.append(tmp)
return mydataset(new)
print("[Core] Loading successfully !")
#ddp_exec("os.system('date')")
return trans2mydataset(train_dataset), trans2mydataset(eval_dataset), trans2mydataset(test_dataset)
def _get_expand_dataset(args, which="15"):
"""
NLPcc and HSK Expand for sighan
"""
print("[Core] Loading Expand Dataset !")
ddp_exec("os.system('date')")
train_data, eval_data, test_data = load_sighan_expand(path_head="")
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading successfully !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_sighan_test(args, which, path_head=""):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if which == "13":
test_data = load_sighan13_test(path_head)
elif which == "14":
test_data = load_sighan14_test(path_head)
elif which == "15":
test_data = load_sighan15_test(path_head)
else:
print("[Core] Error: No such dataset ")
print(which)
exit(0)
test_dataset = mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return test_dataset
def _get_lattice_dataset(args, dataset="sighan", path_head="."):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if dataset == "sighan":
datasets, vocabs, embeddings = load_lattice_sighan(path_head=path_head)
else:
exit()
datasets["train"], datasets["valid"], datasets["test"] = mydataset(datasets["train"]), mydataset(datasets["valid"]), mydataset(datasets["test"])
return datasets, vocabs, embeddings
def _get_magic_plus_dataset(args, dataset="sighan", path_head=""):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if dataset == "sighan":
train_data, eval_data, test_data = load_abs_pos_sighan_plus(path_head=path_head)
else:
exit()
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_magic_dataset(args, dataset="sighan", path_head=""):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if dataset == "sighan":
train_data, eval_data, test_data = load_abs_pos_sighan(path_head=path_head)
else:
exit()
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_magic_lang8_dataset(args, dataset="sighan", path_head=""):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if dataset == "sighan":
train_data, eval_data, test_data = load_abs_pos_sighan_lang8(path_head=path_head)
else:
exit()
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_magic_expand_dataset(args, dataset="sighan", path_head=""):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if dataset == "sighan":
train_data, eval_data, test_data = load_abs_pos_sighan_plus(path_head=path_head)
else:
exit()
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset
def _get_super_magic_dataset(args, dataset="sighan", path_head=""):
"""
"""
print("[Core] Loading Dataset !")
ddp_exec("os.system('date')")
if dataset == "sighan":
train_data, eval_data, test_data, tokenizer = load_abs_pos_and_spe_token_sighan(path_head=path_head)
else:
exit()
train_dataset, eval_dataset, test_dataset = mydataset(train_data), mydataset(eval_data), mydataset(test_data)
print("[Core] Loading Succeed !")
ddp_exec("os.system('date')")
return train_dataset, eval_dataset, test_dataset, tokenizer
def get_metrics(training_args):
if "sighan" in training_args.dataset:
print("[Core] Hint: Using aligned sighan F1_score as metric")
return _get_metrics(training_args)
if "ctc2021" in training_args.dataset :
print("[Core] Hint: Using Seq2Seq ctc2021 score as metric")
return _get_seq2seq_metrics(training_args)
else:
print("[Core] Error when getting metrics.")
exit(0)
class mini_reporter:
def __init__(self):
self.messages = []
def record(self, message):
self.messages.append(message)
def out(self, obj):
from collections import Iterable
if isinstance(obj, Iterable):
for e in obj:
print(e)
else:
print(obj)
def report(self):
print("[Core] [Reporter] ")
for message in self.messages:
self.out(message)
print("[Core] [Over]")
def _get_metrics(training_args):
"""
#https://huggingface.co/metrics
#accuracy,bertscore, bleu, bleurt, coval, gleu, glue, meteor,
#rouge, sacrebleu, seqeval, squad, squad_v2, xnli
metric = load_metric()
"""
import numpy as np
from datasets import load_metric
def compute_metrics(eval_preds):
"""
reference: https://github.com/ACL2020SpellGCN/SpellGCN/blob/master/run_spellgcn.py
"""
Achilles = time.time()
reporter = mini_reporter()
sources, preds, labels = eval_preds# (num, length) np.array
tp, fp, fn = 0, 0, 0
sent_p, sent_n = 0, 0
tp_c, fp_c, fn_c = 0, 0, 0
char_p, char_n = 0, 0
f1_score_char, precision_char, Recall_char = 0, 0, 0
for i in range(len(sources)):
#print(sources[i])
#print(preds[i])
#print(labels[i])
source, pred, label = sources[i], preds[i], labels[i]
source, label = source[ source != -100], label[label != -100]
source, label = source[source != 0], label[label != 0]#pad idx for input_ids
#we guess pretrain Masked Language Model bert lack the surpvised sighan for 101 & 102 ( [CLS] & [SEP] ) , so we just ignore
source, pred, label = np.where(source == 102, 101, source), np.where(pred == 102, 101, pred), np.where(label == 102, 101, label)
source, pred, label = source[ source != 101 ], pred[ pred != 101 ], label[ label != 101]
source = source[:len(label)]
pred = pred[:len(label)]
pred = np.concatenate((pred, np.array([ 0 for i in range(len(label) - len(pred))])), axis=0)
if len(pred) != len(source) or len(label) != len(source):
print("[Core] Warning : something goes wrong when compute metrics, check codes now.")
print(len(source), len(pred), len(label))
print("[Core] source: ", source)
print("[Core] pred: ", pred)
print("[Core] label:", label)
print("[Core] raw source: ", sources[i])
print("[Core] raw pred: ", preds[i])
print("[Core] raw label:", labels[i])
exit()
try:
(pred != source).any()
except:
print(pred, source)
print("[Core] Error Exit ")
exit(0)
#if i < 5:
#print(source)
#print(pred)
#print(label)
#print((pred != source).any())
#print((pred == label).all())
#print((label != source).any())
reporter.record([pred, source, label])
reporter.record([(pred!=source).any(), (pred==label).all()])
if training_args.model_name != "Gector":
# label: [101, 2,... 3, 102]
# sentence level
if (pred != source).any():
sent_p += 1
#print("[Core] sent_p")
if (pred == label).all():
tp += 1
# print("[Core] tp")
if (label != source).any():
sent_n += 1
#print("[Core] sent_n")
# char level
for u in range(len(source)):
if source[u] != label[u]:
char_n += 1
if source[u] != label[u]:
char_p += 1
if pred[u] == label[u]:
tp_c += 1
else:
# label : [ 1,1,1,1,1 ]
if (pred != 1).any():
sent_p += 1
if (pred == label).all():
tp += 1
if (label != 1).any():
sent_n += 1
#print(tp, sent_p, sent_n)
precision = tp / (sent_p + 1e-10)
recall = tp / (sent_n + 1e-10)
F1_score = 2 * precision * recall / (precision + recall + 1e-10)
precision_char, recall_char = tp_c / (char_p + 1e-10), tp_c / (char_n + 1e-10)
F1_score_char = 2 * precision_char * recall_char / (precision_char + recall_char + 1e-10)
Turtle = time.time() - Achilles
if F1_score < 0.05:
print("[Core] Warning : metric score is too Low (< 0.05), maybe something goes wrong, check your codes please.")
#reporter.report()
#exit(0)
return {"F1_score": float(F1_score), "Precision":float(precision), "Recall":float(recall),"Metric_time":Turtle,
"F1_score_char": float(F1_score_char), "Precision_char":float(precision_char), "Recall_char":float(recall_char)
}
return compute_metrics
def _get_seq2seq_metrics(training_args):
"""
Main difference from " get_metrics() " is that seq2seq output & labels doesn't match (length).
#https://huggingface.co/metrics
#accuracy,bertscore, bleu, bleurt, coval, gleu, glue, meteor,
#rouge, sacrebleu, seqeval, squad, squad_v2, xnli
metric = load_metric()
"""
import numpy as np
from datasets import load_metric
def compute_metrics(eval_preds):
"""
reference: https://github.com/destwang/CTC2021
>>> final_score = 0.8 * detect_f1 + 0.2 * correct_f1
"""
Achilles = time.time()
sources, preds, labels = eval_preds# (num, length) np.array
tp_detect, tp_correct = 0, 0
p, n = 0, 0
for i in range(len(sources)):
source, pred, label = sources[i], preds[i], labels[i]
source, label = source[ source != -100], label[label != -100]
source, label = source[source != 0], label[label != 0]#pad idx for input_ids
# source = source[:len(label)]
# pred = pred[:len(label)]
pred = pred[ pred != 0]
#we guess pretrain Masked Language Model bert lack the surpvised sighan for 101 & 102 ( [CLS] & [SEP] ) , so we just ignore
source, pred, label = np.where(source == 102, 101, source), np.where(pred == 102, 101, pred), np.where(label == 102, 101, label)
from utils.levenshtein import levenshtein4seq
# print(source.shape, pred.shape, label.shape)
# print(source)
# print(pred)
# print(label)
pred_edits, gold_edits = levenshtein4seq(source, pred, only_edits=True), levenshtein4seq(source, label, only_edits=True)
# print(pred_edits)
# print(gold_edits)
# gold_pos = [ edit[1] for edit in gold_edits]
p += len(pred_edits)
n += len(gold_edits)
ref_error_set = set(gold_edits)
ref_det_set, ref_cor_set = set( [ (edit[0], edit[1]) for edit in gold_edits ] ), set( [ (edit[-1]) for edit in gold_edits] )
pred_error_set = set(pred_edits)
pred_det_set, pred_cor_set = set( [ (edit[0], edit[1]) for edit in pred_edits ] ), set( [(edit[-1]) for edit in pred_edits] )
n += len(ref_cor_set)
p += len(pred_cor_set)
for error in ref_error_set:
loc, typ, wrong, cor_text = error
if (loc, wrong) in pred_det_set or (cor_text) in pred_cor_set:
tp_detect += 1
tp_correct += len(ref_cor_set & pred_cor_set)
# for edit in gold_edits:
# if edit in pred_edits or edit in pred_cor_set:
# det_right_num += 1
# for edit in pred_edits:
# if edit[1] in gold_pos:
# tp_detect += 1
# if edit in gold_edits:
# tp_correct += 1
def compute_f1score(tp, p, n):
precision = tp / (p + 1e-10)
recall = tp / (n + 1e-10)
F1_score = 2 * precision * recall / (precision + recall + 1e-10)
return F1_score, precision, recall
Turtle = time.time() - Achilles
F1_score_detect, Precision_detect, Recall_detect = compute_f1score(tp_detect, p, n)