trainer.py 3.0 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

X
xiexionghang 已提交
15
import abc
T
tangwei 已提交
16
import os
X
xiexionghang 已提交
17
import time
T
tangwei 已提交
18
import sys
T
tangwei 已提交
19
import yaml
T
tangwei 已提交
20

T
tangwei 已提交
21
from paddle import fluid
T
tangwei 已提交
22

23
from paddlerec.core.utils import envs
T
tangwei 已提交
24

T
tangwei 已提交
25

X
xiexionghang 已提交
26
class Trainer(object):
X
xiexionghang 已提交
27
    """R
T
tangwei 已提交
28
    """
X
xiexionghang 已提交
29
    __metaclass__ = abc.ABCMeta
T
tangwei 已提交
30

T
tangwei 已提交
31
    def __init__(self, config=None):
X
xiexionghang 已提交
32
        self._status_processor = {}
T
tangwei 已提交
33

T
tangwei 已提交
34 35
        self._place = fluid.CPUPlace()
        self._exe = fluid.Executor(self._place)
T
tangwei 已提交
36

T
tangwei 已提交
37
        self._exector_context = {}
X
xiexionghang 已提交
38
        self._context = {'status': 'uninit', 'is_exit': False}
T
tangwei 已提交
39 40 41 42
        self._config_yaml = config

        with open(config, 'r') as rb:
            self._config = yaml.load(rb.read(), Loader=yaml.FullLoader)
T
tangwei 已提交
43

X
xiexionghang 已提交
44
    def regist_context_processor(self, status_name, processor):
X
xiexionghang 已提交
45 46 47
        """
        regist a processor for specify status
        """
X
xiexionghang 已提交
48 49 50
        self._status_processor[status_name] = processor

    def context_process(self, context):
X
xiexionghang 已提交
51 52 53 54 55 56 57
        """
        select a processor to deal specify context
        Args:
            context : context with status
        Return:
            None : run a processor for this status
        """
X
xiexionghang 已提交
58 59 60 61
        if context['status'] in self._status_processor:
            self._status_processor[context['status']](context)
        else:
            self.other_status_processor(context)
T
tangwei 已提交
62

X
xiexionghang 已提交
63
    def other_status_processor(self, context):
X
xiexionghang 已提交
64 65 66 67 68
        """
        if no processor match context.status, use defalut processor
        Return:
            None, just sleep in base
        """
X
xiexionghang 已提交
69
        print('unknow context_status:%s, do nothing' % context['status'])
70
        time.sleep(60)
X
xiexionghang 已提交
71 72

    def reload_train_context(self):
X
xiexionghang 已提交
73 74 75
        """
        context maybe update timely, reload for update
        """
X
xiexionghang 已提交
76 77 78
        pass

    def run(self):
X
xiexionghang 已提交
79 80 81
        """
        keep running by statu context.
        """
X
xiexionghang 已提交
82 83 84 85 86
        while True:
            self.reload_train_context()
            self.context_process(self._context)
            if self._context['is_exit']:
                break
T
tangwei 已提交
87 88 89 90 91 92 93


def user_define_engine(engine_yaml):
    with open(engine_yaml, 'r') as rb:
        _config = yaml.load(rb.read(), Loader=yaml.FullLoader)
    assert _config is not None

T
tangwei 已提交
94
    envs.set_runtime_environs(_config)
T
tangwei 已提交
95 96 97 98 99

    train_location = envs.get_global_env("engine.file")
    train_dirname = os.path.dirname(train_location)
    base_name = os.path.splitext(os.path.basename(train_location))[0]
    sys.path.append(train_dirname)
T
tangwei 已提交
100 101
    trainer_class = envs.lazy_instance_by_fliename(base_name,
                                                   "UserDefineTraining")
T
tangwei 已提交
102
    return trainer_class