test_downpoursgd.py 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2018 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.
14
"""Testcases for Downpour."""
15 16 17 18 19 20 21 22 23 24 25 26 27

from __future__ import print_function

import paddle
import paddle.fluid as fluid
import os
import signal
import subprocess
import time
import unittest
import sys
from op_test import OpTest
from paddle.fluid.trainer_desc import DistMultiTrainer
28
from paddle.fluid.device_worker import DownpourSGD, DownpourSGDOPT
29
from paddle.fluid.incubate.fleet.parameter_server.pslib.node import DownpourWorker
30 31 32 33
from google.protobuf import text_format
import paddle.fluid.incubate.fleet.parameter_server.pslib.ps_pb2 as pslib


34
class TestListenAndServOp(unittest.TestCase):
35 36
    """TestListenAndServOp."""

37 38 39 40
    def setUp(self):
        pass

    def test_device_work_use_cvm(self):
41
        """test device work use_cvm."""
42 43 44 45 46 47
        if sys.platform == 'win32' or sys.platform == 'sys.platform':
            pass
        else:
            print(sys.platform)
            cmd = "wget --no-check-certificate https://pslib.bj.bcebos.com/fleet_desc.prototxt"
            os.system(cmd)
48
            x = fluid.layers.data(name='x', shape=[1], dtype='int64')
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
            x_emb = fluid.layers.embedding(
                input=x, size=[1, 2], is_distributed=True)
            y_predict = fluid.layers.fc(input=x_emb, size=1, act=None)
            y = fluid.layers.data(name='y', shape=[1], dtype='float32')
            cost = fluid.layers.square_error_cost(input=y_predict, label=y)
            avg_cost = fluid.layers.mean(cost)

            ps_param = pslib.PSParameter()
            with open("fleet_desc.prototxt") as f:
                text_format.Merge(f.read(), ps_param)
            fleet_desc = ps_param
            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(fluid.default_startup_program())

            opt_info = {}
            main_program = fluid.default_main_program()
            program_id = str(id(avg_cost.block.program))
            program_configs = {}
            program_configs[program_id] = {
                "pull_sparse": [0],
                "push_sparse": [0]
            }
            program_configs[program_id]["pull_dense"] = [1]
            program_configs[program_id]["push_dense"] = [1]

            worker_skipped_ops = ["lookup_table", "lookup_table_grad"]
            opt_info["program_configs"] = program_configs
            opt_info["trainer"] = "DistMultiTrainer"
            opt_info["device_worker"] = "DownpourSGD"
            opt_info["optimizer"] = "DownpourSGD"
            opt_info["fleet_desc"] = ps_param
            opt_info["worker_skipped_ops"] = worker_skipped_ops
            opt_info["use_cvm"] = True
            opt_info["scale_datanorm"] = -1
            opt_info["dump_slot"] = False
84
            opt_info["stat_var_names"] = []
85 86 87
            worker = DownpourWorker(None)
            worker.get_desc().CopyFrom(ps_param.trainer_param[0])
            opt_info["program_id_to_worker"] = {program_id: worker}
88 89 90 91 92 93 94 95 96 97 98 99 100

            main_program._fleet_opt = opt_info
            trainer = DistMultiTrainer()
            trainer._set_program(main_program)
            device_worker = DownpourSGD()
            device_worker._set_fleet_desc(fleet_desc)
            trainer._set_device_worker(device_worker)
            trainer._set_fleet_desc(fleet_desc)
            trainer._gen_trainer_desc()
            cmd = "rm fleet_desc.prototxt*"
            os.system(cmd)

    def test_device_work(self):
101
        """test devicve worker."""
102 103 104 105 106 107
        if sys.platform == 'win32' or sys.platform == 'sys.platform':
            pass
        else:
            print(sys.platform)
            cmd = "wget --no-check-certificate https://pslib.bj.bcebos.com/fleet_desc.prototxt"
            os.system(cmd)
108
            x = fluid.layers.data(name='x', shape=[1], dtype='int64')
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
            x_emb = fluid.layers.embedding(
                input=x, size=[1, 2], is_distributed=True)
            y_predict = fluid.layers.fc(input=x_emb, size=1, act=None)
            y = fluid.layers.data(name='y', shape=[1], dtype='float32')
            cost = fluid.layers.square_error_cost(input=y_predict, label=y)
            avg_cost = fluid.layers.mean(cost)

            ps_param = pslib.PSParameter()
            with open("fleet_desc.prototxt") as f:
                text_format.Merge(f.read(), ps_param)
            fleet_desc = ps_param
            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(fluid.default_startup_program())

            opt_info = {}
            main_program = fluid.default_main_program()
            program_id = str(id(avg_cost.block.program))
            program_configs = {}
            program_configs[program_id] = {
                "pull_sparse": [0],
                "push_sparse": [0]
            }
            program_configs[program_id]["pull_dense"] = [1]
            program_configs[program_id]["push_dense"] = [1]

            worker_skipped_ops = ["lookup_table", "lookup_table_grad"]
            opt_info["program_configs"] = program_configs
            opt_info["trainer"] = "DistMultiTrainer"
            opt_info["device_worker"] = "DownpourSGD"
            opt_info["optimizer"] = "DownpourSGD"
            opt_info["fleet_desc"] = ps_param
            opt_info["worker_skipped_ops"] = worker_skipped_ops
            opt_info["use_cvm"] = False
            opt_info["scale_datanorm"] = -1
            opt_info["dump_slot"] = False
144
            opt_info["stat_var_names"] = []
145 146 147
            worker = DownpourWorker(None)
            worker.get_desc().CopyFrom(ps_param.trainer_param[0])
            opt_info["program_id_to_worker"] = {program_id: worker}
148 149 150 151 152 153 154 155 156 157 158 159

            main_program._fleet_opt = opt_info
            trainer = DistMultiTrainer()
            trainer._set_program(main_program)
            device_worker = DownpourSGD()
            device_worker._set_fleet_desc(fleet_desc)
            trainer._set_device_worker(device_worker)
            trainer._set_fleet_desc(fleet_desc)
            trainer._gen_trainer_desc()
            cmd = "rm fleet_desc.prototxt*"
            os.system(cmd)

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    def test_downpour_opt_work(self):
        """test devicve worker."""
        if sys.platform == 'win32' or sys.platform == 'sys.platform':
            pass
        else:
            print(sys.platform)
            cmd = "wget --no-check-certificate https://pslib.bj.bcebos.com/fleet_desc.prototxt"
            os.system(cmd)
            x = fluid.layers.data(name='x', shape=[1], dtype='int64')
            x_emb = fluid.layers.embedding(
                input=x, size=[1, 2], is_distributed=True)
            y_predict = fluid.layers.fc(input=x_emb, size=1, act=None)
            y = fluid.layers.data(name='y', shape=[1], dtype='float32')
            cost = fluid.layers.square_error_cost(input=y_predict, label=y)
            avg_cost = fluid.layers.mean(cost)

            ps_param = pslib.PSParameter()
            with open("fleet_desc.prototxt") as f:
                text_format.Merge(f.read(), ps_param)
            fleet_desc = ps_param
            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(fluid.default_startup_program())

            opt_info = {}
            main_program = fluid.default_main_program()
            program_id = str(id(avg_cost.block.program))
            program_configs = {}
            program_configs[program_id] = {
                "pull_sparse": [0],
                "push_sparse": [0]
            }
            program_configs[program_id]["pull_dense"] = [1]
            program_configs[program_id]["push_dense"] = [1]

            worker_skipped_ops = ["lookup_table", "lookup_table_grad"]
            opt_info["program_configs"] = program_configs
            opt_info["trainer"] = "DistMultiTrainer"
            opt_info["device_worker"] = "DownpourSGDOPT"
            opt_info["optimizer"] = "DownpourSGD"
            opt_info["fleet_desc"] = ps_param
            opt_info["worker_skipped_ops"] = worker_skipped_ops
            opt_info["use_cvm"] = False
            opt_info["scale_datanorm"] = -1
            opt_info["dump_slot"] = False
            opt_info["stat_var_names"] = []
            worker = DownpourWorker(None)
            worker.get_desc().CopyFrom(ps_param.trainer_param[0])
            opt_info["program_id_to_worker"] = {program_id: worker}

            main_program._fleet_opt = opt_info
            trainer = DistMultiTrainer()
            trainer._set_program(main_program)
            device_worker = DownpourSGDOPT()
            device_worker._set_fleet_desc(fleet_desc)
            trainer._set_device_worker(device_worker)
            trainer._set_fleet_desc(fleet_desc)
            trainer._gen_trainer_desc()
            cmd = "rm fleet_desc.prototxt*"
            os.system(cmd)

220 221 222

if __name__ == "__main__":
    unittest.main()