-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
153 lines (127 loc) · 5.22 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
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
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
Train TransE/TransD/TransH/TransR models
"""
import datetime
import os
from mindspore import Tensor
from mindspore import context
from mindspore import dtype as mstype
from mindspore.common import set_seed
from mindspore.communication.management import get_group_size
from mindspore.communication.management import get_rank
from mindspore.communication.management import init
from mindspore.context import ParallelMode
from mindspore.nn.optim.sgd import SGD
from mindspore.train.callback import CheckpointConfig
from mindspore.train.callback import LossMonitor
from mindspore.train.callback import ModelCheckpoint
from mindspore.train.callback import TimeMonitor
from mindspore.train.model import Model
from mindspore.train.serialization import load_checkpoint
from mindspore.train.serialization import load_param_into_net
from model_utils.moxing_adapter import config
from model_utils.moxing_adapter import moxing_wrapper
from src.dataset import create_train_dataset
from src.loss import LossWrapperCell
from src.loss import TripletsMarginLoss
from src.model_builder import create_model
from src.utils.logging import get_logger
set_seed(config.seed)
def modelarts_pre_process():
"""modelarts pre process function."""
def _prepare_context():
"""Prepare the MindSpore context"""
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
if config.is_train_distributed:
init()
config.rank = get_rank()
config.group_size = get_group_size()
context.set_context(device_id=config.rank)
device_num = config.group_size
context.reset_auto_parallel_context()
context.set_auto_parallel_context(
device_num=device_num,
parallel_mode=ParallelMode.DATA_PARALLEL,
gradients_mean=True,
)
else:
config.rank = 0
config.group_size = 1
context.set_context(device_id=config.device_id)
@moxing_wrapper(pre_process=modelarts_pre_process)
def run_train():
"""Run train"""
# Prepare the Context (number of devices, their type and IDs)
_prepare_context()
# Determine, do we need to save all checkpoints or only for the process with Rank=0
save_ckpt_flag = False
if config.ckpt_save_on_master_only:
if config.rank == 0:
save_ckpt_flag = True
else:
save_ckpt_flag = True
# logger
time_label = datetime.datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S')
config.train_output_dir = os.path.join(config.train_output_dir, f"{time_label}_{config.model_name}")
config.logger = get_logger(config.train_output_dir, config.rank)
dataset, ent_tot, rel_tot = create_train_dataset(
dataset_root=config.dataset_root,
triplet_file_name=config.train_triplet_file_name,
entities_file_name=config.entities_file_name,
relations_file_name=config.relations_file_name,
negative_sampling_rate=config.negative_sampling_rate,
batch_size=config.train_batch_size,
group_size=config.group_size,
rank=config.rank,
seed=config.seed,
)
batch_num = dataset.get_dataset_size()
config.steps_per_epoch = dataset.get_dataset_size()
config.logger.save_args(config)
# network
config.logger.important_info('start create network')
# get network and init
network = create_model(ent_tot, rel_tot, config)
# pre_trained
if config.pre_trained:
load_param_into_net(network, load_checkpoint(config.pre_trained))
loss = TripletsMarginLoss(config.negative_sampling_rate, config.margin)
# optimizer
opt = SGD(
params=network.trainable_params(),
learning_rate=Tensor(config.lr, mstype.float32),
weight_decay=config.weight_decay,
)
network_with_loss = LossWrapperCell(network, loss)
model = Model(network_with_loss, optimizer=opt)
# define callbacks
callbacks = [
TimeMonitor(data_size=batch_num),
LossMonitor(per_print_times=batch_num),
]
if save_ckpt_flag:
ckpt_config = CheckpointConfig(
save_checkpoint_steps=config.ckpt_save_interval * config.steps_per_epoch,
keep_checkpoint_max=config.keep_checkpoint_max,
saved_network=network,
)
save_ckpt_path = os.path.join(config.train_output_dir, 'ckpt_' + str(config.rank) + '/')
ckpt_cb = ModelCheckpoint(config=ckpt_config, directory=save_ckpt_path, prefix='{}'.format(config.rank))
callbacks.append(ckpt_cb)
model.train(config.epochs_num, dataset, callbacks=callbacks, dataset_sink_mode=config.train_use_data_sink)
if __name__ == '__main__':
run_train()