-
Notifications
You must be signed in to change notification settings - Fork 12
/
train_easyrec.py
179 lines (163 loc) · 5.77 KB
/
train_easyrec.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
import os
import torch
import random
import logging
import numpy as np
import transformers
seed=2024
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
from model import Easyrec
from utility.logger import *
from utility.metric import *
from utility.trainer import *
from datetime import datetime
from utility.load_data import *
from transformers import AutoConfig
from dataclasses import dataclass, field
from typing import Dict, Optional, Sequence, List
@dataclass
class ModelArguments:
# Huggingface's original arguments
model_name_or_path: Optional[str] = field(default="facebook/opt-125m")
# SimCSE's arguments
temp: float = field(
default=0.05,
metadata={
"help": "Temperature for softmax."
}
)
pooler_type: str = field(
default="cls",
metadata={
"help": "What kind of pooler to use (cls, cls_before_pooler, avg, avg_top2, avg_first_last)."
}
)
do_mlm: bool = field(
default=False,
metadata={
"help": "Whether to use MLM auxiliary objective."
}
)
mlm_weight: float = field(
default=0.1,
metadata={
"help": "Weight for MLM auxiliary objective (only effective if --do_mlm)."
}
)
mlp_only_train: bool = field(
default=False,
metadata={
"help": "Use MLP only during training"
}
)
cache_dir: Optional[str] = field(
default=None,
metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},
)
use_auth_token: bool = field(
default=False,
metadata={
"help": "Will use the token generated when running `transformers-cli login` (necessary to use this script "
"with private models)."
},
)
model_revision: str = field(
default="main",
metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
)
use_fast_tokenizer: bool = field(
default=True,
metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."},
)
@dataclass
class DataArguments:
data_path: str = field(default='data/', metadata={"help": "Path to the training data."})
trn_dataset: str = field(default='arts-games-movies-home-electronics-tools', metadata={"help": "Training data."})
val_dataset: str = field(default='arts-games-movies-home-electronics-tools', metadata={"help": "Validation data."})
used_diverse_profile_num: int = field(default=3)
total_diverse_profile_num: int = field(default=3)
add_item_raw_meta: bool = field(default=True, metadata={"help": "Whether to use raw item meta information or not."})
# SimCSE's arguments
max_seq_length: Optional[int] = field(
default=64,
metadata={
"help": "The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated."
},
)
mlm_probability: float = field(
default=0.15,
metadata={"help": "Ratio of tokens to mask for MLM (only effective if --do_mlm)"}
)
@dataclass
class TrainingArguments(transformers.TrainingArguments):
bits: int = field(
default=16,
metadata={"help": "How many bits to use."}
)
def main():
global local_rank
parser = transformers.HfArgumentParser((ModelArguments, DataArguments, TrainingArguments))
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
local_rank = training_args.local_rank
print("training_args.output_dir", training_args.output_dir)
## logger
logger = EasyrecEmbedderTrainingLogger(
model_args=model_args,
data_args=data_args,
training_args=training_args,
)
logger.log(model_args)
logger.log(data_args)
logger.log(training_args)
## load model
if 'roberta' in model_args.model_name_or_path:
config_kwargs = {
"cache_dir": model_args.cache_dir,
"revision": model_args.model_revision,
"use_auth_token": True if model_args.use_auth_token else None,
}
config = AutoConfig.from_pretrained(model_args.model_name_or_path, **config_kwargs)
model = Easyrec.from_pretrained(
model_args.model_name_or_path,
from_tf=bool(".ckpt" in model_args.model_name_or_path),
config=config,
cache_dir=model_args.cache_dir,
revision=model_args.model_revision,
use_auth_token=True if model_args.use_auth_token else None,
model_args=model_args,
torch_dtype=torch.bfloat16,
)
else:
raise NotImplementedError
## tokenizer
tokenizer_kwargs = {
"cache_dir": model_args.cache_dir,
"use_fast": model_args.use_fast_tokenizer,
"revision": model_args.model_revision,
"use_auth_token": True if model_args.use_auth_token else None,
}
tokenizer = transformers.AutoTokenizer.from_pretrained(
model_args.model_name_or_path,
cache_dir=model_args.cache_dir,
padding_side="right",
use_fast=False,
)
## data module
data_module = make_pretrain_embedder_supervised_data_module(tokenizer=tokenizer, data_args=data_args)
## trainer
trainer = EasyrecEmbedderTrainer(model=model,
tokenizer=tokenizer,
args=training_args,
**data_module)
metric = Metric(metrics=['recall'], k=[20])
trainer.add_evaluator(metric)
trainer.add_logger(logger)
## training
trainer.train()
if __name__ == "__main__":
main()