transpiler_trainer.py 4.8 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

"""
T
tangwei 已提交
16
Training use fluid with DistributeTranspiler
T
tangwei 已提交
17 18
"""
import os
T
tangwei 已提交
19

T
tangwei 已提交
20
import paddle.fluid as fluid
T
tangwei 已提交
21 22
from paddle.fluid.incubate.fleet.parameter_server.distribute_transpiler import fleet

T
tangwei 已提交
23
from fleetrec.trainer.trainer import Trainer
T
tangwei 已提交
24
from fleetrec.utils import envs
T
tangwei 已提交
25 26


T
tangwei 已提交
27
class TranspileTrainer(Trainer):
T
tangwei 已提交
28 29
    def __init__(self, config=None):
        Trainer.__init__(self, config)
T
tangwei 已提交
30
        self.processor_register()
T
tangwei 已提交
31
        self.model = None
T
tangwei12 已提交
32 33 34
        self.inference_models = []
        self.increment_models = []

T
tangwei 已提交
35 36
    def processor_register(self):
        print("Need implement by trainer, `self.regist_context_processor('uninit', self.instance)` must be the first")
T
tangwei 已提交
37

T
tangwei12 已提交
38 39 40
    def _get_dataset(self):
        namespace = "train.reader"

T
bug fix  
tangwei 已提交
41
        inputs = self.model.get_inputs()
T
tangwei12 已提交
42 43
        threads = envs.get_global_env("train.threads", None)
        batch_size = envs.get_global_env("batch_size", None, namespace)
T
tangwei 已提交
44 45
        reader_class = envs.get_global_env("class", None, namespace)
        abs_dir = os.path.dirname(os.path.abspath(__file__))
T
tangwei 已提交
46
        reader = os.path.join(abs_dir, '../reader', 'reader_instance.py')
T
tangwei 已提交
47
        pipe_cmd = "python {} {} {} {}".format(reader, reader_class, "TRAIN", self._config)
T
tangwei12 已提交
48 49
        train_data_path = envs.get_global_env("train_data_path", None, namespace)

T
tangwei 已提交
50 51
        dataset = fluid.DatasetFactory().create_dataset()
        dataset.set_use_var(inputs)
T
tangwei 已提交
52
        dataset.set_pipe_command(pipe_cmd)
T
tangwei 已提交
53 54 55
        dataset.set_batch_size(batch_size)
        dataset.set_thread(threads)
        file_list = [
T
tangwei12 已提交
56 57
            os.path.join(train_data_path, x)
            for x in os.listdir(train_data_path)
T
tangwei 已提交
58 59 60 61 62
        ]

        dataset.set_filelist(file_list)
        return dataset

T
tangwei 已提交
63
    def save(self, epoch_id, namespace, is_fleet=False):
T
tangwei12 已提交
64 65 66
        def need_save(epoch_id, epoch_interval, is_last=False):
            if is_last:
                return True
T
tangwei 已提交
67

T
tangwei12 已提交
68 69
            if epoch_id == -1:
                return False
T
tangwei 已提交
70

T
tangwei12 已提交
71 72
            return epoch_id % epoch_interval == 0

T
tangwei 已提交
73
        def save_inference_model():
T
tangwei12 已提交
74
            save_interval = envs.get_global_env("save.inference.epoch_interval", -1, namespace)
T
tangwei 已提交
75 76 77 78

            if not need_save(epoch_id, save_interval, False):
                return

T
tangwei12 已提交
79 80 81 82 83
            print("save inference model is not supported now.")
            return

            feed_varnames = envs.get_global_env("save.inference.feed_varnames", None, namespace)
            fetch_varnames = envs.get_global_env("save.inference.fetch_varnames", None, namespace)
T
tangwei 已提交
84
            fetch_vars = [fluid.global_scope().vars[varname] for varname in fetch_varnames]
T
tangwei12 已提交
85
            dirname = envs.get_global_env("save.inference.dirname", None, namespace)
T
tangwei 已提交
86 87 88

            assert dirname is not None
            dirname = os.path.join(dirname, str(epoch_id))
T
tangwei 已提交
89 90

            if is_fleet:
T
tangwei 已提交
91
                fleet.save_inference_model(dirname, feed_varnames, fetch_vars)
T
tangwei 已提交
92
            else:
T
tangwei 已提交
93
                fluid.io.save_inference_model(dirname, feed_varnames, fetch_vars, self._exe)
T
tangwei12 已提交
94
            self.inference_models.append((epoch_id, dirname))
T
tangwei 已提交
95 96

        def save_persistables():
T
tangwei12 已提交
97
            save_interval = envs.get_global_env("save.increment.epoch_interval", -1, namespace)
T
tangwei 已提交
98 99 100 101

            if not need_save(epoch_id, save_interval, False):
                return

T
tangwei12 已提交
102
            dirname = envs.get_global_env("save.increment.dirname", None, namespace)
T
tangwei 已提交
103 104 105

            assert dirname is not None
            dirname = os.path.join(dirname, str(epoch_id))
T
tangwei 已提交
106 107

            if is_fleet:
T
tangwei 已提交
108
                fleet.save_persistables(self._exe, dirname)
T
tangwei 已提交
109
            else:
T
tangwei 已提交
110
                fluid.io.save_persistables(self._exe, dirname)
T
tangwei12 已提交
111
            self.increment_models.append((epoch_id, dirname))
T
tangwei 已提交
112 113 114 115

        save_persistables()
        save_inference_model()

T
tangwei 已提交
116 117
    def instance(self, context):
        models = envs.get_global_env("train.model.models")
T
bug fix  
tangwei 已提交
118
        model_class = envs.lazy_instance(models, "Model")
T
tangwei 已提交
119
        self.model = model_class(None)
T
tangwei 已提交
120
        context['status'] = 'init_pass'
T
tangwei 已提交
121

T
tangwei 已提交
122 123 124
    def init(self, context):
        print("Need to be implement")
        context['is_exit'] = True
T
tangwei 已提交
125

T
tangwei 已提交
126 127 128
    def train(self, context):
        print("Need to be implement")
        context['is_exit'] = True
T
tangwei 已提交
129

T
tangwei12 已提交
130
    def infer(self, context):
T
tangwei 已提交
131
        context['is_exit'] = True
T
tangwei12 已提交
132 133

    def terminal(self, context):
T
tangwei 已提交
134
        print("clean up and exit")
T
tangwei12 已提交
135
        context['is_exit'] = True