forked from urchade/GLiNER
-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
96 lines (75 loc) · 3.24 KB
/
train.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
import os
os.environ["TOKENIZERS_PARALLELISM"] = "true"
import argparse
import random
import json
from transformers import AutoTokenizer
import torch
from gliner import GLiNERConfig, GLiNER
from gliner.training import Trainer, TrainingArguments
from gliner.data_processing.collator import DataCollatorWithPadding
from gliner.utils import load_config_as_namespace
from gliner.data_processing import WordsSplitter, GLiNERDataset
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default= "config.yaml")
parser.add_argument('--log_dir', type=str, default = 'models/')
parser.add_argument('--compile_model', type=bool, default = False)
args = parser.parse_args()
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
config = load_config_as_namespace(args.config)
config.log_dir = args.log_dir
model_config = GLiNERConfig(**vars(config))
with open(config.train_data, 'r') as f:
data = json.load(f)
print('Dataset size:', len(data))
#shuffle
random.shuffle(data)
print('Dataset is shuffled...')
train_data = data[:int(len(data)*0.9)]
test_data = data[int(len(data)*0.9):]
print('Dataset is splitted...')
tokenizer = AutoTokenizer.from_pretrained(model_config.model_name)
model_config.class_token_index=len(tokenizer)
tokenizer.add_tokens([model_config.ent_token, model_config.sep_token])
model_config.vocab_size = len(tokenizer)
words_splitter = WordsSplitter(model_config.words_splitter_type)
train_dataset = GLiNERDataset(train_data, model_config, tokenizer, words_splitter)
test_dataset = GLiNERDataset(test_data, model_config, tokenizer, words_splitter)
data_collator = DataCollatorWithPadding(model_config)
model = GLiNER(model_config, tokenizer=tokenizer, words_splitter=words_splitter)
model.resize_token_embeddings([model_config.ent_token, model_config.sep_token],
set_class_token_index = False,
add_tokens_to_tokenizer=False)
if args.compile_model:
torch.set_float32_matmul_precision('high')
model.to(device)
model.compile_for_training()
training_args = TrainingArguments(
output_dir=config.log_dir,
learning_rate=float(config.lr_encoder),
weight_decay=float(config.weight_decay_encoder),
others_lr=float(config.lr_others),
others_weight_decay=float(config.weight_decay_other),
lr_scheduler_type=config.scheduler_type,
warmup_ratio=config.warmup_ratio,
per_device_train_batch_size=config.train_batch_size,
per_device_eval_batch_size=config.train_batch_size,
max_grad_norm=config.max_grad_norm,
max_steps=config.num_steps,
evaluation_strategy="epoch",
save_steps = config.eval_every,
save_total_limit=config.save_total_limit,
dataloader_num_workers = 8,
use_cpu = False,
report_to="none",
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=test_dataset,
tokenizer=tokenizer,
data_collator=data_collator,
)
trainer.train()