forked from yourh/AttentionXML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
170 lines (131 loc) · 5.23 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8
"""
Created on 2018/12/9
@author yrh
"""
import os
import random
import torch
from pathlib import Path
import click
import mlflow
import numpy as np
import torch.distributed as dist
from logzero import logger
from ruamel.yaml import YAML
from deepxml.data_utils import get_word_emb
from deepxml.train import (
default_train, default_eval, splitting_head_tail_train, splitting_head_tail_eval,
random_forest_train, random_forest_eval, spectral_clustering_train,
spectral_clustering_eval, transformer_train, transformer_eval,
)
from deepxml.train.utils import log_tag
TRANSFORMER_MODEL_NAMES = ['RobertaXML']
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
@click.command()
@click.option('-d', '--data-cnf', type=click.Path(exists=True), help='Path of dataset configure yaml.')
@click.option('-m', '--model-cnf', type=click.Path(exists=True), help='Path of model configure yaml.')
@click.option('--mode', type=click.Choice(['train', 'eval']), default=None)
@click.option('-t', '--tree-id', type=click.INT, default=None)
@click.option('-s', '--output-suffix', type=click.STRING, default='', help='suffix of output name')
@click.option('--dry-run', is_flag=True, default=False, help='dry run for test code')
def main(data_cnf, model_cnf, mode, tree_id, output_suffix, dry_run):
if not dry_run:
mlflow.start_run()
set_seed(tree_id)
tree_id = F'-Tree-{tree_id}' if tree_id is not None else ''
yaml = YAML(typ='safe')
data_cnf_path = data_cnf
model_cnf_path = model_cnf
data_cnf, model_cnf = yaml.load(Path(data_cnf)), yaml.load(Path(model_cnf))
model, model_name, data_name = None, model_cnf['name'], data_cnf['name']
model_path = os.path.join(model_cnf['path'], F'{model_name}-{data_name}{tree_id}{output_suffix}')
emb_init = get_word_emb(data_cnf['embedding']['emb_init'])
logger.info(F'Model Name: {model_name}')
is_split_head_tail = 'split_head_tail' in data_cnf
is_random_forest = 'random_forest' in model_cnf
is_spectral_clustering = 'spectral_clustering' in model_cnf
is_transformer_model = model_name in TRANSFORMER_MODEL_NAMES
if is_split_head_tail:
split_ratio = data_cnf['split_head_tail']
head_model = None
tail_model = None
head_labels = None
tail_labels = None
elif is_random_forest:
num_tree = model_cnf['random_forest']['num']
elif is_spectral_clustering:
pass
if mode is None or mode == 'train':
if is_transformer_model:
transformer_train(
data_cnf, data_cnf_path, model_cnf, model_cnf_path, model_path,
dry_run,
)
elif is_split_head_tail:
head_model, tail_model, head_labels, tail_labels = splitting_head_tail_train(
data_cnf, data_cnf_path, model_cnf, model_cnf_path, emb_init,
model_path, tree_id, output_suffix, dry_run, split_ratio,
)
elif is_random_forest:
random_forest_train(
data_cnf, data_cnf_path, model_cnf, model_cnf_path, emb_init,
model_path, tree_id, output_suffix, dry_run, num_tree,
)
elif is_spectral_clustering:
spectral_clustering_train(
data_cnf, data_cnf_path, model_cnf, model_cnf_path, emb_init,
model_path, tree_id, output_suffix, dry_run,
)
else:
default_train(
data_cnf, data_cnf_path, model_cnf, model_cnf_path, emb_init,
model_path, tree_id, output_suffix, dry_run,
)
log_tag(dry_run, model_name, data_name, output_suffix)
if mode is None or mode == 'eval':
if is_transformer_model:
transformer_eval(
data_cnf, model_cnf, data_name, model_name, model_path, tree_id,
output_suffix, dry_run,
)
elif is_split_head_tail:
splitting_head_tail_eval(
data_cnf, model_cnf, data_name, model_name, model_path, emb_init,
tree_id, output_suffix, dry_run, split_ratio, head_labels, tail_labels,
head_model, tail_model,
)
elif is_random_forest:
random_forest_eval(
data_cnf, model_cnf, data_name, model_name, model_path, emb_init,
tree_id, output_suffix, dry_run, num_tree,
)
elif is_spectral_clustering:
spectral_clustering_eval(
data_cnf, model_cnf, data_name, model_name, model_path, emb_init,
tree_id, output_suffix, dry_run,
)
else:
default_eval(
data_cnf, model_cnf, data_name, model_name, model_path, emb_init,
tree_id, output_suffix, dry_run,
)
def distributed_train(gpu, args):
"""GPU Distributed training"""
rank = args.nr
dist.init_process_group(
backend='nccl',
init_method='env://',
world_size=args.world_size,
rank=rank,
)
if __name__ == '__main__':
main()